Subversion Repositories Kolibri OS

Rev

Rev 2151 | Rev 2166 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2151 Rev 2156
Line 3... Line 3...
3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved. ;;
3
;; Copyright (C) KolibriOS team 2004-2009. 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: 2151 $
8
$Revision: 2156 $
Line 9... Line 9...
9
 
9
 
10
HASH_IT  = 1
10
HASH_IT  = 1
Line 45... Line 45...
45
           jna @f
45
           jna @f
46
           mov op, 63
46
           mov op, 63
47
@@:
47
@@:
48
}
48
}
Line -... Line 49...
-
 
49
 
-
 
50
align 4
49
 
51
md:
-
 
52
.add_to_used:
-
 
53
           mov eax, [esi+block_base]
-
 
54
           mov ebx, [esi+block_base]
-
 
55
           shr ebx, 6
-
 
56
           add eax, ebx
-
 
57
           shr ebx, 6
-
 
58
           add eax, ebx
-
 
59
           shr eax, 12
-
 
60
           and eax, 63
50
macro remove_from_used op
61
           inc [mem_hash_cnt+eax*4]
-
 
62
 
-
 
63
           lea ecx, [mem_used_list+eax*8]
-
 
64
           list_add esi, ecx
51
{
65
           mov [esi+block_flags], USED_BLOCK
-
 
66
           mov eax, [esi+block_size]
-
 
67
           sub [heap_free], eax
-
 
68
           ret
-
 
69
align 4
52
           mov edx, [op+list_fd]
70
.find_used:
53
           mov ecx, [op+list_bk]
71
           mov ecx, eax
-
 
72
           mov ebx, eax
-
 
73
           shr ebx, 6
-
 
74
           add ecx, ebx
-
 
75
           shr ebx, 6
-
 
76
           add ecx, ebx
-
 
77
           shr ecx, 12
-
 
78
           and ecx, 63
-
 
79
 
54
           mov [edx+list_bk], ecx
80
           lea ebx, [mem_used_list+ecx*8]
-
 
81
           mov esi, ebx
55
           mov [ecx+list_fd], edx
82
.next:
-
 
83
           mov esi, [esi+list_fd]
-
 
84
           cmp esi, ebx
-
 
85
           je .fail
56
           mov [op+list_fd], 0
86
 
-
 
87
           cmp eax, [esi+block_base]
-
 
88
           jne .next
-
 
89
 
-
 
90
           ret
-
 
91
.fail:
-
 
92
           xor esi, esi
-
 
93
           ret
-
 
94
 
-
 
95
align 4
-
 
96
.del_from_used:
-
 
97
           call .find_used
-
 
98
           test esi, esi
-
 
99
           jz .done
-
 
100
 
-
 
101
           cmp [esi+block_flags], USED_BLOCK
57
           mov [op+list_bk], 0
102
           jne .fatal
-
 
103
 
-
 
104
           dec [mem_hash_cnt+ecx*4]
-
 
105
           list_del esi
-
 
106
.done:
-
 
107
           ret
-
 
108
.fatal:                            ;FIXME panic here
-
 
109
           xor esi, esi
Line 58... Line 110...
58
}
110
           ret
59
 
111
 
60
;Initial heap state
112
;Initial heap state
61
;
113
;
Line 73... Line 125...
73
            mov eax, edi
125
            mov eax, edi
74
            stosd
126
            stosd
75
            stosd
127
            stosd
76
            loop @B
128
            loop @B
Line 77... Line 129...
77
 
129
 
78
            mov eax, mem_used.fd
130
            mov ecx, 64
-
 
131
            mov edi, mem_used_list
79
            mov [mem_used.fd], eax
132
@@:
-
 
133
            mov eax, edi
-
 
134
            stosd
-
 
135
            stosd
Line 80... Line 136...
80
            mov [mem_used.bk], eax
136
            loop @B
81
 
137
 
82
            stdcall alloc_pages, dword 32
138
            stdcall alloc_pages, dword 32
83
            mov ecx, 32
139
            mov ecx, 32
Line 138... Line 194...
138
            add eax, MEM_BLOCK.sizeof
194
            add eax, MEM_BLOCK.sizeof
139
            loop @B
195
            loop @B
Line 140... Line 196...
140
 
196
 
Line 141... Line -...
141
            mov [eax-MEM_BLOCK.sizeof], dword 0
-
 
142
 
197
            mov [eax-MEM_BLOCK.sizeof], dword 0
143
 
198
 
144
            mov ecx, heap_mutex
199
            mov ecx, heap_mutex
145
            call mutex_init
200
            call mutex_init
146
            mov [heap_blocks], 4094
201
            mov [heap_blocks], 4094
Line 280... Line 335...
280
           sub [edi+block_size], edx
335
           sub [edi+block_size], edx
Line 281... Line 336...
281
 
336
 
282
           mov eax, [edi+block_size]
337
           mov eax, [edi+block_size]
283
           calc_index eax
338
           calc_index eax
284
           cmp eax, [block_ind]
339
           cmp eax, [block_ind]
Line 285... Line 340...
285
           je .m_eq_ind
340
           je .add_used
Line 286... Line 341...
286
 
341
 
287
           list_del edi
342
           list_del edi
Line 293... Line 348...
293
           btr [mem_block_mask], ecx
348
           btr [mem_block_mask], ecx
294
@@:
349
@@:
295
           bts [mem_block_mask], eax
350
           bts [mem_block_mask], eax
296
           lea edx, [mem_block_list+eax*8]   ;edx= list head
351
           lea edx, [mem_block_list+eax*8]   ;edx= list head
297
           list_add edi, edx
352
           list_add edi, edx
298
.m_eq_ind:
353
.add_used:
299
           mov ecx, mem_used.fd
-
 
300
           mov edx, [ecx+list_fd]
-
 
301
           mov [esi+list_fd], edx
-
 
302
           mov [esi+list_bk], ecx
-
 
303
           mov [ecx+list_fd], esi
-
 
304
           mov [edx+list_bk], esi
-
 
305
 
-
 
306
           mov [esi+block_flags], USED_BLOCK
-
 
307
           mov ebx, [size]
-
 
308
           sub [heap_free], ebx
-
 
Line 309... Line -...
309
 
-
 
310
if HASH_IT
-
 
311
           pushad
-
 
312
           mov eax, [esi+block_base]
-
 
313
           mov ebx, [esi+block_base]
-
 
314
           shr ebx, 6
-
 
315
           add eax, ebx
-
 
316
           shr ebx, 6
-
 
317
           add eax, ebx
-
 
318
           shr eax, 12
-
 
319
           and eax, 63
354
 
320
           inc [mem_hash_cnt+eax*4]
-
 
321
           popad
-
 
Line 322... Line 355...
322
end if
355
           call md.add_to_used
323
 
356
 
324
           mov ecx, heap_mutex
357
           mov ecx, heap_mutex
325
           call mutex_unlock
358
           call mutex_unlock
326
           mov eax, [esi+block_base]
359
           mov eax, [esi+block_base]
327
           pop edi
360
           pop edi
328
           pop esi
361
           pop esi
-
 
362
           pop ebx
329
           pop ebx
363
           ret
330
           ret
364
 
331
.m_eq_size:
365
.m_eq_size:
332
           list_del edi
366
           list_del edi
333
           lea edx, [mem_block_list+ebx*8]
367
           lea edx, [mem_block_list+ebx*8]
334
           cmp edx, [edx]
368
           cmp edx, [edx]
335
           jnz @f
369
           jnz @f
336
           btr [mem_block_mask], ebx
-
 
337
@@:
-
 
338
           mov ecx, mem_used.fd
-
 
339
           mov edx, [ecx+list_fd]
-
 
340
           mov [edi+list_fd], edx
-
 
341
           mov [edi+list_bk], ecx
-
 
342
           mov [ecx+list_fd], edi
-
 
343
           mov [edx+list_bk], edi
-
 
344
 
370
           btr [mem_block_mask], ebx
345
           mov [edi+block_flags], USED_BLOCK
-
 
346
           mov ebx, [size]
-
 
347
           sub [heap_free], ebx
-
 
348
 
-
 
349
if HASH_IT
-
 
350
           pushad
-
 
351
           mov eax, [edi+block_base]
-
 
352
           mov ebx, [edi+block_base]
-
 
353
           shr ebx, 6
-
 
354
           add eax, ebx
371
@@:
355
           shr ebx, 6
-
 
356
           add eax, ebx
-
 
357
           shr eax, 12
-
 
358
           and eax, 63
-
 
359
           inc [mem_hash_cnt+eax*4]
-
 
360
           popad
-
 
361
end if
-
 
362
 
-
 
363
           mov ecx, heap_mutex
-
 
364
           call mutex_unlock
-
 
365
           mov eax, [edi+block_base]
-
 
366
           pop edi
-
 
367
           pop esi
-
 
Line 368... Line 372...
368
           pop ebx
372
           mov esi, edi
369
           ret
373
           jmp .add_used
370
 
374
 
371
.error_unlock:
375
.error_unlock:
Line 386... Line 390...
386
 
390
 
387
           mov ecx, heap_mutex
391
           mov ecx, heap_mutex
Line 388... Line 392...
388
           call mutex_lock
392
           call mutex_lock
389
 
-
 
390
           mov eax, [base]
-
 
391
           mov esi, [mem_used.fd]
-
 
392
@@:
-
 
Line 393... Line -...
393
           cmp esi, mem_used.fd
-
 
394
           je .fail
393
 
395
 
394
           mov eax, [base]
396
           cmp [esi+block_base], eax
-
 
397
           je .found
-
 
398
           mov esi, [esi+list_fd]
-
 
399
           jmp @b
395
 
Line 400... Line -...
400
.found:
-
 
401
           cmp [esi+block_flags], USED_BLOCK
-
 
402
           jne .fail
-
 
403
 
-
 
404
if HASH_IT
-
 
405
           pushad
-
 
406
           mov eax, [esi+block_base]
-
 
407
           mov ebx, [esi+block_base]
-
 
408
           shr ebx, 6
-
 
409
           add eax, ebx
-
 
410
           shr ebx, 6
-
 
411
           add eax, ebx
-
 
412
           shr eax, 12
-
 
413
           and eax, 63
396
           call md.del_from_used
414
           dec [mem_hash_cnt+eax*4]
397
           test esi, esi
Line 415... Line 398...
415
           popad
398
           jz .fail
416
end if
399
 
Line 441... Line 424...
441
.prev:
424
.prev:
442
           mov edi, [esi+block_prev]
425
           mov edi, [esi+block_prev]
443
           cmp [edi+block_flags], FREE_BLOCK
426
           cmp [edi+block_flags], FREE_BLOCK
444
           jne .insert
427
           jne .insert
Line 445... Line -...
445
 
-
 
446
           remove_from_used esi
-
 
447
 
428
 
448
           mov edx, [esi+block_next]
429
           mov edx, [esi+block_next]
449
           mov [edi+block_next], edx
430
           mov [edi+block_next], edx
Line 450... Line 431...
450
           mov [edx+block_prev], edi
431
           mov [edx+block_prev], edi
Line 479... Line 460...
479
           call mutex_unlock
460
           call mutex_unlock
480
           xor eax, eax
461
           xor eax, eax
481
           not eax
462
           not eax
482
           ret
463
           ret
483
.insert:
464
.insert:
484
           remove_from_used esi
-
 
485
           mov [esi+block_flags], FREE_BLOCK
465
           mov [esi+block_flags], FREE_BLOCK
486
           mov eax, [esi+block_size]
466
           mov eax, [esi+block_size]
487
           calc_index eax
467
           calc_index eax
488
           mov edi, esi
468
           mov edi, esi
489
           jmp .add_block
469
           jmp .add_block
Line 569... Line 549...
569
           ret
549
           ret
570
endp
550
endp
Line 571... Line 551...
571
 
551
 
572
align 4
552
align 4
-
 
553
proc kernel_free stdcall, base:dword
573
proc kernel_free stdcall, base:dword
554
 
Line 574... Line 555...
574
           push ebx esi
555
           push ebx esi
575
 
556
 
Line 576... Line 557...
576
           mov ecx, heap_mutex
557
           mov ecx, heap_mutex
577
           call mutex_lock
-
 
578
 
-
 
579
           mov eax, [base]
558
           call mutex_lock
580
           mov esi, [mem_used.fd]
-
 
Line 581... Line -...
581
@@:
-
 
582
           cmp esi, mem_used.fd
-
 
583
           je .fail
559
 
584
 
-
 
585
           cmp [esi+block_base], eax
-
 
586
           je .found
560
           mov eax, [base]
587
           mov esi, [esi+list_fd]
561
           call md.find_used
Line 588... Line 562...
588
           jmp @b
562