diff options
| author | peter <peter@pmikkelsen.com> | 2026-08-10 09:18:23 +0000 |
|---|---|---|
| committer | peter <peter@pmikkelsen.com> | 2026-08-10 09:18:23 +0000 |
| commit | 3a81615601bf37ffc4f7b570030709ac0782e2ed (patch) | |
| tree | fc5a4d59b9bb62521d37a878c1fa7bb34d3d2bb5 | |
| parent | e30299f2cd59c46c2d733e38139da96c93f9acf4 (diff) | |
Avoid calling malloc/free in the library code.
FossilOrigin-Name: 3e7c2c1b4beda2a729c880caf1a41c441e0a8387d40b666ac7163eb1346653b5
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/aplwc.h | 7 | ||||
| -rw-r--r-- | src/aplwc_internal.h | 21 | ||||
| -rw-r--r-- | src/errors.c | 3 | ||||
| -rw-r--r-- | src/libmain.c | 13 | ||||
| -rw-r--r-- | src/main.c | 13 | ||||
| -rw-r--r-- | src/malloc.c | 11 | ||||
| -rw-r--r-- | src/memory.c | 43 |
8 files changed, 85 insertions, 28 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index fd1839b..29f567b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ target_link_options(common INTERFACE add_library(aplwc src/errors.c src/libmain.c - src/malloc.c + src/memory.c ) target_link_libraries(aplwc PRIVATE common) diff --git a/src/aplwc.h b/src/aplwc.h index 7a90147..e01e38c 100644 --- a/src/aplwc.h +++ b/src/aplwc.h @@ -1,15 +1,18 @@ #ifndef APLWC_H #define APLWC_H +#include <stdint.h> +#include <stddef.h> + typedef enum { APLWC_OK, APLWC_NOT_IMPLEMENTED, - APLWC_MALLOC_FAILED, + APLWC_OUT_OF_MEMORY, } AplwcRc; typedef struct Aplwc Aplwc; -AplwcRc aplwc_new(Aplwc **); +AplwcRc aplwc_new(Aplwc **, uint8_t *, size_t); AplwcRc aplwc_destroy(Aplwc *); const char *aplwc_errstr(Aplwc *); diff --git a/src/aplwc_internal.h b/src/aplwc_internal.h index f80c8ab..ccf600f 100644 --- a/src/aplwc_internal.h +++ b/src/aplwc_internal.h @@ -1,6 +1,8 @@ #ifndef APLWC_INTERNAL_H #define APLWC_INTERNAL_H +#include <aplwc.h> + #define UNUSED_PARAMETER(arg) ((void)arg) #define RETURN_ERROR(aplwc, rc, msg) do{ \ @@ -12,21 +14,32 @@ #define NELEM(x) (sizeof(x)/sizeof(x[0])) -#include <stddef.h> -#include <stdint.h> +#include <stdalign.h> +#include <stdbool.h> #include <stdlib.h> #include <stdio.h> #include <string.h> +typedef struct Memory Memory; + +struct Memory { + uint8_t *data; + size_t size; + size_t offset; +}; + struct Aplwc { AplwcRc last_error; char last_error_msg[64]; + Memory memory; }; /* errors.c */ const char *rc_to_string(AplwcRc); -/* malloc.c */ -void *malloc_zero(size_t); +/* memory.c */ +Memory memory_init(uint8_t *, size_t); +Aplwc *memory_alloc_aplwc(Memory *); +void memory_free_aplwc(Memory *, Aplwc *); #endif /* APLWC_INTERNAL_H */ diff --git a/src/errors.c b/src/errors.c index 014f859..cd93df4 100644 --- a/src/errors.c +++ b/src/errors.c @@ -1,4 +1,3 @@ -#include <aplwc.h> #include <aplwc_internal.h> static const char *errors[] = { @@ -6,8 +5,6 @@ static const char *errors[] = { "", [APLWC_NOT_IMPLEMENTED] = "Not yet implemented", - [APLWC_MALLOC_FAILED] = - "Could not allocate memory", }; const char * diff --git a/src/libmain.c b/src/libmain.c index 1458bf8..93ad2a0 100644 --- a/src/libmain.c +++ b/src/libmain.c @@ -1,13 +1,14 @@ -#include <aplwc.h> #include <aplwc_internal.h> AplwcRc -aplwc_new(Aplwc **p) +aplwc_new(Aplwc **p, uint8_t *data, size_t size) { - *p = malloc_zero(sizeof(Aplwc)); + Memory m = memory_init(data, size); + *p = memory_alloc_aplwc(&m); if(*p == NULL) - RETURN_ERROR(*p, MALLOC_FAILED, ""); - + RETURN_ERROR(*p, OUT_OF_MEMORY, ""); + (*p)->memory = m; + return APLWC_OK; } @@ -15,7 +16,7 @@ AplwcRc aplwc_destroy(Aplwc *p) { if(p) - free(p); + memory_free_aplwc(&p->memory, p); return APLWC_OK; } @@ -1,12 +1,21 @@ #include <stdio.h> +#include <stdlib.h> #include <aplwc.h> +#define MEMORY (1024*1024) + int main(void) { Aplwc *aplwc; - int ret = aplwc_new(&aplwc); + uint8_t *data = malloc(MEMORY); + if(data == NULL){ + fprintf(stderr, "Couldn't allocate memory\n"); + return 1; + } + + int ret = aplwc_new(&aplwc, data, MEMORY); if(ret){ fprintf(stderr, "Couldn't create new aplwc instance: %s\n", aplwc_errstr(aplwc)); return 1; @@ -19,5 +28,7 @@ main(void) return 1; } + free(data); + return 0; } diff --git a/src/malloc.c b/src/malloc.c deleted file mode 100644 index abf0884..0000000 --- a/src/malloc.c +++ /dev/null @@ -1,11 +0,0 @@ -#include <aplwc.h> -#include <aplwc_internal.h> - -void * -malloc_zero(size_t size) -{ - void *p = malloc(size); - if(p) - memset(p, 0, size); - return p; -} diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..c2216b2 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,43 @@ +#include <aplwc_internal.h> + +Memory +memory_init(uint8_t *data , size_t size) +{ + Memory mem = { + .data = data, + .size = size, + .offset = 0, + }; + return mem; +} + +static void * +alloc(Memory *m, size_t size, size_t align) +{ + uintptr_t ptr = (uintptr_t)m->data; + size_t alloc_size = size; + if((ptr % align) != 0) + alloc_size += align - (ptr % align); + + void *p = NULL; + if((m->size - m->offset) >= alloc_size){ + p = m->data+m->offset+alloc_size-size; + m->offset += alloc_size; + memset(p, 0, size); + } + return p; +} + +Aplwc * +memory_alloc_aplwc(Memory *m) +{ + Aplwc *p = alloc(m, sizeof(Aplwc), alignof(Aplwc)); + return p; +} + +void +memory_free_aplwc(Memory *m, Aplwc *p) +{ + UNUSED_PARAMETER(m); + UNUSED_PARAMETER(p); +} |