aboutsummaryrefslogtreecommitdiff
path: root/src/mem.hff
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 /src/mem.hff
parente07baf707f097434cbe4739d3fcf1bc27a62d587 (diff)
take alignment in allocator
Diffstat (limited to 'src/mem.hff')
-rw-r--r--src/mem.hff19
1 files changed, 11 insertions, 8 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)) ]