VirtualBox

source: vbox/trunk/src/VBox/Runtime/alloc/heapsimple.cpp@ 329

Last change on this file since 329 was 329, checked in by vboxsync, 18 years ago

test

  • Property svn:keywords set to Id
File size: 30.0 KB
Line 
1/* $Id: heapsimple.cpp 329 2007-01-25 19:56:10Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - A Simple Heap.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP RTLOGGROUP_DEFAULT
27#include <iprt/heap.h>
28#include <iprt/assert.h>
29#include <iprt/asm.h>
30#include <iprt/string.h>
31#include <iprt/err.h>
32#include <iprt/log.h>
33#include <iprt/param.h>
34
35
36/*******************************************************************************
37* Structures and Typedefs *
38*******************************************************************************/
39/** Pointer to the heap anchor block. */
40typedef struct RTHEAPSIMPLEINTERNAL *PRTHEAPSIMPLEINTERNAL;
41/** Pointer to a heap block. */
42typedef struct RTHEAPSIMPLEBLOCK *PRTHEAPSIMPLEBLOCK;
43/** Pointer to a free heap block. */
44typedef struct RTHEAPSIMPLEFREE *PRTHEAPSIMPLEFREE;
45
46/**
47 * Structure describing a simple heap block.
48 * If this block is allocated, it is followed by the user user data.
49 * If this block is free, see RTHEAPSIMPLEFREE.
50 */
51typedef struct RTHEAPSIMPLEBLOCK
52{
53 /** The next block in the global block list. */
54 PRTHEAPSIMPLEBLOCK pNext;
55 /** The previous block in the global block list. */
56 PRTHEAPSIMPLEBLOCK pPrev;
57 /** Pointer to the heap anchor block. */
58 PRTHEAPSIMPLEINTERNAL pHeap;
59 /** Flags + magic. */
60 uintptr_t fFlags;
61} RTHEAPSIMPLEBLOCK;
62AssertCompileSizeAlignment(RTHEAPSIMPLEBLOCK, 16);
63
64/** The block is free if this flag is set. When cleared it's allocated. */
65#define RTHEAPSIMPLEBLOCK_FLAGS_FREE ((uintptr_t)BIT(0))
66/** The magic value. */
67#define RTHEAPSIMPLEBLOCK_FLAGS_MAGIC ((uintptr_t)0xabcdef00)
68/** The mask that needs to be applied to RTHEAPSIMPLEBLOCK::fFalgs to obtain the magic value. */
69#define RTHEAPSIMPLEBLOCK_FLAGS_MAGIC_MASK (~(uintptr_t)BIT(0))
70
71/**
72 * Checks if the specified block is valid or not.
73 * @returns boolean answer.
74 * @param pBlock Pointer to a RTHEAPSIMPLEBLOCK structure.
75 */
76#define RTHEAPSIMPLEBLOCK_IS_VALID(pBlock) \
77 ( ((pBlock)->fFlags & RTHEAPSIMPLEBLOCK_FLAGS_MAGIC_MASK) == RTHEAPSIMPLEBLOCK_FLAGS_MAGIC )
78
79/**
80 * Checks if the specified block is valid and in use.
81 * @returns boolean answer.
82 * @param pBlock Pointer to a RTHEAPSIMPLEBLOCK structure.
83 */
84#define RTHEAPSIMPLEBLOCK_IS_VALID_USED(pBlock) \
85 ( ((pBlock)->fFlags & (RTHEAPSIMPLEBLOCK_FLAGS_MAGIC_MASK | RTHEAPSIMPLEBLOCK_FLAGS_FREE)) \
86 == RTHEAPSIMPLEBLOCK_FLAGS_MAGIC )
87
88/**
89 * Checks if the specified block is valid and free.
90 * @returns boolean answer.
91 * @param pBlock Pointer to a RTHEAPSIMPLEBLOCK structure.
92 */
93#define RTHEAPSIMPLEBLOCK_IS_VALID_FREE(pBlock) \
94 ( ((pBlock)->fFlags & (RTHEAPSIMPLEBLOCK_FLAGS_MAGIC_MASK | RTHEAPSIMPLEBLOCK_FLAGS_FREE)) \
95 == (RTHEAPSIMPLEBLOCK_FLAGS_MAGIC | RTHEAPSIMPLEBLOCK_FLAGS_FREE) )
96
97/**
98 * Checks if the specified block is free or not.
99 * @returns boolean answer.
100 * @param pBlock Pointer to a valid RTHEAPSIMPLEBLOCK structure.
101 */
102#define RTHEAPSIMPLEBLOCK_IS_FREE(pBlock) (!!((pBlock)->fFlags & RTHEAPSIMPLEBLOCK_FLAGS_FREE))
103
104/**
105 * A free heap block.
106 * This is an extended version of RTHEAPSIMPLEBLOCK that takes the unused
107 * user data to store free list pointers and a cached size value.
108 */
109typedef struct RTHEAPSIMPLEFREE
110{
111 /** Core stuff. */
112 RTHEAPSIMPLEBLOCK Core;
113 /** Pointer to the next free block. */
114 PRTHEAPSIMPLEFREE pNext;
115 /** Pointer to the previous free block. */
116 PRTHEAPSIMPLEFREE pPrev;
117 /** The size of the block (excluding the RTHEAPSIMPLEBLOCK part). */
118 size_t cb;
119 /** An alignment filler to make it a multiple of (sizeof(void *) * 2). */
120 size_t Alignment;
121} RTHEAPSIMPLEFREE;
122
123
124/**
125 * The heap anchor block.
126 * This structure is placed at the head of the memory block specified to RTHeapSimpleInit(),
127 * which means that the first RTHEAPSIMPLEBLOCK appears immediately after this structure.
128 */
129typedef struct RTHEAPSIMPLEINTERNAL
130{
131 /** The typical magic (RTHEAPSIMPLE_MAGIC). */
132 size_t uMagic;
133 /** The heap size. (This structure is not included!) */
134 size_t cbHeap;
135 /** Pointer to the end of the heap. */
136 void *pvEnd;
137 /** The amount of free memory in the heap. */
138 size_t cbFree;
139 /** Free head pointer. */
140 PRTHEAPSIMPLEFREE pFreeHead;
141 /** Free tail pointer. */
142 PRTHEAPSIMPLEFREE pFreeTail;
143 /** Make the size of this structure is a multiple of 32. */
144 size_t auAlignment[2];
145} RTHEAPSIMPLEINTERNAL;
146AssertCompileSizeAlignment(RTHEAPSIMPLEINTERNAL, 32);
147
148/** Magic number for RTHEAPSIMPLEINTERNAL::u32Magic. (Kyoichi Katayama) */
149#define RTHEAPSIMPLE_MAGIC 0x19590105
150
151
152/** The minimum allocation size. */
153#define RTHEAPSIMPLE_MIN_BLOCK (sizeof(RTHEAPSIMPLEBLOCK))
154AssertCompile(RTHEAPSIMPLE_MIN_BLOCK >= sizeof(RTHEAPSIMPLEBLOCK));
155AssertCompile(RTHEAPSIMPLE_MIN_BLOCK >= sizeof(RTHEAPSIMPLEFREE) - sizeof(RTHEAPSIMPLEBLOCK));
156
157/** The minimum and default alignment. */
158#define RTHEAPSIMPLE_ALIGNMENT (sizeof(RTHEAPSIMPLEBLOCK))
159
160
161/*******************************************************************************
162* Defined Constants And Macros *
163*******************************************************************************/
164#ifdef RT_STRICT
165# define RTHEAPSIMPLE_STRICT 1
166#endif
167
168#define ASSERT_L(a, b) AssertMsg((uintptr_t)(a) < (uintptr_t)(b), ("a=%p b=%p\n", (uintptr_t)(a), (uintptr_t)(b)))
169#define ASSERT_LE(a, b) AssertMsg((uintptr_t)(a) <= (uintptr_t)(b), ("a=%p b=%p\n", (uintptr_t)(a), (uintptr_t)(b)))
170#define ASSERT_G(a, b) AssertMsg((uintptr_t)(a) > (uintptr_t)(b), ("a=%p b=%p\n", (uintptr_t)(a), (uintptr_t)(b)))
171#define ASSERT_GE(a, b) AssertMsg((uintptr_t)(a) >= (uintptr_t)(b), ("a=%p b=%p\n", (uintptr_t)(a), (uintptr_t)(b)))
172#define ASSERT_ALIGN(a) AssertMsg(!((uintptr_t)(a) & (RTHEAPSIMPLE_ALIGNMENT - 1)), ("a=%p\n", (uintptr_t)(a)))
173
174#define ASSERT_PREV(pHeapInt, pBlock) \
175 do { ASSERT_ALIGN((pBlock)->pPrev); \
176 if ((pBlock)->pPrev) \
177 { \
178 ASSERT_L((pBlock)->pPrev, (pBlock)); \
179 ASSERT_GE((pBlock)->pPrev, (pHeapInt) + 1); \
180 } \
181 else \
182 Assert((pBlock) == (PRTHEAPSIMPLEBLOCK)((pHeapInt) + 1)); \
183 } while (0)
184
185#define ASSERT_NEXT(pHeap, pBlock) \
186 do { ASSERT_ALIGN((pBlock)->pNext); \
187 if ((pBlock)->pNext) \
188 { \
189 ASSERT_L((pBlock)->pNext, (pHeapInt)->pvEnd); \
190 ASSERT_G((pBlock)->pNext, (pBlock)); \
191 } \
192 } while (0)
193
194#define ASSERT_BLOCK(pHeapInt, pBlock) \
195 do { AssertMsg(RTHEAPSIMPLEBLOCK_IS_VALID(pBlock), ("%#x\n", (pBlock)->fFlags)); \
196 AssertMsg((pBlock)->pHeap == (pHeapInt), ("%p != %p\n", (pBlock)->pHeap, (pHeapInt))); \
197 ASSERT_GE((pBlock), (pHeapInt) + 1); \
198 ASSERT_L((pBlock), (pHeapInt)->pvEnd); \
199 ASSERT_NEXT(pHeapInt, pBlock); \
200 ASSERT_PREV(pHeapInt, pBlock); \
201 } while (0)
202
203#define ASSERT_BLOCK_USED(pHeapInt, pBlock) \
204 do { AssertMsg(RTHEAPSIMPLEBLOCK_IS_VALID_USED((pBlock)), ("%#x\n", (pBlock)->fFlags)); \
205 AssertMsg((pBlock)->pHeap == (pHeapInt), ("%p != %p\n", (pBlock)->pHeap, (pHeapInt))); \
206 ASSERT_GE((pBlock), (pHeapInt) + 1); \
207 ASSERT_L((pBlock), (pHeapInt)->pvEnd); \
208 ASSERT_NEXT(pHeapInt, pBlock); \
209 ASSERT_PREV(pHeapInt, pBlock); \
210 } while (0)
211
212#define ASSERT_FREE_PREV(pHeapInt, pBlock) \
213 do { ASSERT_ALIGN((pBlock)->pPrev); \
214 if ((pBlock)->pPrev) \
215 { \
216 ASSERT_GE((pBlock)->pPrev, (pHeapInt)->pFreeHead); \
217 ASSERT_L((pBlock)->pPrev, (pBlock)); \
218 ASSERT_LE((pBlock)->pPrev, (pBlock)->Core.pPrev); \
219 } \
220 else \
221 Assert((pBlock) == (pHeapInt)->pFreeHead); \
222 } while (0)
223
224#define ASSERT_FREE_NEXT(pHeapInt, pBlock) \
225 do { ASSERT_ALIGN((pBlock)->pNext); \
226 if ((pBlock)->pNext) \
227 { \
228 ASSERT_LE((pBlock)->pNext, (pHeapInt)->pFreeTail); \
229 ASSERT_G((pBlock)->pNext, (pBlock)); \
230 ASSERT_GE((pBlock)->pNext, (pBlock)->Core.pNext); \
231 } \
232 else \
233 Assert((pBlock) == (pHeapInt)->pFreeTail); \
234 } while (0)
235
236#ifdef RTHEAPSIMPLE_STRICT
237# define ASSERT_FREE_CB(pHeapInt, pBlock) \
238 do { size_t cbCalc = ((pBlock)->Core.pNext ? (uintptr_t)(pBlock)->Core.pNext : (uintptr_t)(pHeapInt)->pvEnd) \
239 - (uintptr_t)(pBlock) - sizeof(RTHEAPSIMPLEBLOCK); \
240 AssertMsg((pBlock)->cb == cbCalc, ("cb=%#zx cbCalc=%#zx\n", (pBlock)->cb, cbCalc)); \
241 } while (0)
242#else
243# define ASSERT_FREE_CB(pHeapInt, pBlock) do {} while (0)
244#endif
245
246/** Asserts that a free block is valid. */
247#define ASSERT_BLOCK_FREE(pHeapInt, pBlock) \
248 do { ASSERT_BLOCK(pHeapInt, &(pBlock)->Core); \
249 Assert(RTHEAPSIMPLEBLOCK_IS_VALID_FREE(&(pBlock)->Core)); \
250 ASSERT_GE((pBlock), (pHeapInt)->pFreeHead); \
251 ASSERT_LE((pBlock), (pHeapInt)->pFreeTail); \
252 ASSERT_FREE_NEXT(pHeapInt, pBlock); \
253 ASSERT_FREE_PREV(pHeapInt, pBlock); \
254 ASSERT_FREE_CB(pHeapInt, pBlock); \
255 } while (0)
256
257/** Asserts that the heap anchor block is ok. */
258#define ASSERT_ANCHOR(pHeapInt) \
259 do { AssertPtr(pHeapInt);\
260 Assert((pHeapInt)->uMagic == RTHEAPSIMPLE_MAGIC); \
261 } while (0)
262
263
264/*******************************************************************************
265* Internal Functions *
266*******************************************************************************/
267#ifdef RTHEAPSIMPLE_STRICT
268static void rtHeapSimpleAssertAll(PRTHEAPSIMPLEINTERNAL pHeapInt);
269#endif
270static PRTHEAPSIMPLEBLOCK rtHeapSimpleAllocBlock(PRTHEAPSIMPLEINTERNAL pHeapInt, size_t cb, size_t uAlignment);
271static void rtHeapSimpleFreeBlock(PRTHEAPSIMPLEINTERNAL pHeapInt, PRTHEAPSIMPLEBLOCK pBlock);
272
273
274/**
275 * Initializes the heap.
276 *
277 * @returns IPRT status code on success.
278 * @param pHeap Where to store the heap anchor block on success.
279 * @param pvMemory Pointer to the heap memory.
280 * @param cbMemory The size of the heap memory.
281 */
282RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE pHeap, void *pvMemory, size_t cbMemory)
283{
284 /*
285 * Validate input. The imposed minimum heap size is just a convenien value.
286 */
287 AssertReturn(cbMemory >= PAGE_SIZE, VERR_INVALID_PARAMETER);
288 AssertPtrReturn(pvMemory, VERR_INVALID_POINTER);
289 AssertReturn((uintptr_t)pvMemory + (cbMemory - 1) > (uintptr_t)cbMemory, VERR_INVALID_PARAMETER);
290
291 /*
292 * Place the heap anchor block at the start of the heap memory,
293 * enforce 32 byte alignment of it. Also align the heap size correctly.
294 */
295 PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)pvMemory;
296 if ((uintptr_t)pvMemory & 31)
297 {
298 const unsigned off = 32 - ((uintptr_t)pvMemory & 31);
299 cbMemory -= off;
300 pHeapInt = (PRTHEAPSIMPLEINTERNAL)((uintptr_t)pvMemory + off);
301 }
302 cbMemory &= ~(RTHEAPSIMPLE_ALIGNMENT - 1);
303
304
305 /* Init the heap anchor block. */
306 pHeapInt->uMagic = RTHEAPSIMPLE_MAGIC;
307 pHeapInt->pvEnd = (uint8_t *)pHeapInt + cbMemory;
308 pHeapInt->cbHeap = cbMemory;
309 pHeapInt->cbFree = cbMemory
310 - sizeof(RTHEAPSIMPLEBLOCK)
311 - sizeof(RTHEAPSIMPLEINTERNAL);
312 pHeapInt->pFreeTail = pHeapInt->pFreeHead = (PRTHEAPSIMPLEFREE)(pHeapInt + 1);
313 for (unsigned i = 0; i < ELEMENTS(pHeapInt->auAlignment); i++)
314 pHeapInt->auAlignment[i] = ~(size_t)0;
315
316 /* Init the single free block. */
317 PRTHEAPSIMPLEFREE pFree = pHeapInt->pFreeHead;
318 pFree->Core.pNext = NULL;
319 pFree->Core.pPrev = NULL;
320 pFree->Core.pHeap = pHeapInt;
321 pFree->Core.fFlags = RTHEAPSIMPLEBLOCK_FLAGS_MAGIC | RTHEAPSIMPLEBLOCK_FLAGS_FREE;
322 pFree->pNext = NULL;
323 pFree->pPrev = NULL;
324 pFree->cb = pHeapInt->cbFree;
325
326 *pHeap = pHeapInt;
327
328#ifdef RTHEAPSIMPLE_STRICT
329 rtHeapSimpleAssertAll(pHeapInt);
330#endif
331 return VINF_SUCCESS;
332}
333
334
335
336/**
337 * Allocates memory from the specified simple heap.
338 *
339 * @returns Pointer to the allocated memory block on success.
340 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
341 *
342 * @param Heap The heap to allocate the memory on.
343 * @param cb The requested heap block size.
344 * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
345 * Must be a power of 2.
346 */
347RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment)
348{
349 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
350
351 /*
352 * Validate and adjust the input.
353 */
354 AssertPtrReturn(pHeapInt, NULL);
355 if (cb < RTHEAPSIMPLE_MIN_BLOCK)
356 cb = RTHEAPSIMPLE_MIN_BLOCK;
357 else
358 cb = RT_ALIGN_Z(cb, RTHEAPSIMPLE_ALIGNMENT);
359 if (!cbAlignment)
360 cbAlignment = RTHEAPSIMPLE_ALIGNMENT;
361 else
362 {
363 Assert(!(cbAlignment & (cbAlignment - 1)));
364 Assert((cbAlignment & ~(cbAlignment - 1)) == cbAlignment);
365 if (cbAlignment < RTHEAPSIMPLE_ALIGNMENT)
366 cbAlignment = RTHEAPSIMPLE_ALIGNMENT;
367 }
368
369 /*
370 * Do the allocation.
371 */
372 PRTHEAPSIMPLEBLOCK pBlock = rtHeapSimpleAllocBlock(pHeapInt, cb, cbAlignment);
373 if (RT_LIKELY(pBlock))
374 {
375 void *pv = pBlock + 1;
376 return pv;
377 }
378 return NULL;
379}
380
381
382/**
383 * Allocates zeroed memory from the specified simple heap.
384 *
385 * @returns Pointer to the allocated memory block on success.
386 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
387 *
388 * @param Heap The heap to allocate the memory on.
389 * @param cb The requested heap block size.
390 * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
391 * Must be a power of 2.
392 */
393RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment)
394{
395 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
396
397 /*
398 * Validate and adjust the input.
399 */
400 AssertPtrReturn(pHeapInt, NULL);
401 if (cb < RTHEAPSIMPLE_MIN_BLOCK)
402 cb = RTHEAPSIMPLE_MIN_BLOCK;
403 else
404 cb = RT_ALIGN_Z(cb, RTHEAPSIMPLE_ALIGNMENT);
405 if (!cbAlignment)
406 cbAlignment = RTHEAPSIMPLE_ALIGNMENT;
407 else
408 {
409 Assert(!(cbAlignment & (cbAlignment - 1)));
410 Assert((cbAlignment & ~(cbAlignment - 1)) == cbAlignment);
411 if (cbAlignment < RTHEAPSIMPLE_ALIGNMENT)
412 cbAlignment = RTHEAPSIMPLE_ALIGNMENT;
413 }
414
415 /*
416 * Do the allocation.
417 */
418 PRTHEAPSIMPLEBLOCK pBlock = rtHeapSimpleAllocBlock(pHeapInt, cb, cbAlignment);
419 if (RT_LIKELY(pBlock))
420 {
421 void *pv = pBlock + 1;
422 memset(pv, 0, cb);
423 return pv;
424 }
425 return NULL;
426}
427
428
429/**
430 * Allocates a block of memory from the specified heap.
431 *
432 * No parameter validation or adjustment is preformed.
433 *
434 * @returns Pointer to the allocated block.
435 * @returns NULL on failure.
436 * @param pHeap The heap.
437 * @param cb Size of the memory block to allocate.
438 * @param uAlignment The alignment specifications for the allocated block.
439 */
440static PRTHEAPSIMPLEBLOCK rtHeapSimpleAllocBlock(PRTHEAPSIMPLEINTERNAL pHeapInt, size_t cb, size_t uAlignment)
441{
442#ifdef RTHEAPSIMPLE_STRICT
443 rtHeapSimpleAssertAll(pHeapInt);
444#endif
445
446 /*
447 * Search for a fitting block from the lower end of the heap.
448 */
449 PRTHEAPSIMPLEBLOCK pRet = NULL;
450 for (PRTHEAPSIMPLEFREE pFree = pHeapInt->pFreeHead;
451 pFree;
452 pFree = pFree->pNext)
453 {
454 ASSERT_BLOCK_FREE(pHeapInt, pFree);
455
456 /*
457 * Match for size and alignment.
458 */
459 if (pFree->cb < cb)
460 continue;
461 uintptr_t offAlign = (uintptr_t)(&pFree->Core + 1) & (uAlignment - 1);
462 if (offAlign)
463 {
464 offAlign = uAlignment - offAlign;
465 if (pFree->cb - offAlign < cb)
466 continue;
467
468 /*
469 * Make a stack copy of the free block header and adjust the pointer.
470 */
471 RTHEAPSIMPLEFREE Free = *pFree;
472 pFree = (PRTHEAPSIMPLEFREE)((uintptr_t)pFree + offAlign);
473
474 /*
475 * Donate offAlign bytes to the node in front of us.
476 * If we're the head node, we'll have to create a fake node. We'll
477 * mark it USED for simplicity.
478 *
479 * (Should this policy of donating memory to the guy in front of us
480 * cause big 'leaks', we could create a new free node if there is room
481 * for that.)
482 */
483 PRTHEAPSIMPLEBLOCK pPrev = Free.Core.pPrev;
484 if (pPrev)
485 {
486 AssertMsg(!RTHEAPSIMPLEBLOCK_IS_FREE(pPrev), ("Impossible!\n"));
487 pPrev->pNext = &pFree->Core;
488 }
489 else
490 {
491 pPrev = (PRTHEAPSIMPLEBLOCK)(pHeapInt + 1);
492 Assert(pPrev == &pFree->Core);
493 pPrev->pPrev = NULL;
494 pPrev->pNext = &pFree->Core;
495 pPrev->pHeap = pHeapInt;
496 pPrev->fFlags = RTHEAPSIMPLEBLOCK_FLAGS_MAGIC;
497 }
498 pHeapInt->cbFree -= offAlign;
499
500 /*
501 * Recreate pFree in the new position and adjust the neighbours.
502 */
503 *pFree = Free;
504
505 /* the core */
506 if (pFree->Core.pNext)
507 pFree->Core.pNext->pPrev = &pFree->Core;
508 pFree->Core.pPrev = pPrev;
509
510 /* the free part */
511 pFree->cb -= offAlign;
512 if (pFree->pNext)
513 pFree->pNext->pPrev = pFree;
514 else
515 pHeapInt->pFreeTail = pFree;
516 if (pFree->pPrev)
517 pFree->pPrev->pNext = pFree;
518 else
519 pHeapInt->pFreeHead = pFree;
520 ASSERT_BLOCK_FREE(pHeapInt, pFree);
521 ASSERT_BLOCK_USED(pHeapInt, pPrev);
522 }
523
524 /*
525 * Split off a new FREE block?
526 */
527 if (pFree->cb >= cb + RT_ALIGN_Z(sizeof(RTHEAPSIMPLEFREE), RTHEAPSIMPLE_ALIGNMENT))
528 {
529 /*
530 * Move the FREE block up to make room for the new USED block.
531 */
532 PRTHEAPSIMPLEFREE pNew = (PRTHEAPSIMPLEFREE)((uintptr_t)&pFree->Core + cb + sizeof(RTHEAPSIMPLEBLOCK));
533
534 pNew->Core.pNext = pFree->Core.pNext;
535 if (pFree->Core.pNext)
536 pFree->Core.pNext->pPrev = &pNew->Core;
537 pNew->Core.pPrev = &pFree->Core;
538 pNew->Core.pHeap = pHeapInt;
539 pNew->Core.fFlags = RTHEAPSIMPLEBLOCK_FLAGS_MAGIC | RTHEAPSIMPLEBLOCK_FLAGS_FREE;
540
541 pNew->pNext = pFree->pNext;
542 if (pNew->pNext)
543 pNew->pNext->pPrev = pNew;
544 else
545 pHeapInt->pFreeTail = pNew;
546 pNew->pPrev = pFree->pPrev;
547 if (pNew->pPrev)
548 pNew->pPrev->pNext = pNew;
549 else
550 pHeapInt->pFreeHead = pNew;
551 pNew->cb = (pNew->Core.pNext ? (uintptr_t)pNew->Core.pNext : (uintptr_t)pHeapInt->pvEnd) \
552 - (uintptr_t)pNew - sizeof(RTHEAPSIMPLEBLOCK);
553 ASSERT_BLOCK_FREE(pHeapInt, pNew);
554
555 /*
556 * Update the old FREE node making it a USED node.
557 */
558 pFree->Core.fFlags &= ~RTHEAPSIMPLEBLOCK_FLAGS_FREE;
559 pFree->Core.pNext = &pNew->Core;
560 pHeapInt->cbFree -= pFree->cb;
561 pHeapInt->cbFree += pNew->cb;
562 pRet = &pFree->Core;
563 ASSERT_BLOCK_USED(pHeapInt, pRet);
564 }
565 else
566 {
567 /*
568 * Link it out of the free list.
569 */
570 if (pFree->pNext)
571 pFree->pNext->pPrev = pFree->pPrev;
572 else
573 pHeapInt->pFreeTail = pFree->pPrev;
574 if (pFree->pPrev)
575 pFree->pPrev->pNext = pFree->pNext;
576 else
577 pHeapInt->pFreeHead = pFree->pNext;
578
579 /*
580 * Convert it to a used block.
581 */
582 pHeapInt->cbFree -= pFree->cb;
583 pFree->Core.fFlags &= ~RTHEAPSIMPLEBLOCK_FLAGS_FREE;
584 pRet = &pFree->Core;
585 ASSERT_BLOCK_USED(pHeapInt, pRet);
586 }
587 break;
588 }
589
590#ifdef RTHEAPSIMPLE_STRICT
591 rtHeapSimpleAssertAll(pHeapInt);
592#endif
593 return pRet;
594}
595
596
597
598
599/**
600 * Frees memory allocated from a simple heap.
601 *
602 * @param Heap The heap. This is optional and will only be used for strict assertions.
603 * @param pv The heap block returned by RTHeapSimple
604 */
605RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE Heap, void *pv)
606{
607 /*
608 * Validate input.
609 */
610 if (!pv)
611 return;
612 AssertPtr(pv);
613 Assert(RT_ALIGN_P(pv, RTHEAPSIMPLE_ALIGNMENT) == pv);
614
615 /*
616 * Get the block and heap. If in strict mode, validate these.
617 */
618 PRTHEAPSIMPLEBLOCK pBlock = (PRTHEAPSIMPLEBLOCK)pv - 1;
619 PRTHEAPSIMPLEINTERNAL pHeapInt = pBlock->pHeap;
620 ASSERT_BLOCK_USED(pHeapInt, pBlock);
621 ASSERT_ANCHOR(pHeapInt);
622 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)Heap || !Heap);
623
624#ifdef RTHEAPSIMPLE_FREE_POISON
625 /*
626 * Poison the block.
627 */
628 const size_t cbBlock = (pBlock->pNext ? (uintptr_t)pBlock->pNext : (uintptr_t)pHeapInt->pvEnd)
629 - (uintptr_t)pBlock - sizeof(RTHEAPSIMPLEBLOCK);
630 memset(pBlock + 1, RTHEAPSIMPLE_FREE_POISON, cbBlock);
631#endif
632
633 /*
634 * Call worker which does the actual job.
635 */
636 rtHeapSimpleFreeBlock(pHeapInt, pBlock);
637}
638
639
640/**
641 * Free memory a memory block.
642 *
643 * @param pHeapInt The heap.
644 * @param pBlock The memory block to free.
645 */
646static void rtHeapSimpleFreeBlock(PRTHEAPSIMPLEINTERNAL pHeapInt, PRTHEAPSIMPLEBLOCK pBlock)
647{
648 PRTHEAPSIMPLEFREE pFree = (PRTHEAPSIMPLEFREE)pBlock;
649
650#ifdef RTHEAPSIMPLE_STRICT
651 rtHeapSimpleAssertAll(pHeapInt);
652#endif
653
654 /*
655 * Look for the closest free list blocks by walking the blocks right
656 * of us (both list are sorted on address).
657 */
658 PRTHEAPSIMPLEFREE pLeft = NULL;
659 PRTHEAPSIMPLEFREE pRight = NULL;
660 if (pHeapInt->pFreeTail)
661 {
662 pRight = (PRTHEAPSIMPLEFREE)pFree->Core.pNext;
663 while (pRight && !RTHEAPSIMPLEBLOCK_IS_FREE(&pRight->Core))
664 {
665 ASSERT_BLOCK(pHeapInt, &pRight->Core);
666 pRight = (PRTHEAPSIMPLEFREE)pRight->Core.pNext;
667 }
668 if (!pRight)
669 pLeft = pHeapInt->pFreeTail;
670 else
671 {
672 ASSERT_BLOCK_FREE(pHeapInt, pRight);
673 pLeft = pRight->pPrev;
674 }
675 if (pLeft)
676 ASSERT_BLOCK_FREE(pHeapInt, pLeft);
677 }
678 AssertMsgReturnVoid(pLeft != pFree, ("Freed twice! pv=%p (pBlock=%p)\n", pBlock + 1, pBlock));
679 ASSERT_L(pLeft, pFree);
680 Assert(!pRight || (uintptr_t)pRight > (uintptr_t)pFree);
681 Assert(!pLeft || pLeft->pNext == pRight);
682
683 /*
684 * Insert at the head of the free block list?
685 */
686 if (!pLeft)
687 {
688 Assert(pRight == pHeapInt->pFreeHead);
689 pFree->Core.fFlags |= RTHEAPSIMPLEBLOCK_FLAGS_FREE;
690 pFree->pPrev = NULL;
691 pFree->pNext = pRight;
692 if (pRight)
693 pRight->pPrev = pFree;
694 else
695 pHeapInt->pFreeTail = pFree;
696 pHeapInt->pFreeHead = pFree;
697 }
698 else
699 {
700 /*
701 * Can we merge with left hand free block?
702 */
703 if (pLeft->Core.pNext == &pFree->Core)
704 {
705 pLeft->Core.pNext = pFree->Core.pNext;
706 if (pFree->Core.pNext)
707 pFree->Core.pNext->pPrev = &pLeft->Core;
708 pHeapInt->cbFree -= pLeft->cb;
709 pFree = pLeft;
710 }
711 /*
712 * No, just link it into the free list then.
713 */
714 else
715 {
716 pFree->Core.fFlags |= RTHEAPSIMPLEBLOCK_FLAGS_FREE;
717 pFree->pNext = pRight;
718 pFree->pPrev = pLeft;
719 pLeft->pNext = pFree;
720 if (pRight)
721 pRight->pPrev = pFree;
722 else
723 pHeapInt->pFreeTail = pFree;
724 }
725 }
726
727 /*
728 * Can we merge with right hand free block?
729 */
730 if ( pRight
731 && pRight->Core.pPrev == &pFree->Core)
732 {
733 /* core */
734 pFree->Core.pNext = pRight->Core.pNext;
735 if (pRight->Core.pNext)
736 pRight->Core.pNext->pPrev = &pFree->Core;
737
738 /* free */
739 pFree->pNext = pRight->pNext;
740 if (pRight->pNext)
741 pRight->pNext->pPrev = pFree;
742 else
743 pHeapInt->pFreeTail = pFree;
744 pHeapInt->cbFree -= pRight->cb;
745 }
746
747 /*
748 * Calculate the size and update free stats.
749 */
750 pFree->cb = (pFree->Core.pNext ? (uintptr_t)pFree->Core.pNext : (uintptr_t)pHeapInt->pvEnd)
751 - (uintptr_t)pFree - sizeof(RTHEAPSIMPLEBLOCK);
752 pHeapInt->cbFree += pFree->cb;
753 ASSERT_BLOCK_FREE(pHeapInt, pFree);
754
755#ifdef RTHEAPSIMPLE_STRICT
756 rtHeapSimpleAssertAll(pHeapInt);
757#endif
758}
759
760
761#ifdef RTHEAPSIMPLE_STRICT
762/**
763 * Internal consitency check (relying on assertions).
764 * @param pHeapInt
765 */
766static void rtHeapSimpleAssertAll(PRTHEAPSIMPLEINTERNAL pHeapInt)
767{
768 PRTHEAPSIMPLEFREE pPrev = NULL;
769 PRTHEAPSIMPLEFREE pPrevFree = NULL;
770 for (PRTHEAPSIMPLEFREE pBlock = (PRTHEAPSIMPLEFREE)(pHeapInt + 1);
771 pBlock;
772 pBlock = (PRTHEAPSIMPLEFREE)pBlock->Core.pNext)
773 {
774 if (RTHEAPSIMPLEBLOCK_IS_FREE(&pBlock->Core))
775 {
776 ASSERT_BLOCK_FREE(pHeapInt, pBlock);
777 Assert(pBlock->pPrev == pPrevFree);
778 Assert(pPrevFree || pHeapInt->pFreeHead == pBlock);
779 pPrevFree = pBlock;
780 }
781 else
782 ASSERT_BLOCK_USED(pHeapInt, &pBlock->Core);
783 Assert(!pPrev || pPrev == (PRTHEAPSIMPLEFREE)pBlock->Core.pPrev);
784 pPrev = pBlock;
785 }
786 Assert(pHeapInt->pFreeTail == pPrevFree);
787}
788#endif
789
790
791/**
792 * Gets the size of the specified heap block.
793 *
794 * @returns The actual size of the heap block.
795 * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An invalid \a pv
796 * can also cause traps or trigger assertions.
797 * @param Heap The heap. This is optional and will only be used for strict assertions.
798 * @param pv The heap block returned by RTHeapSimple
799 */
800RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE Heap, void *pv)
801{
802 /*
803 * Validate input.
804 */
805 if (!pv)
806 return 0;
807 AssertPtrReturn(pv, 0);
808 AssertReturn(RT_ALIGN_P(pv, RTHEAPSIMPLE_ALIGNMENT) == pv, 0);
809
810 /*
811 * Get the block and heap. If in strict mode, validate these.
812 */
813 PRTHEAPSIMPLEBLOCK pBlock = (PRTHEAPSIMPLEBLOCK)pv - 1;
814 PRTHEAPSIMPLEINTERNAL pHeapInt = pBlock->pHeap;
815 ASSERT_BLOCK_USED(pHeapInt, pBlock);
816 ASSERT_ANCHOR(pHeapInt);
817 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)Heap || !Heap);
818
819 /*
820 * Calculate the block size.
821 */
822 const size_t cbBlock = (pBlock->pNext ? (uintptr_t)pBlock->pNext : (uintptr_t)pHeapInt->pvEnd)
823 - (uintptr_t)pBlock- sizeof(RTHEAPSIMPLEBLOCK);
824 return cbBlock;
825}
826
827
828/**
829 * Gets the size of the heap.
830 *
831 * This size includes all the internal heap structures. So, even if the heap is
832 * empty the RTHeapSimpleGetFreeSize() will never reach the heap size returned
833 * by this function.
834 *
835 * @returns The heap size.
836 * @returns 0 if heap was safely detected as being bad.
837 * @param Heap The heap.
838 */
839RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE Heap)
840{
841 if (Heap == NIL_RTHEAPSIMPLE)
842 return 0;
843 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
844 AssertPtrReturn(pHeapInt, 0);
845 ASSERT_ANCHOR(pHeapInt);
846 return pHeapInt->cbHeap;
847}
848
849
850/**
851 * Returns the sum of all free heap blocks.
852 *
853 * This is the amount of memory you can theoretically allocate
854 * if you do allocations exactly matching the free blocks.
855 *
856 * @returns The size of the free blocks.
857 * @returns 0 if heap was safely detected as being bad.
858 * @param Heap The heap.
859 */
860RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE Heap)
861{
862 if (Heap == NIL_RTHEAPSIMPLE)
863 return 0;
864 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap;
865 AssertPtrReturn(pHeapInt, 0);
866 ASSERT_ANCHOR(pHeapInt);
867 return pHeapInt->cbFree;
868}
869
870
871/**
872 * Dumps the hypervisor heap.
873 *
874 * @param Heap The heap handle.
875 * @param pfnPrintf Printf like function that groks IPRT formatting.
876 */
877RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf)
878{
879 PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)Heap;
880 pfnPrintf("**** Dumping Heap %p - cbHeap=%zx cbFree=%zx ****\n",
881 Heap, pHeapInt->cbHeap, pHeapInt->cbFree);
882
883 for (PRTHEAPSIMPLEFREE pBlock = (PRTHEAPSIMPLEFREE)(pHeapInt + 1);
884 pBlock;
885 pBlock = (PRTHEAPSIMPLEFREE)pBlock->Core.pNext)
886 {
887 size_t cb = (pBlock->pNext ? (uintptr_t)pBlock->Core.pNext : (uintptr_t)pHeapInt->pvEnd)
888 - (uintptr_t)pBlock - sizeof(RTHEAPSIMPLEBLOCK);
889 if (RTHEAPSIMPLEBLOCK_IS_FREE(&pBlock->Core))
890 pfnPrintf("%p %06x FREE pNext=%p pPrev=%p fFlags=%#x cb=%#06x : cb=%#06x pNext=%p pPrev=%p\n",
891 pBlock, (uintptr_t)pBlock - (uintptr_t)(pHeapInt + 1), pBlock->Core.pNext, pBlock->Core.pPrev, pBlock->Core.fFlags, cb,
892 pBlock->cb, pBlock->pNext, pBlock->pPrev);
893 else
894 pfnPrintf("%p %06x USED pNext=%p pPrev=%p fFlags=%#x cb=%#06x\n",
895 pBlock, (uintptr_t)pBlock - (uintptr_t)(pHeapInt + 1), pBlock->Core.pNext, pBlock->Core.pPrev, pBlock->Core.fFlags, cb);
896 }
897 pfnPrintf("**** Done dumping Heap %p ****\n", Heap);
898}
899
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette