diff --git a/Makefile b/Makefile index 9a1574b..4d696f8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ CFLAGS += -std=c11 -g -Wall -Wextra -ftrapv -Wshadow -Wundef -Wcast-align -Wunreachable-code -O1 - SRC = src/*.c TEST_SRC = test/*.c diff --git a/include/mem_pool.h b/include/mem_pool.h index 1f94e73..8166b0c 100644 --- a/include/mem_pool.h +++ b/include/mem_pool.h @@ -11,7 +11,8 @@ * Pass this as the second argument to pool_variable_init if you wan to skip * best fit checks */ -#define MEM_POOL_NO_BEST_FIT -1 +/* #define MEM_POOL_NO_BEST_FIT 101 */ +static const uint16_t MEM_POOL_NO_BEST_FIT = 101; typedef struct FixedMemPool FixedMemPool; @@ -68,8 +69,9 @@ MemPoolError pool_fixed_destroy(FixedMemPool *pool); /** * grow_size deremines the size of a new buffer required from malloc when no more free (fitting) space left * tolerance_percent is the maximum difference in percentage when looking for best fitting free blocks + * if not reuqired, use MEM_POOL_NO_BEST_FIT */ -MemPoolError pool_variable_init(VariableMemPool **pool, size_t grow_size, int16_t tolerance_percent); +MemPoolError pool_variable_init(VariableMemPool **pool, size_t grow_size, uint16_t tolerance_percent); MemPoolError pool_variable_alloc(VariableMemPool *pool, size_t size, void **ptr); diff --git a/src/internals.h b/src/internals.h index df435df..85beeea 100644 --- a/src/internals.h +++ b/src/internals.h @@ -46,10 +46,10 @@ typedef struct Buffer Buffer; struct Buffer { - void *start; - void *prev_ptr; - void *curr_ptr; /* This is only tracked for variadic blocks */ - void *end; + char *start; + char *prev_ptr; + char *curr_ptr; /* This is only tracked for variadic blocks */ + char *end; Buffer *next; }; @@ -79,7 +79,7 @@ static inline bool buffer_has_space(Buffer *buff, size_t size) return (char *)buff->end - (char *)buff->curr_ptr >= (long)size; } -static inline bool buffer_has(Buffer *buff, void *ptr) +static inline bool buffer_has(Buffer *buff, char *ptr) { return ptr >= buff->start && ptr <= buff->end; } diff --git a/src/variable.c b/src/variable.c index 24499d7..63ae631 100644 --- a/src/variable.c +++ b/src/variable.c @@ -10,7 +10,7 @@ struct SizedBlock { struct VariableMemPool { size_t buff_size; size_t header_size; - int16_t tolerance_percent; + uint16_t tolerance_percent; Buffer *buff_head; Buffer *buff_last; SizedBlock *block_head; @@ -18,13 +18,14 @@ struct VariableMemPool { }; -MemPoolError pool_variable_init(VariableMemPool **pool, size_t grow_size, int16_t tolerance_percent) +MemPoolError pool_variable_init(VariableMemPool **pool, size_t grow_size, uint16_t tolerance_percent) { *pool = malloc(sizeof(VariableMemPool)); if (!*pool) { return MEM_POOL_ERR_MALLOC; } - (*pool)->tolerance_percent = max(MEM_POOL_NO_BEST_FIT, tolerance_percent); + /* (*pool)->tolerance_percent = tolerance_percent <= MEM_POOL_NO_BEST_FIT ? MEM_POOL_NO_BEST_FIT : tolerance_percent; */ + (*pool)->tolerance_percent = tolerance_percent >= MEM_POOL_NO_BEST_FIT ? MEM_POOL_NO_BEST_FIT : tolerance_percent; (*pool)->header_size = mem_align(sizeof(Header)); (*pool)->buff_size = grow_size; (*pool)->buff_head = buffer_new(grow_size); @@ -43,9 +44,9 @@ MemPoolError pool_variable_init(VariableMemPool **pool, size_t grow_size, int16_ static void *from_buffer(Buffer *buff, size_t header_size, size_t block_size) { - Header *header = buff->curr_ptr; + Header *header = (void *) buff->curr_ptr; header->size = block_size; - header->prev_in_buff = buff->prev_ptr; + header->prev_in_buff = (void *) buff->prev_ptr; buff->prev_ptr = buff->curr_ptr; buff->curr_ptr += (header_size + block_size); @@ -56,13 +57,14 @@ static void *from_buffer(Buffer *buff, size_t header_size, size_t block_size) static void *best_fit_from_free_list(VariableMemPool *pool, size_t required_size) { SizedBlock **curr = &pool->block_head; - int64_t block_size, diff; - int16_t diff_percent; + size_t block_size; + long diff; + size_t diff_percent; while (*curr) { block_size = (*curr)->header.size; - diff = labs(block_size - (long)required_size); - diff_percent = (diff * 100) / ((block_size + required_size) / 2); + diff = labs((long) block_size - (long) required_size); + diff_percent = ((size_t) diff * 100) / ((block_size + required_size) / 2); if (MEM_POOL_NO_BEST_FIT == pool->tolerance_percent || diff_percent <= pool->tolerance_percent) { SizedBlock *block = *curr; @@ -132,7 +134,7 @@ static SizedBlock *append(SizedBlock *to, SizedBlock *from, size_t header_size) static SizedBlock *merge_next_free_blocks(VariableMemPool *pool, Buffer *buff, SizedBlock *block) { - SizedBlock *next = NULL; + void *next = NULL; while (1) { next = (SizedBlock *)((char *)block + block->header.size + pool->header_size); diff --git a/test/minunit.h b/test/minunit/minunit.h similarity index 100% rename from test/minunit.h rename to test/minunit/minunit.h