;;;; (bytes s) copies, (bytes-view s) aliases — the ruling from the ;;;; INSERTIONSORT dogfooding session. The pin is the exact inverse of the ;;;; old aliasing: a write through the copy leaves the original string ;;;; printing unchanged, where the old (bytes s) either showed the write ;;;; through or trapped, depending on where the string's storage was. (defonce frame Allocator) (defn main [] i32 ;; 1. The copy is writable and independent. Under the old reinterpret this ;; second line printed ZNSERTIONSORT too (or the whole program died in ;; .rodata) — the original staying itself is the whole of the change. (let [s "INSERTIONSORT" b (bytes s)] (set (at b 0) \Z) (println (string b)) ; ZNSERTIONSORT (println s)) ; INSERTIONSORT ;; 2. A literal's copy is writable — the exact form that used to segfault ;; at -O0 and silently do nothing at -O2. (let [b (bytes "hi")] (set (at b 0) \H) (println (string b))) ; Hi ;; 3. The view still costs nothing and reads the string's own storage. (let [v (bytes-view "abc")] (println (len v)) ; 3 (println (at v 2))) ; 99 ;; 4. (bytes s a) names the allocator, like (vec-new T a) and (clone v a): ;; the copy's block comes from the arena and free-all reclaims it. (set frame (arena-new 4096)) (let [b (bytes "arena" frame)] (set (at b 4) \A) (println (string b))) ; arenA (free-all frame) (arena-destroy frame) 0)