aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-19 06:57:21 +0200
committerlemon <lsof@mailbox.org>2022-08-19 06:57:21 +0200
commitf64f691a43f474b450f29795b4bc68bc2ef6231e (patch)
tree7e949c593a66118d4a8a1043f664e363affa96f8
parente07baf707f097434cbe4739d3fcf1bc27a62d587 (diff)
take alignment in allocator
-rw-r--r--src/mem.hff19
-rw-r--r--src/parse.cff6
-rw-r--r--src/vec.hff2
3 files changed, 15 insertions, 12 deletions
diff --git a/src/mem.hff b/src/mem.hff
index 7858f4d..d3dd634 100644
--- a/src/mem.hff
+++ b/src/mem.hff
@@ -23,18 +23,20 @@ struct Arena {
a.r = r;
}
- fn allocf(a *void, n usize) *void {
+ fn allocf(a *void, n usize, align usize) *void {
let a *Arena = a;
- n = ALIGNUP(n, 16);
+ if a.r {
+ a.r.idx = ALIGNUP(a.r.idx, align);
+ }
if n > ARENA_SIZE {
a->addregion(n);
return a.r.mem;
} else if a.r == #null {
a->addregion(ARENA_SIZE);
- return allocf(a, n);
+ return allocf(a, n, align);
} else if n > a.r.siz - a.r.idx {
a->addregion(ARENA_SIZE);
- return allocf(a, n);
+ return allocf(a, n, align);
} else {
let p = &a.r.mem[a.r.idx];
a.r.idx += n;
@@ -52,12 +54,13 @@ struct Arena {
struct Allocator {
a *void,
- allocf *fn(*void, usize) *void,
+ allocf *fn(*void, usize, usize) *void,
freef *fn(*void, *void) void,
- fn alloc(self *Allocator, n usize) *void {
+ fn alloc(self *Allocator, n usize, align usize) *void {
if self.allocf {
- return self.allocf(self.a, n);
+ let p = self.allocf(self.a, n, align);
+ return p;
}
return #null;
}
@@ -69,4 +72,4 @@ struct Allocator {
}
}
-defmacro anew(a, T) [ ((a)->alloc(sizeof T)) ]
+defmacro anew(a, T) [ ((a)->alloc(sizeof T, alignof T)) ]
diff --git a/src/parse.cff b/src/parse.cff
index b42d45a..1449eab 100644
--- a/src/parse.cff
+++ b/src/parse.cff
@@ -858,7 +858,7 @@ fn typematchestarg(targ *const Type, ty *const Type) bool {
}
fn exprdup(alloc *Allocator, ex Expr) *Expr {
- return memcpy(alloc->alloc(sizeof(ex)), &ex, sizeof(ex));
+ return memcpy(alloc->alloc(sizeof(ex), alignof(ex)), &ex, sizeof(ex));
}
fn islvalue(ex Expr) bool {
@@ -955,7 +955,7 @@ fn mergetoktrees(alloc *Allocator, src *[#]Tok, n int) [#]Tok {
++total;
}
}
- let d *Tok = alloc->alloc(total * sizeof Tok);
+ let d *Tok = alloc->alloc(total * sizeof Tok, alignof Tok);
let len = 0;
for let i = 0; i < n; ++i {
if i > 0 {
@@ -1687,7 +1687,7 @@ fn parseexpr(P *Parser) Expr {
////////////////////////
fn stmtdup(alloc *Allocator, st Stmt) *Stmt {
- return memcpy(alloc->alloc(sizeof(st)), &st, sizeof(st));
+ return memcpy(alloc->alloc(sizeof(st), alignof(st)), &st, sizeof(st));
}
typedef StmtYielder *fn(Stmt, *void) void;
diff --git a/src/vec.hff b/src/vec.hff
index 2966579..4d877d8 100644
--- a/src/vec.hff
+++ b/src/vec.hff
@@ -38,7 +38,7 @@ struct Vec<T> {
fn move(vec *Vec, alloc *Allocator) [#]T {
let len = vec.len;
- let dat *T = alloc->alloc(len * sizeof T);
+ let dat *T = alloc->alloc(len * sizeof T, alignof T);
memcpy(dat, vec.dat, len * sizeof T);
vec->clear();
return dat[0::len];