aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2025-09-17 09:49:22 +0200
committerlemon <lsof@mailbox.org>2025-09-17 09:49:22 +0200
commit960f79fa50009fd1e95a294547e3e46494d49484 (patch)
tree93fbb3bb326f7efb8cfdfb249f0d1b76ef25d3bc
parent3d0c49f4eb9e13e00bf12940b53da0f018c0a58c (diff)
alloc changes
-rw-r--r--c.c8
-rw-r--r--common.h4
-rw-r--r--ir.c3
-rw-r--r--lex.c16
-rw-r--r--main.c12
-rw-r--r--mem.c10
-rw-r--r--regalloc.c9
7 files changed, 35 insertions, 27 deletions
diff --git a/c.c b/c.c
index e6bcae0..5cd0ce7 100644
--- a/c.c
+++ b/c.c
@@ -583,7 +583,7 @@ bintypecheck(const struct span *span, enum toktag tt, struct expr *lhs, struct e
static struct expr *
exprdup(struct comp *cm, const struct expr *e)
{
- return memcpy(alloc(&cm->exarena, sizeof *e, 0), e, sizeof *e);
+ return alloccopy(&cm->exarena, e, sizeof *e, 0);
}
static struct expr *
exprdup2(struct comp *cm, const struct expr *e1, const struct expr *e2)
@@ -2185,10 +2185,8 @@ declarator(struct declstate *st, struct comp *cm) {
if (l->param != declparamtmp) free(l->param);
if (l->pqual != declpqualtmp) free(l->pqual);
if (l->prev == &list && l->npar) { /* last */
- st->pnames = alloc(&cm->fnarena, l->npar * sizeof(char *), 0);
- st->pspans = alloc(&cm->fnarena, l->npar * sizeof(struct span), 0);
- memcpy(st->pnames, l->pnames, l->npar * sizeof(char *));
- memcpy(st->pspans, l->pspans, l->npar * sizeof(struct span));
+ st->pnames = alloccopy(&cm->fnarena, l->pnames, l->npar * sizeof(char *), 0);
+ st->pspans = alloccopy(&cm->fnarena, l->pspans, l->npar * sizeof(struct span), 0);
}
if (l->pnames != declpnamestmp) free(l->pnames);
if (l->pspans != declpspanstmp) free(l->pspans);
diff --git a/common.h b/common.h
index 549822a..4587315 100644
--- a/common.h
+++ b/common.h
@@ -342,12 +342,16 @@ struct arena {
uchar mem[];
};
+extern struct arena *globarena;
+
#define vec_of(T) struct { T *p; int _cap; uint n; }
/* libc *alloc wrappers */
+void *xmalloc(size_t n, const char *);
void *xcalloc(size_t n, const char *);
void *xrealloc(void *, size_t n, const char *);
void free(void *);
+#define xmalloc(n) xmalloc(n, __func__)
#define xcalloc(n) xcalloc(n, __func__)
#define xrealloc(p,n) xrealloc(p, n, __func__)
diff --git a/ir.c b/ir.c
index 834ff45..e0d219f 100644
--- a/ir.c
+++ b/ir.c
@@ -64,9 +64,8 @@ irinit(struct function *fn)
cls2siz[KI8] = cls2siz[KF8] = 8;
cls2siz[KPTR] = targ_primsizes[TYPTR];
}
- fn->entry = fn->curblk = alloc(fn->arena, sizeof(struct block), 0);
+ fn->entry = fn->curblk = allocz(fn->arena, sizeof(struct block), 0);
fn->nblk = 1;
- memset(fn->entry, 0, sizeof *fn->entry);
fn->entry->lprev = fn->entry->lnext = fn->entry;
}
diff --git a/lex.c b/lex.c
index d17bef0..401b84d 100644
--- a/lex.c
+++ b/lex.c
@@ -341,8 +341,7 @@ readstrchrlit(struct lexer *lx, struct token *tk, char delim)
} else {
tk->litlit = 0;
vpush(&b, 0);
- tk->s = alloc(lx->tmparena, b.n, 1);
- memcpy((char *)tk->s, b.p, b.n);
+ tk->s = alloccopy(lx->tmparena, b.p, b.n, 1);
}
} else {
if (b.n == 0) {
@@ -359,8 +358,7 @@ readstrchrlit(struct lexer *lx, struct token *tk, char delim)
tk->s = (char *)&lx->dat[beginoff];
} else {
tk->litlit = 0;
- tk->s = alloc(lx->tmparena, tk->len, 1);
- memcpy((char *)tk->s, b.p, tk->len);
+ tk->s = alloccopy(lx->tmparena, b.p, tk->len, 1);
}
}
vfree(&b);
@@ -394,8 +392,7 @@ readheadername(struct lexer *lx, struct token *tk, char delim)
} else {
tk->litlit = 0;
vpush(&b, 0);
- tk->s = alloc(lx->tmparena, b.n, 1);
- memcpy((char *)tk->s, b.p, b.n);
+ tk->s = alloccopy(lx->tmparena, b.p, b.n, 1);
}
vfree(&b);
}
@@ -530,8 +527,7 @@ Begin:
tk->len = n;
if (n == lx->chridx - idx) tk->s = (char *)&lx->dat[idx];
else {
- tk->s = alloc(lx->tmparena, n, 1);
- memcpy((char *)tk->s, tmp, n);
+ tk->s = alloccopy(lx->tmparena, tmp, n, 1);
}
RET(TKNUMLIT);
} else if (c == '_' || aisalpha(c)) {
@@ -1065,7 +1061,7 @@ ppinclude(struct lexer *lx, const struct span *span0)
for (end = base; *end != 0; ++end) {}
for (--end; *end != '/' && end != base; --end) {}
if (*end == '/') ++end;
- path = xcalloc(end - base + tk.len + 1);
+ path = alloc(&globarena, end - base + tk.len + 1, 1);
memcpy(path, base, end - base);
memcpy(path + (end - base), tk.s, tk.len);
path[end - base + tk.len] = 0;
@@ -1077,7 +1073,7 @@ ppinclude(struct lexer *lx, const struct span *span0)
//efmt(">include %'s\n", path);
joinspan(&span.ex, tk.span.ex);
initlexer(&new, &span, path, lx->tmparena);
- new.save = xcalloc(sizeof *new.save);
+ new.save = xmalloc(sizeof *new.save);
memcpy(new.save, lx, sizeof *lx);
*lx = new;
diff --git a/main.c b/main.c
index 7335dd3..2b98cab 100644
--- a/main.c
+++ b/main.c
@@ -52,6 +52,12 @@ ftdetect(const char *s)
return IFTc;
}
+static union {
+ struct arena a;
+ char mem[sizeof(struct arena) + (1<<10)];
+} _arenamem;
+struct arena *globarena = &_arenamem.a;
+
/* withext("x/y.c", "o") -> "y.o"; withext("f9", "s") -> "f9.s" */
static const char *
withext(const char *path, const char *ext)
@@ -69,7 +75,7 @@ withext(const char *path, const char *ext)
len = strlen(file);
else
len = oext - file - 1;
- res = xcalloc(len + 1 + strlen(ext) + 1);
+ res = alloc(&globarena, len + 1 + strlen(ext) + 1, 1);
memcpy(res, file, len);
res[len] = '.';
memcpy(res + len + 1, ext, strlen(ext));
@@ -193,8 +199,9 @@ tempfile(const char *path, const char *ext)
else ioputc(&fbuf, '_');
}
bfmt(&fbuf, "%s%s", &"."[!ext], ext ? ext : "");
+ ioputc(&fbuf, 0);
assert(!fbuf.err);
- return memcpy(xcalloc(fbuf.len + 1), fbuf.buf, fbuf.len);
+ return alloccopy(&globarena, fbuf.buf, fbuf.len, 1);
}
static int cc1(const char *out, const char *in);
@@ -345,6 +352,7 @@ int
main(int argc, char **argv)
{
atexit(flushstd);
+ globarena->cap = sizeof(_arenamem.mem) - sizeof(struct arena);
/* setup defaults */
detectcolor();
diff --git a/mem.c b/mem.c
index 8ed87eb..7faadc6 100644
--- a/mem.c
+++ b/mem.c
@@ -11,6 +11,14 @@
} while (0)
void *
+(xmalloc)(size_t n, const char *f)
+{
+ void *p = malloc(n);
+ if (!p) ALLOCERR(f);
+ return p;
+}
+
+void *
(xcalloc)(size_t n, const char *f)
{
void *p = calloc(n, 1);
@@ -84,7 +92,7 @@ vresize_(void **p, int *pcap, uint *pn, uint siz, uint N)
struct arena *
newarena(uint chunksiz)
{
- struct arena *ar = xcalloc(offsetof(struct arena, mem) + chunksiz);
+ struct arena *ar = xmalloc(offsetof(struct arena, mem) + chunksiz);
assert(chunksiz < 1u<<31 && "toobig");
ar->cap = chunksiz;
ar->dyn = 1;
diff --git a/regalloc.c b/regalloc.c
index 1018e7c..68d6592 100644
--- a/regalloc.c
+++ b/regalloc.c
@@ -780,20 +780,17 @@ linearscan(struct rega *ra)
struct interval **unhandled = NULL;
struct interval *active = NULL, *inactive = NULL, *handled = NULL;
+ if (!intervals->count) return;
/* sort intervals */
{
extern int ninstr;
- unhandled = xcalloc(sizeof *unhandled * intervals->count);
+ unhandled = alloc(ra->arena, sizeof *unhandled * intervals->count, 0);
for (int i = 0; i < ninstr; ++i) {
if (!intervals->temps[i].nrange) continue;
unhandled[nunhandled++] = &intervals->temps[i];
}
assert(nunhandled == intervals->count);
- if (!nunhandled) {
- free(unhandled);
- return;
- }
sortintervals(unhandled, 0, nunhandled-1);
}
@@ -954,8 +951,6 @@ linearscan(struct rega *ra)
active = current;
}
}
-
- free(unhandled);
}
/* replace temps with physical regs, add loads & stores for spilled temps */