flan/test/programs/free-slice.flan

36 lines
1.5 KiB
Plaintext

;;;; (free s) on a slice hands its block back to the context allocator, or to
;;;; the one named. A slice does not carry its allocator, so a dev build checks
;;;; the block against the allocation registry and traps rather than hand one
;;;; allocator another's block. Argument 0 frees correctly both ways; 1 frees
;;;; an arena's copy through the context allocator; 2 frees one copy twice;
;;;; 3 frees a view of an array, which no allocator handed out; 4 frees a
;;;; Vec's storage through a let-bound view of it; 5 frees a formatted
;;;; number's text, which the temp allocator holds.
(defn main [args [str]] i32
(let [which (if (> (length args) 1) (bytes->i64 (bytes-view (at args 1))) 0)
a (arena-new 4096)]
(cond
(= which 1) (let [b (bytes "arena" a)] (free b))
(= which 2) (let [b (bytes "twice")] (free b) (free b))
(= which 3) (let [arr [1 2 3]
s (slice arr)]
(free s))
(= which 4) (let [v (vec-new i32)]
(push v 1)
(push v 2)
(let [s (slice v)] (free s))
(println (at v 1))
(free v))
(= which 5) (let [t (i64->bytes 42)] (free t))
:else
(let [b (bytes "heap")
c (bytes "arena" a)
d (clone (slice [1.5 2.5]) (heap-allocator))]
(println (str b) (str c) (at d 1))
(free b)
(free c a)
(free d (heap-allocator))))
(println "done")
(arena-destroy a))
0)