diff --git a/src/chunk_manager.c b/src/chunk_manager.c index 054319d..c3d81d5 100644 --- a/src/chunk_manager.c +++ b/src/chunk_manager.c @@ -60,4 +60,24 @@ int get_append_unused_chunk(void *root, size_t size, chunk_t *result, size_t new chunk_split(result, size); } return 0; +} + +int get_chunk(void *data_start, chunk_t *result, int *category) +{ + int i; + void *chunk_pos; + + for (i = TINY; i <= LARGE; i++) + { + chunk_pos = raw_get_chunk_by_root(allocs_tree[i], data_start); + if (chunk_pos == NULL) + continue; + break; + } + if (chunk_pos == NULL) + return 1; + chunk_read(chunk_pos, result); + if (category) + *category = i; + return 0; } \ No newline at end of file diff --git a/src/chunk_manager.h b/src/chunk_manager.h index 926dbc9..96ee58c 100644 --- a/src/chunk_manager.h +++ b/src/chunk_manager.h @@ -5,4 +5,5 @@ void chunk_iter(chunk_t *root, void (*f)(chunk_t*)); void chunk_merge(chunk_t *first, chunk_t *second); int get_append_unused_chunk(void *root, size_t size, chunk_t *result, size_t new_block_size); -int chunk_split(chunk_t *chunk, size_t new_size); \ No newline at end of file +int chunk_split(chunk_t *chunk, size_t new_size); +int get_chunk(void *data_start, chunk_t *result, int *category); \ No newline at end of file diff --git a/src/free.c b/src/free.c index de65484..41eaf97 100644 --- a/src/free.c +++ b/src/free.c @@ -1,6 +1,10 @@ #include "chunk.h" #include "malloc.h" +#include "chunk_manager.h" +#include "block.h" +#include "raw_chunk_manager.h" +#include #include void free_tiny(chunk_t *chunk) @@ -47,28 +51,20 @@ static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large}; void ft_free(void *ptr) { - void *raw_chunk; chunk_t chunk; - size_t i; + int category; - if (ptr == NULL) - return; - for (i = TINY; i <= LARGE; i++) - { - raw_chunk = raw_get_chunk(allocs_tree[i], ptr); - if (raw_chunk == NULL) - continue; - break; - } + if (get_chunk(ptr, &chunk, &category)) + { #ifdef DEBUG - if (raw_chunk == NULL) write(2, "chunk not found\n", 16); #endif - chunk_read(raw_chunk, &chunk); + return; + } #ifdef DEBUG if (chunk.is_used == false) write(2, "double free\n", 12); #endif // CALL THE RIGHT FREE FUNCTION DEPEND ON SIZE - free_funcs[i](&chunk); + free_funcs[category](&chunk); } \ No newline at end of file diff --git a/src/raw_chunk_manager.c b/src/raw_chunk_manager.c index 6d2cbd1..3ed06eb 100644 --- a/src/raw_chunk_manager.c +++ b/src/raw_chunk_manager.c @@ -15,7 +15,7 @@ void *raw_get_last_chunk(void **root) return current; } -void *raw_get_chunk(void **root, void *data_start) +void *raw_get_chunk_by_root(void **root, void *data_start) { void**current = root; diff --git a/src/raw_chunk_manager.h b/src/raw_chunk_manager.h index b6c96aa..e737f50 100644 --- a/src/raw_chunk_manager.h +++ b/src/raw_chunk_manager.h @@ -1,6 +1,6 @@ #pragma once void *raw_get_last_chunk(void **root); -void *raw_get_chunk(void **root, void *data_start); +void *raw_get_chunk_by_root(void **root, void *data_start); int get_unused_chunk_raw(void *root, size_t size, chunk_t *result); int is_alone_on_block(const chunk_t *chunk); \ No newline at end of file diff --git a/test/main.c b/test/main.c index a97079a..ecba7aa 100644 --- a/test/main.c +++ b/test/main.c @@ -37,13 +37,13 @@ int main(int ac, char **av) ptr1 = ft_malloc(10000000); ptr2 = ft_malloc(10000000); ptr3 = ft_malloc(10000000); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr1) == NULL), "alloc first", ""); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "alloc second", ""); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr3) == NULL), "alloc third", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr1) == NULL), "alloc first", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "alloc second", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr3) == NULL), "alloc third", ""); ft_free(ptr1); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder1", ""); ft_free(ptr3); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[LARGE], ptr2) == NULL), "free disorder2", ""); ft_free(ptr2); test(NULL, allocs_tree[LARGE], "free disorder3", ""); @@ -57,13 +57,13 @@ int main(int ac, char **av) ptr1 = ft_malloc(4); ptr2 = ft_malloc(4); ptr3 = ft_malloc(4); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr1) == NULL), "alloc first", ""); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "alloc second", ""); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr3) == NULL), "alloc third", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr1) == NULL), "alloc first", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "alloc second", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr3) == NULL), "alloc third", ""); ft_free(ptr1); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder1", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder1", ""); ft_free(ptr3); - test(0, (const void*) (uintptr_t) (raw_get_chunk(allocs_tree[TINY], ptr2) == NULL), "free disorder2", ""); + test(0, (const void*) (uintptr_t) (raw_get_chunk_by_root(allocs_tree[TINY], ptr2) == NULL), "free disorder2", ""); ft_free(ptr2); test(NULL, allocs_tree[TINY], "free disorder3", ""); show_alloc_mem();