2 * 2005 Matthew
Fluet (mfluet@acm
.org
)
6 (* memalloc
-a4
-unix
.sml
8 * Memory
allocation (via malloc
) for Unix
.
9 * Size
of address
: 4 bytes
.
11 * Copyright (c
) 2004 by The Fellowship
of SML
/NJ
13 * Author
: Matthias
Blume (blume@tti
-c
.org
)
15 structure CMemAlloc
: CMEMALLOC
= struct
19 structure Ptr
= MLton
.Pointer
25 structure DL
= DynLinkage
27 fun main
's s
= DL
.lib_symbol (DL
.main_lib
, s
)
28 val malloc_h
= main
's
"malloc"
29 val free_h
= main
's
"free"
31 fun sys_malloc (n
: C_Size
.word) =
32 let val w_p
= _import
* : MLton
.Pointer
.t
-> C_Size
.word -> addr
;
33 val a
= w_p (DL
.addr malloc_h
) n
34 in if a
= Ptr
.null
then raise OutOfMemory
else a
37 fun sys_free (a
: addr
) =
38 let val p_u
= _import
* : MLton
.Pointer
.t
-> addr
-> unit
;
39 in p_u (DL
.addr free_h
) a
43 fun sys_malloc (n
: C_Size
.word) =
44 let val w_p
= _import
"malloc" : C_Size
.word -> addr
;
46 in if a
= Ptr
.null
then raise OutOfMemory
else a
49 fun sys_free (a
: addr
) =
50 let val p_u
= _import
"free" : addr
-> unit
;
54 fun alloc bytes
= sys_malloc (C_Size
.fromLarge (Word.toLarge bytes
))
55 fun free a
= sys_free a