StupidOS/kernel/heap.inc

91 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2024-07-18 09:28:07 +00:00
;; File: heap.inc
2024-07-19 07:53:03 +00:00
;; Base on <https://github.com/CCareaga/heap_allocator/blob/master/commented_heap.c>
struc HeapNode {
.hole dd ?
.size dd ?
.next dd ?
.prev dd ?
}
DEFN HeapNode
struc HeapInfo {
.start dd ?
.end dd ?
.bins dd 9 dup(?)
}
DEFN HeapInfo
2024-07-18 09:28:07 +00:00
;; Function: heap_init
heap_init:
push ebp
mov ebp, esp
sub esp, 4
mov [ebp-4], eax
mov ecx, KERNEL_VIRT_BASE
add eax, ecx
push eax
push ecx
mov esi, szMsgHeapInit
call klog
2024-07-19 07:53:03 +00:00
mov eax, [ebp-4]
mov ecx, KERNEL_VIRT_BASE
mov [ecx + HeapNode.hole], dword 1
sub eax, sizeof.HeapNode + 4
mov [ecx + HeapNode.size], eax
add eax, sizeof.HeapNode
add eax, ecx
mov [eax], ecx
mov ecx, KERNEL_VIRT_BASE
mov [kheap + HeapInfo.start], ecx
mov eax, [ebp-4]
add ecx, eax
mov [kheap + HeapInfo.end], ecx
2024-07-18 09:28:07 +00:00
2024-07-19 07:53:03 +00:00
.end:
2024-07-18 09:28:07 +00:00
leave
ret
heap_alloc:
ret
heap_free:
ret
2024-07-19 07:53:03 +00:00
;; Function: heap_getbin
;;
;; In:
;; EAX - size
heap_getbin:
push ebp
mov ebp, esp
sub esp, 4
mov [ebp-4], dword 0
cmp eax, 4
jge @f
mov eax, 4
@@:
shr eax, 1
cmp eax, 1
or eax, eax
jz @f
inc dword [ebp-4]
jmp @b
@@:
mov eax, [ebp-4]
sub eax, 2
cmp eax, 8
jbe @f
mov eax, 8
@@:
leave
ret
kheap HeapInfo
2024-07-18 09:28:07 +00:00
szMsgHeapInit db "HEAP: initialize %x - %x", 0
2024-07-19 07:53:03 +00:00
szMsgErrorHeap db "ERROR: HEAP: not enough memory", 0