use Mem get-typesize TypeSize cast-ptr use Cnile use Slice Slice use Iter zip class Allocator allocate (self _, count Size) -> Slice a free (self _, slice Slice a) -> Unit # zig-like allocator struct thing. # Allocator # allocate (sz Size) -> Slice U8 # free (p Ptr U8) -> Unit # fn allocate (self Allocator, count Size) -> Slice a # pass # bogus datatype for the C allocator. CAllocator CAllocator fn get-c-allocator (): CAllocator fn idc(): CAllocator inst Allocator CAllocator allocate (CAllocator CAllocator, count Size) -> Slice a size = (get-typesize() as TypeSize a).size ptr = Cnile.malloc(size * count) cast-ptr() # TODO: right now we don't care about allocation failures. return Slice { ptr: ptr, count: count } free (CAllocator, slice Slice a) -> Unit Cnile.free(slice.ptr cast-ptr()) fn default(al Allocator, count Size, x a) -> Slice a s = al allocate(count) s Slice.map(fn _: x) return s fn clone(s Slice a, al Allocator) -> Slice a nus = al allocate(s.count) for (sx, og) in nus Slice.addresses() zip(s) sx <&= og return nus fn deep-free (al Allocator, s Slice a, free-fun (Allocator, a) -> ()) s Iter.for-each(fn ss: free-fun(al, ss)) al free(s)