diff options
author | Danny Holman <dholman@gymli.org> | 2025-03-20 14:11:53 -0500 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2025-03-20 14:11:53 -0500 |
commit | 7281fe8fdbb64a83bc0f569ef01db33a1f26ac98 (patch) | |
tree | 468c67d71db30716795b9145ddb963794ab7f891 /core/alloc.c | |
parent | 65462e5eff7d952d97d30bc3e3720d7f6d8a402e (diff) |
core: alloc: make the mem_block type opaque
Make the mem_block struct into a fully opaque type. This brings the
allocation manager in line with project coding style.
Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'core/alloc.c')
-rw-r--r-- | core/alloc.c | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/core/alloc.c b/core/alloc.c index bec36bc..0ed705c 100644 --- a/core/alloc.c +++ b/core/alloc.c @@ -1,3 +1,24 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman <dholman@gymli.org> + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + #include <rune/core/alloc.h> #include <rune/core/logging.h> #include <stdlib.h> @@ -7,13 +28,13 @@ #define DEADBLOCK ((void*)0xDEADBEEF) -static struct mem_block first_block; +static mem_block_t first_block; -static struct mem_block* _find_free_block(size_t sz) { - struct list_head *temp = &first_block.list; - struct mem_block *block; +static mem_block_t* _find_free_block(size_t sz) { + list_head_t *temp = &first_block.list; + mem_block_t *block; while (temp != NULL) { - block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list)); + block = (mem_block_t*)((void*)temp - offsetof(mem_block_t, list)); if (block->sz == sz && block->free == 1) return block; temp = temp->next; @@ -21,11 +42,11 @@ static struct mem_block* _find_free_block(size_t sz) { return NULL; } -static struct mem_block* _find_block(void *ptr) { - struct list_head *temp = &first_block.list; - struct mem_block *block; +static mem_block_t* _find_block(void *ptr) { + list_head_t *temp = &first_block.list; + mem_block_t *block; while (temp != NULL) { - block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list)); + block = (mem_block_t*)((void*)temp - offsetof(mem_block_t, list)); if (block->ptr == ptr) return block; temp = temp->next; @@ -33,19 +54,19 @@ static struct mem_block* _find_block(void *ptr) { return NULL; } -static struct mem_block* _alloc_block(size_t sz) { +static mem_block_t* _alloc_block(size_t sz) { if (first_block.ptr == NULL) { first_block.ptr == DEADBLOCK; first_block.sz = 0; } - struct mem_block *ret = _find_free_block(sz); + mem_block_t *ret = _find_free_block(sz); if (ret != NULL) { ret->free = 0; return ret->ptr; } - ret = malloc(sizeof(struct mem_block)); + ret = malloc(sizeof(mem_block_t)); if (ret == NULL) { log_output(LOG_ERROR, "Cannot allocate block of size %d", sz); return NULL; @@ -59,7 +80,7 @@ static struct mem_block* _alloc_block(size_t sz) { return ret; } -static void _free_block(struct mem_block *block, int hard) { +static void _free_block(mem_block_t *block, int hard) { if (hard == 1) { list_del(&block->list); log_output(LOG_DEBUG, "Freed block of size %d", block->sz); @@ -73,7 +94,7 @@ void* rune_alloc(size_t sz) { if (sz == 0) return NULL; - struct mem_block *block = _find_free_block(sz); + mem_block_t *block = _find_free_block(sz); if (block != NULL) { block->free = 0; return block->ptr; @@ -87,7 +108,7 @@ void* rune_calloc(size_t nmemb, size_t sz) { if (sz == 0) return NULL; - struct mem_block *block = _find_free_block(sz); + mem_block_t *block = _find_free_block(sz); if (block != NULL) { block->free = 0; return block->ptr; @@ -102,8 +123,8 @@ void* rune_realloc(void *ptr, size_t sz) { if (ptr == NULL || sz == 0) return rune_alloc(sz); - struct mem_block *old = _find_block(ptr); - struct mem_block *new = _alloc_block(sz); + mem_block_t *old = _find_block(ptr); + mem_block_t *new = _alloc_block(sz); memcpy(new->ptr, old->ptr, old->sz); old->free = 1; return new->ptr; @@ -113,17 +134,17 @@ void rune_free(void *ptr) { if (ptr == NULL) return; - struct mem_block *block = _find_block(ptr); + mem_block_t *block = _find_block(ptr); if (block->free == 1) return; block->free = 1; } void rune_free_all(void) { - struct list_head *temp = &first_block.list; - struct mem_block *block; + list_head_t *temp = &first_block.list; + mem_block_t *block; while (temp != NULL) { - block = (struct mem_block*)((void*)temp - offsetof(struct mem_block, list)); + block = (mem_block_t*)((void*)temp - offsetof(mem_block_t, list)); if (block->ptr == DEADBLOCK) { temp = temp->next; continue; |