2 | import Control.Monad.Resource
3 | import Data.C.Integer
5 | import Data.Linear.ELift1
6 | import Data.Linear.Token
14 | export %foreign "C:cptr_malloc, cptr-idris"
15 | "scheme,chez:(lambda (x) (if (= 0 x) 0 (foreign-alloc x)))"
16 | prim__malloc : (size : Bits32) -> AnyPtr
18 | export %foreign "C:cptr_calloc, cptr-idris"
19 | prim__calloc : (n, size : Bits32) -> AnyPtr
21 | export %foreign "C:cptr_free, cptr-idris"
22 | "scheme,chez:(lambda (x) (if (= 0 x) '() (foreign-free x)))"
23 | prim__free : AnyPtr -> PrimIO ()
30 | interface WrappedPtr (0 a : Type) where
32 | unwrap : a -> AnyPtr
43 | interface Struct (0 f : Type -> Type) where
44 | swrap : AnyPtr -> f s
45 | sunwrap : f s -> AnyPtr
48 | Struct f => WrappedPtr (f s) where
54 | freeStruct1 : Struct f => f s -> F1' s
55 | freeStruct1 v = ffi $
prim__free (sunwrap v)
59 | allocStruct1 : (0 f : Type -> Type) -> SizeOf (f s) => Struct f => F1 s (f s)
60 | allocStruct1 f t = swrap (prim__malloc (cast $
sizeof (f s))) # t
64 | callocStruct1 : (0 f : Type -> Type) -> SizeOf (f s) => Struct f => F1 s (f s)
65 | callocStruct1 f t = swrap (prim__calloc 1 (cast $
sizeof (f s))) # t
67 | parameters {auto l1 : Lift1 s m}
70 | freeStruct : Struct f => f s -> m ()
71 | freeStruct v = lift1 (freeStruct1 v)
75 | allocStruct : (0 f : _) -> SizeOf (f s) => Struct f => m (f s)
76 | allocStruct f = lift1 (allocStruct1 f)
80 | callocStruct : (0 f : _) -> SizeOf (f s) => Struct f => m (f s)
81 | callocStruct f = lift1 (callocStruct1 f)
88 | ELift1 s m => Struct f => Resource m (f s) where
89 | cleanup v = lift1 (freeStruct1 v)