Skip to content

Commit e85f010

Browse files
feat: add debug tags
debug_malloc now defines its own version of malloc, mfree, mfree-deep and mlength. These new macro can be used to add tags to dynamicly-placed objects to test for memory leaks. debug_malloc.corth and malloc.corth can no longer be included together.
1 parent 94acbae commit e85f010

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

examples/malloc_example.corth

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ include "core/stack.corth"
33

44
macro malloc:ARRAY-SIZE 0x4000 endmacro
55
macro malloc:AVAIL-STACK-SIZE 0x100 endmacro
6-
include "dynamic/malloc.corth"
76
include "dynamic/debug_malloc.corth"
87

8+
memory index sizeof(int) end
9+
910
proc allocate-space
1011
int -> ptr
1112
in let size in
12-
size malloc let object in
13+
size index @64 malloc let object in
1314
object mlength size != if
1415
"[INFO] Size does not match.\n" puts
16+
1 exit drop
1517
else
16-
"[INFO] Successfully allocated object.\n" puts
18+
"[INFO] Successfully allocated object with index " puts index @64 puti ".\n" puts
19+
index @inc64
1720
end
1821

1922
STDOUT debug-dynamic-memory
@@ -24,10 +27,13 @@ end end
2427
proc deallocate-space
2528
ptr ->
2629
in let container in
27-
container mfree if
28-
"[INFO] Successfully deallocated object.\n" puts
29-
else
30-
"[INFO] Could not deallocate object.\n" puts
30+
container debug-malloc:get-tag let tag in
31+
container mfree if
32+
"[INFO] Successfully deallocated object with index " puts tag puti ".\n" puts
33+
else
34+
"[INFO] Could not deallocate object.\n" puts
35+
1 exit drop
36+
end
3137
end
3238

3339
STDOUT debug-dynamic-memory
@@ -37,6 +43,7 @@ proc main
3743
int int -> int
3844
in let argc argv in
3945
malloc:init
46+
0 index !64
4047

4148
STDOUT debug-dynamic-memory
4249

std/dynamic/debug_malloc.corth

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include "linux_x86/sys.corth"
22
include "linux_x86/io/output.corth"
3-
include "dynamic/malloc.corth"
3+
include "dynamic/_malloc.corth"
44

55

66
// Can be used to debug malloc segments.
@@ -54,7 +54,7 @@ in
5454
true end
5555

5656

57-
// Can bu used to see how much space is available and to check memory leaks.
57+
// Can be used to check the space available and to check memory leaks.
5858
proc get-available-dynamic-memory
5959
-> int
6060
in
@@ -68,3 +68,19 @@ in
6868
sum @64
6969
end
7070
end
71+
72+
macro malloc:init malloc:_init endmacro
73+
74+
// This returns a shifted pointer.
75+
// The created dynamicly-placed object contains debug tag information which is hidden from the user.
76+
macro malloc let _size_ _debug_ in
77+
size 8 + _malloc peek _obj_ in _obj_ isn-null if
78+
_debug_ _obj_ !64
79+
end end 8 +
80+
end endmacro
81+
macro mfree 8 - _mfree endmacro
82+
macro mfree-deep _mfree-deep endmacro
83+
macro mlength 8 - _mlength 8 - endmacro
84+
85+
macro debug-malloc:get-tag 8 - @64 endmacro
86+
macro debug-malloc:set-tag 8 - !64 endmacro

0 commit comments

Comments
 (0)