From 889e4b958e0755eb18f4d545ba4f191d9ea0296c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 19 Aug 2014 12:02:40 -0700 Subject: 53 - simplest possible allocator: just one word at a time But with tests this time. --- mu.arc | 8 ++++++++ new.arc | 17 +++++++++++++++++ new.arc.t | 19 +++++++++++++++++++ new.mu | 14 -------------- 4 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 new.arc create mode 100644 new.arc.t delete mode 100644 new.mu 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 -- cgit 1.4.1-2-gfad0