/
MarshalAlloc.hs
90 lines (72 loc) · 2.61 KB
/
MarshalAlloc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- (c) The FFI task force, [2000..2001]
--
-- Marshalling support: basic routines for memory allocation
module MarshalAlloc (
malloc, -- :: Storable a => IO (Ptr a)
mallocBytes, -- :: Int -> IO (Ptr a)
alloca, -- :: Storable a => (Ptr a -> IO b) -> IO b
allocaBytes, -- :: Int -> (Ptr a -> IO b) -> IO b
reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
free -- :: Ptr a -> IO ()
) where
import IO (bracket)
import Ptr (Ptr, nullPtr)
import Storable (Storable(sizeOf))
import CTypesISO (CSize)
-- exported functions
-- ------------------
-- allocate space for storable type
--
malloc :: Storable a => IO (Ptr a)
malloc = doMalloc undefined
where
doMalloc :: Storable a => a -> IO (Ptr a)
doMalloc dummy = mallocBytes (sizeOf dummy)
-- allocate given number of bytes of storage
--
mallocBytes :: Int -> IO (Ptr a)
mallocBytes size = failWhenNULL "malloc" (_malloc (fromIntegral size))
-- temporarily allocate space for a storable type
--
-- * the pointer passed as an argument to the function must *not* escape from
-- this function; in other words, in `alloca f' the allocated storage must
-- not be used after `f' returns
--
alloca :: Storable a => (Ptr a -> IO b) -> IO b
alloca = doAlloca undefined
where
doAlloca :: Storable a => a -> (Ptr a -> IO b) -> IO b
doAlloca dummy = allocaBytes (sizeOf dummy)
-- temporarily allocate the given number of bytes of storage
--
-- * the pointer passed as an argument to the function must *not* escape from
-- this function; in other words, in `allocaBytes n f' the allocated storage
-- must not be used after `f' returns
--
allocaBytes :: Int -> (Ptr a -> IO b) -> IO b
allocaBytes size = bracket (mallocBytes size) free
-- adjust a malloc'ed storage area to the given size
--
reallocBytes :: Ptr a -> Int -> IO (Ptr a)
reallocBytes ptr size =
failWhenNULL "realloc" (_realloc ptr (fromIntegral size))
-- free malloc'ed storage
--
free :: Ptr a -> IO ()
free = _free
-- auxilliary routines
-- -------------------
-- asserts that the pointer returned from the action in the second argument is
-- non-null
--
failWhenNULL :: String -> IO (Ptr a) -> IO (Ptr a)
failWhenNULL name f = do
addr <- f
if addr == nullPtr
then ioError (userError (name ++ ": out of memory"))
else return addr
-- basic C routines needed for memory allocation
--
foreign import "malloc" unsafe _malloc :: CSize -> IO (Ptr a)
foreign import "realloc" unsafe _realloc :: Ptr a -> CSize -> IO (Ptr a)
foreign import "free" unsafe _free :: Ptr a -> IO ()