Code Monkey home page Code Monkey logo

heap_allocator's People

Contributors

cutwell avatar thevoidabove avatar xtuc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

heap_allocator's Issues

Code is not valid C89/C99/C11

Following the instructions in README.md leads to a segmentation fault. Steps to reproduce:

  1. git clone https://github.com/CCareaga/heap_allocator.git && git checkout b4058f452e8f8052f5813509634c58fd3753e80f
  2. gcc main.c llist.c heap.c -o heap_test && ./heap_test
$ gcc main.c llist.c heap.c -o heap_test && ./heap_test
heap.c:6:27: warning: cast to 'node_t *' (aka 'struct node_t *') from smaller integer type 'uint32_t' (aka 'unsigned int') [-Wint-to-pointer-cast]
    node_t *init_region = (node_t *) start;
                          ^
heap.c:12:5: warning: implicit declaration of function 'add_node' is invalid in C99 [-Wimplicit-function-declaration]
    add_node(heap->bins[get_bin_index(init_region->size)], init_region);
    ^
heap.c:21:21: warning: implicit declaration of function 'get_best_fit' is invalid in C99 [-Wimplicit-function-declaration]
    node_t *found = get_best_fit(temp, size);
                    ^
heap.c:21:13: warning: incompatible integer to pointer conversion initializing 'node_t *' (aka 'struct node_t *') with an expression of type 'int' [-Wint-conversion]
    node_t *found = get_best_fit(temp, size);
            ^       ~~~~~~~~~~~~~~~~~~~~~~~~
heap.c:25:15: warning: incompatible integer to pointer conversion assigning to 'node_t *' (aka 'struct node_t *') from 'int' [-Wint-conversion]
        found = get_best_fit(temp, size);
              ^ ~~~~~~~~~~~~~~~~~~~~~~~~
heap.c:29:17: warning: incompatible pointer types initializing 'node_t *' (aka 'struct node_t *') with an expression of type 'char *' [-Wincompatible-pointer-types]
        node_t *split = ((char *) found + sizeof(node_t) + sizeof(footer_t)) + size;
                ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
heap.c:44:5: warning: implicit declaration of function 'remove_node' is invalid in C99 [-Wimplicit-function-declaration]
    remove_node(heap->bins[index], found);
    ^
heap.c:67:14: warning: comparison between pointer and integer ('node_t *' (aka 'struct node_t *') and 'uint32_t' (aka 'unsigned int'))
    if (head == heap->start) {
        ~~~~ ^  ~~~~~~~~~~~
heap.c:74:20: warning: cast to 'node_t *' (aka 'struct node_t *') from smaller integer type 'uint32_t' (aka 'unsigned int') [-Wint-to-pointer-cast]
    node_t *prev = (node_t *) * ((uint32_t *) ((char *) head - sizeof(footer_t)));
                   ^
heap.c:108:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
heap.c:135:41: warning: cast to 'char *' from smaller integer type 'uint32_t' (aka 'unsigned int') [-Wint-to-pointer-cast]
    footer_t *wild_foot = (footer_t *) ((char *) heap->end - sizeof(footer_t));
                                        ^
11 warnings generated.
[1]    3559 segmentation fault  ./heap_test

Error

heap.c:109:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
heap.c:136:53: warning: cast to 'char *' from smaller integer type 'uint'
(aka 'unsigned int') [-Wint-to-pointer-cast]
footer_t *wild_foot = (footer_t *) (uintptr_t) ((char *) heap->end ...
^
2 warnings generated.

index not bound checked.

I am getting a crash using this for a personal project, and have determined that it is due an error in heap_alloc.

    while (found == NULL) {
        if(index >= BIN_COUNT)
            return NULL;

        temp = heap->bins[++index];
        found = get_best_fit(temp, size);
    }

Notice the index increment, and access, without first checking if it is below BIN_COUNT.

Seems wasting 8 bytes per chunk? Didnt get the point of returning &found->next

Hi:

Nice job!

Got a bit question though. What’s the point of returning &found->next to user in malloc? The whole chunk size is still “sizeof(node_t) + size + sizeof(footer)”, which means although user get 8 bytes in advance, they still didn’t utilize the whole chunk (access finishes at size - 8). Why not just return ‘head + szieof(node_t)’, which simplifies the trick?

memory chunk cannot be merged when being freed

On line 71 in heap.c of current version, when the node being freed is the start of the heap, we still need to check whether it can be merged with its next chunk. For example:

p = heap_alloc(heap, 100);
heap_free(heap, p);

After this 2 lines, the whole heap is still splitted into 2 free chunks, but the 2 free chunks should be merged into 1.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.