2007-03-26 14:18:08 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
|
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-07-27 15:52:03 +02:00
|
|
|
$Revision$
|
|
|
|
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
struc MEM_BLOCK
|
|
|
|
{ .next_block dd ?
|
|
|
|
.prev_block dd ? ;+4
|
2007-02-17 11:09:23 +01:00
|
|
|
.list_fd dd ? ;+8
|
|
|
|
.list_bk dd ? ;+12
|
2006-10-06 08:09:41 +02:00
|
|
|
.base dd ? ;+16
|
|
|
|
.size dd ? ;+20
|
|
|
|
.flags dd ? ;+24
|
|
|
|
.handle dd ? ;+28
|
|
|
|
}
|
|
|
|
|
2007-02-17 11:09:23 +01:00
|
|
|
MEM_LIST_OFFSET equ 8
|
2006-10-06 08:09:41 +02:00
|
|
|
FREE_BLOCK equ 4
|
|
|
|
USED_BLOCK equ 8
|
2007-06-18 18:39:42 +02:00
|
|
|
DONT_FREE_BLOCK equ 10h
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
virtual at 0
|
|
|
|
MEM_BLOCK MEM_BLOCK
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
MEM_BLOCK_SIZE equ 8*4
|
|
|
|
|
|
|
|
block_next equ MEM_BLOCK.next_block
|
|
|
|
block_prev equ MEM_BLOCK.prev_block
|
2007-02-17 11:09:23 +01:00
|
|
|
list_fd equ MEM_BLOCK.list_fd
|
|
|
|
list_bk equ MEM_BLOCK.list_bk
|
2006-10-06 08:09:41 +02:00
|
|
|
block_base equ MEM_BLOCK.base
|
|
|
|
block_size equ MEM_BLOCK.size
|
|
|
|
block_flags equ MEM_BLOCK.flags
|
|
|
|
|
|
|
|
macro calc_index op
|
|
|
|
{ shr op, 12
|
|
|
|
dec op
|
|
|
|
cmp op, 63
|
|
|
|
jna @f
|
|
|
|
mov op, 63
|
|
|
|
@@:
|
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_list op
|
2007-02-17 11:09:23 +01:00
|
|
|
{ mov edx, [op+list_fd]
|
|
|
|
mov ecx, [op+list_bk]
|
2006-10-06 08:09:41 +02:00
|
|
|
test edx, edx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edx+list_bk], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
test ecx, ecx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [ecx+list_fd], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [op+list_fd],0
|
|
|
|
mov [op+list_bk],0
|
2006-10-06 08:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_free op
|
|
|
|
{
|
|
|
|
remove_from_list op
|
|
|
|
|
|
|
|
mov eax, [op+block_size]
|
|
|
|
calc_index eax
|
|
|
|
cmp [mem_block_list+eax*4], op
|
|
|
|
jne @f
|
|
|
|
mov [mem_block_list+eax*4], edx
|
|
|
|
@@:
|
|
|
|
cmp [mem_block_list+eax*4], 0
|
|
|
|
jne @f
|
|
|
|
btr [mem_block_mask], eax
|
|
|
|
@@:
|
|
|
|
}
|
|
|
|
|
|
|
|
macro remove_from_used op
|
|
|
|
{
|
2007-02-17 11:09:23 +01:00
|
|
|
mov edx, [op+list_fd]
|
|
|
|
mov ecx, [op+list_bk]
|
|
|
|
mov [edx+list_bk], ecx
|
|
|
|
mov [ecx+list_fd], edx
|
|
|
|
mov [op+list_fd], 0
|
|
|
|
mov [op+list_bk], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc init_kernel_heap
|
|
|
|
|
|
|
|
mov ecx, 64/4
|
|
|
|
mov edi, mem_block_list
|
|
|
|
xor eax, eax
|
|
|
|
cld
|
|
|
|
rep stosd
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-02-17 11:09:23 +01:00
|
|
|
mov eax, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
mov [mem_used.fd], eax
|
|
|
|
mov [mem_used.bk], eax
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
stdcall alloc_pages, dword 32
|
|
|
|
mov ecx, 32
|
|
|
|
mov edx, eax
|
|
|
|
mov edi, HEAP_BASE
|
|
|
|
.l1:
|
|
|
|
stdcall map_page,edi,edx,PG_SW
|
|
|
|
add edi, 0x1000
|
|
|
|
add edx, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz .l1
|
|
|
|
|
|
|
|
mov edi, HEAP_BASE
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ebx, HEAP_BASE+MEM_BLOCK_SIZE
|
2006-10-06 08:09:41 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov [edi+block_next], ebx
|
|
|
|
mov [edi+block_prev], eax
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], eax
|
|
|
|
mov [edi+list_bk], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [edi+block_base], HEAP_BASE
|
|
|
|
mov [edi+block_size], 4096*MEM_BLOCK_SIZE
|
|
|
|
mov [edi+block_flags], USED_BLOCK
|
|
|
|
|
|
|
|
mov [ebx+block_next], eax
|
|
|
|
mov [ebx+block_prev], eax
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [ebx+list_fd], eax
|
|
|
|
mov [ebx+list_bk], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
|
|
|
|
|
|
|
|
mov ecx, [MEM_AMOUNT]
|
2006-11-16 11:59:49 +01:00
|
|
|
sub ecx, HEAP_BASE + 4096*MEM_BLOCK_SIZE
|
2006-10-08 09:48:38 +02:00
|
|
|
mov [heap_size], ecx
|
|
|
|
mov [heap_free], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [ebx+block_size], ecx
|
|
|
|
mov [ebx+block_flags], FREE_BLOCK
|
|
|
|
|
|
|
|
mov [mem_block_mask], eax
|
|
|
|
mov [mem_block_mask+4],0x80000000
|
|
|
|
|
|
|
|
mov [mem_block_list+63*4], ebx
|
|
|
|
mov byte [mem_block_map], 0xFC
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2006-10-08 09:48:38 +02:00
|
|
|
mov [heap_blocks], 4095
|
|
|
|
mov [free_blocks], 4095
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
; param
|
|
|
|
; eax= required size
|
|
|
|
;
|
|
|
|
; retval
|
|
|
|
; edi= memory block descriptor
|
|
|
|
; ebx= descriptor index
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
align 4
|
|
|
|
get_block:
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ecx, eax
|
2007-02-22 07:41:46 +01:00
|
|
|
shr ecx, 12
|
|
|
|
dec ecx
|
|
|
|
cmp ecx, 63
|
|
|
|
jle .get_index
|
|
|
|
mov ecx, 63
|
|
|
|
.get_index:
|
|
|
|
lea esi, [mem_block_mask]
|
2006-10-06 08:09:41 +02:00
|
|
|
xor ebx, ebx
|
2007-02-22 07:41:46 +01:00
|
|
|
or edx, -1
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp ecx, 32
|
|
|
|
jb .bit_test
|
|
|
|
|
|
|
|
sub ecx, 32
|
|
|
|
add ebx, 32
|
|
|
|
add esi, 4
|
|
|
|
.bit_test:
|
|
|
|
shl edx, cl
|
2007-02-22 07:41:46 +01:00
|
|
|
and edx, [esi]
|
|
|
|
.find:
|
|
|
|
bsf edi, edx
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .high_mask
|
2007-02-22 07:41:46 +01:00
|
|
|
add ebx, edi
|
|
|
|
mov edi, [mem_block_list+ebx*4]
|
|
|
|
.check_size:
|
|
|
|
cmp eax, [edi+block_size]
|
|
|
|
ja .next
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.high_mask:
|
|
|
|
add esi, 4
|
2007-02-22 07:41:46 +01:00
|
|
|
cmp esi, mem_block_mask+8
|
|
|
|
jae .err
|
2006-10-06 08:09:41 +02:00
|
|
|
add ebx, 32
|
2007-02-22 07:41:46 +01:00
|
|
|
mov edx, [esi]
|
|
|
|
jmp .find
|
|
|
|
.next:
|
|
|
|
mov edi, [edi+list_fd]
|
|
|
|
test edi, edi
|
|
|
|
jnz .check_size
|
|
|
|
.err:
|
|
|
|
xor edi, edi
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc 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
|
2006-12-29 08:12:27 +01:00
|
|
|
lea eax,[eax+ebx*8]
|
2006-10-06 08:09:41 +02:00
|
|
|
shl eax, 5
|
|
|
|
add eax, [mem_block_arr]
|
2006-10-08 09:48:38 +02:00
|
|
|
dec [free_blocks]
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
proc free_mem_block
|
2007-02-17 11:09:23 +01:00
|
|
|
mov dword [eax], 0
|
|
|
|
mov dword [eax+4], 0
|
|
|
|
mov dword [eax+8], 0
|
|
|
|
mov dword [eax+12], 0
|
|
|
|
mov dword [eax+16], 0
|
|
|
|
; mov dword [eax+20], 0
|
|
|
|
mov dword [eax+24], 0
|
|
|
|
mov dword [eax+28], 0
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
sub eax, [mem_block_arr]
|
|
|
|
shr eax, 5
|
|
|
|
|
|
|
|
mov ebx, mem_block_map
|
|
|
|
bts [ebx], eax
|
2006-10-08 09:48:38 +02:00
|
|
|
inc [free_blocks]
|
2006-10-06 08:09:41 +02:00
|
|
|
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
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_kernel_space stdcall, size:dword
|
|
|
|
local block_ind:DWORD
|
|
|
|
|
2007-10-20 19:11:22 +02:00
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov eax, [size]
|
2006-11-03 16:19:15 +01:00
|
|
|
add eax, 4095
|
|
|
|
and eax, not 4095
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [size], eax
|
2007-01-18 08:31:56 +01:00
|
|
|
|
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
|
|
|
|
2006-10-08 09:48:38 +02:00
|
|
|
cmp eax, [heap_free]
|
|
|
|
ja .error
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-02-22 07:41:46 +01:00
|
|
|
call get_block ; eax
|
|
|
|
test edi, edi
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .error
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .error
|
|
|
|
|
|
|
|
mov [block_ind], ebx ;index of allocated block
|
|
|
|
|
|
|
|
mov eax, [edi+block_size]
|
|
|
|
cmp eax, [size]
|
|
|
|
je .m_eq_size
|
|
|
|
|
|
|
|
call alloc_mem_block
|
|
|
|
and eax, eax
|
|
|
|
jz .error
|
|
|
|
|
|
|
|
mov esi, eax ;esi - splitted block
|
|
|
|
|
|
|
|
mov [esi+block_next], edi
|
|
|
|
mov eax, [edi+block_prev]
|
|
|
|
mov [esi+block_prev], eax
|
|
|
|
mov [edi+block_prev], esi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_fd], 0
|
|
|
|
mov [esi+list_bk], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
and eax, eax
|
|
|
|
jz @f
|
|
|
|
mov [eax+block_next], esi
|
|
|
|
@@:
|
|
|
|
mov ebx, [edi+block_base]
|
|
|
|
mov [esi+block_base], ebx
|
|
|
|
mov edx, [size]
|
|
|
|
mov [esi+block_size], edx
|
|
|
|
add [edi+block_base], edx
|
|
|
|
sub [edi+block_size], edx
|
|
|
|
|
|
|
|
mov eax, [edi+block_size]
|
|
|
|
shr eax, 12
|
|
|
|
sub eax, 1
|
|
|
|
cmp eax, 63
|
|
|
|
jna @f
|
|
|
|
mov eax, 63
|
|
|
|
@@:
|
|
|
|
cmp eax, [block_ind]
|
|
|
|
je .m_eq_ind
|
|
|
|
|
2006-10-23 22:38:29 +02:00
|
|
|
remove_from_list edi
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov ecx, [block_ind]
|
2006-11-16 06:47:31 +01:00
|
|
|
mov [mem_block_list+ecx*4], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2006-11-16 06:47:31 +01:00
|
|
|
test edx, edx
|
2006-10-06 08:09:41 +02:00
|
|
|
jnz @f
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
mov edx, [mem_block_list+eax*4]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
test edx, edx
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edx+list_bk], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
mov [mem_block_list+eax*4], edi
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
.m_eq_ind:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ecx, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
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
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [esi+block_flags], USED_BLOCK
|
|
|
|
mov eax, [esi+block_base]
|
2006-10-08 09:48:38 +02:00
|
|
|
mov ebx, [size]
|
|
|
|
sub [heap_free], ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.m_eq_size:
|
|
|
|
remove_from_list edi
|
2006-10-23 22:38:29 +02:00
|
|
|
mov [mem_block_list+ebx*4], edx
|
2006-10-06 08:09:41 +02:00
|
|
|
and edx, edx
|
|
|
|
jnz @f
|
2006-10-23 22:38:29 +02:00
|
|
|
btr [mem_block_mask], ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
mov ecx, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
mov edx, [ecx+list_fd]
|
|
|
|
mov [edi+list_fd], edx
|
|
|
|
mov [edi+list_bk], ecx
|
|
|
|
mov [ecx+list_fd], edi
|
|
|
|
mov [edx+list_bk], edi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [edi+block_flags], USED_BLOCK
|
|
|
|
mov eax, [edi+block_base]
|
2006-10-08 09:48:38 +02:00
|
|
|
mov ebx, [size]
|
|
|
|
sub [heap_free], ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.error:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
2007-02-05 15:20:36 +01:00
|
|
|
proc free_kernel_space stdcall uses ebx ecx edx esi edi, base:dword
|
2007-10-20 19:11:22 +02:00
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
2007-01-18 08:31:56 +01:00
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov eax, [base]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [mem_used.fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
cmp esi, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
je .fail
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp [esi+block_base], eax
|
|
|
|
je .found
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [esi+list_fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @b
|
|
|
|
.found:
|
|
|
|
cmp [esi+block_flags], USED_BLOCK
|
|
|
|
jne .fail
|
|
|
|
|
2006-10-08 09:48:38 +02:00
|
|
|
mov eax, [esi+block_size]
|
|
|
|
add [heap_free], eax
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov edi, [esi+block_next]
|
|
|
|
test edi, edi
|
|
|
|
jz .prev
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .prev
|
|
|
|
|
|
|
|
remove_from_free edi
|
|
|
|
|
|
|
|
mov edx, [edi+block_next]
|
|
|
|
mov [esi+block_next], edx
|
|
|
|
test edx, edx
|
|
|
|
jz @f
|
|
|
|
|
|
|
|
mov [edx+block_prev], esi
|
|
|
|
@@:
|
|
|
|
mov ecx, [edi+block_size]
|
|
|
|
add [esi+block_size], ecx
|
|
|
|
|
|
|
|
mov eax, edi
|
|
|
|
call free_mem_block
|
|
|
|
.prev:
|
|
|
|
mov edi, [esi+block_prev]
|
|
|
|
test edi, edi
|
|
|
|
jz .insert
|
|
|
|
|
|
|
|
cmp [edi+block_flags], FREE_BLOCK
|
|
|
|
jne .insert
|
|
|
|
|
|
|
|
remove_from_used esi
|
|
|
|
|
|
|
|
mov edx, [esi+block_next]
|
|
|
|
mov [edi+block_next], edx
|
|
|
|
test edx, edx
|
|
|
|
jz @f
|
|
|
|
mov [edx+block_prev], edi
|
|
|
|
@@:
|
|
|
|
mov eax, esi
|
|
|
|
call free_mem_block
|
|
|
|
|
|
|
|
mov ecx, [edi+block_size]
|
|
|
|
mov eax, [esi+block_size]
|
|
|
|
add eax, ecx
|
|
|
|
mov [edi+block_size], eax
|
|
|
|
|
|
|
|
calc_index eax
|
|
|
|
calc_index ecx
|
|
|
|
cmp eax, ecx
|
|
|
|
je .m_eq
|
|
|
|
|
|
|
|
push ecx
|
|
|
|
remove_from_list edi
|
|
|
|
pop ecx
|
|
|
|
|
|
|
|
cmp [mem_block_list+ecx*4], edi
|
|
|
|
jne @f
|
|
|
|
mov [mem_block_list+ecx*4], edx
|
|
|
|
@@:
|
|
|
|
cmp [mem_block_list+ecx*4], 0
|
|
|
|
jne @f
|
|
|
|
btr [mem_block_mask], ecx
|
|
|
|
@@:
|
|
|
|
mov esi, [mem_block_list+eax*4]
|
|
|
|
mov [mem_block_list+eax*4], edi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_fd], esi
|
2006-10-06 08:09:41 +02:00
|
|
|
test esi, esi
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_bk], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
.m_eq:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-02-05 15:20:36 +01:00
|
|
|
dec eax
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.insert:
|
|
|
|
remove_from_used esi
|
|
|
|
|
|
|
|
mov eax, [esi+block_size]
|
|
|
|
calc_index eax
|
|
|
|
|
|
|
|
mov edi, [mem_block_list+eax*4]
|
|
|
|
mov [mem_block_list+eax*4], esi
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [esi+list_fd], edi
|
2006-10-06 08:09:41 +02:00
|
|
|
test edi, edi
|
|
|
|
jz @f
|
2007-02-17 11:09:23 +01:00
|
|
|
mov [edi+list_bk], esi
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
|
|
|
bts [mem_block_mask], eax
|
|
|
|
mov [esi+block_flags],FREE_BLOCK
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-02-05 15:20:36 +01:00
|
|
|
dec eax
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
.fail:
|
|
|
|
xor eax, eax
|
2007-01-18 08:31:56 +01:00
|
|
|
mov [heap_mutex], eax
|
2007-10-20 19:11:22 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_alloc stdcall, size:dword
|
|
|
|
locals
|
|
|
|
lin_addr dd ?
|
|
|
|
pages_count dd ?
|
|
|
|
endl
|
|
|
|
|
2007-10-20 23:02:38 +02:00
|
|
|
push ebx
|
|
|
|
push edi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov eax, [size]
|
2006-11-03 16:19:15 +01:00
|
|
|
add eax, 4095
|
|
|
|
and eax, not 4095;
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [size], eax
|
|
|
|
and eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ebx, eax
|
|
|
|
shr ebx, 12
|
|
|
|
mov [pages_count], ebx
|
|
|
|
|
|
|
|
stdcall alloc_kernel_space, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
mov [lin_addr], eax
|
|
|
|
|
|
|
|
mov ecx, [pages_count]
|
|
|
|
mov edx, eax
|
|
|
|
mov ebx, ecx
|
|
|
|
|
|
|
|
shr ecx, 3
|
|
|
|
jz .next
|
|
|
|
|
|
|
|
and ebx, not 7
|
|
|
|
push ebx
|
|
|
|
stdcall alloc_pages, ebx
|
|
|
|
pop ecx ; yes ecx!!!
|
|
|
|
and eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov edi, eax
|
|
|
|
mov edx, [lin_addr]
|
|
|
|
@@:
|
|
|
|
stdcall map_page,edx,edi,dword PG_SW
|
|
|
|
add edx, 0x1000
|
|
|
|
add edi, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
|
|
|
.next:
|
|
|
|
mov ecx, [pages_count]
|
|
|
|
and ecx, 7
|
|
|
|
jz .end
|
2007-02-17 11:09:23 +01:00
|
|
|
@@:
|
|
|
|
push ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
call alloc_page
|
|
|
|
pop ecx
|
|
|
|
test eax, eax
|
2007-02-17 11:09:23 +01:00
|
|
|
jz .err
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
stdcall map_page,edx,eax,dword PG_SW
|
|
|
|
add edx, 0x1000
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
|
|
|
.end:
|
|
|
|
mov eax, [lin_addr]
|
2007-10-20 23:02:38 +02:00
|
|
|
pop edi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
2007-02-17 11:09:23 +01:00
|
|
|
.err:
|
2006-10-06 08:09:41 +02:00
|
|
|
xor eax, eax
|
2007-10-20 23:02:38 +02:00
|
|
|
pop edi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc kernel_free stdcall, base:dword
|
2007-02-05 15:20:36 +01:00
|
|
|
push ebx esi
|
2007-01-18 08:31:56 +01:00
|
|
|
|
|
|
|
mov ebx, heap_mutex
|
|
|
|
call wait_mutex ;ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
mov eax, [base]
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [mem_used.fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-17 11:09:23 +01:00
|
|
|
cmp esi, mem_used.fd-MEM_LIST_OFFSET
|
|
|
|
je .fail
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
cmp [esi+block_base], eax
|
|
|
|
je .found
|
2007-02-17 11:09:23 +01:00
|
|
|
mov esi, [esi+list_fd]
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @b
|
|
|
|
.found:
|
|
|
|
cmp [esi+block_flags], USED_BLOCK
|
|
|
|
jne .fail
|
|
|
|
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
|
|
|
|
2007-02-05 15:20:36 +01:00
|
|
|
push ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ecx, [esi+block_size];
|
2007-01-19 04:58:46 +01:00
|
|
|
shr ecx, 12
|
2007-01-18 08:31:56 +01:00
|
|
|
call release_pages ;eax, ecx
|
2007-02-05 15:20:36 +01:00
|
|
|
pop ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
stdcall free_kernel_space, [base]
|
2007-02-05 15:20:36 +01:00
|
|
|
pop esi ebx
|
2007-01-18 08:31:56 +01:00
|
|
|
ret
|
2006-10-06 08:09:41 +02:00
|
|
|
.fail:
|
2007-01-18 08:31:56 +01:00
|
|
|
and [heap_mutex], 0
|
2007-02-05 15:20:36 +01:00
|
|
|
pop esi ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
restore block_next
|
|
|
|
restore block_prev
|
|
|
|
restore block_list
|
|
|
|
restore block_base
|
|
|
|
restore block_size
|
|
|
|
restore block_flags
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;; USER ;;;;;;;;;;;;;;;;;
|
|
|
|
|
2006-10-20 16:02:26 +02:00
|
|
|
HEAP_TOP equ 0x5FC00000
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
align 4
|
2006-10-20 16:02:26 +02:00
|
|
|
proc init_heap
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-04-18 08:37:14 +02:00
|
|
|
mov ebx,[current_slot]
|
|
|
|
mov eax, [ebx+APPDATA.heap_top]
|
2006-10-10 07:11:30 +02:00
|
|
|
test eax, eax
|
|
|
|
jz @F
|
2007-04-18 08:37:14 +02:00
|
|
|
sub eax,[ebx+APPDATA.heap_base]
|
2006-10-10 07:11:30 +02:00
|
|
|
sub eax, 4096
|
|
|
|
ret
|
|
|
|
@@:
|
2007-04-18 08:37:14 +02:00
|
|
|
mov esi, [ebx+APPDATA.mem_size]
|
2006-10-20 16:02:26 +02:00
|
|
|
add esi, 4095
|
|
|
|
and esi, not 4095
|
2007-04-18 08:37:14 +02:00
|
|
|
mov [ebx+APPDATA.mem_size], esi
|
2006-10-20 16:02:26 +02:00
|
|
|
mov eax, HEAP_TOP
|
2007-04-18 08:37:14 +02:00
|
|
|
mov [ebx+APPDATA.heap_base], esi
|
|
|
|
mov [ebx+APPDATA.heap_top], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2006-10-20 16:02:26 +02:00
|
|
|
sub eax, esi
|
|
|
|
shr esi, 10
|
|
|
|
mov ecx, eax
|
2006-10-06 08:09:41 +02:00
|
|
|
sub eax, 4096
|
2006-10-20 16:02:26 +02:00
|
|
|
or ecx, FREE_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+esi], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_alloc stdcall, alloc_size:dword
|
|
|
|
|
2007-10-20 23:02:38 +02:00
|
|
|
push ebx
|
|
|
|
push esi
|
|
|
|
push edi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov ecx, [alloc_size]
|
|
|
|
add ecx, (4095+4096)
|
|
|
|
and ecx, not 4095
|
|
|
|
|
2007-04-18 08:37:14 +02:00
|
|
|
mov ebx, [current_slot]
|
|
|
|
mov esi, dword [ebx+APPDATA.heap_base] ; heap_base
|
|
|
|
mov edi, dword [ebx+APPDATA.heap_top] ; heap_top
|
2006-10-06 08:09:41 +02:00
|
|
|
l_0:
|
|
|
|
cmp esi, edi
|
|
|
|
jae m_exit
|
|
|
|
|
|
|
|
mov ebx, esi
|
|
|
|
shr ebx, 12
|
2007-02-20 13:05:24 +01:00
|
|
|
mov eax, [page_tabs+ebx*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, FREE_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz test_used
|
|
|
|
and eax, 0xFFFFF000
|
|
|
|
cmp eax, ecx ;alloc_size
|
|
|
|
jb m_next
|
2007-01-11 15:43:51 +01:00
|
|
|
jz @f
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-09-05 13:18:20 +02:00
|
|
|
lea edx, [esi+ecx]
|
|
|
|
sub eax, ecx
|
|
|
|
or al, FREE_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
shr edx, 12
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+edx*4], eax
|
2007-01-25 14:42:23 +01:00
|
|
|
@@:
|
2006-10-06 08:09:41 +02:00
|
|
|
or ecx, USED_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+ebx*4], ecx
|
2006-10-06 08:09:41 +02:00
|
|
|
shr ecx, 12
|
|
|
|
inc ebx
|
2007-09-05 13:18:20 +02:00
|
|
|
dec ecx
|
|
|
|
jz .no
|
2006-10-06 08:09:41 +02:00
|
|
|
@@:
|
2007-02-20 13:05:24 +01:00
|
|
|
mov dword [page_tabs+ebx*4], 2
|
2006-10-06 08:09:41 +02:00
|
|
|
inc ebx
|
|
|
|
dec ecx
|
|
|
|
jnz @B
|
2007-09-05 13:18:20 +02:00
|
|
|
.no:
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
|
|
|
mov ebx, [alloc_size]
|
|
|
|
add ebx, 0xFFF
|
|
|
|
and ebx, not 0xFFF
|
|
|
|
add ebx, [edx+APPDATA.mem_size]
|
|
|
|
call update_mem_size
|
2007-01-25 14:42:23 +01:00
|
|
|
|
2007-09-05 13:18:20 +02:00
|
|
|
lea eax, [esi+4096]
|
2007-10-20 23:02:38 +02:00
|
|
|
|
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
test_used:
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, USED_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz m_exit
|
|
|
|
|
|
|
|
and eax, 0xFFFFF000
|
2007-09-05 13:18:20 +02:00
|
|
|
m_next:
|
2006-10-06 08:09:41 +02:00
|
|
|
add esi, eax
|
|
|
|
jmp l_0
|
|
|
|
m_exit:
|
|
|
|
xor eax, eax
|
2007-10-20 23:02:38 +02:00
|
|
|
pop edi
|
|
|
|
pop esi
|
|
|
|
pop ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc user_free stdcall, base:dword
|
|
|
|
|
2007-10-20 23:02:38 +02:00
|
|
|
push esi
|
|
|
|
|
2006-10-06 08:09:41 +02:00
|
|
|
mov esi, [base]
|
|
|
|
test esi, esi
|
|
|
|
jz .exit
|
|
|
|
|
2007-10-20 23:02:38 +02:00
|
|
|
push ebx
|
|
|
|
|
2007-01-25 14:42:23 +01:00
|
|
|
xor ebx, ebx
|
2006-10-06 08:09:41 +02:00
|
|
|
shr esi, 12
|
2007-09-05 13:18:20 +02:00
|
|
|
mov eax, [page_tabs+(esi-1)*4]
|
2007-06-18 18:39:42 +02:00
|
|
|
test al, USED_BLOCK
|
2007-09-05 13:18:20 +02:00
|
|
|
jz .cantfree
|
2007-06-18 18:39:42 +02:00
|
|
|
test al, DONT_FREE_BLOCK
|
|
|
|
jnz .cantfree
|
2006-10-06 08:09:41 +02:00
|
|
|
|
|
|
|
and eax, not 4095
|
|
|
|
mov ecx, eax
|
2007-09-05 13:18:20 +02:00
|
|
|
or al, FREE_BLOCK
|
|
|
|
mov [page_tabs+(esi-1)*4], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
sub ecx, 4096
|
2007-01-25 14:42:23 +01:00
|
|
|
mov ebx, ecx
|
2007-09-05 13:18:20 +02:00
|
|
|
shr ecx, 12
|
|
|
|
jz .released
|
2006-10-06 08:09:41 +02:00
|
|
|
.release:
|
2006-10-20 16:02:26 +02:00
|
|
|
xor eax, eax
|
2007-02-20 13:05:24 +01:00
|
|
|
xchg eax, [page_tabs+esi*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, 1
|
2006-10-20 16:02:26 +02:00
|
|
|
jz @F
|
2006-10-06 08:09:41 +02:00
|
|
|
call free_page
|
2007-04-02 16:29:39 +02:00
|
|
|
mov eax, esi
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
2006-10-20 16:02:26 +02:00
|
|
|
@@:
|
2006-10-06 08:09:41 +02:00
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jnz .release
|
2007-09-05 13:18:20 +02:00
|
|
|
.released:
|
2007-10-20 23:02:38 +02:00
|
|
|
push edi
|
|
|
|
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
|
|
|
mov esi, dword [edx+APPDATA.heap_base]
|
|
|
|
mov edi, dword [edx+APPDATA.heap_top]
|
|
|
|
sub ebx, [edx+APPDATA.mem_size]
|
2007-01-25 14:42:23 +01:00
|
|
|
neg ebx
|
|
|
|
call update_mem_size
|
2007-04-02 16:29:39 +02:00
|
|
|
call user_normalize
|
2007-10-20 23:02:38 +02:00
|
|
|
pop edi
|
|
|
|
pop ebx
|
|
|
|
pop esi
|
2007-04-02 16:29:39 +02:00
|
|
|
ret
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
2007-10-20 23:02:38 +02:00
|
|
|
pop esi
|
2007-04-02 16:29:39 +02:00
|
|
|
ret
|
2007-06-18 18:39:42 +02:00
|
|
|
.cantfree:
|
|
|
|
xor eax, eax
|
2007-10-20 23:02:38 +02:00
|
|
|
pop ebx
|
|
|
|
pop esi
|
2007-06-18 18:39:42 +02:00
|
|
|
ret
|
2007-04-02 16:29:39 +02:00
|
|
|
endp
|
|
|
|
|
|
|
|
user_normalize:
|
|
|
|
; in: esi=heap_base, edi=heap_top
|
|
|
|
; out: eax=0 <=> OK
|
|
|
|
; destroys: ebx,edx,esi,edi
|
2006-10-06 08:09:41 +02:00
|
|
|
shr esi, 12
|
|
|
|
shr edi, 12
|
|
|
|
@@:
|
2007-02-20 13:05:24 +01:00
|
|
|
mov eax, [page_tabs+esi*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, USED_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .test_free
|
|
|
|
shr eax, 12
|
|
|
|
add esi, eax
|
|
|
|
jmp @B
|
|
|
|
.test_free:
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, FREE_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .err
|
|
|
|
mov edx, eax
|
|
|
|
shr edx, 12
|
|
|
|
add edx, esi
|
|
|
|
cmp edx, edi
|
|
|
|
jae .exit
|
|
|
|
|
2007-02-20 13:05:24 +01:00
|
|
|
mov ebx, [page_tabs+edx*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test bl, USED_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .next_free
|
|
|
|
|
|
|
|
shr ebx, 12
|
|
|
|
add edx, ebx
|
|
|
|
mov esi, edx
|
|
|
|
jmp @B
|
|
|
|
.next_free:
|
2007-09-05 13:18:20 +02:00
|
|
|
test bl, FREE_BLOCK
|
2006-10-06 08:09:41 +02:00
|
|
|
jz .err
|
2007-02-20 13:05:24 +01:00
|
|
|
and dword [page_tabs+edx*4], 0
|
2006-10-06 08:09:41 +02:00
|
|
|
add eax, ebx
|
|
|
|
and eax, not 4095
|
|
|
|
or eax, FREE_BLOCK
|
2007-02-20 13:05:24 +01:00
|
|
|
mov [page_tabs+esi*4], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
jmp @B
|
|
|
|
.exit:
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
|
|
|
.err:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2007-04-02 16:29:39 +02:00
|
|
|
|
|
|
|
user_realloc:
|
|
|
|
; in: eax = pointer, ebx = new size
|
|
|
|
; out: eax = new pointer or NULL
|
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
|
|
|
; realloc(NULL,sz) - same as malloc(sz)
|
|
|
|
push ebx
|
|
|
|
call user_alloc
|
|
|
|
ret
|
|
|
|
@@:
|
|
|
|
push ecx edx
|
2007-04-18 08:37:14 +02:00
|
|
|
lea ecx, [eax - 0x1000]
|
2007-04-02 16:29:39 +02:00
|
|
|
shr ecx, 12
|
|
|
|
mov edx, [page_tabs+ecx*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test dl, USED_BLOCK
|
2007-04-02 16:29:39 +02:00
|
|
|
jnz @f
|
|
|
|
; attempt to realloc invalid pointer
|
|
|
|
.ret0:
|
|
|
|
pop edx ecx
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
@@:
|
2007-09-05 13:18:20 +02:00
|
|
|
test dl, DONT_FREE_BLOCK
|
2007-06-18 18:39:42 +02:00
|
|
|
jnz .ret0
|
2007-04-02 16:29:39 +02:00
|
|
|
add ebx, 0x1FFF
|
|
|
|
shr edx, 12
|
|
|
|
shr ebx, 12
|
|
|
|
; edx = allocated size, ebx = new size
|
|
|
|
add edx, ecx
|
|
|
|
add ebx, ecx
|
|
|
|
cmp edx, ebx
|
|
|
|
jb .realloc_add
|
|
|
|
; release part of allocated memory
|
|
|
|
.loop:
|
|
|
|
cmp edx, ebx
|
|
|
|
jz .release_done
|
|
|
|
dec edx
|
|
|
|
xor eax, eax
|
|
|
|
xchg eax, [page_tabs+edx*4]
|
|
|
|
test al, 1
|
|
|
|
jz .loop
|
|
|
|
call free_page
|
|
|
|
mov eax, edx
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
|
|
|
jmp .loop
|
|
|
|
.release_done:
|
|
|
|
sub ebx, ecx
|
|
|
|
cmp ebx, 1
|
|
|
|
jnz .nofreeall
|
|
|
|
mov eax, [page_tabs+ecx*4]
|
|
|
|
and eax, not 0xFFF
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
|
|
|
mov ebx, [APPDATA.mem_size+edx]
|
2007-04-02 16:29:39 +02:00
|
|
|
sub ebx, eax
|
|
|
|
add ebx, 0x1000
|
|
|
|
or al, FREE_BLOCK
|
|
|
|
mov [page_tabs+ecx*4], eax
|
|
|
|
push esi edi
|
2007-04-18 08:37:14 +02:00
|
|
|
mov esi, [APPDATA.heap_base+edx]
|
|
|
|
mov edi, [APPDATA.heap_top+edx]
|
2007-04-02 16:29:39 +02:00
|
|
|
call update_mem_size
|
|
|
|
call user_normalize
|
|
|
|
pop edi esi
|
|
|
|
jmp .ret0 ; all freed
|
|
|
|
.nofreeall:
|
|
|
|
sub edx, ecx
|
|
|
|
shl ebx, 12
|
|
|
|
or ebx, USED_BLOCK
|
|
|
|
xchg [page_tabs+ecx*4], ebx
|
|
|
|
shr ebx, 12
|
|
|
|
sub ebx, edx
|
|
|
|
push ebx ecx edx
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
2007-04-02 16:29:39 +02:00
|
|
|
shl ebx, 12
|
2007-04-18 08:37:14 +02:00
|
|
|
sub ebx, [APPDATA.mem_size+edx]
|
2007-04-02 16:29:39 +02:00
|
|
|
neg ebx
|
|
|
|
call update_mem_size
|
|
|
|
pop edx ecx ebx
|
2007-04-18 08:37:14 +02:00
|
|
|
lea eax, [ecx+1]
|
2007-04-02 16:29:39 +02:00
|
|
|
shl eax, 12
|
|
|
|
push eax
|
|
|
|
add ecx, ebx
|
|
|
|
add edx, ecx
|
|
|
|
shl ebx, 12
|
|
|
|
jz .ret
|
|
|
|
push esi
|
2007-04-18 08:37:14 +02:00
|
|
|
mov esi, [current_slot]
|
|
|
|
mov esi, [APPDATA.heap_top+esi]
|
2007-04-02 16:29:39 +02:00
|
|
|
shr esi, 12
|
|
|
|
@@:
|
|
|
|
cmp edx, esi
|
|
|
|
jae .merge_done
|
|
|
|
mov eax, [page_tabs+edx*4]
|
|
|
|
test al, USED_BLOCK
|
|
|
|
jz .merge_done
|
|
|
|
and dword [page_tabs+edx*4], 0
|
|
|
|
and eax, not 0xFFF
|
|
|
|
add ebx, eax
|
|
|
|
add edx, eax
|
|
|
|
jmp @b
|
|
|
|
.merge_done:
|
|
|
|
pop esi
|
|
|
|
or ebx, FREE_BLOCK
|
|
|
|
mov [page_tabs+ecx*4], ebx
|
|
|
|
.ret:
|
|
|
|
pop eax edx ecx
|
|
|
|
ret
|
|
|
|
.realloc_add:
|
|
|
|
; get some additional memory
|
2007-04-18 08:37:14 +02:00
|
|
|
mov eax, [current_slot]
|
|
|
|
mov eax, [APPDATA.heap_top+eax]
|
2007-04-02 16:29:39 +02:00
|
|
|
shr eax, 12
|
|
|
|
cmp edx, eax
|
|
|
|
jae .cant_inplace
|
|
|
|
mov eax, [page_tabs+edx*4]
|
2007-09-05 13:18:20 +02:00
|
|
|
test al, FREE_BLOCK
|
|
|
|
jz .cant_inplace
|
2007-04-02 16:29:39 +02:00
|
|
|
shr eax, 12
|
|
|
|
add eax, edx
|
|
|
|
sub eax, ebx
|
2007-09-05 13:18:20 +02:00
|
|
|
jb .cant_inplace
|
2007-04-02 16:29:39 +02:00
|
|
|
jz @f
|
|
|
|
shl eax, 12
|
|
|
|
or al, FREE_BLOCK
|
|
|
|
mov [page_tabs+ebx*4], eax
|
|
|
|
@@:
|
|
|
|
mov eax, ebx
|
|
|
|
sub eax, ecx
|
|
|
|
shl eax, 12
|
|
|
|
or al, USED_BLOCK
|
|
|
|
mov [page_tabs+ecx*4], eax
|
2007-04-18 08:37:14 +02:00
|
|
|
lea eax, [ecx+1]
|
2007-04-02 16:29:39 +02:00
|
|
|
shl eax, 12
|
|
|
|
push eax
|
|
|
|
push edi
|
|
|
|
lea edi, [page_tabs+edx*4]
|
|
|
|
mov eax, 2
|
|
|
|
sub ebx, edx
|
|
|
|
mov ecx, ebx
|
|
|
|
cld
|
|
|
|
rep stosd
|
|
|
|
pop edi
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
2007-04-02 16:29:39 +02:00
|
|
|
shl ebx, 12
|
2007-04-18 08:37:14 +02:00
|
|
|
add ebx, [APPDATA.mem_size+edx]
|
2007-04-02 16:29:39 +02:00
|
|
|
call update_mem_size
|
|
|
|
pop eax edx ecx
|
|
|
|
ret
|
|
|
|
.cant_inplace:
|
|
|
|
push esi edi
|
2007-04-18 08:37:14 +02:00
|
|
|
mov eax, [current_slot]
|
|
|
|
mov esi, [APPDATA.heap_base+eax]
|
|
|
|
mov edi, [APPDATA.heap_top+eax]
|
2007-04-02 16:29:39 +02:00
|
|
|
shr esi, 12
|
|
|
|
shr edi, 12
|
|
|
|
sub ebx, ecx
|
|
|
|
.find_place:
|
|
|
|
cmp esi, edi
|
|
|
|
jae .place_not_found
|
|
|
|
mov eax, [page_tabs+esi*4]
|
|
|
|
test al, FREE_BLOCK
|
|
|
|
jz .next_place
|
|
|
|
shr eax, 12
|
|
|
|
cmp eax, ebx
|
|
|
|
jae .place_found
|
|
|
|
add esi, eax
|
|
|
|
jmp .find_place
|
|
|
|
.next_place:
|
|
|
|
shr eax, 12
|
|
|
|
add esi, eax
|
|
|
|
jmp .find_place
|
|
|
|
.place_not_found:
|
|
|
|
pop edi esi
|
|
|
|
jmp .ret0
|
|
|
|
.place_found:
|
|
|
|
sub eax, ebx
|
|
|
|
jz @f
|
|
|
|
push esi
|
2007-09-05 13:18:20 +02:00
|
|
|
add esi, ebx
|
2007-04-02 16:29:39 +02:00
|
|
|
shl eax, 12
|
|
|
|
or al, FREE_BLOCK
|
|
|
|
mov [page_tabs+esi*4], eax
|
|
|
|
pop esi
|
|
|
|
@@:
|
|
|
|
mov eax, ebx
|
|
|
|
shl eax, 12
|
|
|
|
or al, USED_BLOCK
|
|
|
|
mov [page_tabs+esi*4], eax
|
|
|
|
inc esi
|
|
|
|
mov eax, esi
|
|
|
|
shl eax, 12
|
|
|
|
push eax
|
|
|
|
mov eax, [page_tabs+ecx*4]
|
|
|
|
and eax, not 0xFFF
|
|
|
|
or al, FREE_BLOCK
|
|
|
|
sub edx, ecx
|
|
|
|
mov [page_tabs+ecx*4], eax
|
|
|
|
inc ecx
|
2007-09-05 13:18:20 +02:00
|
|
|
dec ebx
|
|
|
|
dec edx
|
|
|
|
jz .no
|
2007-04-02 16:29:39 +02:00
|
|
|
@@:
|
|
|
|
xor eax, eax
|
|
|
|
xchg eax, [page_tabs+ecx*4]
|
|
|
|
mov [page_tabs+esi*4], eax
|
|
|
|
mov eax, ecx
|
|
|
|
shl eax, 12
|
|
|
|
invlpg [eax]
|
|
|
|
inc esi
|
2007-09-05 13:18:20 +02:00
|
|
|
inc ecx
|
2007-04-02 16:29:39 +02:00
|
|
|
dec ebx
|
|
|
|
dec edx
|
|
|
|
jnz @b
|
2007-09-05 13:18:20 +02:00
|
|
|
.no:
|
2007-04-02 16:29:39 +02:00
|
|
|
push ebx
|
2007-04-18 08:37:14 +02:00
|
|
|
mov edx, [current_slot]
|
2007-04-02 16:29:39 +02:00
|
|
|
shl ebx, 12
|
2007-04-18 08:37:14 +02:00
|
|
|
add ebx, [APPDATA.mem_size+edx]
|
2007-04-02 16:29:39 +02:00
|
|
|
call update_mem_size
|
|
|
|
pop ebx
|
|
|
|
@@:
|
|
|
|
mov dword [page_tabs+esi*4], 2
|
|
|
|
inc esi
|
|
|
|
dec ebx
|
|
|
|
jnz @b
|
|
|
|
pop eax edi esi edx ecx
|
|
|
|
ret
|
2006-10-06 08:09:41 +02:00
|
|
|
|
2007-01-18 03:06:00 +01:00
|
|
|
if 0
|
2006-10-06 08:09:41 +02:00
|
|
|
align 4
|
|
|
|
proc alloc_dll
|
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
bsf eax, [dll_map]
|
|
|
|
jnz .find
|
|
|
|
popf
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
.find:
|
|
|
|
btr [dll_map], eax
|
|
|
|
popf
|
|
|
|
shl eax, 5
|
|
|
|
add eax, dll_tab
|
|
|
|
ret
|
|
|
|
endp
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc alloc_service
|
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
bsf eax, [srv_map]
|
|
|
|
jnz .find
|
|
|
|
popf
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2006-11-20 08:06:00 +01:00
|
|
|
.find:
|
|
|
|
btr [srv_map], eax
|
2006-10-06 08:09:41 +02:00
|
|
|
popf
|
2006-11-20 08:06:00 +01:00
|
|
|
shl eax,0x02
|
|
|
|
lea eax,[srv_tab+eax+eax*8] ;srv_tab+eax*36
|
2006-10-06 08:09:41 +02:00
|
|
|
ret
|
|
|
|
endp
|
2007-01-18 03:06:00 +01:00
|
|
|
|
|
|
|
end if
|