diff options
Diffstat (limited to 'src/memory.c')
| -rw-r--r-- | src/memory.c | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/src/memory.c b/src/memory.c index c2216b2..622bb0b 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,42 +1,54 @@ #include <aplwc_internal.h> -Memory +struct memory memory_init(uint8_t *data , size_t size) { - Memory mem = { - .data = data, - .size = size, - .offset = 0, - }; + struct memory mem; + + mem.data = data; + mem.size = size; + mem.offset = 0; return mem; } static void * -alloc(Memory *m, size_t size, size_t align) +alloc(struct 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); + uintptr_t ptr; + size_t alloc_size, adjust; + void *p; + + ptr = (uintptr_t)m->data; + alloc_size = size; + + if(NEVER(align == 0)) + return NULL; + adjust = ptr % align; + + if(adjust) + alloc_size += align - adjust; - 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); - } + }else + p = NULL; + return p; } -Aplwc * -memory_alloc_aplwc(Memory *m) +struct aplwc * +memory_alloc_aplwc(struct memory *m) { - Aplwc *p = alloc(m, sizeof(Aplwc), alignof(Aplwc)); + struct aplwc *p; + + p = alloc(m, sizeof(struct aplwc), alignof(struct aplwc)); return p; } void -memory_free_aplwc(Memory *m, Aplwc *p) +memory_free_aplwc(struct memory *m, struct aplwc *p) { UNUSED_PARAMETER(m); UNUSED_PARAMETER(p); |