-
Notifications
You must be signed in to change notification settings - Fork 4
/
SAPNormalBlocks.pas
405 lines (309 loc) · 9.57 KB
/
SAPNormalBlocks.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{****************************************************************************************
SAPMM v1.01 /17.06.2013/
Normal blocks have the size from cMinMediumBlock to cMaxMediumBlock
Working with normal (medium) blocks
****************************************************************************************}
unit SAPNormalBlocks;
interface
{$Include SAPOptions.inc}
uses SAPDefs;
// Normal block allocation. Block is always 8-aligned
function AllocateNormalBlock(mm: PSAPThreadMM; size: LongWord): Pointer;
// Put block into free blocks list
procedure InsertIntoFreeList(mm: PSAPThreadMM; p: Pointer);
implementation
uses
SAPOSBlocks,
SAPList,
SAPErrCodes
;
//------------------------------------------------------------
// Allocation
function TryAllocateFreeBlock(mm: PSAPThreadMM; size: LongWord): Pointer;
forward;
function AllocateNormalBlock(mm: PSAPThreadMM; size: LongWord): Pointer;
var
os_block: PSAPOSBlock;
p: PSAPAllocatedBlock;
psize: ^ LongWord;
f: PSAPFreeBlock;
inx: LongWord;
begin
if size <= cMaxMediumBlock then
begin
inx := (size + cMediumBlockStep - 1) div cMediumBlockStep;
p := Pointer(mm.medium_table[inx]);
if p <> nil then
begin
UntieFreeBlock(mm.medium_table[inx], Pointer(p));
p.tags := p.tags + [sap_allocated];
Result := p;
Exit;
end;
end;
Result := TryAllocateFreeBlock(mm, size);
if Result <> nil then // success
Exit;
if size > cOSBlockSize - SizeOf(TSAPOSBlock) then
begin
// Allocate whole block directly from OS
os_block := SAPAllocateOSBlock(mm, size + SizeOf(TSAPOSBlock));
if os_block = nil then
Exit;
p := Pointer(LongWord(os_block) + SizeOf(TSAPOSBlock));
p.size := size; // to determine allocated size
p.tags := [sap_allocated, sap_os_block_first, sap_os_block_last];
p.magic := cMagic;
p.block_magic := cNormalBlockMagic;
p.block_mm := mm;
// Set the size at the end of the block
psize := Pointer(LongWord(p) + size - SizeOf(LongWord));
psize^ := size;
Result := p;
{$IFDEF SAP_STAT}
Dec(mm.no_get_normal);
Inc(mm.no_get_os);
{$ENDIF SAP_STAT}
Exit;
end;
// Allocate new OS block (of standard size), add it to free blocks list,
// then try to allocate needed normal block again
os_block := SAPAllocateOSBlock(mm, cOSBlockSize);
if os_block = nil then
Exit;
f := Pointer(LongWord(os_block) + SizeOf(TSAPOSBlock));
f.block.size := cOSBlockSize - SizeOf(TSAPOSBlock);
f.block.tags := [sap_os_block_first, sap_os_block_last];
f.block.magic := cMagic;
f.block.block_magic := cNormalBlockMagic;
f.block.block_mm := mm;
psize := Pointer(LongWord(f) + f.block.size - SizeOf(LongWord));
psize^ := f.block.size;
// Add to free blocks list
if mm.free_blocks = nil then
begin
f.next := f;
f.prev := f;
mm.free_blocks := f;
end
else begin
f.next := mm.free_blocks;
f.prev := mm.free_blocks.prev;
f.next.prev := f;
f.prev.next := f;
mm.free_blocks := f;
end;
// Try to allocate block again
Result := TryAllocateFreeBlock(mm, size);
end;
function TryAllocateFreeBlock(mm: PSAPThreadMM; size: LongWord): Pointer;
var
delta: LongWord;
x: PSAPFreeBlock;
p: PSAPAllocatedBlock;
psize: ^ LongWord;
begin
Result := nil;
if mm.free_blocks = nil then
Exit;
x := mm.free_blocks;
repeat
{$IFDEF SAP_STAT}
Inc(mm.no_alloc_find_cycles);
{$ENDIF SAP_STAT}
if x.block.size >= size then
begin
// Found suitable block
delta := x.block.size - size;
if delta < cMinMediumBlock then
begin
// Allocate whole block
x.block.tags := x.block.tags + [sap_allocated];
// untie from list
if x.next = x then // was the single block in list
mm.free_blocks := nil
else begin
x.next.prev := x.prev;
x.prev.next:= x.next;
mm.free_blocks := x.next; // change free blocks list start pointer for the next search
end;
// Should not set the size at the end of the block!
Result := x;
Exit;
end;
// cut a piece of block from the end, so that the list is not changed
p := Pointer(LongWord(x) + delta);
p.size := size;
p.tags := x.block.tags + [sap_allocated] - [sap_os_block_first];
p.magic := cMagic;
p.block_magic := cNormalBlockMagic;
p.block_mm := mm;
// Set the size at the end of the block
psize := Pointer(LongWord(p) + size - SizeOf(LongWord));
psize^ := size;
x.block.size := delta;
x.block.tags := x.block.tags - [sap_os_block_last];
psize := Pointer(LongWord(x) + delta - SizeOf(LongWord));
psize^ := delta;
if delta <= cMaxMediumBlock then
begin
UntieFreeBlock(mm.free_blocks, x);
TieFreeBlockToSizeList(mm, x, delta);
end;
Result := p;
Exit;
end;
x := x.next;
until x = mm.free_blocks;
end;
//--------------------------------------------------
// Deallocate
// Checks free block, returns 0 if everything is OK, otherwise return error code
function CheckFreeBlock(f: PSAPFreeBlock): Integer;
var
size: LongWord;
psize: ^ LongWord;
begin
// Checking alignment
if LongWord(f) and (cAlignment - 1) <> 0 then
begin
Result := cErrInvPtrAlignment; // pointer is not aligned at cAlignment
Exit;
end;
// Checking size
size := f.block.size;
if size and (cAlignment - 1) <> 0 then
begin
Result := cErrInvSizeAlignment; // size is not aligned at cAlignment
Exit;
end;
psize := Pointer(LongWord(f) + size - SizeOf(LongWord));
//TODO: should check the pointer here
if psize^ <> size then
begin
Result := cErrSizesAreDifferent; // sizes at the beginning and at the end of the blocks differ
Exit;
end;
if not (sap_allocated in f.block.tags) then
begin
if f.next = nil then
begin
Result := cErrDebugCheck1;
Exit;
end;
if f.prev = nil then
begin
Result := cErrDebugCheck2;
Exit;
end;
if f.next.prev <> f then
begin
Result := cErrDebugCheck3; // free blocks list error
Exit;
end;
if f.prev.next <> f then
begin
Result := cErrDebugCheck4; // free blocks list error
Exit;
end;
end;
Result := 0;
end;
//--------------------------------------------
// Returns free block, succeeding given block, or nil
function GetNextFreeBlock(f: PSAPFreeBlock): PSAPFreeBlock;
var
next: PSAPFreeBlock;
begin
Result := nil;
if sap_os_block_last in f.block.tags then
Exit;
next := Pointer(LongWord(f) + f.block.size);
if sap_allocated in next.block.tags then
Exit;
{$IFDEF SAP_CHECKMAGIC}
if CheckFreeBlock(next) <> 0 then
Exit;
{$ENDIF SAP_CHECKMAGIC}
Result := next;
end;
// Return free block, preceding given block, or nil
function GetPrevFreeBlock(f: PSAPFreeBlock): PSAPFreeBlock;
var
psize: ^ LongWord;
prev: PSAPFreeBlock;
begin
Result := nil;
if sap_os_block_first in f.block.tags then
Exit;
psize := Pointer(LongWord(f) - SizeOf(LongWord));
if psize^ and (cAlignment - 1) <> 0 then
Exit;
prev := Pointer(LongWord(f) - psize^);
if sap_allocated in prev.block.tags then
Exit;
{$IFDEF SAP_CHECKMAGIC}
if CheckFreeBlock(prev) <> 0 then
Exit;
{$ENDIF SAP_CHECKMAGIC}
Result := prev;
end;
procedure SetEndSize(f: PSAPFreeBlock); inline;
var
psize: ^LongWord;
begin
psize := Pointer(LongWord(f) + f.block.size - SizeOf(LongWord));
psize^ := f.block.size;
end;
procedure InsertIntoFreeList(mm: PSAPThreadMM; p: Pointer);
var
f: PSAPFreeBlock;
next: PSAPFreeBlock;
prev: PSAPFreeBlock;
pred_size: LongWord;
begin
f := Pointer(LongWord(p) - SizeOf(TSAPAllocatedBlock));
next := GetNextFreeBlock(f);
prev := GetPrevFreeBlock(f);
if (prev <> nil) and (next <> nil) then
begin
// 1. Insertion with both sides merge
// previous block is grown, next block is deleted from list
pred_size := prev.block.size;
Inc(prev.block.size, f.block.size + next.block.size);
prev.block.tags := prev.block.tags + next.block.tags * [sap_os_block_last];
SetEndSize(prev);
UntieFreeBlockFromSizeList(mm, next, next.block.size);
MoveFreeBlockToSizeList(mm, prev, pred_size);
SAPCheckAndFreeOSBlock(mm, prev);
Exit;
end;
if next <> nil then
begin
// 2. Merge with next block
// Block must be grown and inserted into the list, next block is deleted from list
Inc(f.block.size, next.block.size);
f.block.tags := f.block.tags - [sap_allocated] + next.block.tags * [sap_os_block_last];
UntieFreeBlockFromSizeList(mm, next, next.block.size);
TieFreeBlockToSizeList(mm, f, f.block.size);
SetEndSize(f);
SAPCheckAndFreeOSBlock(mm, f);
Exit;
end;
if prev <> nil then
begin
// 3. Merge with previous block
pred_size := prev.block.size;
Inc(prev.block.size, f.block.size);
prev.block.tags := prev.block.tags + f.block.tags * [sap_os_block_last];
SetEndSize(prev);
MoveFreeBlockToSizeList(mm, prev, pred_size);
SAPCheckAndFreeOSBlock(mm, prev);
Exit;
end;
// 4. Insertion without merge
f.block.tags := f.block.tags - [sap_allocated];
TieFreeBlockToSizeList(mm, f, f.block.size);
SAPCheckAndFreeOSBlock(mm, f);
end;
end.