Aria

A low-level systems programming language
git clone git://git.m21c.me/Aria.git
Log | Files | Refs | README | LICENSE

commit 3a3040d32761fefc3411f4a632546c69d566aa2a
parent 60ab6c6b7933a0c7ec3606bdb51075c1eb76471d
Author: m21c <ho*******@gmail.com>
Date:   Tue, 16 Jun 2026 11:26:36 +0200

worked on mem management

Diffstat:
Mcompiler.c | 136++++++++++++++++++++++++++++++++++---------------------------------------------
1 file changed, 58 insertions(+), 78 deletions(-)

diff --git a/compiler.c b/compiler.c @@ -475,6 +475,14 @@ struct MemArena { } MemArena; typedef +struct SourceArenas { + MemArena node, type, env, decl; + MemArena annot, docket; + MemArena record, field; + MemArena block, conduct, gist; +} SourceArenas; + +typedef struct SrcLoc { uint line, column; const char *filename; @@ -800,6 +808,8 @@ struct Analysis { // @section global-vars {{{ Source testsource; +SourceArenas sourcearenas; +SourceArenas *arenas; @@ -893,11 +903,6 @@ const uint8_t opinfo[] = { #define lengthof(array) ((int) sizeof(array) / (int) sizeof(*(array))) #endif -#define unpool(pool, top) \ - (assert((top) < lengthof(pool)), \ - memset((pool) + (top), 0, sizeof*(pool)), \ - (pool) + (top)++) - static int mystrncasecmp(const char *str1, const char *str2, size_t max_len) { @@ -956,16 +961,16 @@ mystrcasecmp(const char *str1, const char *str2) // @section memory arena {{{ -#define arenaalloc(arena, Type) \ - ((Type *) arenaallocinternal(arena, sizeof(Type))) +#define myalloc(arena, Type) \ + ((Type *) myallocimpl(arena, sizeof(Type))) static void * -arenaallocinternal(MemArena *arena, size_t elemsize) +myallocimpl(MemArena *arena, size_t elemsize) { - union {uint8_t *in; void *out;} bitcast; + const size_t pageindex = arena->top / ARENAPAGESIZE; + const size_t pageoffset = arena->top % ARENAPAGESIZE; - size_t pageindex = arena->top / ARENAPAGESIZE; - size_t pageoffset = arena->top % ARENAPAGESIZE; + union {uint8_t *in; void *out;} bitcast; assert(!arena->elemsize || arena->elemsize == elemsize); arena->elemsize = elemsize; @@ -990,6 +995,21 @@ arenaallocinternal(MemArena *arena, size_t elemsize) return bitcast.out; } +static void * +getalloc(MemArena *arena, size_t index) +{ + const size_t pageindex = arena->top / ARENAPAGESIZE; + const size_t pageoffset = arena->top % ARENAPAGESIZE; + + union {uint8_t *in; void *out;} bitcast; + + if (index >= arena->top) + return NULL; + + bitcast.in = arena->pages[pageindex] + pageoffset * arena->elemsize; + return bitcast.out; +} + static void disposearena(MemArena *arena) { @@ -1960,8 +1980,7 @@ getunarysuffix(Source *source) // @section ast-node {{{ Node *poolednodes; -Node nodebuf[4096]; -int nodetop, poolednodecount, totalnodecount; +int poolednodecount, totalnodecount; #define tokennode(source, lhs) makenode(&(source)->tok, (lhs)) @@ -1977,7 +1996,7 @@ makenode(Node *orig, Node *lhs) node->lhs = NULL; --poolednodecount; } else { - node = nodebuf + nodetop++; + node = myalloc(&arenas->node, Node); } ++totalnodecount; @@ -2042,13 +2061,10 @@ deletenode(Node *node) // @section type-struct {{{ -Type typebuf[4096]; -int typetop; - static Type * maketype(SrcLoc *loc, Type *orig, Type *target) { - Type *ty = typebuf + typetop++; + Type *ty = myalloc(&arenas->type, Type); *ty = *orig; ty->loc = *loc; @@ -2063,20 +2079,10 @@ maketype(SrcLoc *loc, Type *orig, Type *target) // @section annotation {{{ -Annot annotbuf[4096]; -int annottop; - -Docket docketbuf[4096]; -int dockettop; - static Annot * makeannot(SrcLoc *loc, int key) { - Annot *annot; - - assert(annottop < lengthof(annotbuf)); - - annot = annotbuf + annottop; + Annot *annot = myalloc(&arenas->annot, Annot); annot->key = key; annot->loc = *loc; @@ -2089,14 +2095,10 @@ makeannot(SrcLoc *loc, int key) static Docket * makedocket(Node *node) { - Docket *docket; + Docket *docket = myalloc(&arenas->docket, Docket); (void) node; /* @todo implement. */ - assert(dockettop < lengthof(docketbuf)); - - docket = docketbuf + dockettop; - /* @todo implement initialization. */ return docket; @@ -2108,9 +2110,6 @@ makedocket(Node *node) // @section environment {{{ -Env envbuf[4096]; -int envtop; - static Decl * finddeclinenv(int key, Env *env) { @@ -2183,7 +2182,7 @@ setheadenv(Source *source, EnvKind kind) { /* @note this might only be useful for parameter => function env * translation */ - Env *env = envbuf + envtop++; + Env *env = myalloc(&arenas->env, Env); env->kind = kind; @@ -2216,7 +2215,7 @@ pushenv(Source *source, EnvKind kind) return source->currenv; } - env = envbuf + envtop++; + env = myalloc(&arenas->env, Env); env->kind = kind; /* @todo make sure that source->tok.loc is the correct * source-location. */ @@ -2315,9 +2314,6 @@ deleteenv(Env *env) // @section declaration {{{ -Decl declbuf[4096]; -int decltop; - static void appenddecltoenv(Decl *decl, Env *targetenv) { @@ -2388,7 +2384,7 @@ makedecl(Source *source, int key, DeclKind kind) ); } - decl = declbuf + decltop++; + decl = myalloc(&arenas->decl, Decl); decl->kind = kind; /* @todo make sure that source->tok.loc is the correct @@ -2415,7 +2411,7 @@ makebundle(Source *source, int key, Decl *parentbundle) /* assert(currenv); */ - decl = declbuf + decltop++; + decl = myalloc(&arenas->decl, Decl); decl->kind = DBUNDLE; /* @todo make sure that source->tok.loc is the correct @@ -2452,7 +2448,7 @@ makedecl2(SrcLoc *loc, Env *env, int key, DeclKind kind) error(loc, "'%s' already declared", getstring(idents, key)); } - decl = declbuf + decltop++; + decl = myalloc(&arenas->decl, Decl); decl->kind = kind; /* @todo make sure that source->tok.loc is the correct @@ -2494,16 +2490,11 @@ defertypedeclaration(Source *source, int key) // @section record {{{ -Record recordbuf[4096]; -int recordtop; - static Record * makerecord(Decl *recorddecl) { - Record *record; + Record *record = myalloc(&arenas->record, Record); - assert(recordtop < lengthof(recordbuf)); - record = recordbuf + recordtop++; record->head = record->tail = NULL; recorddecl->record = record; @@ -2514,16 +2505,11 @@ makerecord(Decl *recorddecl) return record; } -Field fieldbuf[4096 * 16]; -int fieldtop; - static Field * makefield(Record *record, Decl *decl) { - Field *field; + Field *field = myalloc(&arenas->field, Field); - assert(fieldtop < lengthof(fieldbuf)); - field = fieldbuf + fieldtop++; field->decl = decl; field->offset = field->size = 0; field->use = false; @@ -5331,19 +5317,10 @@ For each section there will be a DF-Conduct associated with it. */ -Block blockbuf[1024*8]; -int blocktop; - -Conduct conductbuf[1024*8]; -int conducttop; - -Gist gistbuf[1024*8]; -int gisttop; - static Block * makeblock(BlockKind kind, Env *env) { - Block *block = unpool(blockbuf, blocktop); + Block *block = myalloc(&arenas->block, Block); block->kind = kind; block->env = env; @@ -5354,7 +5331,7 @@ makeblock(BlockKind kind, Env *env) static Conduct * makeconduct(ConductKind kind, Node *label) { - Conduct *conduct = unpool(conductbuf, conducttop); + Conduct *conduct = myalloc(&arenas->conduct, Conduct); conduct->kind = kind; conduct->label = label; @@ -5401,7 +5378,7 @@ appendblock(Conduct *parent, BlockKind kind, Env *env) static Gist * makegist(Decl *decl, Node *where, bool init) { - Gist *gist = unpool(gistbuf, gisttop); + Gist *gist = myalloc(&arenas->gist, Gist); gist->decl = decl; gist->where = where; @@ -8018,7 +7995,7 @@ initsource(Source *source, const char *filename, FILE *file) source->pendingcount = 0; - source->implicitenv = envbuf + envtop++; + source->implicitenv = myalloc(&arenas->env, Env); gettok(source); if (getkind(source) == LINEDELIM) @@ -8069,19 +8046,21 @@ processcommand(Source *source) int i; printf("ast-nodes: %5u, deletes: %5u, total: %5u\n", - nodetop, poolednodecount, totalnodecount); + arenas->node.top, poolednodecount, totalnodecount); + + printf("type-nodes: %5u\n", arenas->type.top); + printf("declarations: %5u\n", arenas->decl.top); + printf("environments: %5u\n", arenas->env.top); - printf("type-nodes: %5u\n", typetop); - printf("declarations: %5u\n", decltop); - printf("environments: %5u\n", envtop); + for (i = 0; i < arenas->node.top; ++i) { + Node *node = getalloc(&arenas->node, i); - for (i = 0; i < nodetop; ++i) { - if (nodebuf[i].kind == 0) + if (node->kind == 0) continue; highlight(stdout, HLINFO); printf("node[%u]:\n", i); - printexpr(stdout, nodebuf + i, 0); + printexpr(stdout, node, 0); printf("\n"); } @@ -8103,7 +8082,7 @@ processcommand(Source *source) i = atoi(command); if (i < 4096 && i >= 0) { - Node *node = nodebuf + i; + Node *node = getalloc(&arenas->node, i); if (node->kind != 0) deletenode(node); @@ -8446,6 +8425,7 @@ int main(int argc, char **argv) { Source *source = &testsource; + arenas = &sourcearenas; initkeywords(); initstrmap(&idents);