VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/PhysHeap.cpp@ 44824

Last change on this file since 44824 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 KB
Line 
1/* $Revision: 44528 $ */
2/** @file
3 * VBoxGuestLibR0 - Physical memory heap.
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#include "VBGLInternal.h"
28
29#include <iprt/assert.h>
30#include <iprt/semaphore.h>
31#include <iprt/alloc.h>
32
33/* Physical memory heap consists of double linked list
34 * of chunks. Memory blocks are allocated inside these chunks
35 * and are members of Allocated and Free double linked lists.
36 *
37 * When allocating a block, we search in Free linked
38 * list for a suitable free block. If there is no such block,
39 * a new chunk is allocated and the new block is taken from
40 * the new chunk as the only chunk-sized free block.
41 * Allocated block is excluded from the Free list and goes to
42 * Alloc list.
43 *
44 * When freeing block, we check the pointer and then
45 * exclude block from Alloc list and move it to free list.
46 *
47 * For each chunk we maintain the allocated blocks counter.
48 * if 2 (or more) entire chunks are free they are immediately
49 * deallocated, so we always have at most 1 free chunk.
50 *
51 * When freeing blocks, two subsequent free blocks are always
52 * merged together. Current implementation merges blocks only
53 * when there is a block after the just freed one.
54 *
55 */
56
57#define VBGL_PH_ASSERT Assert
58#define VBGL_PH_ASSERTMsg AssertMsg
59
60// #define DUMPHEAP
61
62#ifdef DUMPHEAP
63# define VBGL_PH_dprintf(a) RTAssertMsg2Weak a
64#else
65# define VBGL_PH_dprintf(a)
66#endif
67
68/* Heap block signature */
69#define VBGL_PH_BLOCKSIGNATURE (0xADDBBBBB)
70
71
72/* Heap chunk signature */
73#define VBGL_PH_CHUNKSIGNATURE (0xADDCCCCC)
74/* Heap chunk allocation unit */
75#define VBGL_PH_CHUNKSIZE (0x10000)
76
77/* Heap block bit flags */
78#define VBGL_PH_BF_ALLOCATED (0x1)
79
80struct _VBGLPHYSHEAPBLOCK
81{
82 uint32_t u32Signature;
83
84 /* Size of user data in the block. Does not include the block header. */
85 uint32_t cbDataSize;
86
87 uint32_t fu32Flags;
88
89 struct _VBGLPHYSHEAPBLOCK *pNext;
90 struct _VBGLPHYSHEAPBLOCK *pPrev;
91
92 struct _VBGLPHYSHEAPCHUNK *pChunk;
93};
94
95struct _VBGLPHYSHEAPCHUNK
96{
97 uint32_t u32Signature;
98
99 /* Size of the chunk. Includes the chunk header. */
100 uint32_t cbSize;
101
102 /* Physical address of the chunk */
103 RTCCPHYS physAddr;
104
105 /* Number of allocated blocks in the chunk */
106 int32_t cAllocatedBlocks;
107
108 struct _VBGLPHYSHEAPCHUNK *pNext;
109 struct _VBGLPHYSHEAPCHUNK *pPrev;
110};
111
112
113#ifndef DUMPHEAP
114#define dumpheap(a)
115#else
116void dumpheap (char *point)
117{
118 VBGL_PH_dprintf(("VBGL_PH dump at '%s'\n", point));
119
120 VBGL_PH_dprintf(("Chunks:\n"));
121
122 VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead;
123
124 while (pChunk)
125 {
126 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, allocated = %8d, phys = %08X\n",
127 pChunk, pChunk->pNext, pChunk->pPrev, pChunk->u32Signature, pChunk->cbSize, pChunk->cAllocatedBlocks, pChunk->physAddr));
128
129 pChunk = pChunk->pNext;
130 }
131
132 VBGL_PH_dprintf(("Allocated blocks:\n"));
133
134 VBGLPHYSHEAPBLOCK *pBlock = g_vbgldata.pAllocBlocksHead;
135
136 while (pBlock)
137 {
138 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
139 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->u32Signature, pBlock->cbDataSize, pBlock->fu32Flags, pBlock->pChunk));
140
141 pBlock = pBlock->pNext;
142 }
143
144 VBGL_PH_dprintf(("Free blocks:\n"));
145
146 pBlock = g_vbgldata.pFreeBlocksHead;
147
148 while (pBlock)
149 {
150 VBGL_PH_dprintf(("%p: pNext = %p, pPrev = %p, sign = %08X, size = %8d, flags = %08X, pChunk = %p\n",
151 pBlock, pBlock->pNext, pBlock->pPrev, pBlock->u32Signature, pBlock->cbDataSize, pBlock->fu32Flags, pBlock->pChunk));
152
153 pBlock = pBlock->pNext;
154 }
155
156 VBGL_PH_dprintf(("VBGL_PH dump at '%s' done\n", point));
157}
158#endif
159
160
161DECLINLINE(void *) vbglPhysHeapBlock2Data (VBGLPHYSHEAPBLOCK *pBlock)
162{
163 return (void *)(pBlock? (char *)pBlock + sizeof (VBGLPHYSHEAPBLOCK): NULL);
164}
165
166DECLINLINE(VBGLPHYSHEAPBLOCK *) vbglPhysHeapData2Block (void *p)
167{
168 VBGLPHYSHEAPBLOCK *pBlock = (VBGLPHYSHEAPBLOCK *)(p? (char *)p - sizeof (VBGLPHYSHEAPBLOCK): NULL);
169
170 VBGL_PH_ASSERTMsg(pBlock == NULL || pBlock->u32Signature == VBGL_PH_BLOCKSIGNATURE,
171 ("pBlock->u32Signature = %08X\n", pBlock->u32Signature));
172
173 return pBlock;
174}
175
176DECLINLINE(int) vbglPhysHeapEnter (void)
177{
178 int rc = RTSemFastMutexRequest(g_vbgldata.mutexHeap);
179
180 VBGL_PH_ASSERTMsg(RT_SUCCESS(rc),
181 ("Failed to request heap mutex, rc = %Rrc\n", rc));
182
183 return rc;
184}
185
186DECLINLINE(void) vbglPhysHeapLeave (void)
187{
188 RTSemFastMutexRelease(g_vbgldata.mutexHeap);
189}
190
191
192static void vbglPhysHeapInitBlock (VBGLPHYSHEAPBLOCK *pBlock, VBGLPHYSHEAPCHUNK *pChunk, uint32_t cbDataSize)
193{
194 VBGL_PH_ASSERT(pBlock != NULL);
195 VBGL_PH_ASSERT(pChunk != NULL);
196
197 pBlock->u32Signature = VBGL_PH_BLOCKSIGNATURE;
198 pBlock->cbDataSize = cbDataSize;
199 pBlock->fu32Flags = 0;
200 pBlock->pNext = NULL;
201 pBlock->pPrev = NULL;
202 pBlock->pChunk = pChunk;
203}
204
205
206static void vbglPhysHeapInsertBlock (VBGLPHYSHEAPBLOCK *pInsertAfter, VBGLPHYSHEAPBLOCK *pBlock)
207{
208 VBGL_PH_ASSERTMsg(pBlock->pNext == NULL,
209 ("pBlock->pNext = %p\n", pBlock->pNext));
210 VBGL_PH_ASSERTMsg(pBlock->pPrev == NULL,
211 ("pBlock->pPrev = %p\n", pBlock->pPrev));
212
213 if (pInsertAfter)
214 {
215 pBlock->pNext = pInsertAfter->pNext;
216 pBlock->pPrev = pInsertAfter;
217
218 if (pInsertAfter->pNext)
219 {
220 pInsertAfter->pNext->pPrev = pBlock;
221 }
222
223 pInsertAfter->pNext = pBlock;
224 }
225 else
226 {
227 /* inserting to head of list */
228 pBlock->pPrev = NULL;
229
230 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
231 {
232 pBlock->pNext = g_vbgldata.pAllocBlocksHead;
233
234 if (g_vbgldata.pAllocBlocksHead)
235 {
236 g_vbgldata.pAllocBlocksHead->pPrev = pBlock;
237 }
238
239 g_vbgldata.pAllocBlocksHead = pBlock;
240 }
241 else
242 {
243 pBlock->pNext = g_vbgldata.pFreeBlocksHead;
244
245 if (g_vbgldata.pFreeBlocksHead)
246 {
247 g_vbgldata.pFreeBlocksHead->pPrev = pBlock;
248 }
249
250 g_vbgldata.pFreeBlocksHead = pBlock;
251 }
252 }
253}
254
255static void vbglPhysHeapExcludeBlock (VBGLPHYSHEAPBLOCK *pBlock)
256{
257 if (pBlock->pNext)
258 {
259 pBlock->pNext->pPrev = pBlock->pPrev;
260 }
261 else
262 {
263 /* this is tail of list but we do not maintain tails of block lists.
264 * so do nothing.
265 */
266 ;
267 }
268
269 if (pBlock->pPrev)
270 {
271 pBlock->pPrev->pNext = pBlock->pNext;
272 }
273 else
274 {
275 /* this is head of list but we do not maintain tails of block lists. */
276 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
277 {
278 g_vbgldata.pAllocBlocksHead = pBlock->pNext;
279 }
280 else
281 {
282 g_vbgldata.pFreeBlocksHead = pBlock->pNext;
283 }
284 }
285
286 pBlock->pNext = NULL;
287 pBlock->pPrev = NULL;
288}
289
290static VBGLPHYSHEAPBLOCK *vbglPhysHeapChunkAlloc (uint32_t cbSize)
291{
292 RTCCPHYS physAddr;
293 VBGLPHYSHEAPCHUNK *pChunk;
294 VBGLPHYSHEAPBLOCK *pBlock;
295 VBGL_PH_dprintf(("Allocating new chunk of size %d\n", cbSize));
296
297 /* Compute chunk size to allocate */
298 if (cbSize < VBGL_PH_CHUNKSIZE)
299 {
300 /* Includes case of block size 0 during initialization */
301 cbSize = VBGL_PH_CHUNKSIZE;
302 }
303 else
304 {
305 /* Round up to next chunk size, which must be power of 2 */
306 cbSize = (cbSize + (VBGL_PH_CHUNKSIZE - 1)) & ~(VBGL_PH_CHUNKSIZE - 1);
307 }
308
309 physAddr = 0;
310 /* This function allocates physical contiguous memory (below 4GB) according to the IPRT docs.
311 * Address < 4G is required for the port IO.
312 */
313 pChunk = (VBGLPHYSHEAPCHUNK *)RTMemContAlloc (&physAddr, cbSize);
314
315 if (!pChunk)
316 {
317 LogRel(("vbglPhysHeapChunkAlloc: failed to alloc %u contiguous bytes.\n", cbSize));
318 return NULL;
319 }
320
321 pChunk->u32Signature = VBGL_PH_CHUNKSIGNATURE;
322 pChunk->cbSize = cbSize;
323 pChunk->physAddr = physAddr;
324 pChunk->cAllocatedBlocks = 0;
325 pChunk->pNext = g_vbgldata.pChunkHead;
326 pChunk->pPrev = NULL;
327
328 /* Initialize the free block, which now occupies entire chunk. */
329 pBlock = (VBGLPHYSHEAPBLOCK *)((char *)pChunk + sizeof (VBGLPHYSHEAPCHUNK));
330
331 vbglPhysHeapInitBlock (pBlock, pChunk, cbSize - sizeof (VBGLPHYSHEAPCHUNK) - sizeof (VBGLPHYSHEAPBLOCK));
332
333 vbglPhysHeapInsertBlock (NULL, pBlock);
334
335 g_vbgldata.pChunkHead = pChunk;
336
337 VBGL_PH_dprintf(("Allocated chunk %p, block = %p size=%x\n", pChunk, pBlock, cbSize));
338
339 return pBlock;
340}
341
342
343void vbglPhysHeapChunkDelete (VBGLPHYSHEAPCHUNK *pChunk)
344{
345 char *p;
346 VBGL_PH_ASSERT(pChunk != NULL);
347 VBGL_PH_ASSERTMsg(pChunk->u32Signature == VBGL_PH_CHUNKSIGNATURE,
348 ("pChunk->u32Signature = %08X\n", pChunk->u32Signature));
349
350 VBGL_PH_dprintf(("Deleting chunk %p size %x\n", pChunk, pChunk->cbSize));
351
352 /* first scan the chunk and exclude all blocks from lists */
353
354 p = (char *)pChunk + sizeof (VBGLPHYSHEAPCHUNK);
355
356 while (p < (char *)pChunk + pChunk->cbSize)
357 {
358 VBGLPHYSHEAPBLOCK *pBlock = (VBGLPHYSHEAPBLOCK *)p;
359
360 p += pBlock->cbDataSize + sizeof (VBGLPHYSHEAPBLOCK);
361
362 vbglPhysHeapExcludeBlock (pBlock);
363 }
364
365 VBGL_PH_ASSERTMsg(p == (char *)pChunk + pChunk->cbSize,
366 ("p = %p, (char *)pChunk + pChunk->cbSize = %p, pChunk->cbSize = %08X\n",
367 p, (char *)pChunk + pChunk->cbSize, pChunk->cbSize));
368
369 /* Exclude chunk from the chunk list */
370 if (pChunk->pNext)
371 {
372 pChunk->pNext->pPrev = pChunk->pPrev;
373 }
374 else
375 {
376 /* we do not maintain tail */
377 ;
378 }
379
380 if (pChunk->pPrev)
381 {
382 pChunk->pPrev->pNext = pChunk->pNext;
383 }
384 else
385 {
386 /* the chunk was head */
387 g_vbgldata.pChunkHead = pChunk->pNext;
388 }
389
390 RTMemContFree (pChunk, pChunk->cbSize);
391}
392
393
394DECLVBGL(void *) VbglPhysHeapAlloc (uint32_t cbSize)
395{
396 VBGLPHYSHEAPBLOCK *pBlock, *iter;
397 int rc = vbglPhysHeapEnter ();
398
399 if (RT_FAILURE(rc))
400 return NULL;
401
402 dumpheap ("pre alloc");
403
404 pBlock = NULL;
405
406 /* If there are free blocks in the heap, look at them. */
407 iter = g_vbgldata.pFreeBlocksHead;
408
409 /* There will be not many blocks in the heap, so
410 * linear search would be fast enough.
411 */
412
413 while (iter)
414 {
415 if (iter->cbDataSize == cbSize)
416 {
417 /* exact match */
418 pBlock = iter;
419 break;
420 }
421
422 /* Looking for a free block with nearest size */
423 if (iter->cbDataSize > cbSize)
424 {
425 if (pBlock)
426 {
427 if (iter->cbDataSize < pBlock->cbDataSize)
428 {
429 pBlock = iter;
430 }
431 }
432 else
433 {
434 pBlock = iter;
435 }
436 }
437
438 iter = iter->pNext;
439 }
440
441 if (!pBlock)
442 {
443 /* No free blocks, allocate a new chunk,
444 * the only free block of the chunk will
445 * be returned.
446 */
447 pBlock = vbglPhysHeapChunkAlloc (cbSize);
448 }
449
450 if (pBlock)
451 {
452 VBGL_PH_ASSERTMsg(pBlock->u32Signature == VBGL_PH_BLOCKSIGNATURE,
453 ("pBlock = %p, pBlock->u32Signature = %08X\n", pBlock, pBlock->u32Signature));
454 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) == 0,
455 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
456
457 /* We have a free block, either found or allocated. */
458
459 if (pBlock->cbDataSize > 2*(cbSize + sizeof (VBGLPHYSHEAPBLOCK)))
460 {
461 /* Data will occupy less than a half of the block,
462 * the block should be split.
463 */
464 iter = (VBGLPHYSHEAPBLOCK *)((char *)pBlock + sizeof (VBGLPHYSHEAPBLOCK) + cbSize);
465
466 /* Init the new 'iter' block, initialized blocks are always marked as free. */
467 vbglPhysHeapInitBlock (iter, pBlock->pChunk, pBlock->cbDataSize - cbSize - sizeof (VBGLPHYSHEAPBLOCK));
468
469 pBlock->cbDataSize = cbSize;
470
471 /* Insert the new 'iter' block after the 'pBlock' in the free list */
472 vbglPhysHeapInsertBlock (pBlock, iter);
473 }
474
475 /* Exclude pBlock from free list */
476 vbglPhysHeapExcludeBlock (pBlock);
477
478 /* Mark as allocated */
479 pBlock->fu32Flags |= VBGL_PH_BF_ALLOCATED;
480
481 /* Insert to allocated list */
482 vbglPhysHeapInsertBlock (NULL, pBlock);
483
484 /* Adjust the chunk allocated blocks counter */
485 pBlock->pChunk->cAllocatedBlocks++;
486 }
487
488 dumpheap ("post alloc");
489
490 vbglPhysHeapLeave ();
491 VBGL_PH_dprintf(("VbglPhysHeapAlloc %x size %x\n", vbglPhysHeapBlock2Data (pBlock), pBlock->cbDataSize));
492
493 return vbglPhysHeapBlock2Data (pBlock);
494}
495
496DECLVBGL(RTCCPHYS) VbglPhysHeapGetPhysAddr (void *p)
497{
498 RTCCPHYS physAddr = 0;
499 VBGLPHYSHEAPBLOCK *pBlock = vbglPhysHeapData2Block (p);
500
501 if (pBlock)
502 {
503 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) != 0,
504 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
505
506 if (pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED)
507 physAddr = pBlock->pChunk->physAddr + ((char *)p - (char *)pBlock->pChunk);
508 }
509
510 return physAddr;
511}
512
513DECLVBGL(void) VbglPhysHeapFree (void *p)
514{
515 VBGLPHYSHEAPBLOCK *pBlock;
516 VBGLPHYSHEAPBLOCK *pNeighbour;
517
518 int rc = vbglPhysHeapEnter ();
519 if (RT_FAILURE(rc))
520 return;
521
522 dumpheap ("pre free");
523
524 pBlock = vbglPhysHeapData2Block (p);
525
526 if (!pBlock)
527 {
528 vbglPhysHeapLeave ();
529 return;
530 }
531
532 VBGL_PH_ASSERTMsg((pBlock->fu32Flags & VBGL_PH_BF_ALLOCATED) != 0,
533 ("pBlock = %p, pBlock->fu32Flags = %08X\n", pBlock, pBlock->fu32Flags));
534
535 /* Exclude from allocated list */
536 vbglPhysHeapExcludeBlock (pBlock);
537
538 dumpheap ("post exclude");
539
540 VBGL_PH_dprintf(("VbglPhysHeapFree %x size %x\n", p, pBlock->cbDataSize));
541
542 /* Mark as free */
543 pBlock->fu32Flags &= ~VBGL_PH_BF_ALLOCATED;
544
545 /* Insert to free list */
546 vbglPhysHeapInsertBlock (NULL, pBlock);
547
548 dumpheap ("post insert");
549
550 /* Adjust the chunk allocated blocks counter */
551 pBlock->pChunk->cAllocatedBlocks--;
552
553 VBGL_PH_ASSERT(pBlock->pChunk->cAllocatedBlocks >= 0);
554
555 /* Check if we can merge 2 free blocks. To simplify heap maintenance,
556 * we will look at block after the just freed one.
557 * This will not prevent us from detecting free memory chunks.
558 * Also in most cases blocks are deallocated in reverse allocation order
559 * and in that case the merging will work.
560 */
561
562 pNeighbour = (VBGLPHYSHEAPBLOCK *)((char *)p + pBlock->cbDataSize);
563
564 if ((char *)pNeighbour < (char *)pBlock->pChunk + pBlock->pChunk->cbSize
565 && (pNeighbour->fu32Flags & VBGL_PH_BF_ALLOCATED) == 0)
566 {
567 /* The next block is free as well. */
568
569 /* Adjust size of current memory block */
570 pBlock->cbDataSize += pNeighbour->cbDataSize + sizeof (VBGLPHYSHEAPBLOCK);
571
572 /* Exclude the next neighbour */
573 vbglPhysHeapExcludeBlock (pNeighbour);
574 }
575
576 dumpheap ("post merge");
577
578 /* now check if there are 2 or more free chunks */
579 if (pBlock->pChunk->cAllocatedBlocks == 0)
580 {
581 VBGLPHYSHEAPCHUNK *pChunk = g_vbgldata.pChunkHead;
582
583 uint32_t u32FreeChunks = 0;
584
585 while (pChunk)
586 {
587 if (pChunk->cAllocatedBlocks == 0)
588 {
589 u32FreeChunks++;
590 }
591
592 pChunk = pChunk->pNext;
593 }
594
595 if (u32FreeChunks > 1)
596 {
597 /* Delete current chunk, it will also exclude all free blocks
598 * remaining in the chunk from the free list, so the pBlock
599 * will also be invalid after this.
600 */
601 vbglPhysHeapChunkDelete (pBlock->pChunk);
602 }
603 }
604
605 dumpheap ("post free");
606
607 vbglPhysHeapLeave ();
608}
609
610DECLVBGL(int) VbglPhysHeapInit (void)
611{
612 int rc = VINF_SUCCESS;
613
614 /* Allocate the first chunk of the heap. */
615 VBGLPHYSHEAPBLOCK *pBlock = vbglPhysHeapChunkAlloc (0);
616
617 if (!pBlock)
618 rc = VERR_NO_MEMORY;
619
620 RTSemFastMutexCreate(&g_vbgldata.mutexHeap);
621
622 return rc;
623}
624
625DECLVBGL(void) VbglPhysHeapTerminate (void)
626{
627 while (g_vbgldata.pChunkHead)
628 {
629 vbglPhysHeapChunkDelete (g_vbgldata.pChunkHead);
630 }
631
632 RTSemFastMutexDestroy(g_vbgldata.mutexHeap);
633}
634
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