From 61b1a16725ca75cb4159928db5d6c4b9690369a3 Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 31 Jul 2024 14:00:30 +0200 Subject: [PATCH] split free, malloc, realloc --- src/free.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/malloc.c | 78 ------------------------------------------------ src/realloc.c | 0 3 files changed, 82 insertions(+), 78 deletions(-) create mode 100644 src/free.c create mode 100644 src/realloc.c diff --git a/src/free.c b/src/free.c new file mode 100644 index 0000000..1b22556 --- /dev/null +++ b/src/free.c @@ -0,0 +1,82 @@ +#include "chunk.h" +#include "malloc.h" + +#include + +void free_tiny(chunk_t *chunk) +{ + chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; + + if (chunk->prev) + { + chunk_read(chunk->prev, &chunk_prev); + if (chunk_prev.is_used == false && chunk_prev.block_id == chunk->block_id) + first_chunk = &chunk_prev; + } + if (chunk->next) + { + chunk_read(chunk->next, &chunk_next); + if (chunk_next.is_used == false && chunk_next.block_id == chunk->block_id) + last_chunk = &chunk_next; + } + + first_chunk->is_used = false; + chunk_merge(first_chunk, last_chunk); + + if (is_alone_on_block(first_chunk)) + { + if (first_chunk->current == allocs_tree[TINY]) + allocs_tree[TINY] = last_chunk->next; + destroy_block(first_chunk); + } +} + +void free_small(chunk_t *chunk) +{ + (void) chunk; +} + +void free_large(chunk_t *chunk) +{ + if (chunk->current == allocs_tree[LARGE]) + allocs_tree[LARGE] = chunk->next; + destroy_block(chunk); +} + +static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large}; + +void ft_free(void *ptr) +{ + void **root; + void *raw_chunk; + chunk_t chunk; + size_t i; + + if (ptr == NULL) + return; + + for (i = TINY; i <= LARGE + 1; i++) + { +#ifndef DEBUG + if (i > LARGE) + write(2, "chunk not found\n", 16); +#endif + root = allocs_tree[i]; + + raw_chunk = raw_get_chunk(root, ptr); + if (raw_chunk == NULL) + continue; + + break; + } + + chunk_read(raw_chunk, &chunk); + +#ifndef 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); +} \ No newline at end of file diff --git a/src/malloc.c b/src/malloc.c index 6ec3327..2d263ff 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -66,82 +66,4 @@ void *ft_malloc(size_t size) if (size < m) return alloc_small(size); return alloc_large(size); -} - -void free_tiny(chunk_t *chunk) -{ - chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; - - if (chunk->prev) - { - chunk_read(chunk->prev, &chunk_prev); - if (chunk_prev.is_used == false && chunk_prev.block_id == chunk->block_id) - first_chunk = &chunk_prev; - } - if (chunk->next) - { - chunk_read(chunk->next, &chunk_next); - if (chunk_next.is_used == false && chunk_next.block_id == chunk->block_id) - last_chunk = &chunk_next; - } - - first_chunk->is_used = false; - chunk_merge(first_chunk, last_chunk); - - if (is_alone_on_block(first_chunk)) - { - if (first_chunk->current == allocs_tree[TINY]) - allocs_tree[TINY] = last_chunk->next; - destroy_block(first_chunk); - } -} - -void free_small(chunk_t *chunk) -{ - (void) chunk; -} - -void free_large(chunk_t *chunk) -{ - if (chunk->current == allocs_tree[LARGE]) - allocs_tree[LARGE] = chunk->next; - destroy_block(chunk); -} - -static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large}; - -void ft_free(void *ptr) -{ - void **root; - void *raw_chunk; - chunk_t chunk; - size_t i; - - if (ptr == NULL) - return; - - for (i = TINY; i <= LARGE + 1; i++) - { -#ifndef DEBUG - if (i > LARGE) - write(2, "chunk not found\n", 16); -#endif - root = allocs_tree[i]; - - raw_chunk = raw_get_chunk(root, ptr); - if (raw_chunk == NULL) - continue; - - break; - } - - chunk_read(raw_chunk, &chunk); - -#ifndef 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); } \ No newline at end of file diff --git a/src/realloc.c b/src/realloc.c new file mode 100644 index 0000000..e69de29