Subversion Repositories Kolibri OS

Rev

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

Rev 2143 Rev 2151
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 -... Line 8...
-
 
8
$Revision: 2151 $
Line 8... Line 9...
8
$Revision: 2143 $
9
 
9
 
10
HASH_IT  = 1
10
 
11
 
11
struc MEM_BLOCK
12
struc MEM_BLOCK
12
{
13
{
13
   .list        LHEAD
14
   .list        LHEAD
14
   .next_block  dd ? ;+8
15
   .next_block  dd ? ;+8
15
   .prev_block  dd ? ;+4
16
   .prev_block  dd ? ;+4
16
   .base        dd ? ;+16
17
   .base        dd ? ;+16
-
 
18
   .size        dd ? ;+20
17
   .size        dd ? ;+20
19
   .flags       dd ? ;+24
Line 18... Line 20...
18
   .flags       dd ? ;+24
20
   .handle      dd ? ;+28
19
   .handle      dd ? ;+28
21
   .sizeof:
20
}
22
}
Line 21... Line 23...
21
 
23
 
22
FREE_BLOCK      equ  4
24
FREE_BLOCK      equ  4
23
USED_BLOCK      equ  8
25
USED_BLOCK      equ  8
Line 24... Line -...
24
DONT_FREE_BLOCK equ  10h
-
 
Line 25... Line 26...
25
 
26
DONT_FREE_BLOCK equ  10h
26
virtual at 0
27
 
27
  MEM_BLOCK MEM_BLOCK
28
virtual at 0
28
end virtual
29
  MEM_BLOCK MEM_BLOCK
Line 57... Line 58...
57
}
58
}
Line 58... Line 59...
58
 
59
 
59
;Initial heap state
60
;Initial heap state
60
;
61
;
61
;+heap_size               terminator        USED_BLOCK
62
;+heap_size               terminator        USED_BLOCK
62
;+4096*MEM_BLOCK_SIZE   free space        FREE_BLOCK
63
;+4096*MEM_BLOCK.sizeof   free space        FREE_BLOCK
63
;HEAP_BASE                heap_descriptors  USED_BLOCK
64
;HEAP_BASE                heap_descriptors  USED_BLOCK
Line 64... Line 65...
64
;
65
;
65
 
66
 
Line 72... Line 73...
72
            mov eax, edi
73
            mov eax, edi
73
            stosd
74
            stosd
74
            stosd
75
            stosd
75
            loop @B
76
            loop @B
Line 76... Line -...
76
 
-
 
77
            mov ecx, 512/4
-
 
78
            mov edi, mem_block_map
-
 
79
            not eax
-
 
80
            rep stosd
-
 
81
 
-
 
82
            mov [mem_block_start], mem_block_map
-
 
83
            mov [mem_block_end], mem_block_map+512
-
 
84
            mov [mem_block_arr], HEAP_BASE
-
 
85
 
77
 
86
            mov eax, mem_used.fd
78
            mov eax, mem_used.fd
87
            mov [mem_used.fd], eax
79
            mov [mem_used.fd], eax
Line 88... Line 80...
88
            mov [mem_used.bk], eax
80
            mov [mem_used.bk], eax
Line 97... Line 89...
97
            add edx, 0x1000
89
            add edx, 0x1000
98
            dec ecx
90
            dec ecx
99
            jnz .l1
91
            jnz .l1
Line 100... Line 92...
100
 
92
 
101
            mov edi, HEAP_BASE                     ;descriptors
93
            mov edi, HEAP_BASE                     ;descriptors
102
            mov ebx, HEAP_BASE+MEM_BLOCK_SIZE      ;free space
94
            mov ebx, HEAP_BASE+MEM_BLOCK.sizeof      ;free space
Line 103... Line 95...
103
            mov ecx, HEAP_BASE+MEM_BLOCK_SIZE*2    ;terminator
95
            mov ecx, HEAP_BASE+MEM_BLOCK.sizeof*2    ;terminator
104
 
96
 
105
            xor eax, eax
97
            xor eax, eax
106
            mov [edi+block_next], ebx
98
            mov [edi+block_next], ebx
107
            mov [edi+block_prev], eax
99
            mov [edi+block_prev], eax
108
            mov [edi+list_fd], eax
100
            mov [edi+list_fd], eax
109
            mov [edi+list_bk], eax
101
            mov [edi+list_bk], eax
110
            mov [edi+block_base], HEAP_BASE
102
            mov [edi+block_base], HEAP_BASE
Line 111... Line 103...
111
            mov [edi+block_size], 4096*MEM_BLOCK_SIZE
103
            mov [edi+block_size], 4096*MEM_BLOCK.sizeof
112
            mov [edi+block_flags], USED_BLOCK
104
            mov [edi+block_flags], USED_BLOCK
113
 
105
 
114
            mov [ecx+block_next], eax
106
            mov [ecx+block_next], eax
115
            mov [ecx+block_prev], ebx
107
            mov [ecx+block_prev], ebx
116
            mov [edi+list_fd], eax
108
            mov [edi+list_fd], eax
117
            mov [edi+list_bk], eax
109
            mov [edi+list_bk], eax
Line 118... Line 110...
118
            mov [edi+block_base], 0
110
            mov [edi+block_base], eax
119
            mov [edi+block_size], 0
111
            mov [edi+block_size], eax
120
            mov [edi+block_flags], USED_BLOCK
-
 
121
 
-
 
122
            mov [ebx+block_next], ecx
112
            mov [edi+block_flags], USED_BLOCK
Line 123... Line 113...
123
            mov [ebx+block_prev], edi
113
 
124
            mov [ebx+list_fd], eax
114
            mov [ebx+block_next], ecx
125
            mov [ebx+list_bk], eax
115
            mov [ebx+block_prev], edi
126
            mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK_SIZE
116
            mov [ebx+block_base], HEAP_BASE+4096*MEM_BLOCK.sizeof
127
 
117
 
128
            mov ecx, [pg_data.kernel_pages]
118
            mov ecx, [pg_data.kernel_pages]
129
            shl ecx, 12
119
            shl ecx, 12
Line 137... Line 127...
137
            mov [mem_block_mask+4],0x80000000
127
            mov [mem_block_mask+4],0x80000000
Line 138... Line 128...
138
 
128
 
139
            mov ecx, mem_block_list+63*8
129
            mov ecx, mem_block_list+63*8
Line -... Line 130...
-
 
130
            list_add ebx, ecx
-
 
131
 
-
 
132
            mov ecx, 4096-3-1
-
 
133
            mov eax, HEAP_BASE+MEM_BLOCK.sizeof*4
-
 
134
 
140
            list_add ebx, ecx
135
            mov [next_memblock], HEAP_BASE+MEM_BLOCK.sizeof*3
-
 
136
@@:
-
 
137
            mov [eax-MEM_BLOCK.sizeof], eax
-
 
138
            add eax, MEM_BLOCK.sizeof
-
 
139
            loop @B
-
 
140
 
-
 
141
            mov [eax-MEM_BLOCK.sizeof], dword 0
141
 
142
 
142
            mov byte [mem_block_map], 0xFC
143
 
143
            mov ecx, heap_mutex
144
            mov ecx, heap_mutex
144
            call mutex_init
145
            call mutex_init
145
            mov [heap_blocks], 4095
146
            mov [heap_blocks], 4094
146
            mov [free_blocks], 4094
147
            mov [free_blocks], 4093
Line 147... Line 148...
147
            ret
148
            ret
148
endp
149
endp
Line 199... Line 200...
199
           jae .err
200
           jae .err
200
           add ebx, 32
201
           add ebx, 32
201
           mov edx, [esi]
202
           mov edx, [esi]
202
           jmp .find
203
           jmp .find
Line 203... Line -...
203
 
-
 
204
align 4
-
 
205
alloc_mem_block:
-
 
206
 
-
 
207
           mov ebx, [mem_block_start]
-
 
208
           mov ecx, [mem_block_end]
-
 
209
.l1:
-
 
210
           bsf eax,[ebx];
-
 
211
           jnz found
-
 
212
           add ebx,4
-
 
213
           cmp ebx, ecx
-
 
214
           jb .l1
-
 
215
           xor eax,eax
-
 
216
           ret
-
 
217
 
-
 
218
found:
-
 
219
           btr [ebx], eax
-
 
220
           mov [mem_block_start],ebx
-
 
221
           sub ebx, mem_block_map
-
 
222
           lea eax,[eax+ebx*8]
-
 
223
           shl eax, 5
-
 
224
           add eax, [mem_block_arr]
-
 
225
           dec [free_blocks]
-
 
Line 226... Line 204...
226
           ret
204
 
227
 
205
 
228
align 4
-
 
229
free_mem_block:
206
align 4
230
           mov dword [eax], 0
207
free_mem_block:
231
           mov dword [eax+4], 0
-
 
232
           mov dword [eax+8], 0
-
 
233
           mov dword [eax+12], 0
-
 
234
           mov dword [eax+16], 0
-
 
235
;           mov dword [eax+20], 0
-
 
236
           mov dword [eax+24], 0
-
 
237
           mov dword [eax+28], 0
208
           mov ebx, [next_memblock]
238
 
209
           mov [eax], ebx
Line 239... Line 210...
239
           sub eax, [mem_block_arr]
210
           mov [next_memblock], eax
240
           shr eax, 5
211
           xor ebx, ebx
-
 
212
 
-
 
213
           mov dword [eax+4], ebx
-
 
214
           mov dword [eax+8], ebx
-
 
215
           mov dword [eax+12], ebx
-
 
216
           mov dword [eax+16], ebx
241
 
217
;           mov dword [eax+20], 0     ;don't clear block size
242
           mov ebx, mem_block_map
-
 
243
           bts [ebx], eax
-
 
244
           inc [free_blocks]
-
 
245
           shr eax, 3
-
 
246
           and eax, not 3
-
 
247
           add eax, ebx
-
 
248
           cmp [mem_block_start], eax
-
 
249
           ja @f
-
 
250
           ret
-
 
251
@@:
-
 
252
           mov [mem_block_start], eax
-
 
253
           ret
218
           mov dword [eax+24], ebx
Line 254... Line 219...
254
.err:
219
           mov dword [eax+28], ebx
255
           xor eax, eax
220
           inc [free_blocks]
256
           ret
221
           ret
Line 289... Line 254...
289
 
254
 
290
           mov eax, [edi+block_size]
255
           mov eax, [edi+block_size]
291
           cmp eax, [size]
256
           cmp eax, [size]
Line 292... Line 257...
292
           je .m_eq_size
257
           je .m_eq_size
293
 
258
 
294
           call alloc_mem_block
259
           mov esi, [next_memblock]     ;new memory block
Line -... Line 260...
-
 
260
           test esi, esi
-
 
261
           jz .error_unlock
295
           and eax, eax
262
 
Line 296... Line 263...
296
           jz .error_unlock
263
           dec [free_blocks]
297
 
264
           mov eax, [esi]
298
           mov esi, eax           ;esi - splitted block
265
           mov [next_memblock], eax
299
 
266
 
Line 338... Line 305...
338
 
305
 
339
           mov [esi+block_flags], USED_BLOCK
306
           mov [esi+block_flags], USED_BLOCK
340
           mov ebx, [size]
307
           mov ebx, [size]
Line -... Line 308...
-
 
308
           sub [heap_free], ebx
341
           sub [heap_free], ebx
309
 
342
 
310
if HASH_IT
343
;           pushad
311
           pushad
344
;           mov eax, [esi+block_base]
312
           mov eax, [esi+block_base]
345
;           mov ebx, [esi+block_base]
313
           mov ebx, [esi+block_base]
346
;           shr ebx, 6
314
           shr ebx, 6
347
;           add eax, ebx
315
           add eax, ebx
348
;           shr ebx, 6
316
           shr ebx, 6
349
;           add eax, ebx
317
           add eax, ebx
350
;           shr eax, 12
318
           shr eax, 12
351
;           and eax, 63
319
           and eax, 63
-
 
320
           inc [mem_hash_cnt+eax*4]
Line 352... Line 321...
352
;           inc [mem_hash_cnt+eax*4]
321
           popad
353
;           popad
322
end if
354
 
323
 
355
           mov ecx, heap_mutex
324
           mov ecx, heap_mutex
Line 375... Line 344...
375
 
344
 
376
           mov [edi+block_flags], USED_BLOCK
345
           mov [edi+block_flags], USED_BLOCK
377
           mov ebx, [size]
346
           mov ebx, [size]
Line -... Line 347...
-
 
347
           sub [heap_free], ebx
378
           sub [heap_free], ebx
348
 
379
 
349
if HASH_IT
380
;           pushad
350
           pushad
381
;           mov eax, [edi+block_base]
351
           mov eax, [edi+block_base]
382
;           mov ebx, [edi+block_base]
352
           mov ebx, [edi+block_base]
383
;           shr ebx, 6
353
           shr ebx, 6
384
;           add eax, ebx
354
           add eax, ebx
385
;           shr ebx, 6
355
           shr ebx, 6
386
;           add eax, ebx
356
           add eax, ebx
387
;           shr eax, 12
357
           shr eax, 12
388
;           and eax, 63
358
           and eax, 63
-
 
359
           inc [mem_hash_cnt+eax*4]
Line 389... Line 360...
389
;           inc [mem_hash_cnt+eax*4]
360
           popad
390
;           popad
361
end if
391
 
362
 
392
           mov ecx, heap_mutex
363
           mov ecx, heap_mutex
Line 428... Line 399...
428
           jmp @b
399
           jmp @b
429
.found:
400
.found:
430
           cmp [esi+block_flags], USED_BLOCK
401
           cmp [esi+block_flags], USED_BLOCK
431
           jne .fail
402
           jne .fail
Line 432... Line 403...
432
 
403
 
433
 
404
if HASH_IT
434
;           pushad
405
           pushad
435
;           mov eax, [esi+block_base]
406
           mov eax, [esi+block_base]
436
;           mov ebx, [esi+block_base]
407
           mov ebx, [esi+block_base]
437
;           shr ebx, 6
408
           shr ebx, 6
438
;           add eax, ebx
409
           add eax, ebx
439
;           shr ebx, 6
410
           shr ebx, 6
440
;           add eax, ebx
411
           add eax, ebx
441
;           shr eax, 12
412
           shr eax, 12
442
;           and eax, 63
413
           and eax, 63
443
;           dec [mem_hash_cnt+eax*4]
414
           dec [mem_hash_cnt+eax*4]
444
;           popad
415
           popad
445
 
416
end if
446
           mov eax, [esi+block_size]
417
           mov eax, [esi+block_size]
Line 447... Line 418...
447
           add [heap_free], eax
418
           add [heap_free], eax
448
 
419