forked from ark-lang/ark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.ark
executable file
·48 lines (39 loc) · 1016 Bytes
/
mem.ark
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
// todo
!use "std"
// c bindings
[c] {
func malloc(size: usize): ^void;
func realloc(ptr: ^void, size: usize): ^void;
func free(ptr: ^void): int;
}
struct Block {
last_alloc: usize,
}
impl Block {
static func alloc(size: usize): ^void? {
data: ^void = malloc(size);
if !data {
printf("failed to allocate %zd bytes of memory\n", size);
return None;
}
// magical options that don't parse yet!
// todo tagged enums lol
return Some(data);
}
static func realloc(ptr: ^void, size: usize): ^void? {
data: ^void = realloc(ptr, size);
if !data {
printf("failed to reallocate %zd bytes of memory\n", size)
return None;
}
return Some(data);
}
static func free(data: ^void): bool {
result: int = free(data);
if !result {
printf("failed to de-allocate memory\n");
return false;
}
return true;
}
}