Subversion Repositories Kolibri OS

Rev

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

Rev 321 Rev 357
Line 1... Line -...
1
HEAP_BASE equ 0x00C00000
-
 
2
;HEAP_SIZE equ 0x01000000
-
 
3
 
-
 
4
struc MEM_BLOCK
1
struc MEM_BLOCK
5
{  .next_block  dd ?
2
{  .next_block  dd ?
6
   .prev_block  dd ? ;+4
3
   .prev_block  dd ? ;+4
7
   .list_next   dd ? ;+8
4
   .list_fd     dd ? ;+8
8
   .list_prev   dd ? ;+12
5
   .list_bk     dd ? ;+12
9
   .base        dd ? ;+16
6
   .base        dd ? ;+16
10
   .size        dd ? ;+20
7
   .size        dd ? ;+20
11
   .flags       dd ? ;+24
8
   .flags       dd ? ;+24
12
   .handle      dd ? ;+28
9
   .handle      dd ? ;+28
13
}
10
}
Line -... Line 11...
-
 
11
 
14
 
12
MEM_LIST_OFFSET equ  8
15
FREE_BLOCK      equ  4
13
FREE_BLOCK      equ  4
Line 16... Line 14...
16
USED_BLOCK      equ  8
14
USED_BLOCK      equ  8
17
 
15
 
Line 22... Line 20...
22
MEM_BLOCK_SIZE equ 8*4
20
MEM_BLOCK_SIZE equ 8*4
Line 23... Line 21...
23
 
21
 
24
block_next   equ MEM_BLOCK.next_block
22
block_next   equ MEM_BLOCK.next_block
25
block_prev   equ MEM_BLOCK.prev_block
23
block_prev   equ MEM_BLOCK.prev_block
26
list_next    equ MEM_BLOCK.list_next
24
list_fd      equ MEM_BLOCK.list_fd
27
list_prev    equ MEM_BLOCK.list_prev
25
list_bk      equ MEM_BLOCK.list_bk
28
block_base   equ MEM_BLOCK.base
26
block_base   equ MEM_BLOCK.base
29
block_size   equ MEM_BLOCK.size
27
block_size   equ MEM_BLOCK.size
Line 30... Line 28...
30
block_flags  equ MEM_BLOCK.flags
28
block_flags  equ MEM_BLOCK.flags
Line 38... Line 36...
38
@@:
36
@@:
39
}
37
}
40
 
38
 
Line 41... Line 39...
41
macro remove_from_list op
39
macro remove_from_list op
42
{          mov edx, [op+list_next]
40
{          mov edx, [op+list_fd]
43
           mov ecx, [op+list_prev]
41
           mov ecx, [op+list_bk]
44
           test edx, edx
42
           test edx, edx
45
           jz @f
43
           jz @f
46
           mov [edx+list_prev], ecx
44
           mov [edx+list_bk], ecx
47
@@:
45
@@:
48
           test ecx, ecx
46
           test ecx, ecx
49
           jz @f
47
           jz @f
50
           mov [ecx+list_next], edx
48
           mov [ecx+list_fd], edx
51
@@:
49
@@:
52
           mov [op+list_next],0
50
           mov [op+list_fd],0
53
           mov [op+list_prev],0
51
           mov [op+list_bk],0
54
}
52
}
Line 55... Line 53...
55
 
53
 
56
macro remove_from_free op
54
macro remove_from_free op
57
{
55
{
Line 70... Line 68...
70
}
68
}
71
 
69
 
Line 72... Line 70...
72
macro remove_from_used op
70
macro remove_from_used op
73
{
71
{
-
 
72
           mov edx, [op+list_fd]
74
           remove_from_list op
73
           mov ecx, [op+list_bk]
75
           cmp [mem_used_list], op
74
           mov [edx+list_bk], ecx
-
 
75
           mov [ecx+list_fd], edx
76
           jne @f
76
           mov [op+list_fd], 0
77
           mov [mem_used_list], edx
77
           mov [op+list_bk], 0
78
@@:
-
 
79
}
78
}
Line 80... Line 79...
80
 
79
 
81
align 4
80
align 4
Line 95... Line 94...
95
           mov [mem_block_start], mem_block_map
94
           mov [mem_block_start], mem_block_map
96
           mov [mem_block_end], mem_block_map+512
95
           mov [mem_block_end], mem_block_map+512
97
           mov [mem_block_arr], HEAP_BASE
96
           mov [mem_block_arr], HEAP_BASE
Line -... Line 97...
-
 
97
 
-
 
98
           mov eax, mem_used.fd-MEM_LIST_OFFSET
-
 
99
           mov [mem_used.fd], eax
-
 
100
           mov [mem_used.bk], eax
98
 
101
 
99
           stdcall alloc_pages, dword 32
102
           stdcall alloc_pages, dword 32
100
           mov ecx, 32
103
           mov ecx, 32
101
           mov edx, eax
104
           mov edx, eax
102
           mov edi, HEAP_BASE
105
           mov edi, HEAP_BASE
Line 107... Line 110...
107
           dec ecx
110
           dec ecx
108
           jnz .l1
111
           jnz .l1
109
 
112
 
Line 110... Line 113...
110
           mov edi, HEAP_BASE
113
           mov edi, HEAP_BASE
111
           mov ebx, edi
-
 
112
           add ebx, MEM_BLOCK_SIZE
114
           mov ebx, HEAP_BASE+MEM_BLOCK_SIZE
113
           xor eax, eax
115
           xor eax, eax
114
           mov [edi+block_next], ebx
116
           mov [edi+block_next], ebx
115
           mov [edi+block_prev], eax
117
           mov [edi+block_prev], eax
116
           mov [edi+list_next], eax
118
           mov [edi+list_fd], eax
117
           mov [edi+list_prev], eax
119
           mov [edi+list_bk], eax
118
           mov [edi+block_base], HEAP_BASE
120
           mov [edi+block_base], HEAP_BASE
119
           mov [edi+block_size], 4096*MEM_BLOCK_SIZE
121
           mov [edi+block_size], 4096*MEM_BLOCK_SIZE
120
           mov [edi+block_flags], USED_BLOCK
122
           mov [edi+block_flags], USED_BLOCK
Line 121... Line 123...
121
 
123
 
122
           mov [ebx+block_next], eax
124
           mov [ebx+block_next], eax
-
 
125
           mov [ebx+block_prev], eax
123
           mov [ebx+block_prev], eax
126
           mov [ebx+list_fd], eax
124
           mov [ebx+list_next], eax
127
           mov [ebx+list_bk], eax
Line 125... Line 128...
125
           mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
128
           mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
126
 
129
 
127
           mov ecx, [MEM_AMOUNT]
130
           mov ecx, [MEM_AMOUNT]
Line 133... Line 136...
133
 
136
 
Line 134... Line 137...
134
           mov [mem_block_mask], eax
137
           mov [mem_block_mask], eax
135
           mov [mem_block_mask+4],0x80000000
138
           mov [mem_block_mask+4],0x80000000
Line 136... Line -...
136
 
-
 
137
           mov [mem_used_list], eax
139
 
138
           mov [mem_block_list+63*4], ebx
140
           mov [mem_block_list+63*4], ebx
139
           mov byte [mem_block_map], 0xFC
141
           mov byte [mem_block_map], 0xFC
140
           and [heap_mutex], 0
142
           and [heap_mutex], 0
141
           mov [heap_blocks], 4095
143
           mov [heap_blocks], 4095
Line 218... Line 220...
218
           ret
220
           ret
219
endp
221
endp
220
 
222
 
Line 221... Line 223...
221
proc free_mem_block
223
proc free_mem_block
-
 
224
           mov dword [eax], 0
-
 
225
           mov dword [eax+4], 0
-
 
226
           mov dword [eax+8], 0
-
 
227
           mov dword [eax+12], 0
-
 
228
           mov dword [eax+16], 0
-
 
229
;           mov dword [eax+20], 0
-
 
230
           mov dword [eax+24], 0
-
 
231
           mov dword [eax+28], 0
-
 
232
 
222
           sub eax, [mem_block_arr]
233
           sub eax, [mem_block_arr]
223
           shr eax, 5
234
           shr eax, 5
Line 224... Line 235...
224
 
235
 
225
           mov ebx, mem_block_map
236
           mov ebx, mem_block_map
Line 283... Line 294...
283
           mov [esi+block_next], edi
294
           mov [esi+block_next], edi
284
           mov eax, [edi+block_prev]
295
           mov eax, [edi+block_prev]
285
           mov [esi+block_prev], eax
296
           mov [esi+block_prev], eax
286
           mov [edi+block_prev], esi
297
           mov [edi+block_prev], esi
287
           mov [esi+list_next], 0
298
           mov [esi+list_fd], 0
288
           mov [esi+list_prev], 0
299
           mov [esi+list_bk], 0
289
           and eax, eax
300
           and eax, eax
290
           jz @f
301
           jz @f
291
           mov [eax+block_next], esi
302
           mov [eax+block_next], esi
292
@@:
303
@@:
293
           mov ebx, [edi+block_base]
304
           mov ebx, [edi+block_base]
Line 316... Line 327...
316
           jnz @f
327
           jnz @f
317
           btr [mem_block_mask], ecx
328
           btr [mem_block_mask], ecx
318
@@:
329
@@:
319
           mov edx, [mem_block_list+eax*4]
330
           mov edx, [mem_block_list+eax*4]
320
           mov [edi+list_next], edx
331
           mov [edi+list_fd], edx
321
           test edx, edx
332
           test edx, edx
322
           jz @f
333
           jz @f
323
           mov [edx+list_prev], edi
334
           mov [edx+list_bk], edi
324
@@:
335
@@:
325
           mov [mem_block_list+eax*4], edi
336
           mov [mem_block_list+eax*4], edi
326
           bts [mem_block_mask], eax
337
           bts [mem_block_mask], eax
327
.m_eq_ind:
338
.m_eq_ind:
328
           mov ebx, [mem_used_list]
339
           mov ecx, mem_used.fd-MEM_LIST_OFFSET
-
 
340
           mov edx, [ecx+list_fd]
329
           mov [esi+list_next], ebx
341
           mov [esi+list_fd], edx
330
           test ebx, ebx
342
           mov [esi+list_bk], ecx
331
           jz @f
343
           mov [ecx+list_fd], esi
332
           mov [ebx+list_prev], esi
344
           mov [edx+list_bk], esi
333
@@:
345
 
334
           mov [esi+block_flags], USED_BLOCK
346
           mov [esi+block_flags], USED_BLOCK
335
           mov [mem_used_list], esi
347
           mov eax, [esi+block_base]
336
           mov eax, [esi+block_base]
-
 
337
           mov ebx, [size]
348
           mov ebx, [size]
338
           sub [heap_free], ebx
349
           sub [heap_free], ebx
339
           and [heap_mutex], 0
350
           and [heap_mutex], 0
340
           ret
351
           ret
341
.m_eq_size:
352
.m_eq_size:
Line 344... Line 355...
344
           and edx, edx
355
           and edx, edx
345
           jnz @f
356
           jnz @f
346
           btr [mem_block_mask], ebx
357
           btr [mem_block_mask], ebx
347
@@:
358
@@:
348
           mov ecx, [mem_used_list]
359
           mov ecx, mem_used.fd-MEM_LIST_OFFSET
349
           mov [edi+list_next], ecx
360
           mov edx, [ecx+list_fd]
350
           test ecx, ecx
361
           mov [edi+list_fd], edx
351
           jnz @f
362
           mov [edi+list_bk], ecx
352
           mov [ecx+list_prev], edi
363
           mov [ecx+list_fd], edi
353
@@:
364
           mov [edx+list_bk], edi
354
           mov [mem_used_list], edi
-
 
355
           mov [edi+block_flags], USED_BLOCK
365
 
-
 
366
           mov [edi+block_flags], USED_BLOCK
356
           mov eax, [edi+block_base]
367
           mov eax, [edi+block_base]
357
           mov ebx, [size]
368
           mov ebx, [size]
358
           sub [heap_free], ebx
369
           sub [heap_free], ebx
359
           and [heap_mutex], 0
370
           and [heap_mutex], 0
360
           ret
371
           ret
Line 370... Line 381...
370
           mov ebx, heap_mutex
381
           mov ebx, heap_mutex
371
           call wait_mutex    ;ebx
382
           call wait_mutex    ;ebx
Line 372... Line 383...
372
 
383
 
373
           mov eax, [base]
384
           mov eax, [base]
374
           mov esi, [mem_used_list]
385
           mov esi, [mem_used.fd]
375
@@:
386
@@:
376
           test esi, esi
387
           cmp esi, mem_used.fd-MEM_LIST_OFFSET
Line 377... Line 388...
377
           jz .fail
388
           je .fail
378
 
389
 
379
           cmp [esi+block_base], eax
390
           cmp [esi+block_base], eax
380
           je .found
391
           je .found
381
           mov esi, [esi+list_next]
392
           mov esi, [esi+list_fd]
382
           jmp @b
393
           jmp @b
383
.found:
394
.found:
Line 450... Line 461...
450
           btr [mem_block_mask], ecx
461
           btr [mem_block_mask], ecx
451
@@:
462
@@:
452
           mov esi, [mem_block_list+eax*4]
463
           mov esi, [mem_block_list+eax*4]
453
           mov [mem_block_list+eax*4], edi
464
           mov [mem_block_list+eax*4], edi
454
           mov [edi+list_next], esi
465
           mov [edi+list_fd], esi
455
           test esi, esi
466
           test esi, esi
456
           jz @f
467
           jz @f
457
           mov [esi+list_prev], edi
468
           mov [esi+list_bk], edi
458
@@:
469
@@:
459
           bts [mem_block_mask], eax
470
           bts [mem_block_mask], eax
460
.m_eq:
471
.m_eq:
461
           xor eax, eax
472
           xor eax, eax
462
           mov [heap_mutex], eax
473
           mov [heap_mutex], eax
463
           dec eax
474
           dec eax
Line 469... Line 480...
469
           calc_index eax
480
           calc_index eax
470
 
481
 
Line 471... Line 482...
471
           mov edi, [mem_block_list+eax*4]
482
           mov edi, [mem_block_list+eax*4]
472
           mov [mem_block_list+eax*4], esi
483
           mov [mem_block_list+eax*4], esi
473
           mov [esi+list_next], edi
484
           mov [esi+list_fd], edi
474
           test edi, edi
485
           test edi, edi
475
           jz @f
486
           jz @f
476
           mov [edi+list_prev], esi
487
           mov [edi+list_bk], esi
477
@@:
488
@@:
478
           bts [mem_block_mask], eax
489
           bts [mem_block_mask], eax
479
           mov [esi+block_flags],FREE_BLOCK
490
           mov [esi+block_flags],FREE_BLOCK
480
           xor eax, eax
491
           xor eax, eax
481
           mov [heap_mutex], eax
492
           mov [heap_mutex], eax
Line 498... Line 509...
498
           add eax, 4095
509
           add eax, 4095
499
           and eax, not 4095;
510
           and eax, not 4095;
500
           mov [size], eax
511
           mov [size], eax
501
           and eax, eax
512
           and eax, eax
502
           jz .error
513
           jz .err
503
           mov ebx, eax
514
           mov ebx, eax
504
           shr ebx, 12
515
           shr ebx, 12
505
           mov [pages_count], ebx
516
           mov [pages_count], ebx
506
 
517
 
Line 507... Line 518...
507
           stdcall alloc_kernel_space, eax
518
           stdcall alloc_kernel_space, eax
508
           and eax, eax
519
           test eax, eax
509
           jz .error
520
           jz .err
510
           mov [lin_addr], eax
521
           mov [lin_addr], eax
Line 511... Line 522...
511
 
522
 
512
           mov ecx, [pages_count]
523
           mov ecx, [pages_count]
513
           mov edx, eax
524
           mov edx, eax
Line 520... Line 531...
520
           push ebx
531
           push ebx
521
           stdcall alloc_pages, ebx
532
           stdcall alloc_pages, ebx
522
           pop ecx                   ; yes ecx!!!
533
           pop ecx                   ; yes ecx!!!
523
           and eax, eax
534
           and eax, eax
524
           jz .error
535
           jz .err
525
 
536
 
Line 526... Line 537...
526
           mov edi, eax
537
           mov edi, eax
527
           mov edx, [lin_addr]
538
           mov edx, [lin_addr]
528
@@:
539
@@:
529
           stdcall map_page,edx,edi,dword PG_SW
540
           stdcall map_page,edx,edi,dword PG_SW
Line 534... Line 545...
534
.next:
545
.next:
535
           mov ecx, [pages_count]
546
           mov ecx, [pages_count]
536
           and ecx, 7
547
           and ecx, 7
537
           jz .end
548
           jz .end
538
 
549
@@:
539
@@:        push ecx
550
           push ecx
540
           call alloc_page
551
           call alloc_page
541
           pop ecx
552
           pop ecx
542
           test eax, eax
553
           test eax, eax
543
           jz .error
554
           jz .err
544
 
555
 
Line 545... Line 556...
545
           stdcall map_page,edx,eax,dword PG_SW
556
           stdcall map_page,edx,eax,dword PG_SW
546
           add edx, 0x1000
557
           add edx, 0x1000
547
           dec ecx
558
           dec ecx
548
           jnz @B
559
           jnz @B
549
.end:
560
.end:
550
           mov eax, [lin_addr]
561
           mov eax, [lin_addr]
551
           ret
562
           ret
552
.error:
563
.err:
553
           xor eax, eax
564
           xor eax, eax
554
           ret
565
           ret
555
endp
566
endp
Line 556... Line 567...
556
 
567
 
Line 561... Line 572...
561
           mov ebx, heap_mutex
572
           mov ebx, heap_mutex
562
           call wait_mutex    ;ebx
573
           call wait_mutex    ;ebx
Line 563... Line 574...
563
 
574
 
564
           mov eax, [base]
575
           mov eax, [base]
565
           mov esi, [mem_used_list]
576
           mov esi, [mem_used.fd]
566
@@:
577
@@:
567
           test esi, esi
578
           cmp esi, mem_used.fd-MEM_LIST_OFFSET
Line 568... Line 579...
568
           jz .fail
579
           je .fail
569
 
580
 
570
           cmp [esi+block_base], eax
581
           cmp [esi+block_base], eax
571
           je .found
582
           je .found
572
           mov esi, [esi+list_next]
583
           mov esi, [esi+list_fd]
573
           jmp @b
584
           jmp @b
574
.found:
585
.found: