ccareaga / heap_allocator Goto Github PK
View Code? Open in Web Editor NEWA simple heap memory allocator in ~200 lines.
License: MIT License
A simple heap memory allocator in ~200 lines.
License: MIT License
Following the instructions in README.md leads to a segmentation fault. Steps to reproduce:
git clone https://github.com/CCareaga/heap_allocator.git && git checkout b4058f452e8f8052f5813509634c58fd3753e80f
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
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.
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.
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?
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.