53 - simplest possible allocator: just one word at a time

But with tests this time.
This commit is contained in:
Kartik K. Agaram 2014-08-19 12:02:40 -07:00
parent 955ecf4a45
commit 889e4b958e
4 changed files with 44 additions and 14 deletions

8
mu.arc
View File

@ -18,6 +18,14 @@
(= function* (table)))
(enq clear initialization-fns*)
(mac init-fn (name . body)
`(enq (fn () (= (function* ',name) ',body))
initialization-fns*))
(mac on-init body
`(enq (fn () (run ',body))
initialization-fns*))
(def add-fns (fns)
(each (name . body) fns
(= function*.name body)))

17
new.arc Normal file
View File

@ -0,0 +1,17 @@
;; simple slab allocator. Intended only to carve out isolated memory for
;; different threads/routines as they request.
(on-init
((Root_allocator_pointer location) <- literal 1000) ; 1-1000 reserved
)
(init-fn new
((2 integer-address) <- copy (Root_allocator_pointer integer))
((3 integer) <- literal 1)
((Root_allocator_pointer integer) <- add (Root_allocator_pointer integer) (3 integer))
(reply (2 integer-address)))
; tests to express:
; every call increments the pointer
; no other function can increment the pointer
; no later clause can increment the pointer after this base clause
; multiple threads/routines can't call the allocator at once

19
new.arc.t Normal file
View File

@ -0,0 +1,19 @@
(load "mu.arc")
(load "new.arc")
(reset)
(add-fns
'((main)))
(run function*!main)
(if (~iso memory* (obj Root_allocator_pointer 1000))
(prn "F - allocator initialized"))
(reset)
(add-fns
'((main
((x integer-address) <- new)
((x integer-address deref) <- literal 34))))
(run function*!main)
;? (prn memory*)
(if (~iso memory*!Root_allocator_pointer 1001)
(prn "F - 'new' increments allocator pointer"))

14
new.mu
View File

@ -1,14 +0,0 @@
; memory map: 1-1000 reserved for the (currently non-reentrant) allocator
(main
((1 integer) <- literal 1000) ; location 1 contains the high-water mark for the memory allocator
((4 integer-address) <- new)
((5 integer) <- copy (4 integer-address deref))
)
(new
((2 integer-address) <- copy (1 integer))
((3 integer) <- literal 1)
((1 integer) <- add (1 integer) (3 integer))
(reply (2 integer-address)))
;; vim:ft=scheme