staload "ats_array.sats"
implement{a} array_ptr_alloc
(asz) = array_ptr_alloc_tsz {a} (asz, sizeof<a>)
implement{a}
array_ptr_initialize_elt (A0, n0, x0) = let
fun aux {n:nat} {l:addr} .<n>.
(pf: array_v (a?, n, l) | p: ptr l, n: int n, x: a)
:<> (array_v (a, n, l) | void) =
if n > 0 then let
prval (pf1, pf2) = array_v_uncons {a?} (pf)
val () = !p := x
val (pf2 | ()) = aux (pf2 | p + sizeof<a>, n - 1, x)
in
(array_v_cons {a} (pf1, pf2) | ())
end else let
prval () = array_v_unnil (pf) in (array_v_nil {a} () | ())
end prval pf = view@ A0
val (pf | ()) = aux (pf | &A0, n0, x0)
prval () = view@ A0 := pf
in
end
implement{a}
array_ptr_make_elt (n, x) = let
val (pf_gc, pf | p) = array_ptr_alloc_tsz {a} (n, sizeof<a>)
val () = array_ptr_initialize_elt<a> (!p, n, x)
in
(pf_gc, pf | p)
end
implement{a}
array_ptr_initialize_lst (A0, xs0) = let
fun aux {n:nat} {l:addr} .<n>.
(pf: array_v (a?, n, l) | p: ptr l, xs: list (a, n))
:<> (array_v (a, n, l) | void) = begin case+ xs of
| list_cons (x, xs) => let
prval (pf1, pf2) = array_v_uncons {a?} (pf)
val () = !p := x
val (pf2 | ans) = aux (pf2 | p+sizeof<a>, xs)
in
(array_v_cons {a} (pf1, pf2) | ans)
end | list_nil () => let
prval () = array_v_unnil {a?} (pf) in (array_v_nil {a} () | ())
end end val (pf | ()) = aux (view@ A0 | &A0, xs0)
in
view@ A0 := pf
end
implement{a}
array_ptr_make_lst (n, xs) = let
val (pf_gc, pf | p) = array_ptr_alloc_tsz {a} (n, sizeof<a>)
val () = array_ptr_initialize_lst<a> (!p, xs)
in
(pf_gc, pf | p)
end
implement{a} array_ptr_initialize_lst_vt (A0, xs0) = let
fun aux {n:nat} {l:addr} .<n>. (
pf: array_v (a?, n, l)
| p: ptr l, xs: list_vt (a, n)
) :<> (array_v (a, n, l) | void) = begin case+ xs of
| ~list_vt_cons (x, xs) => let
prval (pf1, pf2) = array_v_uncons {a?} (pf)
val () = !p := x
val (pf2 | ans) = aux (pf2 | p+sizeof<a>, xs)
in
(array_v_cons {a} (pf1, pf2) | ans)
end | ~list_vt_nil () => let
prval () = array_v_unnil {a?} (pf) in (array_v_nil {a} () | ())
end end val (pf | ()) = aux (view@ A0 | &A0, xs0)
in
view@ A0 := pf
end
implement{a}
array_ptr_make_lst_vt (n, xs) = let
val (pf_gc, pf | p) = array_ptr_alloc_tsz {a} (n, sizeof<a>)
val () = array_ptr_initialize_lst_vt<a> (!p, xs)
in
(pf_gc, pf | p)
end
%{$
ats_ptr_type
ats_array_ptr_alloc_tsz (
ats_int_type n, ats_size_type tsz
) {
return ATS_MALLOC(n * tsz) ; // uninitialized
} /* end of [ats_array_ptr_alloc_tsz] */
ats_void_type
ats_array_ptr_free
(ats_ptr_type base) { ATS_FREE(base); return ; }
// end of [ats_array_ptr_free]
%}