about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2014-08-19 12:02:40 -0700
committerKartik K. Agaram <vc@akkartik.com>2014-08-19 12:02:40 -0700
commit889e4b958e0755eb18f4d545ba4f191d9ea0296c (patch)
treecf741bd6858a4f4c9eeb5fdbd1c9f4edd578f22c
parent955ecf4a45312c495403aacc0b687a4bc33844ec (diff)
downloadmu-889e4b958e0755eb18f4d545ba4f191d9ea0296c.tar.gz
53 - simplest possible allocator: just one word at a time
But with tests this time.
-rw-r--r--mu.arc8
-rw-r--r--new.arc17
-rw-r--r--new.arc.t19
-rw-r--r--new.mu14
4 files changed, 44 insertions, 14 deletions
diff --git a/mu.arc b/mu.arc
index 3499bb49..df63e916 100644
--- a/mu.arc
+++ b/mu.arc
@@ -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)))
diff --git a/new.arc b/new.arc
new file mode 100644
index 00000000..0f0b4b71
--- /dev/null
+++ b/new.arc
@@ -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
diff --git a/new.arc.t b/new.arc.t
new file mode 100644
index 00000000..e256ea86
--- /dev/null
+++ b/new.arc.t
@@ -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"))
diff --git a/new.mu b/new.mu
deleted file mode 100644
index 04cfbbed..00000000
--- a/new.mu
+++ /dev/null
@@ -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