VirtualBox

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

Last change on this file since 26425 was 26425, checked in by vboxsync, 15 years ago

alternative license for VBoxGuestLib is CDDL

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

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