diff options
| author | peter <peter@pmikkelsen.com> | 2026-08-30 14:20:55 +0000 |
|---|---|---|
| committer | peter <peter@pmikkelsen.com> | 2026-08-30 14:20:55 +0000 |
| commit | 8521b6a69deab8cdabe1c87b09ecdc93d75af98e (patch) | |
| tree | 99a482477d500eef11771e6d35e42c173254da29 | |
| parent | 29479e2c50d41f29b5e21ffcb16416887aa693d2 (diff) | |
Remove the struct memory struct
FossilOrigin-Name: 38568a34f631b2a4eef72542e2988a9fe081f85d3248194c2ff995a9eed6bd5d
| -rw-r--r-- | src/lib/aplwc_internal.h | 40 | ||||
| -rw-r--r-- | src/lib/eval.c | 2 | ||||
| -rw-r--r-- | src/lib/main.c | 22 | ||||
| -rw-r--r-- | src/lib/memory.c | 71 | ||||
| -rw-r--r-- | src/lib/module.c | 4 | ||||
| -rw-r--r-- | src/lib/string.c | 2 | ||||
| -rw-r--r-- | src/lib/syntax.c | 4 |
7 files changed, 70 insertions, 75 deletions
diff --git a/src/lib/aplwc_internal.h b/src/lib/aplwc_internal.h index e394bd8..2e3e274 100644 --- a/src/lib/aplwc_internal.h +++ b/src/lib/aplwc_internal.h @@ -8,16 +8,14 @@ #include <stdbool.h> #include <string.h> -struct memory { - uint8_t *data; - size_t size; - size_t offset; -}; - struct aplwc { enum aplwc_rc last_error; - struct memory memory; struct aplwc_module *modules; + + /* Memory management */ + uint8_t *data; + size_t size; + size_t offset; }; struct aplwc_line { @@ -81,20 +79,20 @@ struct aplwc_module { struct aplwc_value *eval_line(struct aplwc_line *); /* memory.c */ -struct memory memory_init(uint8_t *, size_t); -struct aplwc *memory_alloc_aplwc(struct memory *); -struct aplwc_line *memory_alloc_aplwc_line(struct memory *); -struct string *memory_alloc_string(struct memory *, size_t); -struct token_list *memory_alloc_token_list(struct memory *, size_t); -struct aplwc_value *memory_alloc_aplwc_value(struct memory *); -struct aplwc_module *memory_alloc_aplwc_module(struct memory *); -struct module_imports *memory_alloc_module_imports(struct memory *, size_t); -void memory_free_aplwc(struct memory *, struct aplwc *); -void memory_free_aplwc_line(struct memory *, struct aplwc_line *); -void memory_free_string(struct memory *, struct string *); -void memory_free_token_list(struct memory *, struct token_list *); -void memory_free_aplwc_value(struct memory *, struct aplwc_value *); -void memory_free_aplwc_module(struct memory *, struct aplwc_module *); +void memory_init(struct aplwc *, uint8_t *, size_t); +struct aplwc *memory_alloc_aplwc(struct aplwc *); +struct aplwc_line *memory_alloc_aplwc_line(struct aplwc *); +struct string *memory_alloc_string(struct aplwc *, size_t); +struct token_list *memory_alloc_token_list(struct aplwc *, size_t); +struct aplwc_value *memory_alloc_aplwc_value(struct aplwc *); +struct aplwc_module *memory_alloc_aplwc_module(struct aplwc *); +struct module_imports *memory_alloc_module_imports(struct aplwc *, size_t); +void memory_free_aplwc(struct aplwc *, struct aplwc *); +void memory_free_aplwc_line(struct aplwc *, struct aplwc_line *); +void memory_free_string(struct aplwc *, struct string *); +void memory_free_token_list(struct aplwc *, struct token_list *); +void memory_free_aplwc_value(struct aplwc *, struct aplwc_value *); +void memory_free_aplwc_module(struct aplwc *, struct aplwc_module *); /* string.c */ struct string *chars_to_string(struct aplwc *, const char *, int len); diff --git a/src/lib/eval.c b/src/lib/eval.c index f0cdc55..649914d 100644 --- a/src/lib/eval.c +++ b/src/lib/eval.c @@ -9,7 +9,7 @@ eval_line(struct aplwc_line *line) aplwc = line->aplwc; assert(aplwc->last_error == APLWC_OK); - value = memory_alloc_aplwc_value(&aplwc->memory); + value = memory_alloc_aplwc_value(aplwc); if(value == NULL) aplwc->last_error = APLWC_OUT_OF_MEMORY; value->aplwc = aplwc; 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); } } 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); } diff --git a/src/lib/module.c b/src/lib/module.c index 9a202f9..4c96f64 100644 --- a/src/lib/module.c +++ b/src/lib/module.c @@ -58,7 +58,7 @@ load_module(struct aplwc *aplwc, struct string *name, struct token_list *tokens) enum aplwc_rc rc; struct aplwc_module *module; - module = memory_alloc_aplwc_module(&aplwc->memory); + module = memory_alloc_aplwc_module(aplwc); if(module == NULL){ aplwc->last_error = APLWC_OUT_OF_MEMORY; return aplwc->last_error; @@ -73,7 +73,7 @@ load_module(struct aplwc *aplwc, struct string *name, struct token_list *tokens) module->link = aplwc->modules; aplwc->modules = module; }else{ - memory_free_aplwc_module(&aplwc->memory, module); + memory_free_aplwc_module(aplwc, module); } return rc; diff --git a/src/lib/string.c b/src/lib/string.c index 24f978c..d10fb1f 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -69,7 +69,7 @@ invalid: if(extra != 0) goto invalid; - string = memory_alloc_string(&aplwc->memory, bytecount); + string = memory_alloc_string(aplwc, bytecount); if(string){ string->bytes = bytecount; string->chars = charcount; diff --git a/src/lib/syntax.c b/src/lib/syntax.c index 395c75e..9888bef 100644 --- a/src/lib/syntax.c +++ b/src/lib/syntax.c @@ -246,7 +246,7 @@ scan_string(struct aplwc *aplwc, struct string *text, int oneline) return NULL; } - tokens = memory_alloc_token_list(&aplwc->memory, n); + tokens = memory_alloc_token_list(aplwc, n); if(tokens == NULL){ aplwc->last_error = APLWC_OUT_OF_MEMORY; return NULL; @@ -261,7 +261,7 @@ scan_string(struct aplwc *aplwc, struct string *text, int oneline) for(i++; i < n; i++){ if(tokens->tokens[i].tag != TOKEN_TAG_NEWLINE){ - memory_free_token_list(&aplwc->memory, tokens); + memory_free_token_list(aplwc, tokens); return NULL; } } |