From 8521b6a69deab8cdabe1c87b09ecdc93d75af98e Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 30 Aug 2026 14:20:55 +0000 Subject: Remove the struct memory struct FossilOrigin-Name: 38568a34f631b2a4eef72542e2988a9fe081f85d3248194c2ff995a9eed6bd5d --- src/lib/memory.c | 71 +++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index a348b93..24d3d68 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -1,24 +1,21 @@ #include "aplwc_internal.h" -struct memory -memory_init(uint8_t *data , size_t size) +void +memory_init(struct aplwc *aplwc, uint8_t *data , size_t size) { - struct memory mem; - - mem.data = data; - mem.size = size; - mem.offset = 0; - return mem; + aplwc->data = data; + aplwc->size = size; + aplwc->offset = 0; } static void * -alloc(struct memory *m, size_t size, size_t align) +alloc(struct aplwc *aplwc, size_t size, size_t align) { uintptr_t ptr; size_t alloc_size, adjust; void *p; - ptr = ((uintptr_t)m->data) + m->offset; + ptr = ((uintptr_t)aplwc->data) + aplwc->offset; alloc_size = size; if(NEVER(align == 0)) @@ -28,10 +25,10 @@ alloc(struct memory *m, size_t size, size_t align) if(adjust) alloc_size += align - adjust; - if((m->size - m->offset) >= alloc_size){ - p = m->data+m->offset+alloc_size-size; + if((aplwc->size - aplwc->offset) >= alloc_size){ + p = aplwc->data+aplwc->offset+alloc_size-size; assert((((uintptr_t)p) % align) == 0); - m->offset += alloc_size; + aplwc->offset += alloc_size; memset(p, 0, size); }else p = NULL; @@ -40,97 +37,97 @@ alloc(struct memory *m, size_t size, size_t align) } struct aplwc * -memory_alloc_aplwc(struct memory *m) +memory_alloc_aplwc(struct aplwc *aplwc) { struct aplwc *p; - p = alloc(m, sizeof(struct aplwc), alignof(struct aplwc)); + p = alloc(aplwc, sizeof(struct aplwc), alignof(struct aplwc)); return p; } void -memory_free_aplwc(struct memory *m, struct aplwc *p) +memory_free_aplwc(struct aplwc *aplwc, struct aplwc *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } struct aplwc_line * -memory_alloc_aplwc_line(struct memory *m) +memory_alloc_aplwc_line(struct aplwc *aplwc) { struct aplwc_line *p; - p = alloc(m, sizeof(struct aplwc_line), alignof(struct aplwc_line)); + p = alloc(aplwc, sizeof(struct aplwc_line), alignof(struct aplwc_line)); return p; } void -memory_free_aplwc_line(struct memory *m, struct aplwc_line *p) +memory_free_aplwc_line(struct aplwc *aplwc, struct aplwc_line *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } struct string * -memory_alloc_string(struct memory *m, size_t bytes) +memory_alloc_string(struct aplwc *aplwc, size_t bytes) { struct string *p; - p = alloc(m, sizeof(struct string) + bytes, alignof(struct string)); + p = alloc(aplwc, sizeof(struct string) + bytes, alignof(struct string)); return p; } void -memory_free_string(struct memory *m, struct string *p) +memory_free_string(struct aplwc *aplwc, struct string *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } struct token_list * -memory_alloc_token_list(struct memory *m, size_t tokens) +memory_alloc_token_list(struct aplwc *aplwc, size_t tokens) { struct token_list *p; - p = alloc(m, sizeof(struct token_list) + (tokens * sizeof(struct token)), alignof(struct token_list)); + p = alloc(aplwc, sizeof(struct token_list) + (tokens * sizeof(struct token)), alignof(struct token_list)); return p; } void -memory_free_token_list(struct memory *m, struct token_list *p) +memory_free_token_list(struct aplwc *aplwc, struct token_list *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } struct aplwc_value * -memory_alloc_aplwc_value(struct memory *m) +memory_alloc_aplwc_value(struct aplwc *aplwc) { struct aplwc_value *p; - p = alloc(m, sizeof(struct aplwc_value), alignof(struct aplwc_value)); + p = alloc(aplwc, sizeof(struct aplwc_value), alignof(struct aplwc_value)); return p; } void -memory_free_aplwc_value(struct memory *m, struct aplwc_value *p) +memory_free_aplwc_value(struct aplwc *aplwc, struct aplwc_value *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } struct aplwc_module * -memory_alloc_aplwc_module(struct memory *m) +memory_alloc_aplwc_module(struct aplwc *aplwc) { struct aplwc_module *p; - p = alloc(m, sizeof(struct aplwc_module), alignof(struct aplwc_module)); + p = alloc(aplwc, sizeof(struct aplwc_module), alignof(struct aplwc_module)); return p; } void -memory_free_aplwc_module(struct memory *m, struct aplwc_module *p) +memory_free_aplwc_module(struct aplwc *aplwc, struct aplwc_module *p) { - UNUSED_PARAMETER(m); + UNUSED_PARAMETER(aplwc); UNUSED_PARAMETER(p); } -- cgit v1.2.3 From 2261ffa04ddf30a7c8eae6e64129b293fc8bffd9 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 30 Aug 2026 14:25:56 +0000 Subject: Let the memory allocation code set the last_error to APLWC_OUT_OF_MEMORY, instead of the callers FossilOrigin-Name: 46c74e207a84e861636dbfebc4ba8bb9e7b17401dfd2c4fb9bdeae0fa98fd530 --- src/lib/memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index 24d3d68..ca4babf 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -30,8 +30,10 @@ alloc(struct aplwc *aplwc, size_t size, size_t align) assert((((uintptr_t)p) % align) == 0); aplwc->offset += alloc_size; memset(p, 0, size); - }else + }else{ p = NULL; + aplwc->last_error = APLWC_OUT_OF_MEMORY; + } return p; } -- cgit v1.2.3 From 04827577ea502a9f0edc69e00e008751c8119cbc Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 30 Aug 2026 14:46:38 +0000 Subject: Remove a lot of functions for manually freeing memory (they were not implemented) FossilOrigin-Name: 693765728e62a74a0ab15d657f7d1dd83702a9371fb042dafe6250f84a252959 --- src/lib/memory.c | 51 +++++++++------------------------------------------ 1 file changed, 9 insertions(+), 42 deletions(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index ca4babf..94f903c 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -38,6 +38,15 @@ alloc(struct aplwc *aplwc, size_t size, size_t align) return p; } +void +memory_free_root(struct aplwc *aplwc, void *p) +{ + if(NEVER(p == NULL)) + return; + + UNUSED_PARAMETER(aplwc); +} + struct aplwc * memory_alloc_aplwc(struct aplwc *aplwc) { @@ -47,13 +56,6 @@ memory_alloc_aplwc(struct aplwc *aplwc) return p; } -void -memory_free_aplwc(struct aplwc *aplwc, struct aplwc *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} - struct aplwc_line * memory_alloc_aplwc_line(struct aplwc *aplwc) { @@ -63,13 +65,6 @@ memory_alloc_aplwc_line(struct aplwc *aplwc) return p; } -void -memory_free_aplwc_line(struct aplwc *aplwc, struct aplwc_line *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} - struct string * memory_alloc_string(struct aplwc *aplwc, size_t bytes) { @@ -79,13 +74,6 @@ memory_alloc_string(struct aplwc *aplwc, size_t bytes) return p; } -void -memory_free_string(struct aplwc *aplwc, struct string *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} - struct token_list * memory_alloc_token_list(struct aplwc *aplwc, size_t tokens) { @@ -95,13 +83,6 @@ memory_alloc_token_list(struct aplwc *aplwc, size_t tokens) return p; } -void -memory_free_token_list(struct aplwc *aplwc, struct token_list *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} - struct aplwc_value * memory_alloc_aplwc_value(struct aplwc *aplwc) { @@ -111,13 +92,6 @@ memory_alloc_aplwc_value(struct aplwc *aplwc) return p; } -void -memory_free_aplwc_value(struct aplwc *aplwc, struct aplwc_value *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} - struct aplwc_module * memory_alloc_aplwc_module(struct aplwc *aplwc) { @@ -126,10 +100,3 @@ memory_alloc_aplwc_module(struct aplwc *aplwc) p = alloc(aplwc, sizeof(struct aplwc_module), alignof(struct aplwc_module)); return p; } - -void -memory_free_aplwc_module(struct aplwc *aplwc, struct aplwc_module *p) -{ - UNUSED_PARAMETER(aplwc); - UNUSED_PARAMETER(p); -} -- cgit v1.2.3 From 729964bea692f9683860978b89848784a7fc2f46 Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 30 Aug 2026 20:52:43 +0000 Subject: Still a lot to be done, but I'm going to bed. Two remaining big issues: * aplwc (the parameter passed "everywhere") - should it move or not? * values returned from the public API should not move. FossilOrigin-Name: 31ab3154b8f3d5ac167218496e9e204324bf0ac9d36b7bada8fe2a6c8f93d51c --- src/lib/memory.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 207 insertions(+), 34 deletions(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index 94f903c..8a91d2d 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -1,41 +1,201 @@ #include "aplwc_internal.h" +#define ALIGNMENT 8 +#define HEADER_SIZE (sizeof(struct chunk) + (sizeof(struct chunk) % ALIGNMENT)) + +enum chunk_tag { + CHUNK_TAG_FREE, + CHUNK_TAG_APLWC_LINE, + CHUNK_TAG_STRING, + CHUNK_TAG_TOKEN_LIST, + CHUNK_TAG_APLWC_VALUE, + CHUNK_TAG_APLWC_MODULE +}; + +struct chunk { + struct chunk *next; + size_t size; + enum chunk_tag tag; + uint8_t *data; +}; + void -memory_init(struct aplwc *aplwc, uint8_t *data , size_t size) +memory_init(struct aplwc *aplwc) { - aplwc->data = data; - aplwc->size = size; - aplwc->offset = 0; + aplwc->data = NULL; + aplwc->unaligned_data = NULL; + aplwc->size = 0; + aplwc->memptrs = NULL; } -static void * -alloc(struct aplwc *aplwc, size_t size, size_t align) +void +memory_destroy(struct aplwc *aplwc) { - uintptr_t ptr; - size_t alloc_size, adjust; - void *p; + if(aplwc->fns.dealloc) + aplwc->fns.dealloc(aplwc->unaligned_data); +} - ptr = ((uintptr_t)aplwc->data) + aplwc->offset; - alloc_size = size; +static void +fixrefs(struct aplwc *aplwc, uint8_t *new, uint8_t *old) +{ +#define FIX(ptr) do{ \ + if(ptr){ \ + (ptr) = (void*)(((uint8_t*)(ptr))+add); \ + (ptr) = (void*)(((uint8_t*)(ptr))-sub); \ + } \ +}while(0) - if(NEVER(align == 0)) - return NULL; - adjust = ptr % align; +#define FIX_DATA(type, ptr) FIX(((type *)c->data)->ptr) - if(adjust) - alloc_size += align - adjust; + struct chunk *c; + struct memory_ptr_frame *frame; + size_t add, sub, i; - if((aplwc->size - aplwc->offset) >= alloc_size){ - p = aplwc->data+aplwc->offset+alloc_size-size; - assert((((uintptr_t)p) % align) == 0); - aplwc->offset += alloc_size; - memset(p, 0, size); + if(new > old){ + add = new-old; + sub = 0; }else{ - p = NULL; - aplwc->last_error = APLWC_OUT_OF_MEMORY; + add = 0; + sub = old-new; } - return p; + for(c = (struct chunk *)new; c; c = c->next){ + FIX(c->next); + FIX(c->data); + + switch(c->tag){ + case CHUNK_TAG_APLWC_LINE: + FIX_DATA(struct aplwc_line, text); + FIX_DATA(struct aplwc_line, tokens); + break; + case CHUNK_TAG_STRING: + break; + case CHUNK_TAG_TOKEN_LIST: + FIX_DATA(struct token_list, text); + break; + case CHUNK_TAG_APLWC_VALUE: + break; + case CHUNK_TAG_APLWC_MODULE: + FIX_DATA(struct aplwc_module, name); + FIX_DATA(struct aplwc_module, tokens); + FIX_DATA(struct aplwc_module, link); + break; + case CHUNK_TAG_FREE: + break; + } + } + + for(frame = aplwc->memptrs; frame; frame = frame->link) + for(i = 0; i < frame->count; i++) + if(frame->ptrs[i]) + FIX(*frame->ptrs[i]); + +#undef FIX +#undef FIX_DATA +} + +static enum aplwc_rc +grow(struct aplwc *aplwc, size_t extra) +{ + uint8_t *unaligned_data, *aligned_data; + size_t unaligned_newsize, aligned_newsize, adjust; + struct chunk **free; + + if(aplwc->fns.alloc == NULL) + goto error; + + unaligned_data = aplwc->fns.alloc(aplwc->size + extra + HEADER_SIZE + 2*ALIGNMENT, 0, &unaligned_newsize); + if(unaligned_data == NULL) + goto error; + + adjust = ((uintptr_t)unaligned_data) % ALIGNMENT; + if(adjust) + adjust = ALIGNMENT - adjust; + aligned_newsize = unaligned_newsize - adjust; + aligned_data = unaligned_data + adjust; + + aligned_newsize -= aligned_newsize % ALIGNMENT; + + assert(aligned_newsize >= (aplwc->size + extra)); + + if(aplwc->data){ + memmove(aligned_data, aplwc->data, aplwc->size); + fixrefs(aplwc, aligned_data, aplwc->data); + if(aplwc->fns.dealloc) + aplwc->fns.dealloc(aplwc->unaligned_data); + for(free = (struct chunk **)&aligned_data; (*free); free = &(*free)->next) + assert((*free)->tag != CHUNK_TAG_FREE); + }else + free = (struct chunk **)&aligned_data; + + + (*free) = (struct chunk *)(aligned_data + aplwc->size); + (*free)->tag = CHUNK_TAG_FREE; + (*free)->data = NULL; + (*free)->size = aligned_newsize - aplwc->size; + (*free)->next = NULL; + + aplwc->unaligned_data = unaligned_data; + aplwc->data = aligned_data; + aplwc->size = aligned_newsize; + return 1; + +error: + aplwc->last_error = APLWC_OUT_OF_MEMORY; + return 0; +} + +static struct chunk * +find_free(struct aplwc *aplwc, size_t min) +{ + struct chunk *c; + + min += HEADER_SIZE; + + if(aplwc->data == NULL) + return NULL; + + for(c = (struct chunk *)aplwc->data; c; c = c->next){ + if((c->tag == CHUNK_TAG_FREE) && (c->size >= min)) + break; + } + + return c; +} + +static void * +alloc(struct aplwc *aplwc, size_t size, enum chunk_tag tag) +{ + struct chunk *free; + struct chunk *new; + +retry: + free = find_free(aplwc, size); + if(!free){ + if(grow(aplwc, size)) + goto retry; + else + return NULL; + } + + assert(free->data == NULL); + + new = free; + + if(free->size > (size + 2*HEADER_SIZE)){ + free = (struct chunk *)(((uint8_t*)free) + HEADER_SIZE + size); + free->tag = CHUNK_TAG_FREE; + free->size = new->size - (HEADER_SIZE + size); + free->data = NULL; + } + + new->tag = tag; + new->size = sizeof(struct chunk) + size; + new->data = ((uint8_t*)new) + sizeof(struct chunk); + + new->data += ((uintptr_t)new->data) % ALIGNMENT; + + return new->data; } void @@ -47,13 +207,25 @@ memory_free_root(struct aplwc *aplwc, void *p) UNUSED_PARAMETER(aplwc); } -struct aplwc * -memory_alloc_aplwc(struct aplwc *aplwc) +void +memptrs_add(struct aplwc *aplwc, struct memory_ptr_frame *frame, void ***ptrs, size_t count) { - struct aplwc *p; + size_t i; - p = alloc(aplwc, sizeof(struct aplwc), alignof(struct aplwc)); - return p; + frame->ptrs = ptrs; + frame->link = aplwc->memptrs; + frame->count = count; + aplwc->memptrs = frame; + + for(i = 0; i < count; i++) + frame->ptrs[i] = NULL; +} + +void +memptrs_remove(struct aplwc *aplwc, struct memory_ptr_frame *frame) +{ + assert(aplwc->memptrs == frame); + aplwc->memptrs = frame->link; } struct aplwc_line * @@ -61,7 +233,7 @@ memory_alloc_aplwc_line(struct aplwc *aplwc) { struct aplwc_line *p; - p = alloc(aplwc, sizeof(struct aplwc_line), alignof(struct aplwc_line)); + p = alloc(aplwc, sizeof(struct aplwc_line), CHUNK_TAG_APLWC_LINE); return p; } @@ -70,7 +242,8 @@ memory_alloc_string(struct aplwc *aplwc, size_t bytes) { struct string *p; - p = alloc(aplwc, sizeof(struct string) + bytes, alignof(struct string)); + p = alloc(aplwc, sizeof(struct string) + bytes, CHUNK_TAG_STRING); + return p; } @@ -79,7 +252,7 @@ memory_alloc_token_list(struct aplwc *aplwc, size_t tokens) { struct token_list *p; - p = alloc(aplwc, sizeof(struct token_list) + (tokens * sizeof(struct token)), alignof(struct token_list)); + p = alloc(aplwc, sizeof(struct token_list) + (tokens * sizeof(struct token)), CHUNK_TAG_TOKEN_LIST); return p; } @@ -88,7 +261,7 @@ memory_alloc_aplwc_value(struct aplwc *aplwc) { struct aplwc_value *p; - p = alloc(aplwc, sizeof(struct aplwc_value), alignof(struct aplwc_value)); + p = alloc(aplwc, sizeof(struct aplwc_value), CHUNK_TAG_APLWC_VALUE); return p; } @@ -97,6 +270,6 @@ memory_alloc_aplwc_module(struct aplwc *aplwc) { struct aplwc_module *p; - p = alloc(aplwc, sizeof(struct aplwc_module), alignof(struct aplwc_module)); + p = alloc(aplwc, sizeof(struct aplwc_module), CHUNK_TAG_APLWC_MODULE); return p; } -- cgit v1.2.3 From 7f7a2fb4fb23433e7be3d6953725690c8ea958d2 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 31 Aug 2026 19:44:56 +0000 Subject: The objects returned to the user are now integer-based references, not actual pointers. This means the aplwc internals can update those pointers without the user noticing. FossilOrigin-Name: 654c7d0ec85da295a7fb6ed381500410686b280f342fa81a30481f5e9a20c58a --- src/lib/memory.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index 8a91d2d..b6e044a 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -26,6 +26,7 @@ memory_init(struct aplwc *aplwc) aplwc->unaligned_data = NULL; aplwc->size = 0; aplwc->memptrs = NULL; + aplwc->rootptrs = NULL; } void @@ -49,6 +50,7 @@ fixrefs(struct aplwc *aplwc, uint8_t *new, uint8_t *old) struct chunk *c; struct memory_ptr_frame *frame; + struct root_ptr *root; size_t add, sub, i; if(new > old){ @@ -89,6 +91,10 @@ fixrefs(struct aplwc *aplwc, uint8_t *new, uint8_t *old) for(i = 0; i < frame->count; i++) if(frame->ptrs[i]) FIX(*frame->ptrs[i]); + + for(root = aplwc->rootptrs; root; root = root->link) + if(root->ptr) + FIX(root->ptr); #undef FIX #undef FIX_DATA @@ -198,13 +204,71 @@ retry: return new->data; } +struct aplwc_ref +memory_root_new(struct aplwc *aplwc, void *p) +{ + struct aplwc_ref ref; + struct root_ptr *root; + size_t size; + + clear_ref(&ref); + if(aplwc->fns.alloc == NULL){ + aplwc->last_error = APLWC_OUT_OF_MEMORY; + goto end; + } + + root = aplwc->fns.alloc(sizeof(*root), sizeof(*root), &size); + if(root == NULL){ + aplwc->last_error = APLWC_OUT_OF_MEMORY; + goto end; + } + + root->id = aplwc->rootptrs ? (aplwc->rootptrs->id+1) : 0; + root->ptr = p; + root->link = aplwc->rootptrs; + aplwc->rootptrs = root; + + ref.id = root->id; + ref.aplwc = aplwc; +end: + return ref; +} + +void * +memory_root_get(struct aplwc_ref ref) +{ + struct root_ptr *root; + + if(!valid_ref(ref)) + return NULL; + + for(root = ref.aplwc->rootptrs; root; root = root->link) + if(root->id == ref.id) + return root->ptr; + + return NULL; +} + void -memory_free_root(struct aplwc *aplwc, void *p) +memory_root_free(struct aplwc_ref ref) { - if(NEVER(p == NULL)) + struct aplwc *aplwc; + struct root_ptr **root, *tmp; + + if(!valid_ref(ref)) return; - UNUSED_PARAMETER(aplwc); + aplwc = ref.aplwc; + + for(root = &aplwc->rootptrs; (*root); root = &(*root)->link){ + if((*root)->id == ref.id){ + tmp = *root; + *root = tmp->link; + if(aplwc->fns.dealloc) + aplwc->fns.dealloc(tmp); + break; + } + } } void @@ -273,3 +337,16 @@ memory_alloc_aplwc_module(struct aplwc *aplwc) p = alloc(aplwc, sizeof(struct aplwc_module), CHUNK_TAG_APLWC_MODULE); return p; } + +void +clear_ref(struct aplwc_ref *ref) +{ + ref->id = -1; + ref->aplwc = NULL; +} + +int +valid_ref(struct aplwc_ref ref) +{ + return ref.id >= 0; +} -- cgit v1.2.3 From 0091c055edceca2598dfb7dd64cdcb081e4c7507 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 31 Aug 2026 20:04:50 +0000 Subject: Fix problems in release builds FossilOrigin-Name: 98227d25674f373968f51df0dcdccf3cf14d646a13e56a9229103d1aeb3ad73d --- src/lib/memory.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/lib/memory.c') diff --git a/src/lib/memory.c b/src/lib/memory.c index b6e044a..694a31b 100644 --- a/src/lib/memory.c +++ b/src/lib/memory.c @@ -105,7 +105,7 @@ grow(struct aplwc *aplwc, size_t extra) { uint8_t *unaligned_data, *aligned_data; size_t unaligned_newsize, aligned_newsize, adjust; - struct chunk **free; + struct chunk **free, *head; if(aplwc->fns.alloc == NULL) goto error; @@ -123,16 +123,17 @@ grow(struct aplwc *aplwc, size_t extra) aligned_newsize -= aligned_newsize % ALIGNMENT; assert(aligned_newsize >= (aplwc->size + extra)); + head = (struct chunk *)aligned_data; if(aplwc->data){ memmove(aligned_data, aplwc->data, aplwc->size); fixrefs(aplwc, aligned_data, aplwc->data); if(aplwc->fns.dealloc) aplwc->fns.dealloc(aplwc->unaligned_data); - for(free = (struct chunk **)&aligned_data; (*free); free = &(*free)->next) + for(free = &head; (*free); free = &(*free)->next) assert((*free)->tag != CHUNK_TAG_FREE); }else - free = (struct chunk **)&aligned_data; + free = &head; (*free) = (struct chunk *)(aligned_data + aplwc->size); -- cgit v1.2.3