diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-01-26 02:56:08 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-01-26 02:56:08 -0800 |
commit | ab1916bb210ebba36d9bdf2823a4208e7b255f70 (patch) | |
tree | e9ff3db314d42357ef21660e3db18460bb486fba | |
parent | 0d8f88f7c2cd73a0c85d85907d20c0e37e4ea101 (diff) | |
download | mu-ab1916bb210ebba36d9bdf2823a4208e7b255f70.tar.gz |
629
-rw-r--r-- | mu.arc | 26 | ||||
-rw-r--r-- | mu.arc.t | 34 |
2 files changed, 45 insertions, 15 deletions
diff --git a/mu.arc b/mu.arc index d8b45205..50ba0b95 100644 --- a/mu.arc +++ b/mu.arc @@ -1017,25 +1017,23 @@ ; memory allocation +(def alloc (sz) + (when (> sz (- rep.routine*!alloc-max rep.routine*!alloc)) + (let curr-alloc Memory-allocated-until + (= rep.routine*!alloc curr-alloc) + (++ Memory-allocated-until Allocation-chunk) + (= rep.routine*!alloc-max Memory-allocated-until))) + (ret result rep.routine*!alloc + (++ rep.routine*!alloc sz))) + (def new-scalar (type) ;? (tr "new scalar: @type") - (let sz (sizeof `((_ ,type))) - (when (> sz (- rep.routine*!alloc-max rep.routine*!alloc)) - (let curr-alloc Memory-allocated-until - (= rep.routine*!alloc curr-alloc) - (++ Memory-allocated-until Allocation-chunk) - (= rep.routine*!alloc-max Memory-allocated-until))) - (ret result rep.routine*!alloc - (++ rep.routine*!alloc sz)))) + (alloc (sizeof `((_ ,type))))) (def new-array (type size) ;? (tr "new array: @type @size") - (ret result rep.routine*!alloc - (++ rep.routine*!alloc (+ 1 (* (sizeof `((_ ,@type*.type!elem))) size))) -;? (tr "new-array: @result => @rep.routine*!alloc") - (= memory*.result size) - (assert (< rep.routine*!alloc rep.routine*!alloc-max) "allocation overflowed routine space @rep.routine*!alloc - @rep.routine*!alloc-max") - )) + (ret result (alloc (+ 1 (* (sizeof `((_ ,@type*.type!elem))) size))) + (= memory*.result size))) (def new-string (literal-string) ;? (tr "new string: @literal-string") diff --git a/mu.arc.t b/mu.arc.t index 51316e1c..efa0bf57 100644 --- a/mu.arc.t +++ b/mu.arc.t @@ -1952,7 +1952,39 @@ (~is rep.routine!alloc 92) (~is rep.routine!alloc-max 100) (~is Memory-allocated-until 100)) - (prn "F - 'new' skips allocates a new chunk if insufficient space"))) + (prn "F - 'new' allocates a new chunk if insufficient space"))) + +(reset) +(new-trace "new-array-skip-noncontiguous") +(add-code + '((function main [ + (1:integer-array-address <- new integer-array:literal 4:literal) + ]))) +; start allocating from address 30, in chunks of 10 locations each +(= Memory-allocated-until 30 + Allocation-chunk 10) +(let routine make-routine!main + (assert:is rep.routine!alloc 30) + (assert:is rep.routine!alloc-max 40) + ; pretend the current chunk has just one location left + (= rep.routine!alloc 39) + ; pretend we allocated more memory since we created the routine + (= Memory-allocated-until 90) + (enq routine running-routines*) + ; request 4 locations + (run) + (each routine completed-routines* + (aif rep.routine!error (prn "error - " it))) +;? (prn memory*.1) ;? 1 +;? (prn rep.routine) ;? 1 +;? (prn Memory-allocated-until) ;? 1 + (when (or (~is memory*.1 90) + (~is rep.routine!alloc 95) + (~is rep.routine!alloc-max 100) + (~is Memory-allocated-until 100)) + (prn "F - 'new-array' allocates a new chunk if insufficient space"))) + +;? (quit) ;? 1 ; Even though our memory locations can now have names, the names are all ; globals, accessible from any function. To isolate functions from their |