flan/test/programs/into-owning.flan

23 lines
969 B
Plaintext

;;;; into over elements that own storage. Without a (map f) in the chain the
;;;; elements are pushed as they stand, which copies their headers and shares
;;;; their blocks — so that is refused, and (map clone) is the copy that
;;;; compiles. The outer Vecs are in an arena, as a container of owning
;;;; elements has to be; the inner ones are on the heap, where growing one
;;;; through a shared header would free the block the other still points at.
(defn main [] i32
(let [a (arena-new 65536)
v (vec-new (Vec i32) a)]
(push v (vec-new i32))
(push (at v 0) 1)
(let [w (into v (vec-new (Vec i32) a) (map clone))]
(dotimes [i 100] (push (at w 0) i))
(println (at (at v 0) 0)) ; 1
(println (length (at v 0))) ; 1
(println (length (at w 0))) ; 101
(println (at (at w 0) 100)) ; 99
(free (at w 0)))
(free (at v 0))
(arena-destroy a)
0))