1 | /* $Id: HGSMIMemAlloc.cpp 62492 2016-07-22 18:42:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Host Guest Shared Memory Interface (HGSMI) - Memory allocator.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * Memory allocator
|
---|
20 | * ----------------
|
---|
21 | *
|
---|
22 | * Area [0; AreaSize) contains only the data, control structures are separate.
|
---|
23 | * Block sizes are power of 2: 32B, ..., 1MB
|
---|
24 | * Area size can be anything and will be divided initially to largest possible free blocks.
|
---|
25 | *
|
---|
26 | * The entire area is described by a list of 32 bit block descriptors:
|
---|
27 | * * bits 0..3 - order, which is log2 size of the block - 5: 2^(0+5) ... 2^(15+5) == 32B .. 1MB
|
---|
28 | * * bit 4 - 1 for free blocks.
|
---|
29 | * * bits 5..31 - block offset.
|
---|
30 | *
|
---|
31 | * 31 ... 5 | 4 | 3 ... 0
|
---|
32 | * offset F order
|
---|
33 | *
|
---|
34 | * There is a sorted collection of all block descriptors
|
---|
35 | * (key is the block offset, bits 0...4 do not interfere with sorting).
|
---|
36 | * Also there are lists of free blocks for each size for fast allocation.
|
---|
37 | *
|
---|
38 | *
|
---|
39 | * Implementation
|
---|
40 | * --------------
|
---|
41 | *
|
---|
42 | * The blocks collection is a sorted linear list.
|
---|
43 | *
|
---|
44 | * Initially the entire area consists of one or more largest blocks followed by smaller blocks:
|
---|
45 | * * 100B area - 64B block with descriptor: 0x00000011
|
---|
46 | * 32B block with descriptor: 0x00000030
|
---|
47 | * 4B unused
|
---|
48 | * * 64K area - one 64K block with descriptor: 0x0000001C
|
---|
49 | * * 512K area - one 512K block with descriptor: 0x0000001F
|
---|
50 | *
|
---|
51 | * When allocating a new block:
|
---|
52 | * * larger free blocks are splitted when there are no smaller free blocks;
|
---|
53 | * * smaller free blocks are merged if they can build a requested larger block.
|
---|
54 | */
|
---|
55 | #include <VBox/HGSMI/HGSMIMemAlloc.h>
|
---|
56 | #include <VBox/HGSMI/HGSMI.h>
|
---|
57 |
|
---|
58 | #include <iprt/err.h>
|
---|
59 | #include <iprt/string.h>
|
---|
60 |
|
---|
61 |
|
---|
62 | DECLINLINE(HGSMIOFFSET) hgsmiMADescriptor(HGSMIOFFSET off, bool fFree, HGSMIOFFSET order)
|
---|
63 | {
|
---|
64 | return (off & HGSMI_MA_DESC_OFFSET_MASK) |
|
---|
65 | (fFree? HGSMI_MA_DESC_FREE_MASK: 0) |
|
---|
66 | (order & HGSMI_MA_DESC_ORDER_MASK);
|
---|
67 | }
|
---|
68 |
|
---|
69 | static void hgsmiMABlockFree(HGSMIMADATA *pMA, HGSMIMABLOCK *pBlock)
|
---|
70 | {
|
---|
71 | pMA->env.pfnFree(pMA->env.pvEnv, pBlock);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int hgsmiMABlockAlloc(HGSMIMADATA *pMA, HGSMIMABLOCK **ppBlock)
|
---|
75 | {
|
---|
76 | int rc = VINF_SUCCESS;
|
---|
77 |
|
---|
78 | HGSMIMABLOCK *pBlock = (HGSMIMABLOCK *)pMA->env.pfnAlloc(pMA->env.pvEnv, sizeof(HGSMIMABLOCK));
|
---|
79 | if (pBlock)
|
---|
80 | {
|
---|
81 | RT_ZERO(pBlock->nodeBlock);
|
---|
82 | *ppBlock = pBlock;
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | rc = VERR_NO_MEMORY;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Divide entire area to free blocks. */
|
---|
93 | static int hgsmiMAFormat(HGSMIMADATA *pMA)
|
---|
94 | {
|
---|
95 | int rc = VINF_SUCCESS;
|
---|
96 |
|
---|
97 | /* Initial value, it will be updated in the loop below. */
|
---|
98 | pMA->cbMaxBlock = HGSMI_MA_BLOCK_SIZE_MIN;
|
---|
99 | pMA->cBlocks = 0;
|
---|
100 |
|
---|
101 | HGSMISIZE cbBlock = HGSMI_MA_BLOCK_SIZE_MAX;
|
---|
102 | HGSMISIZE cbRemaining = pMA->area.cbArea;
|
---|
103 | HGSMIOFFSET off = 0;
|
---|
104 |
|
---|
105 | while (cbBlock >= HGSMI_MA_BLOCK_SIZE_MIN)
|
---|
106 | {
|
---|
107 | /* Build a list of free memory blocks with u32BlockSize. */
|
---|
108 | uint32_t cBlocks = cbRemaining / cbBlock;
|
---|
109 | if (cBlocks > 0)
|
---|
110 | {
|
---|
111 | if (pMA->cbMaxBlock < cbBlock)
|
---|
112 | {
|
---|
113 | pMA->cbMaxBlock = cbBlock;
|
---|
114 | }
|
---|
115 |
|
---|
116 | HGSMIOFFSET order = HGSMIMASize2Order(cbBlock);
|
---|
117 |
|
---|
118 | uint32_t i;
|
---|
119 | for (i = 0; i < cBlocks; ++i)
|
---|
120 | {
|
---|
121 | /* A new free block. */
|
---|
122 | HGSMIMABLOCK *pBlock;
|
---|
123 | rc = hgsmiMABlockAlloc(pMA, &pBlock);
|
---|
124 | if (RT_FAILURE(rc))
|
---|
125 | {
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | pBlock->descriptor = hgsmiMADescriptor(off, true, order);
|
---|
130 | RTListAppend(&pMA->listBlocks, &pBlock->nodeBlock);
|
---|
131 | ++pMA->cBlocks;
|
---|
132 |
|
---|
133 | off += cbBlock;
|
---|
134 | cbRemaining -= cbBlock;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (RT_FAILURE(rc))
|
---|
139 | {
|
---|
140 | break;
|
---|
141 | }
|
---|
142 |
|
---|
143 | cbBlock /= 2;
|
---|
144 | }
|
---|
145 |
|
---|
146 | return rc;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static int hgsmiMARebuildFreeLists(HGSMIMADATA *pMA)
|
---|
150 | {
|
---|
151 | int rc = VINF_SUCCESS;
|
---|
152 |
|
---|
153 | HGSMIMABLOCK *pIter;
|
---|
154 | RTListForEach(&pMA->listBlocks, pIter, HGSMIMABLOCK, nodeBlock)
|
---|
155 | {
|
---|
156 | if (HGSMI_MA_DESC_IS_FREE(pIter->descriptor))
|
---|
157 | {
|
---|
158 | HGSMIOFFSET order = HGSMI_MA_DESC_ORDER(pIter->descriptor);
|
---|
159 | RTListAppend(&pMA->aListFreeBlocks[order], &pIter->nodeFree);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int hgsmiMARestore(HGSMIMADATA *pMA, HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock)
|
---|
167 | {
|
---|
168 | int rc = VINF_SUCCESS;
|
---|
169 |
|
---|
170 | pMA->cbMaxBlock = cbMaxBlock;
|
---|
171 | pMA->cBlocks = 0;
|
---|
172 |
|
---|
173 | HGSMISIZE cbRemaining = pMA->area.cbArea;
|
---|
174 | HGSMIOFFSET off = 0;
|
---|
175 |
|
---|
176 | uint32_t i;
|
---|
177 | for (i = 0; i < cDescriptors; ++i)
|
---|
178 | {
|
---|
179 | /* Verify the descriptor. */
|
---|
180 | HGSMISIZE cbBlock = HGSMIMAOrder2Size(HGSMI_MA_DESC_ORDER(paDescriptors[i]));
|
---|
181 | if ( off != HGSMI_MA_DESC_OFFSET(paDescriptors[i])
|
---|
182 | || cbBlock > cbRemaining
|
---|
183 | || cbBlock > pMA->cbMaxBlock)
|
---|
184 | {
|
---|
185 | rc = VERR_INVALID_PARAMETER;
|
---|
186 | break;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* A new free block. */
|
---|
190 | HGSMIMABLOCK *pBlock;
|
---|
191 | rc = hgsmiMABlockAlloc(pMA, &pBlock);
|
---|
192 | if (RT_FAILURE(rc))
|
---|
193 | {
|
---|
194 | break;
|
---|
195 | }
|
---|
196 |
|
---|
197 | pBlock->descriptor = paDescriptors[i];
|
---|
198 | RTListAppend(&pMA->listBlocks, &pBlock->nodeBlock);
|
---|
199 | ++pMA->cBlocks;
|
---|
200 |
|
---|
201 | off += cbBlock;
|
---|
202 | cbRemaining -= cbBlock;
|
---|
203 | }
|
---|
204 |
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static HGSMIMABLOCK *hgsmiMAGetFreeBlock(HGSMIMADATA *pMA, HGSMIOFFSET order)
|
---|
209 | {
|
---|
210 | HGSMIMABLOCK *pBlock = NULL;
|
---|
211 |
|
---|
212 | HGSMIOFFSET i;
|
---|
213 | for (i = order; i < RT_ELEMENTS(pMA->aListFreeBlocks); ++i)
|
---|
214 | {
|
---|
215 | pBlock = RTListGetFirst(&pMA->aListFreeBlocks[i], HGSMIMABLOCK, nodeFree);
|
---|
216 | if (pBlock)
|
---|
217 | {
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | if (pBlock)
|
---|
223 | {
|
---|
224 | AssertReturn(HGSMI_MA_DESC_IS_FREE(pBlock->descriptor), NULL);
|
---|
225 |
|
---|
226 | /* Where the block starts. */
|
---|
227 | HGSMIOFFSET off = HGSMI_MA_DESC_OFFSET(pBlock->descriptor);
|
---|
228 |
|
---|
229 | /* 'i' is the order of the block. */
|
---|
230 | while (i != order)
|
---|
231 | {
|
---|
232 | /* A larger block was found and need to be split to 2 smaller blocks. */
|
---|
233 | HGSMIMABLOCK *pBlock2;
|
---|
234 | int rc = hgsmiMABlockAlloc(pMA, &pBlock2);
|
---|
235 | if (RT_FAILURE(rc))
|
---|
236 | {
|
---|
237 | pBlock = NULL;
|
---|
238 | break;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Create 2 blocks with descreased order. */
|
---|
242 | --i;
|
---|
243 |
|
---|
244 | /* Remove from the free list. */
|
---|
245 | RTListNodeRemove(&pBlock->nodeFree);
|
---|
246 |
|
---|
247 | pBlock->descriptor = hgsmiMADescriptor(off, true, i);
|
---|
248 | pBlock2->descriptor = hgsmiMADescriptor(off + HGSMIMAOrder2Size(i), true, i);
|
---|
249 |
|
---|
250 | /* Update list of all blocks by inserting pBlock2 after pBlock. */
|
---|
251 | RTListNodeInsertAfter(&pBlock->nodeBlock, &pBlock2->nodeBlock);
|
---|
252 | ++pMA->cBlocks;
|
---|
253 |
|
---|
254 | /* Update the free list. */
|
---|
255 | RTListAppend(&pMA->aListFreeBlocks[i], &pBlock->nodeFree);
|
---|
256 | RTListAppend(&pMA->aListFreeBlocks[i], &pBlock2->nodeFree);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | return pBlock;
|
---|
261 | }
|
---|
262 |
|
---|
263 | static void hgsmiMAReformatFreeBlocks(HGSMIMADATA *pMA, HGSMIOFFSET maxId,
|
---|
264 | HGSMIMABLOCK *pStart, HGSMIMABLOCK *pEnd, HGSMISIZE cbBlocks)
|
---|
265 | {
|
---|
266 | int rc = VINF_SUCCESS;
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Blocks starting from pStart until pEnd will be replaced with
|
---|
270 | * another set of blocks.
|
---|
271 | *
|
---|
272 | * The new set will include the block with the required order.
|
---|
273 | * Since the required order is larger than any existing block,
|
---|
274 | * it will replace at least two existing blocks.
|
---|
275 | * The new set will also have minimal possible number of blocks.
|
---|
276 | * Therefore the new set will have at least one block less.
|
---|
277 | * Blocks will be updated in place and remaining blocks will be
|
---|
278 | * deallocated.
|
---|
279 | */
|
---|
280 |
|
---|
281 | HGSMISIZE u32BlockSize = HGSMIMAOrder2Size(maxId);
|
---|
282 | HGSMISIZE cbRemaining = cbBlocks;
|
---|
283 | HGSMIOFFSET off = HGSMI_MA_DESC_OFFSET(pStart->descriptor);
|
---|
284 | HGSMIMABLOCK *pBlock = pStart;
|
---|
285 |
|
---|
286 | while (u32BlockSize >= HGSMI_MA_BLOCK_SIZE_MIN && cbRemaining)
|
---|
287 | {
|
---|
288 | /* Build a list of free memory blocks with u32BlockSize. */
|
---|
289 | uint32_t cBlocks = cbRemaining / u32BlockSize;
|
---|
290 | if (cBlocks > 0)
|
---|
291 | {
|
---|
292 | HGSMIOFFSET order = HGSMIMASize2Order(u32BlockSize);
|
---|
293 |
|
---|
294 | uint32_t i;
|
---|
295 | for (i = 0; i < cBlocks; ++i)
|
---|
296 | {
|
---|
297 | if (pBlock == pEnd)
|
---|
298 | {
|
---|
299 | /* Should never happen because the new set of blocks is supposed to be smaller. */
|
---|
300 | AssertFailed();
|
---|
301 | rc = VERR_OUT_OF_RESOURCES;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /* Remove from the free list. */
|
---|
306 | RTListNodeRemove(&pBlock->nodeFree);
|
---|
307 |
|
---|
308 | pBlock->descriptor = hgsmiMADescriptor(off, true, order);
|
---|
309 |
|
---|
310 | RTListAppend(&pMA->aListFreeBlocks[order], &pBlock->nodeFree);
|
---|
311 |
|
---|
312 | off += u32BlockSize;
|
---|
313 | cbRemaining -= u32BlockSize;
|
---|
314 |
|
---|
315 | pBlock = RTListGetNext(&pMA->listBlocks, pBlock, HGSMIMABLOCK, nodeBlock);
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (RT_FAILURE(rc))
|
---|
320 | {
|
---|
321 | break;
|
---|
322 | }
|
---|
323 |
|
---|
324 | u32BlockSize /= 2;
|
---|
325 | }
|
---|
326 |
|
---|
327 | Assert(cbRemaining == 0);
|
---|
328 |
|
---|
329 | if (RT_SUCCESS(rc))
|
---|
330 | {
|
---|
331 | /* Remove remaining free blocks from pBlock until pEnd */
|
---|
332 | for (;;)
|
---|
333 | {
|
---|
334 | bool fEnd = (pBlock == pEnd);
|
---|
335 | HGSMIMABLOCK *pNext = RTListGetNext(&pMA->listBlocks, pBlock, HGSMIMABLOCK, nodeBlock);
|
---|
336 |
|
---|
337 | RTListNodeRemove(&pBlock->nodeFree);
|
---|
338 | RTListNodeRemove(&pBlock->nodeBlock);
|
---|
339 | --pMA->cBlocks;
|
---|
340 |
|
---|
341 | hgsmiMABlockFree(pMA, pBlock);
|
---|
342 |
|
---|
343 | if (fEnd)
|
---|
344 | {
|
---|
345 | break;
|
---|
346 | }
|
---|
347 |
|
---|
348 | pBlock = pNext;
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void hgsmiMAQueryFreeRange(HGSMIMADATA *pMA, HGSMIMABLOCK *pBlock, HGSMISIZE cbRequired,
|
---|
354 | HGSMIMABLOCK **ppStart, HGSMIMABLOCK **ppEnd, HGSMISIZE *pcbBlocks)
|
---|
355 | {
|
---|
356 | Assert(HGSMI_MA_DESC_IS_FREE(pBlock->descriptor));
|
---|
357 |
|
---|
358 | *pcbBlocks = HGSMIMAOrder2Size(HGSMI_MA_DESC_ORDER(pBlock->descriptor));
|
---|
359 | *ppStart = pBlock;
|
---|
360 | *ppEnd = pBlock;
|
---|
361 |
|
---|
362 | HGSMIMABLOCK *p;
|
---|
363 | for (;;)
|
---|
364 | {
|
---|
365 | p = RTListGetNext(&pMA->listBlocks, *ppEnd, HGSMIMABLOCK, nodeBlock);
|
---|
366 | if (!p || !HGSMI_MA_DESC_IS_FREE(p->descriptor))
|
---|
367 | {
|
---|
368 | break;
|
---|
369 | }
|
---|
370 | *pcbBlocks += HGSMIMAOrder2Size(HGSMI_MA_DESC_ORDER(p->descriptor));
|
---|
371 | *ppEnd = p;
|
---|
372 |
|
---|
373 | if (cbRequired && *pcbBlocks >= cbRequired)
|
---|
374 | {
|
---|
375 | return;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | for (;;)
|
---|
379 | {
|
---|
380 | p = RTListGetPrev(&pMA->listBlocks, *ppStart, HGSMIMABLOCK, nodeBlock);
|
---|
381 | if (!p || !HGSMI_MA_DESC_IS_FREE(p->descriptor))
|
---|
382 | {
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | *pcbBlocks += HGSMIMAOrder2Size(HGSMI_MA_DESC_ORDER(p->descriptor));
|
---|
386 | *ppStart = p;
|
---|
387 |
|
---|
388 | if (cbRequired && *pcbBlocks >= cbRequired)
|
---|
389 | {
|
---|
390 | return;
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | static void hgsmiMAMergeFreeBlocks(HGSMIMADATA *pMA, HGSMIOFFSET order)
|
---|
396 | {
|
---|
397 | /* Try to create a free block with the order from smaller free blocks. */
|
---|
398 | if (order == 0)
|
---|
399 | {
|
---|
400 | /* No smaller blocks. */
|
---|
401 | return;
|
---|
402 | }
|
---|
403 |
|
---|
404 | HGSMISIZE cbRequired = HGSMIMAOrder2Size(order);
|
---|
405 |
|
---|
406 | /* Scan all free lists of smaller blocks.
|
---|
407 | *
|
---|
408 | * Get the sequence of free blocks before and after each free block.
|
---|
409 | * If possible, re-split the sequence to get the required block and other free block(s).
|
---|
410 | * If not possible, try the next free block.
|
---|
411 | *
|
---|
412 | * Free blocks are scanned from i to 0 orders.
|
---|
413 | */
|
---|
414 | HGSMIOFFSET i = order - 1;
|
---|
415 | for (;;)
|
---|
416 | {
|
---|
417 | HGSMIMABLOCK *pIter;
|
---|
418 | RTListForEach(&pMA->aListFreeBlocks[i], pIter, HGSMIMABLOCK, nodeFree)
|
---|
419 | {
|
---|
420 | Assert(HGSMI_MA_DESC_ORDER(pIter->descriptor) == i);
|
---|
421 |
|
---|
422 | HGSMISIZE cbBlocks;
|
---|
423 | HGSMIMABLOCK *pFreeStart;
|
---|
424 | HGSMIMABLOCK *pFreeEnd;
|
---|
425 | hgsmiMAQueryFreeRange(pMA, pIter, cbRequired, &pFreeStart, &pFreeEnd, &cbBlocks);
|
---|
426 |
|
---|
427 | Assert((cbBlocks / HGSMI_MA_BLOCK_SIZE_MIN) * HGSMI_MA_BLOCK_SIZE_MIN == cbBlocks);
|
---|
428 |
|
---|
429 | /* Verify whether cbBlocks is enough for the requested block. */
|
---|
430 | if (cbBlocks >= cbRequired)
|
---|
431 | {
|
---|
432 | /* Build new free blocks starting from the requested. */
|
---|
433 | hgsmiMAReformatFreeBlocks(pMA, order, pFreeStart, pFreeEnd, cbBlocks);
|
---|
434 | i = 0; /* Leave the loop. */
|
---|
435 | break;
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (i == 0)
|
---|
440 | {
|
---|
441 | break;
|
---|
442 | }
|
---|
443 |
|
---|
444 | --i;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | static HGSMIOFFSET hgsmiMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb)
|
---|
449 | {
|
---|
450 | if (cb > pMA->cbMaxBlock)
|
---|
451 | {
|
---|
452 | return HGSMIOFFSET_VOID;
|
---|
453 | }
|
---|
454 |
|
---|
455 | if (cb < HGSMI_MA_BLOCK_SIZE_MIN)
|
---|
456 | {
|
---|
457 | cb = HGSMI_MA_BLOCK_SIZE_MIN;
|
---|
458 | }
|
---|
459 |
|
---|
460 | HGSMIOFFSET order = HGSMIPopCnt32(cb - 1) - HGSMI_MA_DESC_ORDER_BASE;
|
---|
461 |
|
---|
462 | AssertReturn(HGSMIMAOrder2Size(order) >= cb, HGSMIOFFSET_VOID);
|
---|
463 | AssertReturn(order < RT_ELEMENTS(pMA->aListFreeBlocks), HGSMIOFFSET_VOID);
|
---|
464 |
|
---|
465 | HGSMIMABLOCK *pBlock = hgsmiMAGetFreeBlock(pMA, order);
|
---|
466 | if (RT_UNLIKELY(pBlock == NULL))
|
---|
467 | {
|
---|
468 | /* No free block with large enough size. Merge smaller free blocks and try again. */
|
---|
469 | hgsmiMAMergeFreeBlocks(pMA, order);
|
---|
470 | pBlock = hgsmiMAGetFreeBlock(pMA, order);
|
---|
471 | }
|
---|
472 |
|
---|
473 | if (RT_LIKELY(pBlock != NULL))
|
---|
474 | {
|
---|
475 | RTListNodeRemove(&pBlock->nodeFree);
|
---|
476 | pBlock->descriptor &= ~HGSMI_MA_DESC_FREE_MASK;
|
---|
477 | return HGSMI_MA_DESC_OFFSET(pBlock->descriptor);
|
---|
478 | }
|
---|
479 |
|
---|
480 | return HGSMIOFFSET_VOID;
|
---|
481 | }
|
---|
482 |
|
---|
483 | static void hgsmiMAFree(HGSMIMADATA *pMA, HGSMIOFFSET off)
|
---|
484 | {
|
---|
485 | if (off == HGSMIOFFSET_VOID)
|
---|
486 | {
|
---|
487 | return;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /* Find the block corresponding to the offset. */
|
---|
491 | Assert((off / HGSMI_MA_BLOCK_SIZE_MIN) * HGSMI_MA_BLOCK_SIZE_MIN == off);
|
---|
492 |
|
---|
493 | HGSMIMABLOCK *pBlock = HGSMIMASearchOffset(pMA, off);
|
---|
494 | if (pBlock)
|
---|
495 | {
|
---|
496 | if (HGSMI_MA_DESC_OFFSET(pBlock->descriptor) == off)
|
---|
497 | {
|
---|
498 | /* Found the right block, mark it as free. */
|
---|
499 | pBlock->descriptor |= HGSMI_MA_DESC_FREE_MASK;
|
---|
500 | RTListAppend(&pMA->aListFreeBlocks[HGSMI_MA_DESC_ORDER(pBlock->descriptor)], &pBlock->nodeFree);
|
---|
501 | return;
|
---|
502 | }
|
---|
503 | }
|
---|
504 |
|
---|
505 | AssertFailed();
|
---|
506 | }
|
---|
507 |
|
---|
508 | int HGSMIMAInit(HGSMIMADATA *pMA, const HGSMIAREA *pArea,
|
---|
509 | HGSMIOFFSET *paDescriptors, uint32_t cDescriptors, HGSMISIZE cbMaxBlock,
|
---|
510 | const HGSMIENV *pEnv)
|
---|
511 | {
|
---|
512 | AssertReturn(pArea->cbArea < UINT32_C(0x80000000), VERR_INVALID_PARAMETER);
|
---|
513 | AssertReturn(pArea->cbArea >= HGSMI_MA_BLOCK_SIZE_MIN, VERR_INVALID_PARAMETER);
|
---|
514 |
|
---|
515 | RT_ZERO(*pMA);
|
---|
516 |
|
---|
517 | HGSMISIZE cb = (pArea->cbArea / HGSMI_MA_BLOCK_SIZE_MIN) * HGSMI_MA_BLOCK_SIZE_MIN;
|
---|
518 |
|
---|
519 | int rc = HGSMIAreaInitialize(&pMA->area, pArea->pu8Base, cb, 0);
|
---|
520 | if (RT_SUCCESS(rc))
|
---|
521 | {
|
---|
522 | pMA->env = *pEnv;
|
---|
523 |
|
---|
524 | uint32_t i;
|
---|
525 | for (i = 0; i < RT_ELEMENTS(pMA->aListFreeBlocks); ++i)
|
---|
526 | {
|
---|
527 | RTListInit(&pMA->aListFreeBlocks[i]);
|
---|
528 | }
|
---|
529 | RTListInit(&pMA->listBlocks);
|
---|
530 |
|
---|
531 | if (cDescriptors)
|
---|
532 | {
|
---|
533 | rc = hgsmiMARestore(pMA, paDescriptors, cDescriptors, cbMaxBlock);
|
---|
534 | }
|
---|
535 | else
|
---|
536 | {
|
---|
537 | rc = hgsmiMAFormat(pMA);
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | {
|
---|
542 | rc = hgsmiMARebuildFreeLists(pMA);
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 | return rc;
|
---|
547 | }
|
---|
548 |
|
---|
549 | void HGSMIMAUninit(HGSMIMADATA *pMA)
|
---|
550 | {
|
---|
551 | HGSMIMABLOCK *pIter;
|
---|
552 | HGSMIMABLOCK *pNext;
|
---|
553 |
|
---|
554 | RTListForEachSafe(&pMA->listBlocks, pIter, pNext, HGSMIMABLOCK, nodeBlock)
|
---|
555 | {
|
---|
556 | RTListNodeRemove(&pIter->nodeBlock);
|
---|
557 | hgsmiMABlockFree(pMA, pIter);
|
---|
558 | }
|
---|
559 |
|
---|
560 | RT_ZERO(*pMA);
|
---|
561 | }
|
---|
562 |
|
---|
563 | HGSMIOFFSET HGSMIMAPointerToOffset(const HGSMIMADATA *pMA, const void *pv)
|
---|
564 | {
|
---|
565 | if (HGSMIAreaContainsPointer(&pMA->area, pv))
|
---|
566 | {
|
---|
567 | return HGSMIPointerToOffset(&pMA->area, pv);
|
---|
568 | }
|
---|
569 |
|
---|
570 | AssertFailed();
|
---|
571 | return HGSMIOFFSET_VOID;
|
---|
572 | }
|
---|
573 |
|
---|
574 | void *HGSMIMAOffsetToPointer(const HGSMIMADATA *pMA, HGSMIOFFSET off)
|
---|
575 | {
|
---|
576 | if (HGSMIAreaContainsOffset(&pMA->area, off))
|
---|
577 | {
|
---|
578 | return HGSMIOffsetToPointer(&pMA->area, off);
|
---|
579 | }
|
---|
580 |
|
---|
581 | AssertFailed();
|
---|
582 | return NULL;
|
---|
583 | }
|
---|
584 |
|
---|
585 | void *HGSMIMAAlloc(HGSMIMADATA *pMA, HGSMISIZE cb)
|
---|
586 | {
|
---|
587 | HGSMIOFFSET off = hgsmiMAAlloc(pMA, cb);
|
---|
588 | return HGSMIMAOffsetToPointer(pMA, off);
|
---|
589 | }
|
---|
590 |
|
---|
591 | void HGSMIMAFree(HGSMIMADATA *pMA, void *pv)
|
---|
592 | {
|
---|
593 | HGSMIOFFSET off = HGSMIMAPointerToOffset(pMA, pv);
|
---|
594 | if (off != HGSMIOFFSET_VOID)
|
---|
595 | {
|
---|
596 | hgsmiMAFree(pMA, off);
|
---|
597 | }
|
---|
598 | else
|
---|
599 | {
|
---|
600 | AssertFailed();
|
---|
601 | }
|
---|
602 | }
|
---|
603 |
|
---|
604 | HGSMIMABLOCK *HGSMIMASearchOffset(HGSMIMADATA *pMA, HGSMIOFFSET off)
|
---|
605 | {
|
---|
606 | /* Binary search in the block list for the offset. */
|
---|
607 | HGSMIMABLOCK *pStart = RTListGetFirst(&pMA->listBlocks, HGSMIMABLOCK, nodeBlock);
|
---|
608 | HGSMIMABLOCK *pEnd = RTListGetLast(&pMA->listBlocks, HGSMIMABLOCK, nodeBlock);
|
---|
609 | HGSMIMABLOCK *pMiddle;
|
---|
610 |
|
---|
611 | uint32_t iStart = 0;
|
---|
612 | uint32_t iEnd = pMA->cBlocks;
|
---|
613 | uint32_t iMiddle;
|
---|
614 |
|
---|
615 | for (;;)
|
---|
616 | {
|
---|
617 | pMiddle = pStart;
|
---|
618 | iMiddle = iStart + (iEnd - iStart) / 2;
|
---|
619 | if (iMiddle == iStart)
|
---|
620 | {
|
---|
621 | break;
|
---|
622 | }
|
---|
623 |
|
---|
624 | /* Find the block with the iMiddle index. Never go further than pEnd. */
|
---|
625 | uint32_t i;
|
---|
626 | for (i = iStart; i < iMiddle && pMiddle != pEnd; ++i)
|
---|
627 | {
|
---|
628 | pMiddle = RTListNodeGetNext(&pMiddle->nodeBlock, HGSMIMABLOCK, nodeBlock);
|
---|
629 | }
|
---|
630 |
|
---|
631 | HGSMIOFFSET offMiddle = HGSMI_MA_DESC_OFFSET(pMiddle->descriptor);
|
---|
632 | if (offMiddle > off)
|
---|
633 | {
|
---|
634 | pEnd = pMiddle;
|
---|
635 | iEnd = iMiddle;
|
---|
636 | }
|
---|
637 | else
|
---|
638 | {
|
---|
639 | pStart = pMiddle;
|
---|
640 | iStart = iMiddle;
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | return pMiddle;
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Helper.
|
---|
650 | */
|
---|
651 |
|
---|
652 | uint32_t HGSMIPopCnt32(uint32_t u32)
|
---|
653 | {
|
---|
654 | uint32_t c = 0;
|
---|
655 | if (u32 > 0xFFFF) { c += 16; u32 >>= 16; }
|
---|
656 | if (u32 > 0xFF) { c += 8; u32 >>= 8; }
|
---|
657 | if (u32 > 0xF) { c += 4; u32 >>= 4; }
|
---|
658 | if (u32 > 0x3) { c += 2; u32 >>= 2; }
|
---|
659 | if (u32 > 0x1) { c += 1; u32 >>= 1; }
|
---|
660 | return c + u32;
|
---|
661 | }
|
---|