diff options
| author | Karsten Schmidt <k@postspectacular.com> | 2017-07-14 14:39:23 +0100 |
|---|---|---|
| committer | Karsten Schmidt <k@postspectacular.com> | 2017-07-14 14:39:23 +0100 |
| commit | ad15f9bba1efece0c8954484900e9e2c5338f711 (patch) | |
| tree | 9f51be310605700dd18d6fbf22f09d9cea8907f9 | |
| parent | 3184e82a0d8af3de2d97a01e8fe3f6f9796da275 (diff) | |
add talloc.h, rename types & functions
| -rw-r--r-- | talloc.c (renamed from malloc.c) | 123 | ||||
| -rw-r--r-- | talloc.h | 31 |
2 files changed, 83 insertions, 71 deletions
@@ -1,55 +1,34 @@ -#include <stdbool.h> -#include <stddef.h> - -#ifndef CT_HEAP_ALIGN -#define CT_HEAP_ALIGN 8 -#endif - -#ifndef CT_HEAP_BASE -#define CT_HEAP_BASE 0x400 -#endif - -#ifndef CT_HEAP_START -#define CT_HEAP_START 0x444 -#endif - -#ifndef CT_HEAP_LIMIT -#define CT_HEAP_LIMIT (1 << 24) -#endif - -#ifndef CT_HEAP_BLOCKS -#define CT_HEAP_BLOCKS 0x4 -#endif +#include "talloc.h" extern void print_s(char *); extern void print_i(size_t); extern void print_f(float); -typedef struct CT_Block CT_Block; +typedef struct Block Block; -struct CT_Block { +struct Block { void *addr; - CT_Block *next; + Block *next; size_t size; }; typedef struct { - CT_Block *free; // first free block - CT_Block *used; // first used block - CT_Block *avail; // first available blank block - size_t top; // top free addr - size_t limit; // heap limit - CT_Block blocks[CT_HEAP_BLOCKS]; -} CT_Heap; + Block *free; // first free block + Block *used; // first used block + Block *avail; // first available blank block + size_t top; // top free addr + size_t limit; // heap limit + Block blocks[TA_HEAP_BLOCKS]; +} Heap; -static CT_Heap *heap = (CT_Heap *)CT_HEAP_BASE; +static Heap *heap = (Heap *)TA_BASE; /** * Insert block into free list, sorted by addr. */ -static void insert_block(CT_Block *block) { - CT_Block *ptr = heap->free; - CT_Block *prev = NULL; +static void insert_block(Block *block) { + Block *ptr = heap->free; + Block *prev = NULL; while (ptr != NULL) { if ((size_t)block->addr <= (size_t)ptr->addr) { print_s("insert"); @@ -71,8 +50,8 @@ static void insert_block(CT_Block *block) { block->next = ptr; } -static void release_blocks(CT_Block *scan, CT_Block *to) { - CT_Block *scan_next; +static void release_blocks(Block *scan, Block *to) { + Block *scan_next; while (scan != to) { print_s("release"); print_i((size_t)scan); @@ -86,16 +65,16 @@ static void release_blocks(CT_Block *scan, CT_Block *to) { } static void compress() { - CT_Block *ptr = heap->free; - CT_Block *prev = NULL; - CT_Block *scan; + Block *ptr = heap->free; + Block *prev = NULL; + Block *scan; while (ptr != NULL) { prev = ptr; scan = ptr->next; size_t base = (size_t)ptr->addr; while (scan != NULL && (size_t)prev->addr + prev->size == (size_t)scan->addr) { - print_s("match"); + print_s("merge"); print_i((size_t)scan); prev = scan; scan = scan->next; @@ -104,8 +83,8 @@ static void compress() { size_t new_size = prev->addr + prev->size - ptr->addr; print_s("new size"); print_i(new_size); - ptr->size = new_size; - CT_Block *next = prev->next; + ptr->size = new_size; + Block *next = prev->next; // make merged blocks available release_blocks(ptr->next, prev->next); // relink @@ -117,22 +96,23 @@ static void compress() { } } -void ct_heap_init() { - heap->free = NULL; - heap->used = NULL; - heap->avail = heap->blocks; - heap->top = CT_HEAP_BASE + sizeof(CT_Heap); - heap->limit = CT_HEAP_LIMIT; - CT_Block *block = heap->blocks; - for (size_t i = CT_HEAP_BLOCKS - 1; i > 0; i--) { +bool talloc_init() { + heap->free = NULL; + heap->used = NULL; + heap->avail = heap->blocks; + heap->top = TA_BASE + sizeof(Heap); + heap->limit = TA_HEAP_LIMIT; + Block *block = heap->blocks; + for (size_t i = TA_HEAP_BLOCKS - 1; i > 0; i--) { block->next = block + 1; block++; } + return true; } -bool ct_free(void *free) { - CT_Block *block = heap->used; - CT_Block *prev = NULL; +bool talloc_free(void *free) { + Block *block = heap->used; + Block *prev = NULL; while (block != NULL) { if (free == block->addr) { if (prev) { @@ -150,11 +130,11 @@ bool ct_free(void *free) { return false; } -void *ct_malloc(size_t num) { - CT_Block *ptr = heap->free; - CT_Block *prev = NULL; - size_t top = heap->top; - num = (num + CT_HEAP_ALIGN - 1) & -CT_HEAP_ALIGN; +void *talloc(size_t num) { + Block *ptr = heap->free; + Block *prev = NULL; + size_t top = heap->top; + num = (num + TA_ALIGN - 1) & -TA_ALIGN; while (ptr != NULL) { const int is_top = (size_t)ptr->addr + ptr->size >= top; if (is_top || ptr->size >= num) { @@ -171,11 +151,11 @@ void *ct_malloc(size_t num) { heap->top = (size_t)ptr->addr + num; } else if (heap->avail != NULL) { size_t excess = ptr->size - num; - if (excess >= CT_HEAP_ALIGN) { - ptr->size = num; - CT_Block *split = heap->avail; - heap->avail = split->next; - split->addr = ptr->addr + num; + if (excess >= TA_ALIGN) { + ptr->size = num; + Block *split = heap->avail; + heap->avail = split->next; + split->addr = ptr->addr + num; print_s("split"); print_i((size_t)split->addr); split->size = excess; @@ -204,7 +184,7 @@ void *ct_malloc(size_t num) { return NULL; } -static size_t count_blocks(CT_Block *ptr) { +static size_t count_blocks(Block *ptr) { size_t num = 0; while (ptr != NULL) { num++; @@ -213,18 +193,19 @@ static size_t count_blocks(CT_Block *ptr) { return num; } -size_t ct_num_free() { +size_t talloc_num_free() { return count_blocks(heap->free); } -size_t ct_num_used() { +size_t talloc_num_used() { return count_blocks(heap->used); } -size_t ct_num_avail() { +size_t talloc_num_avail() { return count_blocks(heap->avail); } -bool ct_check() { - return ct_num_free() + ct_num_used() + ct_num_avail() == CT_HEAP_BLOCKS; +bool talloc_check() { + return TA_HEAP_BLOCKS == + talloc_num_free() + talloc_num_used() + talloc_num_avail(); } diff --git a/talloc.h b/talloc.h new file mode 100644 index 0000000..f96cc43 --- /dev/null +++ b/talloc.h @@ -0,0 +1,31 @@ +#include <stdbool.h> +#include <stddef.h> + +#ifndef TA_ALIGN +#define TA_ALIGN 8 +#endif + +#ifndef TA_BASE +#define TA_BASE 0x400 +#endif + +#ifndef TA_HEAP_START +#define TA_HEAP_START 0x444 +#endif + +#ifndef TA_HEAP_LIMIT +#define TA_HEAP_LIMIT (1 << 24) +#endif + +#ifndef TA_HEAP_BLOCKS +#define TA_HEAP_BLOCKS 0x4 +#endif + +bool talloc_init(); +void *talloc(size_t num); +bool talloc_free(void *ptr); + +size_t talloc_num_free(); +size_t talloc_num_used(); +size_t talloc_num_avail(); +bool talloc_check(); |
