Rev 7136 | Rev 7967 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7136 | Rev 7965 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2020. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 7... | Line 7... | ||
7 | 7 | ||
Line 8... | Line 8... | ||
8 | $Revision: 7136 $ |
8 | $Revision: 7965 $ |
9 | 9 | ||
10 | 10 | ||
Line 16... | Line 16... | ||
16 | size dd ? ;+20 |
16 | size dd ? ;+20 |
17 | flags dd ? ;+24 |
17 | flags dd ? ;+24 |
18 | handle dd ? ;+28 |
18 | handle dd ? ;+28 |
19 | ends |
19 | ends |
Line -... | Line 20... | ||
- | 20 | ||
20 | 21 | MEM_BLOCK_RESERVED = 0x02 ; Will be allocated on first access (lazy allocation) |
|
21 | FREE_BLOCK = 4 |
22 | MEM_BLOCK_FREE = 0x04 |
22 | USED_BLOCK = 8 |
23 | MEM_BLOCK_USED = 0x08 |
23 | DONT_FREE_BLOCK = 10h |
- | |
24 | - | ||
25 | - | ||
26 | block_next equ MEM_BLOCK.next_block |
- | |
27 | block_prev equ MEM_BLOCK.prev_block |
- | |
28 | list_fd equ MEM_BLOCK.list.next |
- | |
29 | list_bk equ MEM_BLOCK.list.prev |
- | |
30 | block_base equ MEM_BLOCK.base |
- | |
31 | block_size equ MEM_BLOCK.size |
- | |
Line 32... | Line 24... | ||
32 | block_flags equ MEM_BLOCK.flags |
24 | MEM_BLOCK_DONT_FREE = 0x10 |
33 | 25 | ||
34 | macro calc_index op |
26 | macro calc_index op |
35 | { shr op, 12 |
27 | { shr op, 12 |
Line 41... | Line 33... | ||
41 | } |
33 | } |
Line 42... | Line 34... | ||
42 | 34 | ||
43 | align 4 |
35 | align 4 |
44 | md: |
36 | md: |
45 | .add_to_used: |
37 | .add_to_used: |
46 | mov eax, [esi+block_base] |
38 | mov eax, [esi + MEM_BLOCK.base] |
47 | mov ebx, [esi+block_base] |
39 | mov ebx, [esi + MEM_BLOCK.base] |
48 | shr ebx, 6 |
40 | shr ebx, 6 |
49 | add eax, ebx |
41 | add eax, ebx |
50 | shr ebx, 6 |
42 | shr ebx, 6 |
51 | add eax, ebx |
43 | add eax, ebx |
52 | shr eax, 12 |
44 | shr eax, 12 |
53 | and eax, 63 |
45 | and eax, 63 |
Line 54... | Line 46... | ||
54 | inc [mem_hash_cnt+eax*4] |
46 | inc [mem_hash_cnt + eax*4] |
55 | 47 | ||
56 | lea ecx, [mem_used_list+eax*8] |
48 | lea ecx, [mem_used_list + eax*8] |
57 | list_add esi, ecx |
49 | list_add esi, ecx |
58 | mov [esi+block_flags], USED_BLOCK |
50 | mov [esi + MEM_BLOCK.flags], MEM_BLOCK_USED |
59 | mov eax, [esi+block_size] |
51 | mov eax, [esi + MEM_BLOCK.size] |
60 | sub [heap_free], eax |
52 | sub [heap_free], eax |
61 | ret |
53 | ret |
62 | align 4 |
54 | align 4 |
Line 71... | Line 63... | ||
71 | and ecx, 63 |
63 | and ecx, 63 |
Line 72... | Line 64... | ||
72 | 64 | ||
73 | lea ebx, [mem_used_list+ecx*8] |
65 | lea ebx, [mem_used_list + ecx*8] |
74 | mov esi, ebx |
66 | mov esi, ebx |
75 | .next: |
67 | .next: |
76 | mov esi, [esi+list_fd] |
68 | mov esi, [esi + MEM_BLOCK.list.next] |
77 | cmp esi, ebx |
69 | cmp esi, ebx |
Line 78... | Line 70... | ||
78 | je .fail |
70 | je .fail |
79 | 71 | ||
Line 80... | Line 72... | ||
80 | cmp eax, [esi+block_base] |
72 | cmp eax, [esi + MEM_BLOCK.base] |
81 | jne .next |
73 | jne .next |
82 | 74 | ||
Line 89... | Line 81... | ||
89 | .del_from_used: |
81 | .del_from_used: |
90 | call .find_used |
82 | call .find_used |
91 | test esi, esi |
83 | test esi, esi |
92 | jz .done |
84 | jz .done |
Line 93... | Line 85... | ||
93 | 85 | ||
94 | cmp [esi+block_flags], USED_BLOCK |
86 | cmp [esi + MEM_BLOCK.flags], MEM_BLOCK_USED |
Line 95... | Line 87... | ||
95 | jne .fatal |
87 | jne .fatal |
96 | 88 | ||
97 | dec [mem_hash_cnt+ecx*4] |
89 | dec [mem_hash_cnt + ecx*4] |
Line 102... | Line 94... | ||
102 | xor esi, esi |
94 | xor esi, esi |
103 | ret |
95 | ret |
Line 104... | Line 96... | ||
104 | 96 | ||
105 | ;Initial heap state |
97 | ;Initial heap state |
106 | ; |
98 | ; |
107 | ;+heap_size terminator USED_BLOCK |
99 | ; + heap_size terminator MEM_BLOCK_USED |
108 | ;+4096*MEM_BLOCK.sizeof free space FREE_BLOCK |
100 | ; + 4096*MEM_BLOCK.sizeof free space MEM_BLOCK_FREE |
109 | ;HEAP_BASE heap_descriptors USED_BLOCK |
101 | ;HEAP_BASE heap_descriptors MEM_BLOCK_USED |
Line 110... | Line 102... | ||
110 | ; |
102 | ; |
111 | 103 | ||
Line 138... | Line 130... | ||
138 | mov edi, HEAP_BASE ;descriptors |
130 | mov edi, HEAP_BASE ;descriptors |
139 | mov ebx, HEAP_BASE+sizeof.MEM_BLOCK ;free space |
131 | mov ebx, HEAP_BASE + sizeof.MEM_BLOCK ;free space |
140 | mov ecx, HEAP_BASE+sizeof.MEM_BLOCK*2 ;terminator |
132 | mov ecx, HEAP_BASE + sizeof.MEM_BLOCK*2 ;terminator |
Line 141... | Line 133... | ||
141 | 133 | ||
142 | xor eax, eax |
134 | xor eax, eax |
143 | mov [edi+block_next], ebx |
135 | mov [edi + MEM_BLOCK.next_block], ebx |
144 | mov [edi+block_prev], eax |
136 | mov [edi + MEM_BLOCK.prev_block], eax |
145 | mov [edi+list_fd], eax |
137 | mov [edi + MEM_BLOCK.list.next], eax |
146 | mov [edi+list_bk], eax |
138 | mov [edi + MEM_BLOCK.list.prev], eax |
147 | mov [edi+block_base], HEAP_BASE |
139 | mov [edi + MEM_BLOCK.base], HEAP_BASE |
148 | mov [edi+block_size], 4096*sizeof.MEM_BLOCK |
140 | mov [edi + MEM_BLOCK.size], 4096*sizeof.MEM_BLOCK |
149 | mov [edi+block_flags], USED_BLOCK |
141 | mov [edi + MEM_BLOCK.flags], MEM_BLOCK_USED |
150 | 142 | ||
151 | mov [ecx+block_next], eax |
143 | mov [ecx + MEM_BLOCK.next_block], eax |
152 | mov [ecx+block_prev], ebx |
144 | mov [ecx + MEM_BLOCK.prev_block], ebx |
153 | mov [ecx+list_fd], eax |
145 | mov [ecx + MEM_BLOCK.list.next], eax |
154 | mov [ecx+list_bk], eax |
146 | mov [ecx + MEM_BLOCK.list.prev], eax |
155 | mov [ecx+block_base], eax |
147 | mov [ecx + MEM_BLOCK.base], eax |
156 | mov [ecx+block_size], eax |
148 | mov [ecx + MEM_BLOCK.size], eax |
157 | mov [ecx+block_flags], USED_BLOCK |
149 | mov [ecx + MEM_BLOCK.flags], MEM_BLOCK_USED |
158 | 150 | ||
159 | mov [ebx+block_next], ecx |
151 | mov [ebx + MEM_BLOCK.next_block], ecx |
160 | mov [ebx+block_prev], edi |
152 | mov [ebx + MEM_BLOCK.prev_block], edi |
Line 161... | Line 153... | ||
161 | mov [ebx+block_base], HEAP_BASE+4096*sizeof.MEM_BLOCK |
153 | mov [ebx + MEM_BLOCK.base], HEAP_BASE + 4096*sizeof.MEM_BLOCK |
162 | 154 | ||
163 | mov ecx, [pg_data.kernel_pages] |
155 | mov ecx, [pg_data.kernel_pages] |
164 | shl ecx, 12 |
156 | shl ecx, 12 |
165 | sub ecx, HEAP_BASE-OS_BASE+4096*sizeof.MEM_BLOCK |
157 | sub ecx, HEAP_BASE-OS_BASE + 4096*sizeof.MEM_BLOCK |
166 | mov [heap_size], ecx |
158 | mov [heap_size], ecx |
167 | mov [heap_free], ecx |
159 | mov [heap_free], ecx |
Line 168... | Line 160... | ||
168 | mov [ebx+block_size], ecx |
160 | mov [ebx + MEM_BLOCK.size], ecx |
169 | mov [ebx+block_flags], FREE_BLOCK |
161 | mov [ebx + MEM_BLOCK.flags], MEM_BLOCK_FREE |
Line 170... | Line 162... | ||
170 | 162 | ||
Line 181... | Line 173... | ||
181 | @@: |
173 | @@: |
182 | mov [eax-sizeof.MEM_BLOCK], eax |
174 | mov [eax-sizeof.MEM_BLOCK], eax |
183 | add eax, sizeof.MEM_BLOCK |
175 | add eax, sizeof.MEM_BLOCK |
184 | loop @B |
176 | loop @B |
Line 185... | Line 177... | ||
185 | 177 | ||
Line 186... | Line 178... | ||
186 | mov [eax-sizeof.MEM_BLOCK], dword 0 |
178 | mov dword[eax-sizeof.MEM_BLOCK], 0 |
187 | 179 | ||
188 | mov ecx, heap_mutex |
180 | mov ecx, heap_mutex |
189 | call mutex_init |
181 | call mutex_init |
Line 226... | Line 218... | ||
226 | jz .high_mask |
218 | jz .high_mask |
227 | add ebx, edi |
219 | add ebx, edi |
228 | lea ecx, [mem_block_list+ebx*8] |
220 | lea ecx, [mem_block_list + ebx*8] |
229 | mov edi, ecx |
221 | mov edi, ecx |
230 | .next: |
222 | .next: |
231 | mov edi, [edi+list_fd] |
223 | mov edi, [edi + MEM_BLOCK.list.next] |
232 | cmp edi, ecx |
224 | cmp edi, ecx |
233 | je .err |
225 | je .err |
234 | cmp eax, [edi+block_size] |
226 | cmp eax, [edi + MEM_BLOCK.size] |
235 | ja .next |
227 | ja .next |
236 | ret |
228 | ret |
237 | .err: |
229 | .err: |
238 | xor edi, edi |
230 | xor edi, edi |
239 | ret |
231 | ret |
Line 247... | Line 239... | ||
247 | jmp .find |
239 | jmp .find |
Line 248... | Line 240... | ||
248 | 240 | ||
249 | 241 | ||
- | 242 | align 4 |
|
250 | align 4 |
243 | free_mem_block: |
251 | free_mem_block: |
244 | |
252 | mov ebx, [next_memblock] |
245 | mov ebx, [next_memblock] |
253 | mov [eax], ebx |
- | |
Line -... | Line 246... | ||
- | 246 | mov [eax], ebx |
|
254 | mov [next_memblock], eax |
247 | mov [next_memblock], eax |
255 | xor ebx, ebx |
248 | |
256 | 249 | xor ebx, ebx |
|
257 | mov dword [eax+4], ebx |
250 | mov dword[eax + 4], ebx |
258 | mov dword [eax+8], ebx |
251 | mov dword[eax + 8], ebx |
259 | mov dword [eax+12], ebx |
252 | mov dword[eax + 12], ebx |
260 | mov dword [eax+16], ebx |
253 | mov dword[eax + 16], ebx |
- | 254 | ; mov dword[eax + 20], 0 ;don't clear block size |
|
261 | ; mov dword [eax+20], 0 ;don't clear block size |
255 | mov dword[eax + 24], ebx |
- | 256 | mov dword[eax + 28], ebx |
|
262 | mov dword [eax+24], ebx |
257 | |
Line 263... | Line 258... | ||
263 | mov dword [eax+28], ebx |
258 | inc [free_blocks] |
264 | inc [free_blocks] |
259 | |
265 | ret |
260 | ret |
Line 286... | Line 281... | ||
286 | 281 | ||
287 | call get_small_block ; eax |
282 | call get_small_block ; eax |
288 | test edi, edi |
283 | test edi, edi |
Line 289... | Line 284... | ||
289 | jz .error_unlock |
284 | jz .error_unlock |
290 | 285 | ||
Line 291... | Line 286... | ||
291 | cmp [edi+block_flags], FREE_BLOCK |
286 | cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE |
Line 292... | Line 287... | ||
292 | jne .error_unlock |
287 | jne .error_unlock |
293 | 288 | ||
294 | mov [block_ind], ebx ;index of allocated block |
289 | mov [block_ind], ebx ;index of allocated block |
Line 295... | Line 290... | ||
295 | 290 | ||
296 | mov eax, [edi+block_size] |
291 | mov eax, [edi + MEM_BLOCK.size] |
Line 303... | Line 298... | ||
303 | 298 | ||
304 | dec [free_blocks] |
299 | dec [free_blocks] |
305 | mov eax, [esi] |
300 | mov eax, [esi] |
Line 306... | Line 301... | ||
306 | mov [next_memblock], eax |
301 | mov [next_memblock], eax |
307 | 302 | ||
308 | mov [esi+block_next], edi |
303 | mov [esi + MEM_BLOCK.next_block], edi |
309 | mov eax, [edi+block_prev] |
304 | mov eax, [edi + MEM_BLOCK.prev_block] |
310 | mov [esi+block_prev], eax |
305 | mov [esi + MEM_BLOCK.prev_block], eax |
311 | mov [edi+block_prev], esi |
306 | mov [edi + MEM_BLOCK.prev_block], esi |
312 | mov [esi+list_fd], 0 |
307 | mov [esi + MEM_BLOCK.list.next], 0 |
Line 313... | Line 308... | ||
313 | mov [esi+list_bk], 0 |
308 | mov [esi + MEM_BLOCK.list.prev], 0 |
314 | mov [eax+block_next], esi |
309 | mov [eax + MEM_BLOCK.next_block], esi |
315 | 310 | ||
316 | mov ebx, [edi+block_base] |
311 | mov ebx, [edi + MEM_BLOCK.base] |
317 | mov [esi+block_base], ebx |
312 | mov [esi + MEM_BLOCK.base], ebx |
318 | mov edx, [size] |
313 | mov edx, [size] |
Line 319... | Line 314... | ||
319 | mov [esi+block_size], edx |
314 | mov [esi + MEM_BLOCK.size], edx |
320 | add [edi+block_base], edx |
315 | add [edi + MEM_BLOCK.base], edx |
321 | sub [edi+block_size], edx |
316 | sub [edi + MEM_BLOCK.size], edx |
322 | 317 | ||
Line 323... | Line 318... | ||
323 | mov eax, [edi+block_size] |
318 | mov eax, [edi + MEM_BLOCK.size] |
Line 339... | Line 334... | ||
339 | .add_used: |
334 | .add_used: |
Line 340... | Line 335... | ||
340 | 335 | ||
Line 341... | Line 336... | ||
341 | call md.add_to_used |
336 | call md.add_to_used |
342 | 337 | ||
343 | spin_unlock_irqrestore heap_mutex |
338 | spin_unlock_irqrestore heap_mutex |
344 | mov eax, [esi+block_base] |
339 | mov eax, [esi + MEM_BLOCK.base] |
345 | pop edi |
340 | pop edi |
346 | pop esi |
341 | pop esi |
Line 376... | Line 371... | ||
376 | 371 | ||
377 | call md.del_from_used |
372 | call md.del_from_used |
378 | test esi, esi |
373 | test esi, esi |
Line 379... | Line 374... | ||
379 | jz .fail |
374 | jz .fail |
380 | 375 | ||
Line 381... | Line 376... | ||
381 | mov eax, [esi+block_size] |
376 | mov eax, [esi + MEM_BLOCK.size] |
382 | add [heap_free], eax |
377 | add [heap_free], eax |
383 | 378 | ||
Line 384... | Line 379... | ||
384 | mov edi, [esi+block_next] |
379 | mov edi, [esi + MEM_BLOCK.next_block] |
Line 385... | Line 380... | ||
385 | cmp [edi+block_flags], FREE_BLOCK |
380 | cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE |
386 | jne .prev |
381 | jne .prev |
387 | 382 | ||
388 | list_del edi |
383 | list_del edi |
389 | 384 | ||
Line 390... | Line 385... | ||
390 | mov edx, [edi+block_next] |
385 | mov edx, [edi + MEM_BLOCK.next_block] |
Line 391... | Line 386... | ||
391 | mov [esi+block_next], edx |
386 | mov [esi + MEM_BLOCK.next_block], edx |
392 | mov [edx+block_prev], esi |
387 | mov [edx + MEM_BLOCK.prev_block], esi |
Line 401... | Line 396... | ||
401 | btr [mem_block_mask], ecx |
396 | btr [mem_block_mask], ecx |
402 | @@: |
397 | @@: |
403 | mov eax, edi |
398 | mov eax, edi |
404 | call free_mem_block |
399 | call free_mem_block |
405 | .prev: |
400 | .prev: |
406 | mov edi, [esi+block_prev] |
401 | mov edi, [esi + MEM_BLOCK.prev_block] |
407 | cmp [edi+block_flags], FREE_BLOCK |
402 | cmp [edi + MEM_BLOCK.flags], MEM_BLOCK_FREE |
408 | jne .insert |
403 | jne .insert |
Line 409... | Line 404... | ||
409 | 404 | ||
410 | mov edx, [esi+block_next] |
405 | mov edx, [esi + MEM_BLOCK.next_block] |
411 | mov [edi+block_next], edx |
406 | mov [edi + MEM_BLOCK.next_block], edx |
Line 412... | Line 407... | ||
412 | mov [edx+block_prev], edi |
407 | mov [edx + MEM_BLOCK.prev_block], edi |
413 | 408 | ||
Line 414... | Line 409... | ||
414 | mov eax, esi |
409 | mov eax, esi |
415 | call free_mem_block |
410 | call free_mem_block |
416 | 411 | ||
417 | mov ecx, [edi+block_size] |
412 | mov ecx, [edi + MEM_BLOCK.size] |
Line 418... | Line 413... | ||
418 | mov eax, [esi+block_size] |
413 | mov eax, [esi + MEM_BLOCK.size] |
419 | add eax, ecx |
414 | add eax, ecx |
420 | mov [edi+block_size], eax |
415 | mov [edi + MEM_BLOCK.size], eax |
421 | 416 | ||
Line 441... | Line 436... | ||
441 | spin_unlock_irqrestore heap_mutex |
436 | spin_unlock_irqrestore heap_mutex |
442 | xor eax, eax |
437 | xor eax, eax |
443 | not eax |
438 | not eax |
444 | ret |
439 | ret |
445 | .insert: |
440 | .insert: |
446 | mov [esi+block_flags], FREE_BLOCK |
441 | mov [esi + MEM_BLOCK.flags], MEM_BLOCK_FREE |
447 | mov eax, [esi+block_size] |
442 | mov eax, [esi + MEM_BLOCK.size] |
448 | calc_index eax |
443 | calc_index eax |
449 | mov edi, esi |
444 | mov edi, esi |
450 | jmp .add_block |
445 | jmp .add_block |
Line 451... | Line 446... | ||
451 | 446 | ||
Line 530... | Line 525... | ||
530 | spin_lock_irqsave heap_mutex |
525 | spin_lock_irqsave heap_mutex |
Line 531... | Line 526... | ||
531 | 526 | ||
532 | mov eax, [base] |
527 | mov eax, [base] |
Line 533... | Line 528... | ||
533 | call md.find_used |
528 | call md.find_used |
534 | 529 | ||
Line 535... | Line 530... | ||
535 | cmp [esi+block_flags], USED_BLOCK |
530 | cmp [esi + MEM_BLOCK.flags], MEM_BLOCK_USED |
Line 536... | Line 531... | ||
536 | jne .fail |
531 | jne .fail |
537 | 532 | ||
538 | spin_unlock_irqrestore heap_mutex |
533 | spin_unlock_irqrestore heap_mutex |
539 | 534 | ||
540 | mov eax, [esi+block_base] |
535 | mov eax, [esi + MEM_BLOCK.base] |
541 | mov ecx, [esi+block_size] |
536 | mov ecx, [esi + MEM_BLOCK.size] |
542 | shr ecx, 12 |
537 | shr ecx, 12 |
Line 549... | Line 544... | ||
549 | xor eax, eax |
544 | xor eax, eax |
550 | pop esi ebx |
545 | pop esi ebx |
551 | ret |
546 | ret |
552 | endp |
547 | endp |
Line 553... | Line -... | ||
553 | - | ||
554 | restore block_next |
- | |
555 | restore block_prev |
- | |
556 | restore block_list |
- | |
557 | restore block_base |
- | |
558 | restore block_size |
- | |
559 | restore block_flags |
- | |
560 | 548 | ||
Line 561... | Line 549... | ||
561 | ;;;;;;;;;;;;;; USER HEAP ;;;;;;;;;;;;;;;;; |
549 | ;;;;;;;;;;;;;; USER HEAP ;;;;;;;;;;;;;;;;; |
Line 562... | Line 550... | ||
562 | 550 | ||
Line 586... | Line 574... | ||
586 | 574 | ||
587 | sub eax, esi |
575 | sub eax, esi |
588 | shr esi, 10 |
576 | shr esi, 10 |
589 | mov ecx, eax |
577 | mov ecx, eax |
590 | sub eax, PAGE_SIZE |
578 | sub eax, PAGE_SIZE |
591 | or ecx, FREE_BLOCK |
579 | or ecx, MEM_BLOCK_FREE |
592 | mov [page_tabs+esi], ecx |
580 | mov [page_tabs + esi], ecx |
593 | ret |
581 | ret |
Line 594... | Line 582... | ||
594 | endp |
582 | endp |
595 | 583 | ||
- | 584 | align 4 |
|
596 | align 4 |
585 | proc user_alloc stdcall, alloc_size:dword |
- | 586 | ||
597 | proc user_alloc stdcall, alloc_size:dword |
587 | push ebx esi edi |
598 | push ebx esi edi |
588 | |
599 | mov ebx, [current_process] |
589 | mov ebx, [current_process] |
- | 590 | lea ecx, [ebx + PROC.heap_lock] |
|
600 | lea ecx, [ebx+PROC.heap_lock] |
591 | call mutex_lock |
601 | call mutex_lock |
592 | |
602 | mov ecx, [alloc_size] |
593 | mov ecx, [alloc_size] |
603 | add ecx, (4095+PAGE_SIZE) |
594 | add ecx, (4095 + PAGE_SIZE) |
604 | and ecx, not 4095 |
595 | and ecx, not 4095 |
605 | mov esi, dword [ebx+PROC.heap_base] ; heap_base |
596 | mov esi, [ebx + PROC.heap_base] |
606 | mov edi, dword [ebx+PROC.heap_top] ; heap_top |
597 | mov edi, [ebx + PROC.heap_top] |
607 | .scan: |
598 | .scan: |
Line 608... | Line 599... | ||
608 | cmp esi, edi |
599 | cmp esi, edi |
609 | jae .m_exit |
600 | jae .m_exit |
610 | 601 | ||
611 | mov ebx, esi |
602 | mov ebx, esi |
612 | shr ebx, 12 |
603 | shr ebx, 12 |
613 | mov eax, [page_tabs+ebx*4] |
604 | mov eax, [page_tabs + ebx*4] |
614 | test al, FREE_BLOCK |
605 | test al, MEM_BLOCK_FREE |
615 | jz .test_used |
606 | jz .test_used |
616 | and eax, 0xFFFFF000 |
607 | and eax, 0xFFFFF000 |
Line 617... | Line 608... | ||
617 | cmp eax, ecx ;alloc_size |
608 | cmp eax, ecx ;alloc_size |
618 | jb .m_next |
609 | jb .m_next |
619 | jz @f |
610 | jz @f |
620 | 611 | ||
621 | lea edx, [esi+ecx] |
612 | lea edx, [esi + ecx] |
622 | sub eax, ecx |
613 | sub eax, ecx |
623 | or al, FREE_BLOCK |
614 | or al, MEM_BLOCK_FREE |
624 | shr edx, 12 |
615 | shr edx, 12 |
625 | mov [page_tabs+edx*4], eax |
616 | mov [page_tabs + edx*4], eax |
626 | @@: |
617 | @@: |
627 | or ecx, USED_BLOCK |
618 | or ecx, MEM_BLOCK_USED |
628 | mov [page_tabs+ebx*4], ecx |
619 | mov [page_tabs + ebx*4], ecx |
629 | shr ecx, 12 |
620 | shr ecx, 12 |
630 | inc ebx |
621 | inc ebx |
631 | dec ecx |
622 | dec ecx |
632 | jz .no |
623 | jz .no |
633 | @@: |
624 | @@: |
634 | mov dword [page_tabs+ebx*4], 2 |
625 | mov dword [page_tabs + ebx*4], MEM_BLOCK_RESERVED |
Line 651... | Line 642... | ||
651 | pop edi |
642 | pop edi |
652 | pop esi |
643 | pop esi |
653 | pop ebx |
644 | pop ebx |
654 | ret |
645 | ret |
655 | .test_used: |
646 | .test_used: |
656 | test al, USED_BLOCK |
647 | test al, MEM_BLOCK_USED |
657 | jz .m_exit |
648 | jz .m_exit |
Line 658... | Line 649... | ||
658 | 649 | ||
659 | and eax, 0xFFFFF000 |
650 | and eax, 0xFFFFF000 ; not PAGESIZE |
660 | .m_next: |
651 | .m_next: |
661 | add esi, eax |
652 | add esi, eax |
662 | jmp .scan |
653 | jmp .scan |
663 | .m_exit: |
654 | .m_exit: |
Line 714... | Line 705... | ||
714 | pop edi |
705 | pop edi |
715 | pop esi |
706 | pop esi |
716 | pop ebx |
707 | pop ebx |
717 | ret |
708 | ret |
718 | .found: |
709 | .found: |
719 | test al, FREE_BLOCK |
710 | test al, MEM_BLOCK_FREE |
720 | jz .error |
711 | jz .error |
721 | mov eax, ecx |
712 | mov eax, ecx |
722 | sub eax, edx |
713 | sub eax, edx |
723 | sub eax, 0x1000 |
714 | sub eax, 0x1000 |
724 | cmp eax, [alloc_size] |
715 | cmp eax, [alloc_size] |
Line 731... | Line 722... | ||
731 | ; free at [edx+0x1000+ALIGN_UP(alloc_size,0x1000), ecx) |
722 | ; free at [edx + 0x1000 + ALIGN_UP(alloc_size,0x1000), ecx) |
732 | ; First or third block (or both) may be absent. |
723 | ; First or third block (or both) may be absent. |
733 | mov eax, edx |
724 | mov eax, edx |
734 | sub eax, esi |
725 | sub eax, esi |
735 | jz .nofirst |
726 | jz .nofirst |
736 | or al, FREE_BLOCK |
727 | or al, MEM_BLOCK_FREE |
737 | mov [page_tabs+ebx*4], eax |
728 | mov [page_tabs + ebx*4], eax |
738 | .nofirst: |
729 | .nofirst: |
739 | mov eax, [alloc_size] |
730 | mov eax, [alloc_size] |
740 | add eax, 0x1FFF |
731 | add eax, 0x1FFF |
741 | and eax, not 0xFFF |
732 | and eax, not 0xFFF |
742 | mov ebx, edx |
733 | mov ebx, edx |
743 | add edx, eax |
734 | add edx, eax |
744 | shr ebx, 12 |
735 | shr ebx, 12 |
745 | or al, USED_BLOCK |
736 | or al, MEM_BLOCK_USED |
746 | mov [page_tabs+ebx*4], eax |
737 | mov [page_tabs + ebx*4], eax |
747 | shr eax, 12 |
738 | shr eax, 12 |
748 | dec eax |
739 | dec eax |
749 | jz .second_nofill |
740 | jz .second_nofill |
750 | inc ebx |
741 | inc ebx |
751 | .fill: |
742 | .fill: |
752 | mov dword [page_tabs+ebx*4], 2 |
743 | mov dword [page_tabs + ebx*4], MEM_BLOCK_RESERVED |
753 | inc ebx |
744 | inc ebx |
754 | dec eax |
745 | dec eax |
755 | jnz .fill |
746 | jnz .fill |
Line 756... | Line 747... | ||
756 | 747 | ||
757 | .second_nofill: |
748 | .second_nofill: |
758 | sub ecx, edx |
749 | sub ecx, edx |
759 | jz .nothird |
750 | jz .nothird |
760 | or cl, FREE_BLOCK |
751 | or cl, MEM_BLOCK_FREE |
Line 761... | Line 752... | ||
761 | mov [page_tabs+ebx*4], ecx |
752 | mov [page_tabs + ebx*4], ecx |
762 | 753 | ||
763 | .nothird: |
754 | .nothird: |
Line 794... | Line 785... | ||
794 | call mutex_lock |
785 | call mutex_lock |
Line 795... | Line 786... | ||
795 | 786 | ||
796 | xor ebx, ebx |
787 | xor ebx, ebx |
797 | shr esi, 12 |
788 | shr esi, 12 |
798 | mov eax, [page_tabs+(esi-1)*4] |
789 | mov eax, [page_tabs + (esi-1)*4] |
799 | test al, USED_BLOCK |
790 | test al, MEM_BLOCK_USED |
800 | jz .cantfree |
791 | jz .cantfree |
801 | test al, DONT_FREE_BLOCK |
792 | test al, MEM_BLOCK_DONT_FREE |
Line 802... | Line 793... | ||
802 | jnz .cantfree |
793 | jnz .cantfree |
803 | 794 | ||
804 | and eax, not 4095 |
795 | and eax, not 4095 |
805 | mov ecx, eax |
796 | mov ecx, eax |
806 | or al, FREE_BLOCK |
797 | or al, MEM_BLOCK_FREE |
807 | mov [page_tabs+(esi-1)*4], eax |
798 | mov [page_tabs + (esi-1)*4], eax |
808 | sub ecx, 4096 |
799 | sub ecx, 4096 |
809 | mov ebx, ecx |
800 | mov ebx, ecx |
Line 871... | Line 862... | ||
871 | js .error |
862 | js .error |
Line 872... | Line 863... | ||
872 | 863 | ||
873 | shr ebx, 12 ; chek block attributes |
864 | shr ebx, 12 ; chek block attributes |
874 | lea ebx, [page_tabs+ebx*4] |
865 | lea ebx, [page_tabs + ebx*4] |
875 | mov eax, [ebx-4] ; block attributes |
866 | mov eax, [ebx-4] ; block attributes |
876 | test al, USED_BLOCK |
867 | test al, MEM_BLOCK_USED |
877 | jz .error |
868 | jz .error |
878 | test al, DONT_FREE_BLOCK |
869 | test al, MEM_BLOCK_DONT_FREE |
Line 879... | Line 870... | ||
879 | jnz .error |
870 | jnz .error |
880 | 871 | ||
Line 900... | Line 891... | ||
900 | mov eax, [edx] ; get page addres |
891 | mov eax, [edx] ; get page addres |
901 | test al, 1 ; page mapped ? |
892 | test al, 1 ; page mapped ? |
902 | jz @F |
893 | jz @F |
903 | test eax, PG_SHARED ; page shared ? |
894 | test eax, PG_SHARED ; page shared ? |
904 | jnz @F |
895 | jnz @F |
905 | mov [edx], dword 2 |
896 | mov dword[edx], MEM_BLOCK_RESERVED |
906 | ; mark page as reserved |
897 | ; mark page as reserved |
907 | invlpg [ebx] ; when we start using |
898 | invlpg [ebx] ; when we start using |
908 | call free_page ; empty c-o-w page instead this ? |
899 | call free_page ; empty c-o-w page instead this ? |
909 | @@: |
900 | @@: |
910 | add ebx, 4096 |
901 | add ebx, 4096 ; PAGESIZE? |
911 | add edx, 4 |
902 | add edx, 4 |
912 | dec ecx |
903 | dec ecx |
913 | jnz .unmap |
904 | jnz .unmap |
Line 914... | Line 905... | ||
914 | 905 | ||
Line 928... | Line 919... | ||
928 | ; destroys: ebx,edx,esi,edi |
919 | ; destroys: ebx,edx,esi,edi |
929 | shr esi, 12 |
920 | shr esi, 12 |
930 | shr edi, 12 |
921 | shr edi, 12 |
931 | @@: |
922 | @@: |
932 | mov eax, [page_tabs+esi*4] |
923 | mov eax, [page_tabs + esi*4] |
933 | test al, USED_BLOCK |
924 | test al, MEM_BLOCK_USED |
934 | jz .test_free |
925 | jz .test_free |
935 | shr eax, 12 |
926 | shr eax, 12 |
936 | add esi, eax |
927 | add esi, eax |
937 | jmp @B |
928 | jmp @B |
938 | .test_free: |
929 | .test_free: |
939 | test al, FREE_BLOCK |
930 | test al, MEM_BLOCK_FREE |
940 | jz .err |
931 | jz .err |
941 | mov edx, eax |
932 | mov edx, eax |
942 | shr edx, 12 |
933 | shr edx, 12 |
943 | add edx, esi |
934 | add edx, esi |
944 | cmp edx, edi |
935 | cmp edx, edi |
945 | jae .exit |
936 | jae .exit |
Line 946... | Line 937... | ||
946 | 937 | ||
947 | mov ebx, [page_tabs+edx*4] |
938 | mov ebx, [page_tabs + edx*4] |
948 | test bl, USED_BLOCK |
939 | test bl, MEM_BLOCK_USED |
Line 949... | Line 940... | ||
949 | jz .next_free |
940 | jz .next_free |
950 | 941 | ||
951 | shr ebx, 12 |
942 | shr ebx, 12 |
952 | add edx, ebx |
943 | add edx, ebx |
953 | mov esi, edx |
944 | mov esi, edx |
954 | jmp @B |
945 | jmp @B |
955 | .next_free: |
946 | .next_free: |
956 | test bl, FREE_BLOCK |
947 | test bl, MEM_BLOCK_FREE |
957 | jz .err |
948 | jz .err |
958 | and dword [page_tabs+edx*4], 0 |
949 | and dword[page_tabs + edx*4], 0 |
959 | add eax, ebx |
950 | add eax, ebx |
960 | and eax, not 4095 |
951 | and eax, not 4095 ; not (PAGESIZE - 1) ? |
961 | or eax, FREE_BLOCK |
952 | or eax, MEM_BLOCK_FREE |
962 | mov [page_tabs+esi*4], eax |
953 | mov [page_tabs + esi*4], eax |
963 | jmp @B |
954 | jmp @B |
964 | .exit: |
955 | .exit: |
Line 988... | Line 979... | ||
988 | pop eax |
979 | pop eax |
Line 989... | Line 980... | ||
989 | 980 | ||
990 | lea ecx, [eax - 0x1000] |
981 | lea ecx, [eax - 0x1000] |
991 | shr ecx, 12 |
982 | shr ecx, 12 |
992 | mov edx, [page_tabs+ecx*4] |
983 | mov edx, [page_tabs + ecx*4] |
993 | test dl, USED_BLOCK |
984 | test dl, MEM_BLOCK_USED |
994 | jnz @f |
985 | jnz @f |
995 | ; attempt to realloc invalid pointer |
986 | ; attempt to realloc invalid pointer |
996 | .ret0: |
987 | .ret0: |
997 | mov ecx, [current_process] |
988 | mov ecx, [current_process] |
Line 1000... | Line 991... | ||
1000 | 991 | ||
1001 | pop edx ecx |
992 | pop edx ecx |
1002 | xor eax, eax |
993 | xor eax, eax |
1003 | ret |
994 | ret |
1004 | @@: |
995 | @@: |
1005 | test dl, DONT_FREE_BLOCK |
996 | test dl, MEM_BLOCK_DONT_FREE |
1006 | jnz .ret0 |
997 | jnz .ret0 |
1007 | add ebx, 0x1FFF |
998 | add ebx, 0x1FFF |
1008 | shr edx, 12 |
999 | shr edx, 12 |
1009 | shr ebx, 12 |
1000 | shr ebx, 12 |
Line 1034... | Line 1025... | ||
1034 | and eax, not 0xFFF |
1025 | and eax, not 0xFFF |
1035 | mov edx, [current_process] |
1026 | mov edx, [current_process] |
1036 | mov ebx, [edx+PROC.mem_used] |
1027 | mov ebx, [edx + PROC.mem_used] |
1037 | sub ebx, eax |
1028 | sub ebx, eax |
1038 | add ebx, 0x1000 |
1029 | add ebx, 0x1000 |
1039 | or al, FREE_BLOCK |
1030 | or al, MEM_BLOCK_FREE |
1040 | mov [page_tabs+ecx*4], eax |
1031 | mov [page_tabs + ecx*4], eax |
1041 | push esi edi |
1032 | push esi edi |
1042 | mov esi, [edx+PROC.heap_base] |
1033 | mov esi, [edx + PROC.heap_base] |
1043 | mov edi, [edx+PROC.heap_top] |
1034 | mov edi, [edx + PROC.heap_top] |
1044 | mov [edx+PROC.mem_used], ebx |
1035 | mov [edx + PROC.mem_used], ebx |
Line 1046... | Line 1037... | ||
1046 | pop edi esi |
1037 | pop edi esi |
1047 | jmp .ret0 ; all freed |
1038 | jmp .ret0 ; all freed |
1048 | .nofreeall: |
1039 | .nofreeall: |
1049 | sub edx, ecx |
1040 | sub edx, ecx |
1050 | shl ebx, 12 |
1041 | shl ebx, 12 |
1051 | or ebx, USED_BLOCK |
1042 | or ebx, MEM_BLOCK_USED |
1052 | xchg [page_tabs+ecx*4], ebx |
1043 | xchg [page_tabs + ecx*4], ebx |
1053 | shr ebx, 12 |
1044 | shr ebx, 12 |
1054 | sub ebx, edx |
1045 | sub ebx, edx |
1055 | push ebx ecx edx |
1046 | push ebx ecx edx |
1056 | mov edx, [current_process] |
1047 | mov edx, [current_process] |
Line 1072... | Line 1063... | ||
1072 | shr esi, 12 |
1063 | shr esi, 12 |
1073 | @@: |
1064 | @@: |
1074 | cmp edx, esi |
1065 | cmp edx, esi |
1075 | jae .merge_done |
1066 | jae .merge_done |
1076 | mov eax, [page_tabs+edx*4] |
1067 | mov eax, [page_tabs + edx*4] |
1077 | test al, USED_BLOCK |
1068 | test al, MEM_BLOCK_USED |
1078 | jnz .merge_done |
1069 | jnz .merge_done |
1079 | and dword [page_tabs+edx*4], 0 |
1070 | and dword [page_tabs + edx*4], 0 |
1080 | shr eax, 12 |
1071 | shr eax, 12 |
1081 | add edx, eax |
1072 | add edx, eax |
1082 | shl eax, 12 |
1073 | shl eax, 12 |
1083 | add ebx, eax |
1074 | add ebx, eax |
1084 | jmp @b |
1075 | jmp @b |
1085 | .merge_done: |
1076 | .merge_done: |
1086 | pop esi |
1077 | pop esi |
1087 | or ebx, FREE_BLOCK |
1078 | or ebx, MEM_BLOCK_FREE |
1088 | mov [page_tabs+ecx*4], ebx |
1079 | mov [page_tabs + ecx*4], ebx |
1089 | .ret: |
1080 | .ret: |
1090 | mov ecx, [current_process] |
1081 | mov ecx, [current_process] |
1091 | lea ecx, [ecx+PROC.heap_lock] |
1082 | lea ecx, [ecx + PROC.heap_lock] |
1092 | call mutex_unlock |
1083 | call mutex_unlock |
Line 1099... | Line 1090... | ||
1099 | mov eax, [eax+PROC.heap_top] |
1090 | mov eax, [eax + PROC.heap_top] |
1100 | shr eax, 12 |
1091 | shr eax, 12 |
1101 | cmp edx, eax |
1092 | cmp edx, eax |
1102 | jae .cant_inplace |
1093 | jae .cant_inplace |
1103 | mov eax, [page_tabs+edx*4] |
1094 | mov eax, [page_tabs + edx*4] |
1104 | test al, FREE_BLOCK |
1095 | test al, MEM_BLOCK_FREE |
1105 | jz .cant_inplace |
1096 | jz .cant_inplace |
1106 | shr eax, 12 |
1097 | shr eax, 12 |
1107 | add eax, edx |
1098 | add eax, edx |
1108 | sub eax, ebx |
1099 | sub eax, ebx |
1109 | jb .cant_inplace |
1100 | jb .cant_inplace |
1110 | jz @f |
1101 | jz @f |
1111 | shl eax, 12 |
1102 | shl eax, 12 |
1112 | or al, FREE_BLOCK |
1103 | or al, MEM_BLOCK_FREE |
1113 | mov [page_tabs+ebx*4], eax |
1104 | mov [page_tabs + ebx*4], eax |
1114 | @@: |
1105 | @@: |
1115 | mov eax, ebx |
1106 | mov eax, ebx |
1116 | sub eax, ecx |
1107 | sub eax, ecx |
1117 | shl eax, 12 |
1108 | shl eax, 12 |
1118 | or al, USED_BLOCK |
1109 | or al, MEM_BLOCK_USED |
1119 | mov [page_tabs+ecx*4], eax |
1110 | mov [page_tabs + ecx*4], eax |
1120 | lea eax, [ecx+1] |
1111 | lea eax, [ecx + 1] |
1121 | shl eax, 12 |
1112 | shl eax, 12 |
1122 | push eax |
1113 | push eax |
1123 | push edi |
1114 | push edi |
Line 1148... | Line 1139... | ||
1148 | sub ebx, ecx |
1139 | sub ebx, ecx |
1149 | .find_place: |
1140 | .find_place: |
1150 | cmp esi, edi |
1141 | cmp esi, edi |
1151 | jae .place_not_found |
1142 | jae .place_not_found |
1152 | mov eax, [page_tabs+esi*4] |
1143 | mov eax, [page_tabs + esi*4] |
1153 | test al, FREE_BLOCK |
1144 | test al, MEM_BLOCK_FREE |
1154 | jz .next_place |
1145 | jz .next_place |
1155 | shr eax, 12 |
1146 | shr eax, 12 |
1156 | cmp eax, ebx |
1147 | cmp eax, ebx |
1157 | jae .place_found |
1148 | jae .place_found |
1158 | add esi, eax |
1149 | add esi, eax |
Line 1168... | Line 1159... | ||
1168 | sub eax, ebx |
1159 | sub eax, ebx |
1169 | jz @f |
1160 | jz @f |
1170 | push esi |
1161 | push esi |
1171 | add esi, ebx |
1162 | add esi, ebx |
1172 | shl eax, 12 |
1163 | shl eax, 12 |
1173 | or al, FREE_BLOCK |
1164 | or al, MEM_BLOCK_FREE |
1174 | mov [page_tabs+esi*4], eax |
1165 | mov [page_tabs + esi*4], eax |
1175 | pop esi |
1166 | pop esi |
1176 | @@: |
1167 | @@: |
1177 | mov eax, ebx |
1168 | mov eax, ebx |
1178 | shl eax, 12 |
1169 | shl eax, 12 |
1179 | or al, USED_BLOCK |
1170 | or al, MEM_BLOCK_USED |
1180 | mov [page_tabs+esi*4], eax |
1171 | mov [page_tabs + esi*4], eax |
1181 | inc esi |
1172 | inc esi |
1182 | mov eax, esi |
1173 | mov eax, esi |
1183 | shl eax, 12 |
1174 | shl eax, 12 |
1184 | push eax |
1175 | push eax |
1185 | mov eax, [page_tabs+ecx*4] |
1176 | mov eax, [page_tabs + ecx*4] |
1186 | and eax, not 0xFFF |
1177 | and eax, not 0xFFF |
1187 | or al, FREE_BLOCK |
1178 | or al, MEM_BLOCK_FREE |
1188 | sub edx, ecx |
1179 | sub edx, ecx |
1189 | mov [page_tabs+ecx*4], eax |
1180 | mov [page_tabs + ecx*4], eax |
1190 | inc ecx |
1181 | inc ecx |
1191 | dec ebx |
1182 | dec ebx |
1192 | dec edx |
1183 | dec edx |
Line 1208... | Line 1199... | ||
1208 | mov edx, [current_process] |
1199 | mov edx, [current_process] |
1209 | shl ebx, 12 |
1200 | shl ebx, 12 |
1210 | add [edx+PROC.mem_used], ebx |
1201 | add [edx + PROC.mem_used], ebx |
1211 | pop ebx |
1202 | pop ebx |
1212 | @@: |
1203 | @@: |
1213 | mov dword [page_tabs+esi*4], 2 |
1204 | mov dword [page_tabs + esi*4], MEM_BLOCK_RESERVED |
1214 | inc esi |
1205 | inc esi |
1215 | dec ebx |
1206 | dec ebx |
1216 | jnz @b |
1207 | jnz @b |
Line 1217... | Line 1208... | ||
1217 | 1208 | ||
Line 1524... | Line 1515... | ||
1524 | pop edi |
1515 | pop edi |
1525 | pop esi |
1516 | pop esi |
1526 | .fail: |
1517 | .fail: |
1527 | ret |
1518 | ret |
1528 | endp=> |
1519 | endp=> |
- | 1520 |