Try   HackMD

2023q1 Homework6 (quiz5)

contributed by < Jerejere0808 >

測驗 1

解釋程式碼運作原理,指出其設計和實作缺失,並予以改進。

原本的程式碼只實作 first-fit 演算法,請重新實作為 best-fit

static inline void *get_loc_to_place(void *current, int size)
{
    block_t *best_fit = NULL; 
    block_t *parse = current; 

    while (parse != NULL) {
        if (parse->size >= (size + header_size)) {
            if (best_fit == NULL || parse->size < best_fit->size) {
                best_fit = parse; 
            }
        }
        parse = parse->prev; 
    }

    parse = ((block_t *) current)->next;
    while (parse != NULL) {
        if (parse->size >= (size + header_size)) {           
            if (best_fit == NULL || parse->size < best_fit->size) {
                best_fit = parse; 
            }
        }
        parse = parse->next; 
    }

    if (best_fit != NULL) {
        return best_fit;
    } else {
        return NULL;
    }
}

測驗 2