Skip to content

Commit

Permalink
Allow multiple tinyalloc heaps (eliminate static variables)
Browse files Browse the repository at this point in the history
Multiple heaps can be useful for dividing memory into multiple areas
that can be used for different purposes, with individual limits.

- Add a config struct (ta_cfg_t, can be declared const)
- Pass config pointer to all tinyalloc functions
  • Loading branch information
jlindgren90 committed Mar 23, 2024
1 parent ccdaa00 commit 9cab682
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 59 deletions.
95 changes: 46 additions & 49 deletions tinyalloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "tinyalloc.h"

#include <stdint.h>

#ifdef TA_DEBUG
Expand Down Expand Up @@ -30,19 +31,13 @@ typedef struct {
size_t top; // top free addr
} Heap;

static Heap *heap = NULL;
static const void *heap_limit = NULL;
static size_t heap_split_thresh;
static size_t heap_alignment;
static size_t heap_max_blocks;

/**
* If compaction is enabled, inserts block
* into free list, sorted by addr.
* If disabled, add block has new head of
* the free list.
*/
static void insert_block(Block *block) {
static void insert_block(Heap *heap, Block *block) {
#ifndef TA_DISABLE_COMPACT
Block *ptr = heap->free;
Block *prev = NULL;
Expand Down Expand Up @@ -72,7 +67,7 @@ static void insert_block(Block *block) {
}

#ifndef TA_DISABLE_COMPACT
static void release_blocks(Block *scan, Block *to) {
static void release_blocks(Heap *heap, Block *scan, Block *to) {
Block *scan_next;
while (scan != to) {
print_s("release");
Expand All @@ -86,7 +81,7 @@ static void release_blocks(Block *scan, Block *to) {
}
}

static void compact() {
static void compact(Heap *heap) {
Block *ptr = heap->free;
Block *prev;
Block *scan;
Expand All @@ -108,7 +103,7 @@ static void compact() {
ptr->size = new_size;
Block *next = prev->next;
// make merged blocks available
release_blocks(ptr->next, prev->next);
release_blocks(heap, ptr->next, prev->next);
// relink
ptr->next = next;
}
Expand All @@ -117,32 +112,27 @@ static void compact() {
}
#endif

bool ta_init(const void *base, const void *limit, const size_t heap_blocks, const size_t split_thresh, const size_t alignment) {
heap = (Heap *)base;
heap_limit = limit;
heap_split_thresh = split_thresh;
heap_alignment = alignment;
heap_max_blocks = heap_blocks;

heap->free = NULL;
heap->used = NULL;
heap->fresh = (Block *)(heap + 1);
heap->top = (size_t)(heap->fresh + heap_blocks);
void ta_init(const ta_cfg_t *cfg) {
Heap *heap = (Heap *)cfg->base;
heap->free = NULL;
heap->used = NULL;
heap->fresh = (Block *)(heap + 1);
heap->top = (size_t)(heap->fresh + cfg->max_blocks);

Block *block = heap->fresh;
size_t i = heap_max_blocks - 1;
size_t i = cfg->max_blocks - 1;
while (i--) {
block->next = block + 1;
block++;
}
block->next = NULL;
return true;
}

bool ta_free(void *free) {
bool ta_free(const ta_cfg_t *cfg, void *free) {
if (free == NULL) {
return false;
}
Heap *heap = (Heap *)cfg->base;
Block *block = heap->used;
Block *prev = NULL;
while (block != NULL) {
Expand All @@ -152,9 +142,9 @@ bool ta_free(void *free) {
} else {
heap->used = block->next;
}
insert_block(block);
insert_block(heap, block);
#ifndef TA_DISABLE_COMPACT
compact();
compact(heap);
#endif
return true;
}
Expand All @@ -164,17 +154,19 @@ bool ta_free(void *free) {
return false;
}

static Block *alloc_block(size_t num) {
static Block *alloc_block(const ta_cfg_t *cfg, size_t num) {
Heap *heap = (Heap *)cfg->base;
Block *ptr = heap->free;
Block *prev = NULL;
size_t top = heap->top;
size_t orig = num;
num = (num + heap_alignment - 1) & -heap_alignment;
num = (num + cfg->alignment - 1) & -cfg->alignment;
if (num < orig) {
return NULL; // overflow
}
while (ptr != NULL) {
const int is_top = ((size_t)ptr->addr + ptr->size >= top) && ((size_t)ptr->addr + num <= (size_t)heap_limit);
const int is_top = ((size_t)ptr->addr + ptr->size >= top) &&
((size_t)ptr->addr + num <= (size_t)cfg->limit);
if (is_top || ptr->size >= num) {
if (prev != NULL) {
prev->next = ptr->next;
Expand All @@ -190,17 +182,17 @@ static Block *alloc_block(size_t num) {
#ifndef TA_DISABLE_SPLIT
} else if (heap->fresh != NULL) {
size_t excess = ptr->size - num;
if (excess >= heap_split_thresh) {
if (excess >= cfg->split_thresh) {
ptr->size = num;
Block *split = heap->fresh;
heap->fresh = split->next;
split->addr = (void *)((size_t)ptr->addr + num);
print_s("split");
print_i((size_t)split->addr);
split->size = excess;
insert_block(split);
insert_block(heap, split);
#ifndef TA_DISABLE_COMPACT
compact();
compact(heap);
#endif
}
#endif
Expand All @@ -213,7 +205,7 @@ static Block *alloc_block(size_t num) {
// no matching free blocks
// see if any other blocks available
size_t new_top = top + num;
if (heap->fresh != NULL && new_top <= (size_t)heap_limit) {
if (heap->fresh != NULL && new_top <= (size_t)cfg->limit) {
ptr = heap->fresh;
heap->fresh = ptr->next;
ptr->addr = (void *)top;
Expand All @@ -226,8 +218,8 @@ static Block *alloc_block(size_t num) {
return NULL;
}

void *ta_alloc(size_t num) {
Block *block = alloc_block(num);
void *ta_alloc(const ta_cfg_t *cfg, size_t num) {
Block *block = alloc_block(cfg, num);
if (block != NULL) {
return block->addr;
}
Expand All @@ -254,13 +246,13 @@ static void memclear(void *ptr, size_t num) {
}
#endif

void *ta_calloc(size_t num, size_t size) {
void *ta_calloc(const ta_cfg_t *cfg, size_t num, size_t size) {
size_t orig = num;
num *= size;
if (size != 0 && num / size != orig) {
goto overflow;
}
Block *block = alloc_block(num);
Block *block = alloc_block(cfg, num);
if (block != NULL) {
memclear(block->addr, block->size);
return block->addr;
Expand All @@ -281,26 +273,31 @@ static size_t count_blocks(Block *ptr) {
return num;
}

size_t ta_num_free() {
size_t ta_num_free(const ta_cfg_t *cfg) {
Heap *heap = (Heap *)cfg->base;
return count_blocks(heap->free);
}

size_t ta_num_used() {
size_t ta_num_used(const ta_cfg_t *cfg) {
Heap *heap = (Heap *)cfg->base;
return count_blocks(heap->used);
}

size_t ta_num_fresh() {
size_t ta_num_fresh(const ta_cfg_t *cfg) {
Heap *heap = (Heap *)cfg->base;
return count_blocks(heap->fresh);
}

bool ta_check() {
return heap_max_blocks == ta_num_free() + ta_num_used() + ta_num_fresh();
bool ta_check(const ta_cfg_t *cfg) {
return cfg->max_blocks ==
ta_num_free(cfg) + ta_num_used(cfg) + ta_num_fresh(cfg);
}

size_t ta_getsize(void *ptr) {
size_t ta_getsize(const ta_cfg_t *cfg, void *ptr) {
if (ptr == NULL) {
return 0;
}
Heap *heap = (Heap *)cfg->base;
Block *block = heap->used;
while (block != NULL) {
if (ptr == block->addr) {
Expand All @@ -313,21 +310,21 @@ size_t ta_getsize(void *ptr) {

/* requires memcpy() */
#ifdef TA_USE_STDLIB
void *ta_realloc(void *ptr, size_t num) {
void *ta_realloc(const ta_cfg_t *cfg, void *ptr, size_t num) {
if (ptr == NULL) {
return ta_alloc(num);
return ta_alloc(cfg, num);
} else if (num == 0) {
ta_free(ptr);
ta_free(cfg, ptr);
return NULL;
}
size_t size = ta_getsize(ptr);
Block *block = alloc_block(num);
size_t size = ta_getsize(cfg, ptr);
Block *block = alloc_block(cfg, num);
if (block != NULL) {
if (size > num) {
size = num;
}
memcpy(block->addr, ptr, size);
ta_free(ptr);
ta_free(cfg, ptr);
return block->addr;
}
errno = ENOMEM;
Expand Down
28 changes: 18 additions & 10 deletions tinyalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ extern "C" {
#include <stdbool.h>
#include <stddef.h>

bool ta_init(const void *base, const void *limit, const size_t heap_blocks, const size_t split_thresh, const size_t alignment);
void *ta_alloc(size_t num);
void *ta_calloc(size_t num, size_t size);
bool ta_free(void *ptr);
size_t ta_getsize(void *ptr);
typedef struct {
void *base;
void *limit;
size_t max_blocks;
size_t split_thresh;
size_t alignment;
} ta_cfg_t;

void ta_init(const ta_cfg_t *cfg);
void *ta_alloc(const ta_cfg_t *cfg, size_t num);
void *ta_calloc(const ta_cfg_t *cfg, size_t num, size_t size);
bool ta_free(const ta_cfg_t *cfg, void *ptr);
size_t ta_getsize(const ta_cfg_t *cfg, void *ptr);

/* requires memcpy() */
#ifdef TA_USE_STDLIB
void *ta_realloc(void *ptr, size_t num);
void *ta_realloc(const ta_cfg_t *cfg, void *ptr, size_t num);
#endif

size_t ta_num_free();
size_t ta_num_used();
size_t ta_num_fresh();
bool ta_check();
size_t ta_num_free(const ta_cfg_t *cfg);
size_t ta_num_used(const ta_cfg_t *cfg);
size_t ta_num_fresh(const ta_cfg_t *cfg);
bool ta_check(const ta_cfg_t *cfg);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 9cab682

Please sign in to comment.