Skip to content

Commit 6ceeb12

Browse files
committed
Merge free.dasm into malloc.dasm.
There are references to undeclared labels. This should be fixed.
1 parent c5ee618 commit 6ceeb12

File tree

2 files changed

+67
-71
lines changed

2 files changed

+67
-71
lines changed

mem/free.dasm

Lines changed: 0 additions & 70 deletions
This file was deleted.

mem/malloc.dasm

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
;; licensed under MIT.
44
;;
55
;; http://u.jdiez.me/0x42c/kernel.html#x01e-kern-malloc
6-
;; declares: malloc
6+
;; http://u.jdiez.me/0x42c/kernel.html#x022-kern-free
7+
;; declares: malloc, free
8+
;; NOTE: There are a few references to undeclared labels. FIXME jdiez
79

810
; void* malloc(int owner, int words)
911
:malloc
@@ -78,3 +80,67 @@
7880
add a, 2 ; real start of memory
7981
set i, pop
8082
set pc, pop
83+
84+
; void free(void* addr)
85+
:free
86+
sub a, 2 ; user gives the first allocated word, header is two words back
87+
set [a], 0xffff ; free memory
88+
set push, a
89+
90+
:free_check_forward
91+
add a, 1
92+
set b, [a]
93+
add a, b
94+
add a, 2 ; next header
95+
ife [a], 0xffff
96+
set pc, free_merge_forward
97+
98+
:free_check_backwards
99+
set a, peek
100+
sub a, 1
101+
ifg a, [userspacemem]
102+
set pc free_check_backwards_continue
103+
104+
set pc, free_end
105+
106+
:free_check_backwards_continue
107+
ife [a], 0x0 ; if previous footer is empty
108+
set pc, free_end
109+
110+
set b, [a]
111+
set a, b
112+
ifn [a], 0xffff
113+
set pc, free_end
114+
115+
set peek, a ; the previous push is unnecesary
116+
add a, 1
117+
set b, [a]
118+
add a, b
119+
add a, 2
120+
121+
set pc, free_merge_forward ; no need to check since we already know there's a blob of free memory there
122+
123+
:free_merge_forward
124+
set [a], 0x0
125+
add a, 1
126+
set b, [a]
127+
set c, b ; previous block length
128+
set [a], 0x0
129+
add a, b
130+
ifn a, [userspaceend]
131+
add a, 1 ; footer of this block
132+
set b, pop
133+
set push, b ; we'll need it at the end
134+
set [a], b
135+
set a, b
136+
add a, 1
137+
set b, [a]
138+
add b, c
139+
add b, 3 ; considering overhead
140+
set [a], b
141+
set pc, free_end
142+
143+
:free_end
144+
set a, pop
145+
set pc, pop
146+

0 commit comments

Comments
 (0)