Skip to content

Commit e261ca3

Browse files
committed
stdlib: implement a crappy allocator
It allocates in multiples of systems page size. It does not do any internal bookkeeping at all. To summarize, it is very inefficient. But it should suffice for creating simple programs which need a heap.
1 parent 3d92e90 commit e261ca3

File tree

17 files changed

+240
-0
lines changed

17 files changed

+240
-0
lines changed

compiler/main/typeinference/typeinfer_expr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ static struct Type* infer_type_expr_ptr_arithmetic(struct ST* st, struct Type* t
9191
return NULL;
9292
}
9393

94+
if (t2->pointer_type) {
95+
if (op == OP_EQ || op == OP_NEQ || op == OP_GE || op == OP_GT || op == OP_LE || op == OP_LT) {
96+
return typeFromStrPrimitive(st, "bool");
97+
}
98+
}
99+
94100
if (t2->basic_type && t2->basic_type->simple_type) {
95101
struct SimpleType* stype = t2->basic_type->simple_type;
96102

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
fn main () -> int {
3+
4+
local *uint8 ptr;
5+
6+
ptr = malloc(0);
7+
8+
if ptr != 0 {
9+
return 1;
10+
}
11+
12+
return 0;
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

examples/stdlib/base/allocator/alloc_0_bytes/alloc_0_bytes.stdlib

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
fn main () -> int {
3+
4+
// test that the allocations do not exhaus memory
5+
6+
for i in 0 .. 10000 {
7+
8+
*uint64 p = malloc(3000);
9+
10+
if p == 0 {
11+
return 1;
12+
}
13+
14+
if free(p) != 0 {
15+
return 1;
16+
}
17+
}
18+
19+
return 0;
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

examples/stdlib/base/allocator/loop_test/loop_test.stdlib

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
fn main () -> int {
3+
4+
*uint64 ptr1 = malloc(30);
5+
*uint64 ptr2 = malloc(30);
6+
7+
if ((ptr1 + 30) > ptr2 && (ptr1 <= ptr2)){
8+
return 1;
9+
}
10+
11+
if ((ptr2 + 30) > ptr1 && (ptr2 <= ptr1)){
12+
return 1;
13+
}
14+
15+
free(ptr1);
16+
free(ptr2);
17+
18+
return 0;
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

examples/stdlib/base/allocator/no_overlap/no_overlap.stdlib

Whitespace-only changes.

0 commit comments

Comments
 (0)