|
| 1 | +(doc Box |
| 2 | + "A heap-allocated, owning box: a single managed value behind one pointer. |
| 3 | + |
| 4 | +`Box` is an ordinary user type that opts into heap indirection via the |
| 5 | +`recursive` flag, so it can break value-recursion the same way a built-in |
| 6 | +indirection would, e.g. |
| 7 | + |
| 8 | + (deftype IntList (Cons [Int (Box IntList)]) (Nil [])) |
| 9 | + |
| 10 | +It is the reference implementation of a `recursive` type.") |
| 11 | +(deftype (Box a) [data (Ptr a)]) |
| 12 | +(recursive Box) |
| 13 | + |
1 | 14 | (defmodule Box |
| 15 | + (hidden heap-alloc) |
| 16 | + (deftemplate heap-alloc (Fn [a] (Ptr a)) "$a* $NAME($a v)" |
| 17 | + "$DECL { $a* p = CARP_MALLOC(sizeof($a)); *p = v; return p; }") |
| 18 | + |
| 19 | + (hidden heap-free) |
| 20 | + (deftemplate heap-free (Fn [(Ptr a)] ()) "void $NAME($a* p)" |
| 21 | + "$DECL { CARP_FREE(p); }") |
| 22 | + |
| 23 | + ; the value-taking `init` below shadows the generated field constructor |
| 24 | + (hidden from-ptr) |
| 25 | + (deftemplate from-ptr (Fn [(Ptr a)] (Box a)) "Box__$a $NAME($a* p)" |
| 26 | + "$DECL { Box__$a b; b.data = p; return b; }") |
| 27 | + |
| 28 | + (doc init "Initializes a box pointing to value `t`.") |
| 29 | + (sig init (Fn [a] (Box a))) |
| 30 | + (defn init [value] (Box.from-ptr (Box.heap-alloc value))) |
| 31 | + |
| 32 | + (doc peek |
| 33 | + "Returns a reference to the value stored in a box without an allocation.") |
| 34 | + (sig peek (Fn [(Ref (Box a) q)] (Ref a q))) |
| 35 | + (defn peek [b] (Pointer.to-ref @(Box.data b))) |
| 36 | + |
| 37 | + (doc unbox "Converts a boxed value to a local value, deleting the box.") |
| 38 | + (sig unbox (Fn [(Box a)] a)) |
| 39 | + (defn unbox [b] |
| 40 | + (let-do [p @(Box.data &b) |
| 41 | + local (Pointer.to-value p)] |
| 42 | + (Box.heap-free p) |
| 43 | + (Unsafe.leak b) |
| 44 | + local)) |
| 45 | + |
| 46 | + (doc copy "Copies a box and its contents.") |
| 47 | + (sig copy (Fn [(Ref (Box a) q)] (Box a))) |
| 48 | + (defn copy [b] (Box.init @(Box.peek b))) |
| 49 | + (implements copy Box.copy) |
| 50 | + |
| 51 | + (doc delete "Deletes a box, deleting its contents and freeing its memory.") |
| 52 | + (sig delete (Fn [(Box a)] ())) |
| 53 | + (defn delete [b] |
| 54 | + (let-do [p @(Box.data &b)] |
| 55 | + (ignore (Pointer.to-value p)) |
| 56 | + (Box.heap-free p))) |
| 57 | + (implements delete Box.delete) |
| 58 | + |
| 59 | + (doc prn "Returns a string representation of a Box.") |
| 60 | + (sig prn (Fn [(Ref (Box a) q)] String)) |
| 61 | + (defn prn [b] (fmt "(Box %s)" &(prn (Box.peek b)))) |
| 62 | + (implements prn Box.prn) |
| 63 | + |
| 64 | + (doc str "Returns a string representation of a Box.") |
| 65 | + (sig str (Fn [(Ref (Box a) q)] String)) |
| 66 | + (defn str [b] (Box.prn b)) |
| 67 | + (implements str Box.str) |
| 68 | + |
| 69 | + (doc = "Equality on boxes, by their contents.") |
2 | 70 | (defn = [box-a box-b] |
3 | | - (= (Box.unbox @box-a) (Box.unbox @box-b))) |
4 | | - (implements = =) |
5 | | -) |
| 71 | + (= (Box.peek box-a) (Box.peek box-b))) |
| 72 | + (implements = Box.=)) |
0 commit comments