diff --git a/include/malloc.h b/include/malloc.h index 2451f36..a7da037 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -2,6 +2,8 @@ #include +#define DEBUG + void ft_free(void *ptr); void *ft_malloc(size_t size); void *ft_realloc(void *ptr, size_t size); diff --git a/src/block.c b/src/block.c index 2983b01..8d922ff 100644 --- a/src/block.c +++ b/src/block.c @@ -7,7 +7,8 @@ #include #include "align.h" -#include "./chunk.h" +#include "chunk.h" +#include "chunk_manager.h" int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used) { @@ -19,7 +20,7 @@ int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_u new_block = mmap(NULL, size + ALIGN_MARGING + CHUNK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - init_chunk(chunk, size + ALIGN_MARGING - get_align_increment(new_block + CHUNK_SIZE), block_id, prev, new_block, next, is_used); + chunk_init(chunk, size + ALIGN_MARGING - get_align_increment(new_block + CHUNK_SIZE), block_id, prev, new_block, next, is_used); if (prev != NULL) { @@ -31,4 +32,41 @@ int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_u block_id++; return 0; +} + +void destroy_block(chunk_t *chunk) +{ + chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; + + if (chunk->prev != NULL) + { + chunk_read(chunk->prev, &chunk_prev); + if (chunk_prev.block_id == chunk->block_id && chunk_prev.is_used == false) + chunk_merge(&chunk_prev, chunk); + first_chunk = &chunk_prev; + } + if (chunk->next != NULL) + { + chunk_read(chunk->next, &chunk_next); + if (chunk_next.block_id == chunk->block_id && chunk_next.is_used == false) + chunk_merge(chunk, &chunk_next); + last_chunk = &chunk_next; + } + + if (last_chunk != chunk) + { + last_chunk->prev = chunk->prev; + chunk_write(last_chunk); + } + + chunk->is_used = false; + chunk_write(chunk); + + if (first_chunk != chunk) + { + first_chunk->next = chunk->next; + chunk_write(first_chunk); + } + + munmap(chunk->current, get_physical_size(chunk)); } \ No newline at end of file diff --git a/src/block.h b/src/block.h index e336612..8dcf9c5 100644 --- a/src/block.h +++ b/src/block.h @@ -4,4 +4,5 @@ #include "chunk.h" -int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used); \ No newline at end of file +int add_new_block(chunk_t* chunk, size_t size, void *prev, void *next, bool is_used); +void destroy_block(chunk_t *chunk); \ No newline at end of file diff --git a/src/chunk.c b/src/chunk.c index a80ef4d..cb586e9 100644 --- a/src/chunk.c +++ b/src/chunk.c @@ -30,7 +30,7 @@ void chunk_read(void * const *block, chunk_t *chunk) chunk->data_start = block[CHUNK_DATA_START_POS]; } -void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used) +void chunk_init(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used) { void *data_start = current + CHUNK_SIZE; diff --git a/src/chunk.h b/src/chunk.h index ee61d91..6ab803e 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -33,6 +33,6 @@ enum { void chunk_read(void * const *block, chunk_t *chunk); void chunk_write(const chunk_t *chunk); -void init_chunk(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used); +void chunk_init(chunk_t *chunk, size_t size, long block_id, void *prev, void *current, void *next, bool is_used); size_t get_physical_size(const chunk_t *chunk); void chunk_print(const chunk_t *chunk); \ No newline at end of file diff --git a/src/chunk_manager.c b/src/chunk_manager.c index dae1357..9696528 100644 --- a/src/chunk_manager.c +++ b/src/chunk_manager.c @@ -1,5 +1,6 @@ #include "chunk.h" #include "align.h" +#include "print.h" int get_unused_chunk(chunk_t *root, size_t size, chunk_t *result) { @@ -21,8 +22,10 @@ int get_unused_chunk(chunk_t *root, size_t size, chunk_t *result) void chunk_merge(chunk_t *first, chunk_t *second) { - //if (first->block_id != second->block_id) - +#ifndef DEBUG + if (first->block_id != second->block_id) + putstr("Attempt merge two chunk on different block\n"); +#endif first->size = first->size + second->size + CHUNK_SIZE + ALIGN_MARGING - get_align_increment(first->next); first->next = second->next; } @@ -35,8 +38,8 @@ int chunk_split(chunk_t *chunk, chunk_t *new_chunk, size_t new_size) void *new_chunk_pos = chunk->data_start + new_size; - init_chunk(new_chunk, chunk->size - new_size - CHUNK_SIZE - get_align_increment(new_chunk_pos + CHUNK_SIZE), chunk->block_id, chunk->current, new_chunk_pos, chunk->next, true); - init_chunk(chunk, new_size, chunk->block_id, chunk->prev, chunk->current, new_chunk->current, false); + chunk_init(new_chunk, chunk->size - new_size - CHUNK_SIZE - get_align_increment(new_chunk_pos + CHUNK_SIZE), chunk->block_id, chunk->current, new_chunk_pos, chunk->next, true); + chunk_init(chunk, new_size, chunk->block_id, chunk->prev, chunk->current, new_chunk->current, false); return 0; } @@ -52,39 +55,4 @@ void chunk_iter(chunk_t *root, void (*f)(chunk_t*)) break; chunk_read(current->next, current); } -} - -void destroy_chunk(chunk_t *chunk) -{ - chunk_t chunk_prev, chunk_next, *first_chunk = chunk, *last_chunk = chunk; - - if (chunk->prev != NULL) - { - chunk_read(chunk->prev, &chunk_prev); - if (chunk_prev.block_id == chunk->block_id && chunk_prev.is_used == false) - chunk_merge(&chunk_prev, chunk); - first_chunk = &chunk_prev; - } - if (chunk->next != NULL) - { - chunk_read(chunk->next, &chunk_next); - if (chunk_next.block_id == chunk->block_id && chunk_next.is_used == false) - chunk_merge(chunk, &chunk_next); - last_chunk = &chunk_next; - } - - if (last_chunk != chunk) - { - last_chunk->prev = chunk->prev; - chunk_write(last_chunk); - } - - chunk->is_used = false; - chunk_write(chunk); - - if (first_chunk != chunk) - { - first_chunk->next = chunk->next; - chunk_write(first_chunk); - } } \ No newline at end of file diff --git a/src/chunk_manager.h b/src/chunk_manager.h index 86d1dd7..2df733a 100644 --- a/src/chunk_manager.h +++ b/src/chunk_manager.h @@ -2,5 +2,5 @@ #include "chunk.h" -void destroy_chunk(chunk_t *chunk); -void chunk_iter(chunk_t *root, void (*f)(chunk_t*)); \ No newline at end of file +void chunk_iter(chunk_t *root, void (*f)(chunk_t*)); +void chunk_merge(chunk_t *first, chunk_t *second); \ No newline at end of file diff --git a/src/malloc.c b/src/malloc.c index a620441..8fbd08e 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -52,7 +52,6 @@ void *ft_malloc(size_t size) if (size < m) return alloc_small(size); return alloc_large(size); - //mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); } void free_tiny(chunk_t *chunk) @@ -69,8 +68,7 @@ void free_large(chunk_t *chunk) { if (chunk->current == allocs_tree[LARGE]) allocs_tree[LARGE] = chunk->next; - destroy_chunk(chunk); - munmap(chunk->current, get_physical_size(chunk)); + destroy_block(chunk); } static void (*free_funcs[3])(chunk_t*) = {free_tiny, free_small, free_large};