コード例 #1
0
ファイル: quick.c プロジェクト: joelpet/malloc
/* malloc_quick
 *
 * malloc_quick returns the start address of the newly allocated memory.
 *
 */
void *malloc_quick(
        size_t nbytes) /* number of bytes of memory to allocate */
{
    Header *moreroce(unsigned);
    int list_index, i;

    list_index = get_quick_fit_list_index(nbytes);

    /* 
     * Use another strategy for too large allocations. We want the allocation
     * to be quick, so use malloc_first().
     */

    if (list_index >= NRQUICKLISTS) {
        return malloc_first(nbytes);
    }


    /*
     * Initialize the quick fit lists if this is the first run.
     */

    if (first_run) {
        for (i = 0; i < NRQUICKLISTS; ++i) {
            quick_fit_lists[i] = NULL;
        }
        first_run = false;
    }


    /*
     * If the quick fit list pointer is NULL, then there are no free memory
     * blocks present, so we will have to create some before continuing.
     */

    if (quick_fit_lists[list_index] == NULL) {
        Header* new_quick_fit_list = init_quick_fit_list(list_index);
        if (new_quick_fit_list == NULL) {
            return NULL;
        } else {
            quick_fit_lists[list_index] = new_quick_fit_list;
        }
    }


    /*
     * Now that we know there is at least one free quick fit memory block,
     * let's use return that and also update the quick fit list pointer so that
     * it points to the next in the list.
     */

    void* pointer_to_return = (void *)(quick_fit_lists[list_index] + 1);
    quick_fit_lists[list_index] = quick_fit_lists[list_index]->s.ptr;

    return pointer_to_return;
}
コード例 #2
0
ファイル: openmalloc.c プロジェクト: montao/openmalloc
/* number of bytes of memory to allocate */
void *malloc(size_t nbytes) {
    if (nbytes == 0) {
        return NULL;
    }

#if   STRATEGY == 1
    return malloc_first(nbytes);
#elif STRATEGY == 2
    return malloc_best(nbytes);
#elif STRATEGY == 3
    return malloc_worst(nbytes);
#elif STRATEGY == 4
    return malloc_quick(nbytes);
#else
    exit(1);
#endif
}
コード例 #3
0
ファイル: malloc.c プロジェクト: joelpet/malloc
/* malloc
 *
 * malloc returns the start address of dynamically allocated memory.
 * This is simply a wrapper function that calls the different algorithm
 * implementations depending on the value of STRATEGY.
 *
 */
void *malloc(
    size_t nbytes) /* number of bytes of memory to allocate */
{
    if (nbytes == 0) {
        return NULL;
    }

#if   STRATEGY == 1
    return malloc_first(nbytes);
#elif STRATEGY == 2
    return malloc_best(nbytes);
#elif STRATEGY == 3
    return malloc_worst(nbytes);
#elif STRATEGY == 4
    return malloc_quick(nbytes);
#else
    exit(1);
#endif
}