diff options
| author | peter <peter@pmikkelsen.com> | 2026-08-31 20:22:47 +0000 |
|---|---|---|
| committer | peter <peter@pmikkelsen.com> | 2026-08-31 20:22:47 +0000 |
| commit | 8b0beb9e6a57c1d4f1e7b0b06523dd0fc538771a (patch) | |
| tree | 9bb8b689020d5098ea3c31251ddcd2fbb9524a7c /src/lib/main.c | |
| parent | d361abaeb8dcf19446a9322737ea600b22568a5b (diff) | |
| parent | f9f9b10c1d17077632d7a09e447e629b8d33b23d (diff) | |
Merge memory manager changes into trunk
FossilOrigin-Name: af583e3ba70b2d10efdead8e03a42fe5c1b94d6b387522833606c138b527a653
Diffstat (limited to 'src/lib/main.c')
| -rw-r--r-- | src/lib/main.c | 190 |
1 files changed, 118 insertions, 72 deletions
diff --git a/src/lib/main.c b/src/lib/main.c index d6de538..4571c78 100644 --- a/src/lib/main.c +++ b/src/lib/main.c @@ -1,26 +1,45 @@ #include "aplwc_internal.h" #include "boot.h" -enum aplwc_rc -aplwc_new(struct aplwc **p, uint8_t *data, size_t size) +struct aplwc * +aplwc_new(struct aplwc_fns fns, enum aplwc_rc *rcp) { - struct memory m; + enum aplwc_rc rc; + struct aplwc *aplwc; + size_t size; - m = memory_init(data, size); + aplwc = NULL; + if(fns.alloc == NULL){ + rc = APLWC_OUT_OF_MEMORY; + goto end; + } + aplwc = fns.alloc(sizeof(*aplwc), sizeof(*aplwc), &size); + if(aplwc == NULL){ + rc = APLWC_OUT_OF_MEMORY; + goto end; + } - *p = memory_alloc_aplwc(&m); - if(*p == NULL) - return APLWC_OUT_OF_MEMORY; - (*p)->memory = m; + aplwc->fns = fns; + memory_init(aplwc); - return aplwc_load_module(*p, "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); + assert(rcp); + *rcp = rc; + return aplwc; } void aplwc_destroy(struct aplwc *p) { - if(p) - memory_free_aplwc(&p->memory, p); + if(p == NULL) + return; + + memory_destroy(p); + if(p->fns.dealloc) + p->fns.dealloc(p); } const char * @@ -64,67 +83,80 @@ 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; - text = chars_to_string(aplwc, line, -1); - if(text == NULL) - return NULL; + MEMPTRS(aplwc, 2); - tokens = scan_string(aplwc, text, 1); - if(tokens == NULL) - return NULL; + aplwc_clear_line(&ret); - aplwc->last_error = check_tokens(tokens); - if(aplwc->last_error != APLWC_OK) - return NULL; + if((text = chars_to_string(aplwc, linestr, -1)) == NULL) + goto end; + MEMPTR(0, text); - ret = memory_alloc_aplwc_line(&aplwc->memory); - if(ret == NULL){ - aplwc->last_error = APLWC_OUT_OF_MEMORY; - return NULL; - } + if((tokens = scan_string(aplwc, text, 1)) == NULL) + goto end; + MEMPTR(1, tokens); + + if((aplwc->last_error = check_tokens(tokens)) != APLWC_OK) + goto end; + + if((line = memory_alloc_aplwc_line(aplwc)) == NULL) + goto end; - ret->aplwc = aplwc; - ret->text = text; - ret->tokens = tokens; + 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 == NULL) - return; - memory_free_aplwc_line(&line->aplwc->memory, 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; + struct aplwc *aplwc; + + aplwc = line.ref.aplwc; + + aplwc_clear_value(&value); + l = memory_root_get(line.ref); + aplwc->last_error = APLWC_OK; + v = eval_line(aplwc, l); + if(v) + value.ref = memory_root_new(aplwc, v); + return value; } void -aplwc_free_value(struct aplwc_value *value) +aplwc_free_value(AplwcValue value) { - if(value == NULL) - return; - memory_free_aplwc_value(&value->aplwc->memory, 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 "???"; } @@ -136,32 +168,46 @@ 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; + MEMPTRS(aplwc, 3); - namestr = chars_to_string(aplwc, name, -1); - if(namestr == NULL) - goto error; + if((namestr = chars_to_string(aplwc, name, -1)) == NULL) + goto end; + MEMPTR(0, namestr); - text = chars_to_string(aplwc, src, srclen); - if(text == NULL) - goto error; - - tokens = scan_string(aplwc, text, 0); - if(tokens == NULL) - goto error; - - if(load_module(aplwc, namestr, tokens) == APLWC_OK) - return APLWC_OK; - else{ -error: - if(tokens != NULL) - memory_free_token_list(&aplwc->memory, tokens); - if(text != NULL) - memory_free_string(&aplwc->memory, text); - if(namestr != NULL) - memory_free_string(&aplwc->memory, namestr); - return aplwc_last_error(aplwc); - } + if((text = chars_to_string(aplwc, src, srclen)) == NULL) + goto end; + MEMPTR(1, text); + + if((tokens = scan_string(aplwc, text, 0)) == NULL) + goto end; + MEMPTR(2, tokens); + + aplwc->last_error = load_module(aplwc, namestr, tokens); +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); } |