-
Notifications
You must be signed in to change notification settings - Fork 1
/
Region.milone
29 lines (24 loc) · 901 Bytes
/
Region.milone
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
module rec Std.Region
open Std.Ptr
/// Allocates a block of memory in the current region.
/// (Similar to `calloc`.)
///
/// Contents are zeroed.
let rawRegionAlloc (count: int) (size: int) : voidptr =
if count >= 0 && size >= 0 && not (count = 0 && size = 0) then
__nativeExpr ("milone_region_alloc({0}, {1})", count, size)
else
Ptr.nullPtr
module Region =
/// Runs a function in new region.
let run (action: unit -> int) : int =
__nativeStmt "milone_region_enter();"
let code = action ()
__nativeStmt "milone_region_leave();"
code
/// Allocates a block of memory in current region.
let alloc (count: int) : OutPtr<'T> =
__nativeCast (rawRegionAlloc count sizeof<'T>)
/// Registers a function to be executed when to leave of current region.
let defer (action: unit -> unit) : unit =
__nativeStmt ("milone_region_defer({0}.fun, {0}.env);", action)