kernel_heap: hash table for memory descriptors

git-svn-id: svn://kolibrios.org@2156 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Sergey Semyonov (Serge) 2011-09-05 09:36:30 +00:00
parent 69abfe5c20
commit 37118c3bc0
2 changed files with 85 additions and 113 deletions

View File

@ -47,15 +47,67 @@ macro calc_index op
@@: @@:
} }
macro remove_from_used op align 4
{ md:
mov edx, [op+list_fd] .add_to_used:
mov ecx, [op+list_bk] mov eax, [esi+block_base]
mov [edx+list_bk], ecx mov ebx, [esi+block_base]
mov [ecx+list_fd], edx shr ebx, 6
mov [op+list_fd], 0 add eax, ebx
mov [op+list_bk], 0 shr ebx, 6
} add eax, ebx
shr eax, 12
and eax, 63
inc [mem_hash_cnt+eax*4]
lea ecx, [mem_used_list+eax*8]
list_add esi, ecx
mov [esi+block_flags], USED_BLOCK
mov eax, [esi+block_size]
sub [heap_free], eax
ret
align 4
.find_used:
mov ecx, eax
mov ebx, eax
shr ebx, 6
add ecx, ebx
shr ebx, 6
add ecx, ebx
shr ecx, 12
and ecx, 63
lea ebx, [mem_used_list+ecx*8]
mov esi, ebx
.next:
mov esi, [esi+list_fd]
cmp esi, ebx
je .fail
cmp eax, [esi+block_base]
jne .next
ret
.fail:
xor esi, esi
ret
align 4
.del_from_used:
call .find_used
test esi, esi
jz .done
cmp [esi+block_flags], USED_BLOCK
jne .fatal
dec [mem_hash_cnt+ecx*4]
list_del esi
.done:
ret
.fatal: ;FIXME panic here
xor esi, esi
ret
;Initial heap state ;Initial heap state
; ;
@ -75,9 +127,13 @@ proc init_kernel_heap
stosd stosd
loop @B loop @B
mov eax, mem_used.fd mov ecx, 64
mov [mem_used.fd], eax mov edi, mem_used_list
mov [mem_used.bk], eax @@:
mov eax, edi
stosd
stosd
loop @B
stdcall alloc_pages, dword 32 stdcall alloc_pages, dword 32
mov ecx, 32 mov ecx, 32
@ -140,7 +196,6 @@ proc init_kernel_heap
mov [eax-MEM_BLOCK.sizeof], dword 0 mov [eax-MEM_BLOCK.sizeof], dword 0
mov ecx, heap_mutex mov ecx, heap_mutex
call mutex_init call mutex_init
mov [heap_blocks], 4094 mov [heap_blocks], 4094
@ -282,7 +337,7 @@ proc alloc_kernel_space stdcall, size:dword
mov eax, [edi+block_size] mov eax, [edi+block_size]
calc_index eax calc_index eax
cmp eax, [block_ind] cmp eax, [block_ind]
je .m_eq_ind je .add_used
list_del edi list_del edi
@ -295,31 +350,9 @@ proc alloc_kernel_space stdcall, size:dword
bts [mem_block_mask], eax bts [mem_block_mask], eax
lea edx, [mem_block_list+eax*8] ;edx= list head lea edx, [mem_block_list+eax*8] ;edx= list head
list_add edi, edx list_add edi, edx
.m_eq_ind: .add_used:
mov ecx, mem_used.fd
mov edx, [ecx+list_fd]
mov [esi+list_fd], edx
mov [esi+list_bk], ecx
mov [ecx+list_fd], esi
mov [edx+list_bk], esi
mov [esi+block_flags], USED_BLOCK call md.add_to_used
mov ebx, [size]
sub [heap_free], ebx
if HASH_IT
pushad
mov eax, [esi+block_base]
mov ebx, [esi+block_base]
shr ebx, 6
add eax, ebx
shr ebx, 6
add eax, ebx
shr eax, 12
and eax, 63
inc [mem_hash_cnt+eax*4]
popad
end if
mov ecx, heap_mutex mov ecx, heap_mutex
call mutex_unlock call mutex_unlock
@ -328,6 +361,7 @@ end if
pop esi pop esi
pop ebx pop ebx
ret ret
.m_eq_size: .m_eq_size:
list_del edi list_del edi
lea edx, [mem_block_list+ebx*8] lea edx, [mem_block_list+ebx*8]
@ -335,38 +369,8 @@ end if
jnz @f jnz @f
btr [mem_block_mask], ebx btr [mem_block_mask], ebx
@@: @@:
mov ecx, mem_used.fd mov esi, edi
mov edx, [ecx+list_fd] jmp .add_used
mov [edi+list_fd], edx
mov [edi+list_bk], ecx
mov [ecx+list_fd], edi
mov [edx+list_bk], edi
mov [edi+block_flags], USED_BLOCK
mov ebx, [size]
sub [heap_free], ebx
if HASH_IT
pushad
mov eax, [edi+block_base]
mov ebx, [edi+block_base]
shr ebx, 6
add eax, ebx
shr ebx, 6
add eax, ebx
shr eax, 12
and eax, 63
inc [mem_hash_cnt+eax*4]
popad
end if
mov ecx, heap_mutex
call mutex_unlock
mov eax, [edi+block_base]
pop edi
pop esi
pop ebx
ret
.error_unlock: .error_unlock:
mov ecx, heap_mutex mov ecx, heap_mutex
@ -388,32 +392,11 @@ proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword
call mutex_lock call mutex_lock
mov eax, [base] mov eax, [base]
mov esi, [mem_used.fd]
@@:
cmp esi, mem_used.fd
je .fail
cmp [esi+block_base], eax call md.del_from_used
je .found test esi, esi
mov esi, [esi+list_fd] jz .fail
jmp @b
.found:
cmp [esi+block_flags], USED_BLOCK
jne .fail
if HASH_IT
pushad
mov eax, [esi+block_base]
mov ebx, [esi+block_base]
shr ebx, 6
add eax, ebx
shr ebx, 6
add eax, ebx
shr eax, 12
and eax, 63
dec [mem_hash_cnt+eax*4]
popad
end if
mov eax, [esi+block_size] mov eax, [esi+block_size]
add [heap_free], eax add [heap_free], eax
@ -443,8 +426,6 @@ end if
cmp [edi+block_flags], FREE_BLOCK cmp [edi+block_flags], FREE_BLOCK
jne .insert jne .insert
remove_from_used esi
mov edx, [esi+block_next] mov edx, [esi+block_next]
mov [edi+block_next], edx mov [edi+block_next], edx
mov [edx+block_prev], edi mov [edx+block_prev], edi
@ -481,7 +462,6 @@ end if
not eax not eax
ret ret
.insert: .insert:
remove_from_used esi
mov [esi+block_flags], FREE_BLOCK mov [esi+block_flags], FREE_BLOCK
mov eax, [esi+block_size] mov eax, [esi+block_size]
calc_index eax calc_index eax
@ -571,22 +551,16 @@ endp
align 4 align 4
proc kernel_free stdcall, base:dword proc kernel_free stdcall, base:dword
push ebx esi push ebx esi
mov ecx, heap_mutex mov ecx, heap_mutex
call mutex_lock call mutex_lock
mov eax, [base] mov eax, [base]
mov esi, [mem_used.fd] call md.find_used
@@:
cmp esi, mem_used.fd
je .fail
cmp [esi+block_base], eax mov ecx, heap_mutex
je .found
mov esi, [esi+list_fd]
jmp @b
.found:
cmp [esi+block_flags], USED_BLOCK cmp [esi+block_flags], USED_BLOCK
jne .fail jne .fail

View File

@ -296,21 +296,19 @@ cur_saved_data rb 4096
fpu_data: rb 512 fpu_data: rb 512
mem_block_list rd 64*2 mem_block_list rd 64*2
mem_block_mask rd 2 mem_used_list rd 64*2
mem_used.fd rd 1
mem_used.bk 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
heap_blocks rd 1 heap_blocks rd 1
free_blocks rd 1 free_blocks rd 1
mem_block_mask rd 2
next_memblock rd 1
mst MEM_STATE mst MEM_STATE
page_start rd 1 page_start rd 1