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/main.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index d6de538..fa7e547 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -4,14 +4,14 @@ enum aplwc_rc aplwc_new(struct aplwc **p, uint8_t *data, size_t size) { - struct memory m; + struct aplwc tmp_aplwc; - m = memory_init(data, size); + memory_init(&tmp_aplwc, data, size); - *p = memory_alloc_aplwc(&m); + *p = memory_alloc_aplwc(&tmp_aplwc); if(*p == NULL) return APLWC_OUT_OF_MEMORY; - (*p)->memory = m; + memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc)); return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len); } @@ -20,7 +20,7 @@ void aplwc_destroy(struct aplwc *p) { if(p) - memory_free_aplwc(&p->memory, p); + memory_free_aplwc(p, p); } const char * @@ -83,7 +83,7 @@ aplwc_read(struct aplwc *aplwc, const char *line) if(aplwc->last_error != APLWC_OK) return NULL; - ret = memory_alloc_aplwc_line(&aplwc->memory); + ret = memory_alloc_aplwc_line(aplwc); if(ret == NULL){ aplwc->last_error = APLWC_OUT_OF_MEMORY; return NULL; @@ -101,7 +101,7 @@ aplwc_free_line(struct aplwc_line *line) { if(line == NULL) return; - memory_free_aplwc_line(&line->aplwc->memory, line); + memory_free_aplwc_line(line->aplwc, line); } struct aplwc_value * @@ -116,7 +116,7 @@ aplwc_free_value(struct aplwc_value *value) { if(value == NULL) return; - memory_free_aplwc_value(&value->aplwc->memory, value); + memory_free_aplwc_value(value->aplwc, value); } const char * @@ -157,11 +157,11 @@ aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int sr else{ error: if(tokens != NULL) - memory_free_token_list(&aplwc->memory, tokens); + memory_free_token_list(aplwc, tokens); if(text != NULL) - memory_free_string(&aplwc->memory, text); + memory_free_string(aplwc, text); if(namestr != NULL) - memory_free_string(&aplwc->memory, namestr); + memory_free_string(aplwc, namestr); return aplwc_last_error(aplwc); } } -- 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/main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index fa7e547..2bd374b 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -10,7 +10,7 @@ aplwc_new(struct aplwc **p, uint8_t *data, size_t size) *p = memory_alloc_aplwc(&tmp_aplwc); if(*p == NULL) - return APLWC_OUT_OF_MEMORY; + return tmp_aplwc.last_error; memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc)); return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len); @@ -84,10 +84,8 @@ aplwc_read(struct aplwc *aplwc, const char *line) return NULL; ret = memory_alloc_aplwc_line(aplwc); - if(ret == NULL){ - aplwc->last_error = APLWC_OUT_OF_MEMORY; + if(ret == NULL) return NULL; - } ret->aplwc = aplwc; ret->text = text; -- 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/main.c | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index 2bd374b..81cd6bb 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -19,8 +19,7 @@ aplwc_new(struct aplwc **p, uint8_t *data, size_t size) void aplwc_destroy(struct aplwc *p) { - if(p) - memory_free_aplwc(p, p); + UNUSED_PARAMETER(p); } const char * @@ -97,9 +96,8 @@ aplwc_read(struct aplwc *aplwc, const char *line) void aplwc_free_line(struct aplwc_line *line) { - if(line == NULL) - return; - memory_free_aplwc_line(line->aplwc, line); + if(line) + memory_free_root(line->aplwc, line); } struct aplwc_value * @@ -112,9 +110,8 @@ aplwc_eval(struct aplwc_line *line) void aplwc_free_value(struct aplwc_value *value) { - if(value == NULL) - return; - memory_free_aplwc_value(value->aplwc, value); + if(value) + memory_free_root(value->aplwc, value); } const char * @@ -134,32 +131,16 @@ aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int sr struct string *text, *namestr; struct token_list *tokens; - namestr = NULL; - text = NULL; - tokens = NULL; - - namestr = chars_to_string(aplwc, name, -1); - if(namestr == NULL) + if((namestr = chars_to_string(aplwc, name, -1)) == NULL) goto error; - - text = chars_to_string(aplwc, src, srclen); - if(text == NULL) + if((text = chars_to_string(aplwc, src, srclen)) == NULL) goto error; - - tokens = scan_string(aplwc, text, 0); - if(tokens == NULL) + if((tokens = scan_string(aplwc, text, 0)) == NULL) goto error; - - if(load_module(aplwc, namestr, tokens) == APLWC_OK) - return APLWC_OK; - else{ + if(load_module(aplwc, namestr, tokens) != APLWC_OK){ error: - if(tokens != NULL) - memory_free_token_list(aplwc, tokens); - if(text != NULL) - memory_free_string(aplwc, text); - if(namestr != NULL) - memory_free_string(aplwc, namestr); return aplwc_last_error(aplwc); } + + return APLWC_OK; } -- 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/main.c | 82 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 32 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index 81cd6bb..03c31a5 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -2,24 +2,32 @@ #include "boot.h" enum aplwc_rc -aplwc_new(struct aplwc **p, uint8_t *data, size_t size) +aplwc_new(struct aplwc **p, struct aplwc_fns fns) { - struct aplwc tmp_aplwc; - - memory_init(&tmp_aplwc, data, size); - - *p = memory_alloc_aplwc(&tmp_aplwc); - if(*p == NULL) - return tmp_aplwc.last_error; - memcpy(*p, &tmp_aplwc, sizeof(tmp_aplwc)); - - return aplwc_load_module(*p, "Boot", (const char *)boot_src, boot_src_len); + struct aplwc *aplwc; + size_t size; + + if(fns.alloc == NULL) + return APLWC_OUT_OF_MEMORY; + aplwc = fns.alloc(sizeof(*aplwc), sizeof(*aplwc), &size); + if(aplwc == NULL) + return APLWC_OUT_OF_MEMORY; + *p = aplwc; + + aplwc->fns = fns; + memory_init(aplwc); + return aplwc_load_module(aplwc, "Boot", (const char *)boot_src, boot_src_len); } void aplwc_destroy(struct aplwc *p) { - UNUSED_PARAMETER(p); + if(p == NULL) + return; + + memory_destroy(p); + if(p->fns.dealloc) + p->fns.dealloc(p); } const char * @@ -70,26 +78,30 @@ aplwc_read(struct aplwc *aplwc, const char *line) struct token_list *tokens; struct aplwc_line *ret; - text = chars_to_string(aplwc, line, -1); - if(text == NULL) - return NULL; + MEMPTRS(aplwc, 2); + + ret = NULL; - tokens = scan_string(aplwc, text, 1); - if(tokens == NULL) - return NULL; + if((text = chars_to_string(aplwc, line, -1)) == NULL) + goto end; + MEMPTR(0, text); - aplwc->last_error = check_tokens(tokens); - if(aplwc->last_error != APLWC_OK) - return NULL; + if((tokens = scan_string(aplwc, text, 1)) == NULL) + goto end; + MEMPTR(1, tokens); - ret = memory_alloc_aplwc_line(aplwc); - if(ret == NULL) - return NULL; + if((aplwc->last_error = check_tokens(tokens)) != APLWC_OK) + goto end; + + if((ret = memory_alloc_aplwc_line(aplwc)) == NULL) + goto end; ret->aplwc = aplwc; ret->text = text; ret->tokens = tokens; +end: + MEMPTRS_EXIT(aplwc); return ret; } @@ -131,16 +143,22 @@ aplwc_load_module(struct aplwc *aplwc, const char *name, const char *src, int sr struct string *text, *namestr; struct token_list *tokens; + MEMPTRS(aplwc, 3); + if((namestr = chars_to_string(aplwc, name, -1)) == NULL) - goto error; + goto end; + MEMPTR(0, namestr); + if((text = chars_to_string(aplwc, src, srclen)) == NULL) - goto error; + goto end; + MEMPTR(1, text); + if((tokens = scan_string(aplwc, text, 0)) == NULL) - goto error; - if(load_module(aplwc, namestr, tokens) != APLWC_OK){ -error: - return aplwc_last_error(aplwc); - } + goto end; + MEMPTR(2, tokens); - return APLWC_OK; + aplwc->last_error = load_module(aplwc, namestr, tokens); +end: + MEMPTRS_EXIT(aplwc); + return aplwc_last_error(aplwc); } -- 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/main.c | 107 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 30 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index 03c31a5..454ad1c 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -1,22 +1,34 @@ #include "aplwc_internal.h" #include "boot.h" -enum aplwc_rc -aplwc_new(struct aplwc **p, struct aplwc_fns fns) +struct aplwc * +aplwc_new(struct aplwc_fns fns, enum aplwc_rc *rcp) { + enum aplwc_rc rc; struct aplwc *aplwc; size_t size; - if(fns.alloc == NULL) - return APLWC_OUT_OF_MEMORY; + aplwc = NULL; + if(fns.alloc == NULL){ + rc = APLWC_OUT_OF_MEMORY; + goto end; + } aplwc = fns.alloc(sizeof(*aplwc), sizeof(*aplwc), &size); - if(aplwc == NULL) - return APLWC_OUT_OF_MEMORY; - *p = aplwc; + if(aplwc == NULL){ + rc = APLWC_OUT_OF_MEMORY; + goto end; + } aplwc->fns = fns; memory_init(aplwc); - return aplwc_load_module(aplwc, "Boot", (const char *)boot_src, boot_src_len); + + rc = aplwc_load_module(aplwc, "Boot", (const char *)boot_src, boot_src_len); +end: + if(rc != APLWC_OK) + aplwc_destroy(aplwc); + if(rcp) + *rcp = rc; + return aplwc; } void @@ -71,18 +83,19 @@ aplwc_last_error(struct aplwc *aplwc) return aplwc->last_error; } -struct aplwc_line * -aplwc_read(struct aplwc *aplwc, const char *line) +AplwcLine +aplwc_read(struct aplwc *aplwc, const char *linestr) { struct string *text; struct token_list *tokens; - struct aplwc_line *ret; + struct aplwc_line *line; + AplwcLine ret; MEMPTRS(aplwc, 2); - ret = NULL; + aplwc_clear_line(&ret); - if((text = chars_to_string(aplwc, line, -1)) == NULL) + if((text = chars_to_string(aplwc, linestr, -1)) == NULL) goto end; MEMPTR(0, text); @@ -93,45 +106,55 @@ aplwc_read(struct aplwc *aplwc, const char *line) if((aplwc->last_error = check_tokens(tokens)) != APLWC_OK) goto end; - if((ret = memory_alloc_aplwc_line(aplwc)) == NULL) + if((line = memory_alloc_aplwc_line(aplwc)) == NULL) goto end; - ret->aplwc = aplwc; - ret->text = text; - ret->tokens = tokens; + line->aplwc = aplwc; + line->text = text; + line->tokens = tokens; + ret.ref = memory_root_new(aplwc, line); end: MEMPTRS_EXIT(aplwc); return ret; } void -aplwc_free_line(struct aplwc_line *line) +aplwc_free_line(AplwcLine line) { - if(line) - memory_free_root(line->aplwc, line); + memory_root_free(line.ref); } -struct aplwc_value * -aplwc_eval(struct aplwc_line *line) +AplwcValue +aplwc_eval(AplwcLine line) { - line->aplwc->last_error = APLWC_OK; - return eval_line(line); + AplwcValue value; + struct aplwc_line *l; + struct aplwc_value *v; + + aplwc_clear_value(&value); + l = memory_root_get(line.ref); + l->aplwc->last_error = APLWC_OK; + v = eval_line(l); + if(v) + value.ref = memory_root_new(line.ref.aplwc, v); + return value; } void -aplwc_free_value(struct aplwc_value *value) +aplwc_free_value(AplwcValue value) { - if(value) - memory_free_root(value->aplwc, value); + memory_root_free(value.ref); } const char * -aplwc_value_string(struct aplwc_value *value) +aplwc_value_string(AplwcValue value) { - assert(value != NULL); + struct aplwc_value *v; + assert(aplwc_valid_value(value)); - switch(value->tag){ + v = memory_root_get(value.ref); + switch(v->tag){ default: return "???"; } @@ -162,3 +185,27 @@ end: MEMPTRS_EXIT(aplwc); return aplwc_last_error(aplwc); } + +void +aplwc_clear_value(AplwcValue *v) +{ + clear_ref(&v->ref); +} + +void +aplwc_clear_line(AplwcLine *l) +{ + clear_ref(&l->ref); +} + +int +aplwc_valid_value(AplwcValue v) +{ + return valid_ref(v.ref); +} + +int +aplwc_valid_line(AplwcLine l) +{ + return valid_ref(l.ref); +} -- cgit v1.2.3 From 34189c9b67f3427e077152e3a6c8cb21f4ba3441 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 31 Aug 2026 19:49:48 +0000 Subject: Remove some unneeded references to struct aplwc * in various structures. FossilOrigin-Name: 3b4b7806320d604b0df3654896cfe5d885f116d893ae15cfb3422d6d2de7193c --- src/lib/main.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index 454ad1c..dcf4a1c 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -109,7 +109,6 @@ aplwc_read(struct aplwc *aplwc, const char *linestr) if((line = memory_alloc_aplwc_line(aplwc)) == NULL) goto end; - line->aplwc = aplwc; line->text = text; line->tokens = tokens; @@ -131,13 +130,16 @@ aplwc_eval(AplwcLine line) AplwcValue value; struct aplwc_line *l; struct aplwc_value *v; + struct aplwc *aplwc; + + aplwc = line.ref.aplwc; aplwc_clear_value(&value); l = memory_root_get(line.ref); - l->aplwc->last_error = APLWC_OK; - v = eval_line(l); + aplwc->last_error = APLWC_OK; + v = eval_line(aplwc, l); if(v) - value.ref = memory_root_new(line.ref.aplwc, v); + value.ref = memory_root_new(aplwc, v); return value; } -- cgit v1.2.3 From f9f9b10c1d17077632d7a09e447e629b8d33b23d Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 31 Aug 2026 20:21:54 +0000 Subject: A little bit of testing FossilOrigin-Name: 70deba994bb488d4aa5df36577d1e184d6651a3edeff042e9666f54589cdc6f9 --- src/lib/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/lib/main.c') diff --git a/src/lib/main.c b/src/lib/main.c index dcf4a1c..4571c78 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -26,8 +26,8 @@ aplwc_new(struct aplwc_fns fns, enum aplwc_rc *rcp) end: if(rc != APLWC_OK) aplwc_destroy(aplwc); - if(rcp) - *rcp = rc; + assert(rcp); + *rcp = rc; return aplwc; } -- cgit v1.2.3