-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSAPSmallBlocks.pas
244 lines (183 loc) · 6.3 KB
/
SAPSmallBlocks.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
{****************************************************************************************
SAPMM v1.01 /17.06.2013/
Working with small blocks.
Small blocks have the size <= cMaxSmallBlockSize.
Small blocks are arranged in size lists with a step of cSmallBlockStep
Small blocks are allocated by groups ("pieces"), each time group grows twice in size,
to the limit of cSmallBlocksMaxPieceSize bytes in group
****************************************************************************************}
unit SAPSmallBlocks;
interface
{$Include SAPOptions.inc}
uses SAPDefs;
function AllocateSmallBlock(mm: PSAPThreadMM; size: LongWord): PSAPSmallAllocatedBlock; inline;
procedure FreeSmallBlock(mm: PSAPThreadMM; block: PSAPSmallAllocatedBlock); inline;
procedure FreeSmallBlocks(mm: PSAPThreadMM; h: PSAPSmallBlocksHeader);
//----------------------------------
function AllocateSmallBlocks(mm: PSAPThreadMM; inx: LongWord): PSAPSmallBlocksHeader;
procedure InitSmallBlocksTable(mm: PSAPThreadMM);
implementation
uses SAPNormalBlocks, SAPErrCodes;
//function AllocateSmallBlocks(mm: PSAPThreadMM; inx: LongWord): PSAPSmallBlocksHeader;
//forward;
procedure InitSmallBlocksTable(mm: PSAPThreadMM);
var
i: Integer;
block_size: LongWord;
begin
for i := Low(mm.small_table) to High(mm.small_table) do
begin
block_size := (i + 1) * cSmallBlockStep;
mm.small_table[i].block_size := block_size;
mm.small_table[i].max_blocks_in_piece :=
(cSmallBlocksMaxPieceSize - SizeOf(TSAPSmallBlocksHeader)) div block_size;
if block_size <= 128 then
mm.small_table[i].blocks_in_piece := 256
else
mm.small_table[i].blocks_in_piece := 64
;
end;
end;
function AllocateSmallBlock(mm: PSAPThreadMM; size: LongWord): PSAPSmallAllocatedBlock;
var
inx: Integer;
h: PSAPSmallBlocksHeader;
f: PSAPSmallFreeBlock;
begin
inx := (size + SizeOf(TSAPSmallAllocatedBlock)) div cSmallBlockStep;
h := mm.small_table[inx].headers;
if h = nil then
begin
h := AllocateSmallBlocks(mm, inx);
if h = nil then
begin
Result := nil;
Exit;
end;
end;
f := h.blocks;
h.blocks := f.next;
Dec(h.free_count);
if h.blocks = nil then
begin
// assert(mm.small_table[inx] = h);
// delete from list
mm.small_table[inx].headers := h.next;
if h.next <> nil then
h.next.prev := nil;
h.next := nil;
h.prev := nil;
end;
f.block.tags := f.block.tags + [sap_allocated];
Result := PSAPSmallAllocatedBlock(f);
end;
function AllocateSmallBlocks(mm: PSAPThreadMM; inx: LongWord): PSAPSmallBlocksHeader;
var
block_size: LongWord;
blocks_in_piece: LongWord;
size: LongWord;
p: Pointer;
h: PSAPSmallBlocksHeader;
x: PSAPSmallFreeBlock;
i: Integer;
begin
Result := nil;
block_size := (inx + 1) * cSmallBlockStep;
blocks_in_piece := mm.small_table[inx].blocks_in_piece;
size :=
( block_size * blocks_in_piece // space for the blocks
+ SizeOf(TSAPSmallBlocksHeader) // small block header
+ SizeOf(TSAPAllocatedBlock) // allocated block header
+ SizeOf(LongWord) // size at the end of the block
+ (cNormalAlignment - 1) // alignment
) and not (cNormalAlignment - 1);
p := AllocateNormalBlock(mm, size);
if p = nil then
Exit;
// Recalc for the next allocation
if blocks_in_piece < mm.small_table[inx].max_blocks_in_piece then
begin
mm.small_table[inx].blocks_in_piece := blocks_in_piece * 2;
if mm.small_table[inx].blocks_in_piece > mm.small_table[inx].max_blocks_in_piece then
mm.small_table[inx].blocks_in_piece := mm.small_table[inx].max_blocks_in_piece;
end;
PSAPAllocatedBlock(p).tags := PSAPAllocatedBlock(p).tags + [sap_small_blocks_piece];
h := Pointer(LongWord(p) + SizeOf(TSAPAllocatedBlock));
h.header_magic := cSmallBlockHeaderMagic;
h.block_mm := mm;
h.block_size := block_size;
h.blocks_no := blocks_in_piece;
h.free_count := blocks_in_piece;
h.blocks := nil;
h.prev := nil;
h.next := mm.small_table[inx].headers;
mm.small_table[inx].headers := h;
// Prepare the list of allocated small blocks
x := Pointer(LongWord(h) + SizeOf(TSAPSmallBlocksHeader));
for i := 1 to blocks_in_piece do
begin
x.block.magic := cMagic;
x.block.tags := [sap_small_block];
x.block.ofs := LongWord(x) - LongWord(h);
x.header := h;
x.next := h.blocks;
h.blocks := x;
x := Pointer(LongWord(x) + block_size);
end;
Result := h;
end;
procedure FreeSmallBlock(mm: PSAPThreadMM; block: PSAPSmallAllocatedBlock);
var
h: PSAPSmallBlocksHeader;
inx: Integer;
begin
h := Pointer(LongWord(block) - block.ofs);
{$IFDEF SAP_CHECKMAGIC}
Assert(h.header_magic = cSmallBlockHeaderMagic);
{$ENDIF}
block.tags := block.tags - [sap_allocated];
Inc(h.free_count);
PSAPSmallFreeBlock(block).header := h;
if h.blocks = nil then
begin
// insert block into the list of small blocks of appropriate size
inx := h.block_size div cSmallBlockStep - 1;
h.next := mm.small_table[inx].headers;
h.prev := nil;
if h.next <> nil then
h.next.prev := h;
mm.small_table[inx].headers := h;
end;
PSAPSmallFreeBlock(block).next := h.blocks;
h.blocks := PSAPSmallFreeBlock(block);
(* Never delete blocks. Maybe should delete if there are too much of them
(if there are peak allocations of blocks of the same size)
*)
// if all blocks of the group are free and the group is not single
if (h.free_count = h.blocks_no) and ((h.next <> nil) or (h.prev <> nil)) then
FreeSmallBlocks(mm, h);
//*)
end;
procedure FreeSmallBlocks(mm: PSAPThreadMM; h: PSAPSmallBlocksHeader);
var
inx: Integer;
p: PSAPAllocatedBlock;
begin
inx := h.block_size div cSmallBlockStep - 1;
if mm.small_table[inx].headers = h then
begin
mm.small_table[inx].headers := h.next;
if h.next <> nil then
h.next.prev := nil;
end
else
begin
h.prev.next := h.next;
if h.next <> nil then
h.next.prev := h.prev;
end;
p := Pointer(LongWord(h) - SizeOf(TSAPAllocatedBlock));
p.tags := p.tags - [sap_small_blocks_piece];
InsertIntoFreeList(mm, h);
end;
end.