[# a small fixed-slot free-list allocator built on a raw block #]
make string "\n" nl
make int64 head
define newline result nothing
address nl
output text
set nothing
return
end
define getslot address array of int64 blk int64 idx result int64
move idx
read
push
move blk
read
move at
pop
move index
read
return
end
define setslot address array of int64 blk int64 idx int64 val result nothing
move val
read
push
move idx
read
push
move blk
read
move at
pop
move index
pop
write
set nothing
return
end
define alloc address array of int64 blk result int64
move head
read
push
move blk
read
push
move head
read
push
call getslot
move head
write
pop
return
end
define dealloc address array of int64 blk int64 slot result nothing
move blk
read
push
move slot
read
push
move head
read
push
call setslot
move slot
read
move head
write
set nothing
return
end
define putval int64 v result nothing
move v
read
output
call newline
set nothing
return
end
define main
make 17 of int64 pool
make int64 i
set 1
move i
write
set 0
home
mark build
move i
read
compare 16
jump capped when greater-or-equal
set 0
home
move pool
read
push
move i
read
push
move i
read
add 1
push
call setslot
move i
read
add 1
move i
write
set 0
home
jump build
mark capped
move pool
read
push
set 16
push
set 0
push
call setslot
set 1
move head
write
set 0
home
move pool
read
push
call alloc
push
call putval
move pool
read
push
call alloc
push
call putval
move pool
read
push
call alloc
push
call putval
move pool
read
push
set 2
push
call dealloc
move pool
read
push
call alloc
push
call putval
move pool
read
free
return
end