forked from KolibriOS/kolibrios
kernel heap: new allocator for block descriptors
git-svn-id: svn://kolibrios.org@2151 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
parent
d7ea1d9270
commit
81a07b7206
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
$Revision$
|
$Revision$
|
||||||
|
|
||||||
|
HASH_IT = 1
|
||||||
|
|
||||||
struc MEM_BLOCK
|
struc MEM_BLOCK
|
||||||
{
|
{
|
||||||
@ -17,6 +18,7 @@ struc MEM_BLOCK
|
|||||||
.size dd ? ;+20
|
.size dd ? ;+20
|
||||||
.flags dd ? ;+24
|
.flags dd ? ;+24
|
||||||
.handle dd ? ;+28
|
.handle dd ? ;+28
|
||||||
|
.sizeof:
|
||||||
}
|
}
|
||||||
|
|
||||||
FREE_BLOCK equ 4
|
FREE_BLOCK equ 4
|
||||||
@ -27,7 +29,6 @@ virtual at 0
|
|||||||
MEM_BLOCK MEM_BLOCK
|
MEM_BLOCK MEM_BLOCK
|
||||||
end virtual
|
end virtual
|
||||||
|
|
||||||
MEM_BLOCK_SIZE equ 8*4
|
|
||||||
|
|
||||||
block_next equ MEM_BLOCK.next_block
|
block_next equ MEM_BLOCK.next_block
|
||||||
block_prev equ MEM_BLOCK.prev_block
|
block_prev equ MEM_BLOCK.prev_block
|
||||||
@ -59,7 +60,7 @@ macro remove_from_used op
|
|||||||
;Initial heap state
|
;Initial heap state
|
||||||
;
|
;
|
||||||
;+heap_size terminator USED_BLOCK
|
;+heap_size terminator USED_BLOCK
|
||||||
;+4096*MEM_BLOCK_SIZE free space FREE_BLOCK
|
;+4096*MEM_BLOCK.sizeof free space FREE_BLOCK
|
||||||
;HEAP_BASE heap_descriptors USED_BLOCK
|
;HEAP_BASE heap_descriptors USED_BLOCK
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -74,15 +75,6 @@ proc init_kernel_heap
|
|||||||
stosd
|
stosd
|
||||||
loop @B
|
loop @B
|
||||||
|
|
||||||
mov ecx, 512/4
|
|
||||||
mov edi, mem_block_map
|
|
||||||
not eax
|
|
||||||
rep stosd
|
|
||||||
|
|
||||||
mov [mem_block_start], mem_block_map
|
|
||||||
mov [mem_block_end], mem_block_map+512
|
|
||||||
mov [mem_block_arr], HEAP_BASE
|
|
||||||
|
|
||||||
mov eax, mem_used.fd
|
mov eax, mem_used.fd
|
||||||
mov [mem_used.fd], eax
|
mov [mem_used.fd], eax
|
||||||
mov [mem_used.bk], eax
|
mov [mem_used.bk], eax
|
||||||
@ -99,8 +91,8 @@ proc init_kernel_heap
|
|||||||
jnz .l1
|
jnz .l1
|
||||||
|
|
||||||
mov edi, HEAP_BASE ;descriptors
|
mov edi, HEAP_BASE ;descriptors
|
||||||
mov ebx, HEAP_BASE+MEM_BLOCK_SIZE ;free space
|
mov ebx, HEAP_BASE+MEM_BLOCK.sizeof ;free space
|
||||||
mov ecx, HEAP_BASE+MEM_BLOCK_SIZE*2 ;terminator
|
mov ecx, HEAP_BASE+MEM_BLOCK.sizeof*2 ;terminator
|
||||||
|
|
||||||
xor eax, eax
|
xor eax, eax
|
||||||
mov [edi+block_next], ebx
|
mov [edi+block_next], ebx
|
||||||
@ -108,26 +100,24 @@ proc init_kernel_heap
|
|||||||
mov [edi+list_fd], eax
|
mov [edi+list_fd], eax
|
||||||
mov [edi+list_bk], eax
|
mov [edi+list_bk], eax
|
||||||
mov [edi+block_base], HEAP_BASE
|
mov [edi+block_base], HEAP_BASE
|
||||||
mov [edi+block_size], 4096*MEM_BLOCK_SIZE
|
mov [edi+block_size], 4096*MEM_BLOCK.sizeof
|
||||||
mov [edi+block_flags], USED_BLOCK
|
mov [edi+block_flags], USED_BLOCK
|
||||||
|
|
||||||
mov [ecx+block_next], eax
|
mov [ecx+block_next], eax
|
||||||
mov [ecx+block_prev], ebx
|
mov [ecx+block_prev], ebx
|
||||||
mov [edi+list_fd], eax
|
mov [edi+list_fd], eax
|
||||||
mov [edi+list_bk], eax
|
mov [edi+list_bk], eax
|
||||||
mov [edi+block_base], 0
|
mov [edi+block_base], eax
|
||||||
mov [edi+block_size], 0
|
mov [edi+block_size], eax
|
||||||
mov [edi+block_flags], USED_BLOCK
|
mov [edi+block_flags], USED_BLOCK
|
||||||
|
|
||||||
mov [ebx+block_next], ecx
|
mov [ebx+block_next], ecx
|
||||||
mov [ebx+block_prev], edi
|
mov [ebx+block_prev], edi
|
||||||
mov [ebx+list_fd], eax
|
mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK.sizeof
|
||||||
mov [ebx+list_bk], eax
|
|
||||||
mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
|
|
||||||
|
|
||||||
mov ecx, [pg_data.kernel_pages]
|
mov ecx, [pg_data.kernel_pages]
|
||||||
shl ecx, 12
|
shl ecx, 12
|
||||||
sub ecx, HEAP_BASE-OS_BASE+4096*MEM_BLOCK_SIZE
|
sub ecx, HEAP_BASE-OS_BASE+4096*MEM_BLOCK.sizeof
|
||||||
mov [heap_size], ecx
|
mov [heap_size], ecx
|
||||||
mov [heap_free], ecx
|
mov [heap_free], ecx
|
||||||
mov [ebx+block_size], ecx
|
mov [ebx+block_size], ecx
|
||||||
@ -139,11 +129,22 @@ proc init_kernel_heap
|
|||||||
mov ecx, mem_block_list+63*8
|
mov ecx, mem_block_list+63*8
|
||||||
list_add ebx, ecx
|
list_add ebx, ecx
|
||||||
|
|
||||||
mov byte [mem_block_map], 0xFC
|
mov ecx, 4096-3-1
|
||||||
|
mov eax, HEAP_BASE+MEM_BLOCK.sizeof*4
|
||||||
|
|
||||||
|
mov [next_memblock], HEAP_BASE+MEM_BLOCK.sizeof*3
|
||||||
|
@@:
|
||||||
|
mov [eax-MEM_BLOCK.sizeof], eax
|
||||||
|
add eax, MEM_BLOCK.sizeof
|
||||||
|
loop @B
|
||||||
|
|
||||||
|
mov [eax-MEM_BLOCK.sizeof], dword 0
|
||||||
|
|
||||||
|
|
||||||
mov ecx, heap_mutex
|
mov ecx, heap_mutex
|
||||||
call mutex_init
|
call mutex_init
|
||||||
mov [heap_blocks], 4095
|
mov [heap_blocks], 4094
|
||||||
mov [free_blocks], 4094
|
mov [free_blocks], 4093
|
||||||
ret
|
ret
|
||||||
endp
|
endp
|
||||||
|
|
||||||
@ -201,58 +202,22 @@ get_small_block:
|
|||||||
mov edx, [esi]
|
mov edx, [esi]
|
||||||
jmp .find
|
jmp .find
|
||||||
|
|
||||||
align 4
|
|
||||||
alloc_mem_block:
|
|
||||||
|
|
||||||
mov ebx, [mem_block_start]
|
|
||||||
mov ecx, [mem_block_end]
|
|
||||||
.l1:
|
|
||||||
bsf eax,[ebx];
|
|
||||||
jnz found
|
|
||||||
add ebx,4
|
|
||||||
cmp ebx, ecx
|
|
||||||
jb .l1
|
|
||||||
xor eax,eax
|
|
||||||
ret
|
|
||||||
|
|
||||||
found:
|
|
||||||
btr [ebx], eax
|
|
||||||
mov [mem_block_start],ebx
|
|
||||||
sub ebx, mem_block_map
|
|
||||||
lea eax,[eax+ebx*8]
|
|
||||||
shl eax, 5
|
|
||||||
add eax, [mem_block_arr]
|
|
||||||
dec [free_blocks]
|
|
||||||
ret
|
|
||||||
|
|
||||||
align 4
|
align 4
|
||||||
free_mem_block:
|
free_mem_block:
|
||||||
mov dword [eax], 0
|
mov ebx, [next_memblock]
|
||||||
mov dword [eax+4], 0
|
mov [eax], ebx
|
||||||
mov dword [eax+8], 0
|
mov [next_memblock], eax
|
||||||
mov dword [eax+12], 0
|
xor ebx, ebx
|
||||||
mov dword [eax+16], 0
|
|
||||||
; mov dword [eax+20], 0
|
|
||||||
mov dword [eax+24], 0
|
|
||||||
mov dword [eax+28], 0
|
|
||||||
|
|
||||||
sub eax, [mem_block_arr]
|
mov dword [eax+4], ebx
|
||||||
shr eax, 5
|
mov dword [eax+8], ebx
|
||||||
|
mov dword [eax+12], ebx
|
||||||
mov ebx, mem_block_map
|
mov dword [eax+16], ebx
|
||||||
bts [ebx], eax
|
; mov dword [eax+20], 0 ;don't clear block size
|
||||||
|
mov dword [eax+24], ebx
|
||||||
|
mov dword [eax+28], ebx
|
||||||
inc [free_blocks]
|
inc [free_blocks]
|
||||||
shr eax, 3
|
|
||||||
and eax, not 3
|
|
||||||
add eax, ebx
|
|
||||||
cmp [mem_block_start], eax
|
|
||||||
ja @f
|
|
||||||
ret
|
|
||||||
@@:
|
|
||||||
mov [mem_block_start], eax
|
|
||||||
ret
|
|
||||||
.err:
|
|
||||||
xor eax, eax
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
align 4
|
align 4
|
||||||
@ -291,11 +256,13 @@ proc alloc_kernel_space stdcall, size:dword
|
|||||||
cmp eax, [size]
|
cmp eax, [size]
|
||||||
je .m_eq_size
|
je .m_eq_size
|
||||||
|
|
||||||
call alloc_mem_block
|
mov esi, [next_memblock] ;new memory block
|
||||||
and eax, eax
|
test esi, esi
|
||||||
jz .error_unlock
|
jz .error_unlock
|
||||||
|
|
||||||
mov esi, eax ;esi - splitted block
|
dec [free_blocks]
|
||||||
|
mov eax, [esi]
|
||||||
|
mov [next_memblock], eax
|
||||||
|
|
||||||
mov [esi+block_next], edi
|
mov [esi+block_next], edi
|
||||||
mov eax, [edi+block_prev]
|
mov eax, [edi+block_prev]
|
||||||
@ -340,17 +307,19 @@ proc alloc_kernel_space stdcall, size:dword
|
|||||||
mov ebx, [size]
|
mov ebx, [size]
|
||||||
sub [heap_free], ebx
|
sub [heap_free], ebx
|
||||||
|
|
||||||
; pushad
|
if HASH_IT
|
||||||
; mov eax, [esi+block_base]
|
pushad
|
||||||
; mov ebx, [esi+block_base]
|
mov eax, [esi+block_base]
|
||||||
; shr ebx, 6
|
mov ebx, [esi+block_base]
|
||||||
; add eax, ebx
|
shr ebx, 6
|
||||||
; shr ebx, 6
|
add eax, ebx
|
||||||
; add eax, ebx
|
shr ebx, 6
|
||||||
; shr eax, 12
|
add eax, ebx
|
||||||
; and eax, 63
|
shr eax, 12
|
||||||
; inc [mem_hash_cnt+eax*4]
|
and eax, 63
|
||||||
; popad
|
inc [mem_hash_cnt+eax*4]
|
||||||
|
popad
|
||||||
|
end if
|
||||||
|
|
||||||
mov ecx, heap_mutex
|
mov ecx, heap_mutex
|
||||||
call mutex_unlock
|
call mutex_unlock
|
||||||
@ -377,17 +346,19 @@ proc alloc_kernel_space stdcall, size:dword
|
|||||||
mov ebx, [size]
|
mov ebx, [size]
|
||||||
sub [heap_free], ebx
|
sub [heap_free], ebx
|
||||||
|
|
||||||
; pushad
|
if HASH_IT
|
||||||
; mov eax, [edi+block_base]
|
pushad
|
||||||
; mov ebx, [edi+block_base]
|
mov eax, [edi+block_base]
|
||||||
; shr ebx, 6
|
mov ebx, [edi+block_base]
|
||||||
; add eax, ebx
|
shr ebx, 6
|
||||||
; shr ebx, 6
|
add eax, ebx
|
||||||
; add eax, ebx
|
shr ebx, 6
|
||||||
; shr eax, 12
|
add eax, ebx
|
||||||
; and eax, 63
|
shr eax, 12
|
||||||
; inc [mem_hash_cnt+eax*4]
|
and eax, 63
|
||||||
; popad
|
inc [mem_hash_cnt+eax*4]
|
||||||
|
popad
|
||||||
|
end if
|
||||||
|
|
||||||
mov ecx, heap_mutex
|
mov ecx, heap_mutex
|
||||||
call mutex_unlock
|
call mutex_unlock
|
||||||
@ -430,19 +401,19 @@ proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword
|
|||||||
cmp [esi+block_flags], USED_BLOCK
|
cmp [esi+block_flags], USED_BLOCK
|
||||||
jne .fail
|
jne .fail
|
||||||
|
|
||||||
|
if HASH_IT
|
||||||
; pushad
|
pushad
|
||||||
; mov eax, [esi+block_base]
|
mov eax, [esi+block_base]
|
||||||
; mov ebx, [esi+block_base]
|
mov ebx, [esi+block_base]
|
||||||
; shr ebx, 6
|
shr ebx, 6
|
||||||
; add eax, ebx
|
add eax, ebx
|
||||||
; shr ebx, 6
|
shr ebx, 6
|
||||||
; add eax, ebx
|
add eax, ebx
|
||||||
; shr eax, 12
|
shr eax, 12
|
||||||
; and eax, 63
|
and eax, 63
|
||||||
; dec [mem_hash_cnt+eax*4]
|
dec [mem_hash_cnt+eax*4]
|
||||||
; popad
|
popad
|
||||||
|
end if
|
||||||
mov eax, [esi+block_size]
|
mov eax, [esi+block_size]
|
||||||
add [heap_free], eax
|
add [heap_free], eax
|
||||||
|
|
||||||
|
@ -805,7 +805,6 @@ end if
|
|||||||
mov dword [SLOT_BASE+ecx+APPDATA.io_map], ebx
|
mov dword [SLOT_BASE+ecx+APPDATA.io_map], ebx
|
||||||
mov dword [page_tabs + (tss._io_map_0 shr 10)], ebx
|
mov dword [page_tabs + (tss._io_map_0 shr 10)], ebx
|
||||||
mov cr3, eax
|
mov cr3, eax
|
||||||
; mov [irq_tab+5*4], 0
|
|
||||||
sti
|
sti
|
||||||
|
|
||||||
popad
|
popad
|
||||||
|
@ -295,19 +295,16 @@ align 16
|
|||||||
cur_saved_data rb 4096
|
cur_saved_data rb 4096
|
||||||
fpu_data: rb 512
|
fpu_data: rb 512
|
||||||
|
|
||||||
mem_block_map rb 512
|
|
||||||
mem_block_list rd 64*2
|
mem_block_list rd 64*2
|
||||||
mem_block_mask rd 2
|
mem_block_mask rd 2
|
||||||
|
|
||||||
mem_used.fd rd 1
|
mem_used.fd rd 1
|
||||||
mem_used.bk rd 1
|
mem_used.bk rd 1
|
||||||
|
|
||||||
mem_block_arr rd 1
|
|
||||||
mem_block_start rd 1
|
|
||||||
mem_block_end rd 1
|
|
||||||
|
|
||||||
mem_hash_cnt rd 64
|
mem_hash_cnt rd 64
|
||||||
|
|
||||||
|
next_memblock rd 1
|
||||||
|
|
||||||
heap_mutex MUTEX
|
heap_mutex MUTEX
|
||||||
heap_size rd 1
|
heap_size rd 1
|
||||||
heap_free rd 1
|
heap_free rd 1
|
||||||
|
Loading…
Reference in New Issue
Block a user