commit f8708cc8bb968349c0e030013212ac2cb723e773
parent 6bd1db337c748f2b4a21ec550025de9e818fd857
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 4 Jul 2023 14:41:34 -0400
lib/alloc: alignment discipline
Diffstat:
4 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/fs/doc/lib/alloc.txt b/fs/doc/lib/alloc.txt
@@ -35,10 +35,14 @@ struct which provides a common API.
available. If not, do its "thing" (rollover, create a new arena, etc.). Return
the (still unallocated!) address of that memory.
+ That address is always aligned to 4 bytes.
+
:allot ( n self -- a )
allocate n bytes in the allocator and return the address of the allocated
memory space.
+ That address is always aligned to 4 bytes.
+
:move ( src u self -- a )
allocate u bytes and copy the range "src u" into it. Then, return the
allocated memory space.
diff --git a/fs/lib/alloc.fs b/fs/lib/alloc.fs
@@ -19,10 +19,10 @@ struct[ Allocator
: :freesz dup :)buf swap ptr - ;
: :ensure ( n self -- a )
- dup :freesz rot < if dup :findspace tuck swap to ptr else ptr then ;
+ dup :freesz rot < if dup :findspace tuck swap align4 to ptr else ptr then ;
: :allot ( n self -- a )
- 2dup :ensure rot> to+ ptr ;
+ 2dup :ensure rot> align4 to+ ptr ;
: :move ( src u self -- a )
over swap :allot ( src u dst ) tuck >r move r> ;
@@ -35,7 +35,7 @@ struct[ Allocator
: :[ ( size self -- ) :allot dup to _ptr to@! here to _here ;
: :] ( self -- a )
here over ptr > if abort" allocator :[ overflow" then
- _here to@! here swap to ptr _ptr ;
+ _here to@! here swap align4 to ptr _ptr ;
: :, ( n self -- a ) CELLSZ swap :allot tuck ! ;
: :s, ( str self -- a ) swap c@+ rot :[]>str ;
]struct
diff --git a/fs/lib/arena.fs b/fs/lib/arena.fs
@@ -6,7 +6,7 @@ struct[ ArenaBuf
sfield nextbuf
SZ &+ buf
: :)buf buf ARENASZ + ;
- : :new ( -- buf ) herefree# here 0 , ARENASZ allot ;
+ : :new ( -- buf ) herefree# here# 0 , ARENASZ allot ;
: :next dup nextbuf ?dup not if :new tuck swap to nextbuf else nip then ;
]struct
@@ -19,7 +19,7 @@ extends Allocator struct[ Arena
dup current ArenaBuf :next ( self arenabuf )
tuck swap to current ( arenabuf ) ArenaBuf buf ;
: :new ( -- arena )
- ArenaBuf :new here swap dup ( arena arenabuf )
+ ArenaBuf :new here# swap dup ( arena arenabuf )
ArenaBuf buf ( ptr ) , ['] _)buf , ['] _findspace ,
dup ( root ) , ( current ) , ;
: :reset ( self -- )
diff --git a/fs/lib/malloc.fs b/fs/lib/malloc.fs
@@ -4,7 +4,7 @@ struct[ _Buf
sfield used
SZ &+ buf
: :)buf bi buf | size + ;
- : :new ( sz -- buf ) herefree# here 0 , over , 0 , swap allot ;
+ : :new ( sz -- buf ) herefree# here# 0 , over , 0 , swap allot ;
\ Find an unused buffer with a size >= "sz".
: :find ( sz self -- buf-or-0 ) swap >r \ V1=sz
begin dup while ( buf )