Skip to content

Commit

Permalink
fixing compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Isty001 committed May 12, 2019
1 parent f458c01 commit b61bf87
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 4 additions & 2 deletions include/mem_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 5 additions & 5 deletions src/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 12 additions & 10 deletions src/variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ 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;
pthread_mutex_t mutex;
};


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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
File renamed without changes.

0 comments on commit b61bf87

Please sign in to comment.