1 | /* $Id: MMHeap.cpp 714 2007-02-06 14:56:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MM - Memory Monitor(/Manager) - 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 LOG_GROUP_MM_HEAP
|
---|
27 | #include <VBox/mm.h>
|
---|
28 | #include <VBox/pgm.h>
|
---|
29 | #include "MMInternal.h"
|
---|
30 | #include <VBox/vm.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 | #include <VBox/param.h>
|
---|
33 | #include <VBox/log.h>
|
---|
34 |
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Internal Functions *
|
---|
42 | *******************************************************************************/
|
---|
43 | static void * mmr3HeapAlloc(PMMHEAP pHeap, MMTAG enmTag, size_t cbSize, bool fZero);
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Allocate and initialize a heap structure and it's associated substructures.
|
---|
49 | *
|
---|
50 | * @returns VBox status.
|
---|
51 | * @param pVM The handle to the VM the heap should be associated with.
|
---|
52 | * @param ppHeap Where to store the heap pointer.
|
---|
53 | */
|
---|
54 | int mmr3HeapCreate(PVM pVM, PMMHEAP *ppHeap)
|
---|
55 | {
|
---|
56 | PMMHEAP pHeap = (PMMHEAP)RTMemAllocZ(sizeof(MMHEAP) + sizeof(MMHEAPSTAT));
|
---|
57 | if (pHeap)
|
---|
58 | {
|
---|
59 | int rc = RTCritSectInit(&pHeap->Lock);
|
---|
60 | if (VBOX_SUCCESS(rc))
|
---|
61 | {
|
---|
62 | /*
|
---|
63 | * Initialize the global stat record.
|
---|
64 | */
|
---|
65 | pHeap->pVM = pVM;
|
---|
66 |
|
---|
67 | pHeap->Stat.pHeap = pHeap;
|
---|
68 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
69 | PMMHEAPSTAT pStat = &pHeap->Stat;
|
---|
70 | STAMR3Register(pVM, &pStat->cAllocations, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cAllocations", STAMUNIT_CALLS, "Number or MMR3HeapAlloc() calls.");
|
---|
71 | STAMR3Register(pVM, &pStat->cReallocations, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cReallocations", STAMUNIT_CALLS, "Number of MMR3HeapRealloc() calls.");
|
---|
72 | STAMR3Register(pVM, &pStat->cFrees, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cFrees", STAMUNIT_CALLS, "Number of MMR3HeapFree() calls.");
|
---|
73 | STAMR3Register(pVM, &pStat->cFailures, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cFailures", STAMUNIT_COUNT, "Number of failures.");
|
---|
74 | STAMR3Register(pVM, &pStat->cbCurAllocated, sizeof(pStat->cbCurAllocated) == sizeof(uint32_t) ? STAMTYPE_U32 : STAMTYPE_U64,
|
---|
75 | STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cbCurAllocated", STAMUNIT_BYTES, "Number of bytes currently allocated.");
|
---|
76 | STAMR3Register(pVM, &pStat->cbAllocated, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cbAllocated", STAMUNIT_BYTES, "Total number of bytes allocated.");
|
---|
77 | STAMR3Register(pVM, &pStat->cbFreed, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, "/MM/R3Heap/cbFreed", STAMUNIT_BYTES, "Total number of bytes freed.");
|
---|
78 | #endif
|
---|
79 | *ppHeap = pHeap;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 | AssertRC(rc);
|
---|
83 | RTMemFree(pHeap);
|
---|
84 | }
|
---|
85 | AssertMsgFailed(("failed to allocate heap structure\n"));
|
---|
86 | return VERR_NO_MEMORY;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Destroy a heap.
|
---|
92 | *
|
---|
93 | * @param pHeap Heap handle.
|
---|
94 | */
|
---|
95 | void mmr3HeapDestroy(PMMHEAP pHeap)
|
---|
96 | {
|
---|
97 | /*
|
---|
98 | * Start by deleting the lock, that'll trap anyone
|
---|
99 | * attempting to use the heap.
|
---|
100 | */
|
---|
101 | RTCritSectDelete(&pHeap->Lock);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Walk the node list and free all the memory.
|
---|
105 | */
|
---|
106 | PMMHEAPHDR pHdr = pHeap->pHead;
|
---|
107 | while (pHdr)
|
---|
108 | {
|
---|
109 | void *pv = pHdr;
|
---|
110 | pHdr = pHdr->pNext;
|
---|
111 | RTMemFree(pv);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Free the stat nodes.
|
---|
116 | */
|
---|
117 | /** @todo free all nodes in a AVL tree. */
|
---|
118 | RTMemFree(pHeap);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Allocate memory associating it with the VM for collective cleanup.
|
---|
124 | *
|
---|
125 | * The memory will be allocated from the default heap but a header
|
---|
126 | * is added in which we keep track of which VM it belongs to and chain
|
---|
127 | * all the allocations together so they can be freed in a one go.
|
---|
128 | *
|
---|
129 | * This interface is typically used for memory block which will not be
|
---|
130 | * freed during the life of the VM.
|
---|
131 | *
|
---|
132 | * @returns Pointer to allocated memory.
|
---|
133 | * @param pVM VM handle.
|
---|
134 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
135 | * basis in addition to a global one. Thus we can easily
|
---|
136 | * identify how memory is used by the VM.
|
---|
137 | * @param cbSize Size of the block.
|
---|
138 | */
|
---|
139 | MMR3DECL(void *) MMR3HeapAlloc(PVM pVM, MMTAG enmTag, size_t cbSize)
|
---|
140 | {
|
---|
141 | if (!pVM->mm.s.pHeap)
|
---|
142 | {
|
---|
143 | int rc = mmr3HeapCreate(pVM, &pVM->mm.s.pHeap);
|
---|
144 | if (VBOX_FAILURE(rc))
|
---|
145 | return NULL;
|
---|
146 | }
|
---|
147 | return mmr3HeapAlloc(pVM->mm.s.pHeap, enmTag, cbSize, false);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Same as MMR3HeapAlloc().
|
---|
153 | *
|
---|
154 | *
|
---|
155 | * @returns Pointer to allocated memory.
|
---|
156 | * @param pVM VM handle.
|
---|
157 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
158 | * basis in addition to a global one. Thus we can easily
|
---|
159 | * identify how memory is used by the VM.
|
---|
160 | * @param cbSize Size of the block.
|
---|
161 | * @param ppv Where to store the pointer to the allocated memory on success.
|
---|
162 | */
|
---|
163 | MMR3DECL(int) MMR3HeapAllocEx(PVM pVM, MMTAG enmTag, size_t cbSize, void **ppv)
|
---|
164 | {
|
---|
165 | if (!pVM->mm.s.pHeap)
|
---|
166 | {
|
---|
167 | int rc = mmr3HeapCreate(pVM, &pVM->mm.s.pHeap);
|
---|
168 | if (VBOX_FAILURE(rc))
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 | void *pv = mmr3HeapAlloc(pVM->mm.s.pHeap, enmTag, cbSize, false);
|
---|
172 | if (pv)
|
---|
173 | {
|
---|
174 | *ppv = pv;
|
---|
175 | return VINF_SUCCESS;
|
---|
176 | }
|
---|
177 | return VERR_NO_MEMORY;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Same as MMR3HeapAlloc() only the memory is zeroed.
|
---|
183 | *
|
---|
184 | *
|
---|
185 | * @returns Pointer to allocated memory.
|
---|
186 | * @param pVM VM handle.
|
---|
187 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
188 | * basis in addition to a global one. Thus we can easily
|
---|
189 | * identify how memory is used by the VM.
|
---|
190 | * @param cbSize Size of the block.
|
---|
191 | */
|
---|
192 | MMR3DECL(void *) MMR3HeapAllocZ(PVM pVM, MMTAG enmTag, size_t cbSize)
|
---|
193 | {
|
---|
194 | if (!pVM->mm.s.pHeap)
|
---|
195 | {
|
---|
196 | int rc = mmr3HeapCreate(pVM, &pVM->mm.s.pHeap);
|
---|
197 | if (VBOX_FAILURE(rc))
|
---|
198 | return NULL;
|
---|
199 | }
|
---|
200 | return mmr3HeapAlloc(pVM->mm.s.pHeap, enmTag, cbSize, true);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Same as MMR3HeapAllocZ().
|
---|
206 | *
|
---|
207 | *
|
---|
208 | * @returns Pointer to allocated memory.
|
---|
209 | * @param pVM VM handle.
|
---|
210 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
211 | * basis in addition to a global one. Thus we can easily
|
---|
212 | * identify how memory is used by the VM.
|
---|
213 | * @param cbSize Size of the block.
|
---|
214 | * @param ppv Where to store the pointer to the allocated memory on success.
|
---|
215 | */
|
---|
216 | MMR3DECL(int) MMR3HeapAllocZEx(PVM pVM, MMTAG enmTag, size_t cbSize, void **ppv)
|
---|
217 | {
|
---|
218 | if (!pVM->mm.s.pHeap)
|
---|
219 | {
|
---|
220 | int rc = mmr3HeapCreate(pVM, &pVM->mm.s.pHeap);
|
---|
221 | if (VBOX_FAILURE(rc))
|
---|
222 | return rc;
|
---|
223 | }
|
---|
224 | void *pv = mmr3HeapAlloc(pVM->mm.s.pHeap, enmTag, cbSize, true);
|
---|
225 | if (pv)
|
---|
226 | {
|
---|
227 | *ppv = pv;
|
---|
228 | return VINF_SUCCESS;
|
---|
229 | }
|
---|
230 | return VERR_NO_MEMORY;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Allocate memory from the heap.
|
---|
236 | *
|
---|
237 | * @returns Pointer to allocated memory.
|
---|
238 | * @param pHeap Heap handle.
|
---|
239 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
240 | * basis in addition to a global one. Thus we can easily
|
---|
241 | * identify how memory is used by the VM.
|
---|
242 | * @param cbSize Size of the block.
|
---|
243 | * @param fZero Whether or not to zero the memory block.
|
---|
244 | */
|
---|
245 | void * mmr3HeapAlloc(PMMHEAP pHeap, MMTAG enmTag, size_t cbSize, bool fZero)
|
---|
246 | {
|
---|
247 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
248 | RTCritSectEnter(&pHeap->Lock);
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Find/alloc statistics nodes.
|
---|
252 | */
|
---|
253 | pHeap->Stat.cAllocations++;
|
---|
254 | PMMHEAPSTAT pStat = (PMMHEAPSTAT)RTAvlULGet(&pHeap->pStatTree, (AVLULKEY)enmTag);
|
---|
255 | if (pStat)
|
---|
256 | {
|
---|
257 | pStat->cAllocations++;
|
---|
258 |
|
---|
259 | RTCritSectLeave(&pHeap->Lock);
|
---|
260 | }
|
---|
261 | else
|
---|
262 | {
|
---|
263 | pStat = (PMMHEAPSTAT)RTMemAllocZ(sizeof(MMHEAPSTAT));
|
---|
264 | if (!pStat)
|
---|
265 | {
|
---|
266 | pHeap->Stat.cFailures++;
|
---|
267 | AssertMsgFailed(("Failed to allocate heap stat record.\n"));
|
---|
268 | RTCritSectLeave(&pHeap->Lock);
|
---|
269 | return NULL;
|
---|
270 | }
|
---|
271 | pStat->Core.Key = (AVLULKEY)enmTag;
|
---|
272 | pStat->pHeap = pHeap;
|
---|
273 | RTAvlULInsert(&pHeap->pStatTree, &pStat->Core);
|
---|
274 |
|
---|
275 | pStat->cAllocations++;
|
---|
276 | RTCritSectLeave(&pHeap->Lock);
|
---|
277 |
|
---|
278 | /* register the statistics */
|
---|
279 | PVM pVM = pHeap->pVM;
|
---|
280 | char szName[80];
|
---|
281 | const char *pszTag = mmR3GetTagName(enmTag);
|
---|
282 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cAllocations", pszTag);
|
---|
283 | STAMR3Register(pVM, &pStat->cAllocations, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_CALLS, "Number or MMR3HeapAlloc() calls.");
|
---|
284 |
|
---|
285 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cReallocations", pszTag);
|
---|
286 | STAMR3Register(pVM, &pStat->cReallocations, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_CALLS, "Number of MMR3HeapRealloc() calls.");
|
---|
287 |
|
---|
288 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cFrees", pszTag);
|
---|
289 | STAMR3Register(pVM, &pStat->cFrees, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_CALLS, "Number of MMR3HeapFree() calls.");
|
---|
290 |
|
---|
291 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cFailures", pszTag);
|
---|
292 | STAMR3Register(pVM, &pStat->cFailures, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_COUNT, "Number of failures.");
|
---|
293 |
|
---|
294 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cbCurAllocated", pszTag);
|
---|
295 | STAMR3Register(pVM, &pStat->cbCurAllocated, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_BYTES, "Number of bytes currently allocated.");
|
---|
296 |
|
---|
297 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cbAllocated", pszTag);
|
---|
298 | STAMR3Register(pVM, &pStat->cbAllocated, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_BYTES, "Total number of bytes allocated.");
|
---|
299 |
|
---|
300 | RTStrPrintf(szName, sizeof(szName), "/MM/R3Heap/%s/cbFreed", pszTag);
|
---|
301 | STAMR3Register(pVM, &pStat->cbFreed, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_BYTES, "Total number of bytes freed.");
|
---|
302 | }
|
---|
303 | #endif
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Validate input.
|
---|
307 | */
|
---|
308 | if (cbSize == 0)
|
---|
309 | {
|
---|
310 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
311 | RTCritSectEnter(&pHeap->Lock);
|
---|
312 | pStat->cFailures++;
|
---|
313 | pHeap->Stat.cFailures++;
|
---|
314 | RTCritSectLeave(&pHeap->Lock);
|
---|
315 | #endif
|
---|
316 | return NULL;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /*
|
---|
320 | * Allocate heap block.
|
---|
321 | */
|
---|
322 | cbSize = RT_ALIGN_Z(cbSize, MMR3HEAP_SIZE_ALIGNMENT) + sizeof(MMHEAPHDR);
|
---|
323 | PMMHEAPHDR pHdr = (PMMHEAPHDR)(fZero ? RTMemAllocZ(cbSize) : RTMemAlloc(cbSize));
|
---|
324 | if (!pHdr)
|
---|
325 | {
|
---|
326 | AssertMsgFailed(("Failed to allocate heap block %d, enmTag=%x(%.4s).\n", cbSize, enmTag, &enmTag));
|
---|
327 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
328 | RTCritSectEnter(&pHeap->Lock);
|
---|
329 | pStat->cFailures++;
|
---|
330 | pHeap->Stat.cFailures++;
|
---|
331 | RTCritSectLeave(&pHeap->Lock);
|
---|
332 | #endif
|
---|
333 | return NULL;
|
---|
334 | }
|
---|
335 | Assert(!((uintptr_t)pHdr & (RTMEM_ALIGNMENT - 1)));
|
---|
336 |
|
---|
337 | RTCritSectEnter(&pHeap->Lock);
|
---|
338 |
|
---|
339 | /*
|
---|
340 | * Init and link in the header.
|
---|
341 | */
|
---|
342 | pHdr->pNext = NULL;
|
---|
343 | pHdr->pPrev = pHeap->pTail;
|
---|
344 | if (pHdr->pPrev)
|
---|
345 | pHdr->pPrev->pNext = pHdr;
|
---|
346 | else
|
---|
347 | pHeap->pHead = pHdr;
|
---|
348 | pHeap->pTail = pHdr;
|
---|
349 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
350 | pHdr->pStat = pStat;
|
---|
351 | #else
|
---|
352 | pHdr->pStat = &pHeap->Stat;
|
---|
353 | #endif
|
---|
354 | pHdr->cbSize = cbSize;
|
---|
355 |
|
---|
356 | /*
|
---|
357 | * Update statistics
|
---|
358 | */
|
---|
359 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
360 | pStat->cbAllocated += cbSize;
|
---|
361 | pStat->cbCurAllocated += cbSize;
|
---|
362 | pHeap->Stat.cbAllocated += cbSize;
|
---|
363 | pHeap->Stat.cbCurAllocated += cbSize;
|
---|
364 | #endif
|
---|
365 |
|
---|
366 | RTCritSectLeave(&pHeap->Lock);
|
---|
367 |
|
---|
368 | return pHdr + 1;
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Reallocate memory allocated with MMR3HeapAlloc() or MMR3HeapRealloc().
|
---|
374 | *
|
---|
375 | * @returns Pointer to reallocated memory.
|
---|
376 | * @param pv Pointer to the memory block to reallocate.
|
---|
377 | * Must not be NULL!
|
---|
378 | * @param cbNewSize New block size.
|
---|
379 | */
|
---|
380 | MMR3DECL(void *) MMR3HeapRealloc(void *pv, size_t cbNewSize)
|
---|
381 | {
|
---|
382 | AssertMsg(pv, ("Invalid pointer pv=%p\n", pv));
|
---|
383 | if (!pv)
|
---|
384 | return NULL;
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * If newsize is zero then this is a free.
|
---|
388 | */
|
---|
389 | if (!cbNewSize)
|
---|
390 | {
|
---|
391 | MMR3HeapFree(pv);
|
---|
392 | return NULL;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Validate header.
|
---|
397 | */
|
---|
398 | PMMHEAPHDR pHdr = (PMMHEAPHDR)pv - 1;
|
---|
399 | if ( pHdr->cbSize & (MMR3HEAP_SIZE_ALIGNMENT - 1)
|
---|
400 | || (uintptr_t)pHdr & (RTMEM_ALIGNMENT - 1))
|
---|
401 | {
|
---|
402 | AssertMsgFailed(("Invalid heap header! pv=%p, size=%#x\n", pv, pHdr->cbSize));
|
---|
403 | return NULL;
|
---|
404 | }
|
---|
405 | Assert(pHdr->pStat != NULL);
|
---|
406 | Assert(!((uintptr_t)pHdr->pNext & (RTMEM_ALIGNMENT - 1)));
|
---|
407 | Assert(!((uintptr_t)pHdr->pPrev & (RTMEM_ALIGNMENT - 1)));
|
---|
408 |
|
---|
409 | PMMHEAP pHeap = pHdr->pStat->pHeap;
|
---|
410 |
|
---|
411 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
412 | RTCritSectEnter(&pHeap->Lock);
|
---|
413 | pHdr->pStat->cReallocations++;
|
---|
414 | pHeap->Stat.cReallocations++;
|
---|
415 | RTCritSectLeave(&pHeap->Lock);
|
---|
416 | #endif
|
---|
417 |
|
---|
418 | /*
|
---|
419 | * Rellocate the block.
|
---|
420 | */
|
---|
421 | cbNewSize = RT_ALIGN_Z(cbNewSize, MMR3HEAP_SIZE_ALIGNMENT) + sizeof(MMHEAPHDR);
|
---|
422 | PMMHEAPHDR pHdrNew = (PMMHEAPHDR)RTMemRealloc(pHdr, cbNewSize);
|
---|
423 | if (!pHdrNew)
|
---|
424 | {
|
---|
425 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
426 | RTCritSectEnter(&pHeap->Lock);
|
---|
427 | pHdr->pStat->cFailures++;
|
---|
428 | pHeap->Stat.cFailures++;
|
---|
429 | RTCritSectLeave(&pHeap->Lock);
|
---|
430 | #endif
|
---|
431 | return NULL;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /*
|
---|
435 | * Update pointers.
|
---|
436 | */
|
---|
437 | if (pHdrNew != pHdr)
|
---|
438 | {
|
---|
439 | RTCritSectEnter(&pHeap->Lock);
|
---|
440 | if (pHdrNew->pPrev)
|
---|
441 | pHdrNew->pPrev->pNext = pHdrNew;
|
---|
442 | else
|
---|
443 | pHeap->pHead = pHdrNew;
|
---|
444 |
|
---|
445 | if (pHdrNew->pNext)
|
---|
446 | pHdrNew->pNext->pPrev = pHdrNew;
|
---|
447 | else
|
---|
448 | pHeap->pTail = pHdrNew;
|
---|
449 | RTCritSectLeave(&pHeap->Lock);
|
---|
450 | }
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * Update statistics.
|
---|
454 | */
|
---|
455 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
456 | RTCritSectEnter(&pHeap->Lock);
|
---|
457 | pHdrNew->pStat->cbAllocated += cbNewSize - pHdrNew->cbSize;
|
---|
458 | pHeap->Stat.cbAllocated += cbNewSize - pHdrNew->cbSize;
|
---|
459 | RTCritSectLeave(&pHeap->Lock);
|
---|
460 | #endif
|
---|
461 |
|
---|
462 | pHdrNew->cbSize = cbNewSize;
|
---|
463 |
|
---|
464 | return pHdrNew + 1;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Duplicates the specified string.
|
---|
470 | *
|
---|
471 | * @returns Pointer to the duplicate.
|
---|
472 | * @returns NULL on failure or when input NULL.
|
---|
473 | * @param pVM The VM handle.
|
---|
474 | * @param enmTag Statistics tag. Statistics are collected on a per tag
|
---|
475 | * basis in addition to a global one. Thus we can easily
|
---|
476 | * identify how memory is used by the VM.
|
---|
477 | * @param psz The string to duplicate. NULL is allowed.
|
---|
478 | */
|
---|
479 | MMR3DECL(char *) MMR3HeapStrDup(PVM pVM, MMTAG enmTag, const char *psz)
|
---|
480 | {
|
---|
481 | if (!psz)
|
---|
482 | return NULL;
|
---|
483 | Assert(VALID_PTR(psz));
|
---|
484 | size_t cch = strlen(psz) + 1;
|
---|
485 | char *pszDup = (char *)MMR3HeapAlloc(pVM, enmTag, cch);
|
---|
486 | if (pszDup)
|
---|
487 | memcpy(pszDup, psz, cch);
|
---|
488 | return pszDup;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Releases memory allocated with MMR3HeapAlloc() or MMR3HeapRealloc().
|
---|
494 | *
|
---|
495 | * @param pv Pointer to the memory block to free.
|
---|
496 | */
|
---|
497 | MMR3DECL(void) MMR3HeapFree(void *pv)
|
---|
498 | {
|
---|
499 | /* Ignore NULL pointers. */
|
---|
500 | if (!pv)
|
---|
501 | return;
|
---|
502 |
|
---|
503 | /*
|
---|
504 | * Validate header.
|
---|
505 | */
|
---|
506 | PMMHEAPHDR pHdr = (PMMHEAPHDR)pv - 1;
|
---|
507 | if ( pHdr->cbSize & (MMR3HEAP_SIZE_ALIGNMENT - 1)
|
---|
508 | || (uintptr_t)pHdr & (RTMEM_ALIGNMENT - 1))
|
---|
509 | {
|
---|
510 | AssertMsgFailed(("Invalid heap header! pv=%p, size=%#x\n", pv, pHdr->cbSize));
|
---|
511 | return;
|
---|
512 | }
|
---|
513 | Assert(pHdr->pStat != NULL);
|
---|
514 | Assert(!((uintptr_t)pHdr->pNext & (RTMEM_ALIGNMENT - 1)));
|
---|
515 | Assert(!((uintptr_t)pHdr->pPrev & (RTMEM_ALIGNMENT - 1)));
|
---|
516 |
|
---|
517 | /*
|
---|
518 | * Update statistics
|
---|
519 | */
|
---|
520 | PMMHEAP pHeap = pHdr->pStat->pHeap;
|
---|
521 | RTCritSectEnter(&pHeap->Lock);
|
---|
522 |
|
---|
523 | #ifdef MMR3HEAP_WITH_STATISTICS
|
---|
524 | pHdr->pStat->cFrees++;
|
---|
525 | pHeap->Stat.cFrees++;
|
---|
526 | pHdr->pStat->cbFreed += pHdr->cbSize;
|
---|
527 | pHeap->Stat.cbFreed += pHdr->cbSize;
|
---|
528 | pHdr->pStat->cbCurAllocated -= pHdr->cbSize;
|
---|
529 | pHeap->Stat.cbCurAllocated -= pHdr->cbSize;
|
---|
530 | #endif
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Unlink it.
|
---|
534 | */
|
---|
535 | if (pHdr->pPrev)
|
---|
536 | pHdr->pPrev->pNext = pHdr->pNext;
|
---|
537 | else
|
---|
538 | pHeap->pHead = pHdr->pNext;
|
---|
539 |
|
---|
540 | if (pHdr->pNext)
|
---|
541 | pHdr->pNext->pPrev = pHdr->pPrev;
|
---|
542 | else
|
---|
543 | pHeap->pTail = pHdr->pPrev;
|
---|
544 |
|
---|
545 | RTCritSectLeave(&pHeap->Lock);
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * Free the memory.
|
---|
549 | */
|
---|
550 | RTMemFree(pHdr);
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /**
|
---|
555 | * Gets the string name of a memory tag.
|
---|
556 | *
|
---|
557 | * @returns name of enmTag.
|
---|
558 | * @param enmTag The tag.
|
---|
559 | */
|
---|
560 | const char *mmR3GetTagName(MMTAG enmTag)
|
---|
561 | {
|
---|
562 | switch (enmTag)
|
---|
563 | {
|
---|
564 | #define TAG2STR(tag) case MM_TAG_##tag: return #tag
|
---|
565 |
|
---|
566 | TAG2STR(CFGM);
|
---|
567 | TAG2STR(CFGM_BYTES);
|
---|
568 | TAG2STR(CFGM_STRING);
|
---|
569 | TAG2STR(CFGM_USER);
|
---|
570 |
|
---|
571 | TAG2STR(CSAM);
|
---|
572 | TAG2STR(CSAM_PATCH);
|
---|
573 |
|
---|
574 | TAG2STR(DBGF);
|
---|
575 | TAG2STR(DBGF_INFO);
|
---|
576 | TAG2STR(DBGF_LINE);
|
---|
577 | TAG2STR(DBGF_LINE_DUP);
|
---|
578 | TAG2STR(DBGF_STACK);
|
---|
579 | TAG2STR(DBGF_SYMBOL);
|
---|
580 | TAG2STR(DBGF_SYMBOL_DUP);
|
---|
581 | TAG2STR(DBGF_MODULE);
|
---|
582 |
|
---|
583 | TAG2STR(EM);
|
---|
584 |
|
---|
585 | TAG2STR(IOM);
|
---|
586 | TAG2STR(IOM_STATS);
|
---|
587 |
|
---|
588 | TAG2STR(MM);
|
---|
589 | TAG2STR(MM_LOOKUP_GUEST);
|
---|
590 | TAG2STR(MM_LOOKUP_PHYS);
|
---|
591 | TAG2STR(MM_LOOKUP_VIRT);
|
---|
592 | TAG2STR(MM_PAGE);
|
---|
593 |
|
---|
594 | TAG2STR(PATM);
|
---|
595 | TAG2STR(PATM_PATCH);
|
---|
596 |
|
---|
597 | TAG2STR(PDM);
|
---|
598 | TAG2STR(PDM_DEVICE);
|
---|
599 | TAG2STR(PDM_DEVICE_USER);
|
---|
600 | TAG2STR(PDM_DRIVER);
|
---|
601 | TAG2STR(PDM_DRIVER_USER);
|
---|
602 | TAG2STR(PDM_LUN);
|
---|
603 | TAG2STR(PDM_QUEUE);
|
---|
604 |
|
---|
605 | TAG2STR(PGM);
|
---|
606 | TAG2STR(PGM_HANDLERS);
|
---|
607 | TAG2STR(PGM_POOL);
|
---|
608 |
|
---|
609 | TAG2STR(REM);
|
---|
610 |
|
---|
611 | TAG2STR(SELM);
|
---|
612 |
|
---|
613 | TAG2STR(SSM);
|
---|
614 |
|
---|
615 | TAG2STR(STAM);
|
---|
616 |
|
---|
617 | TAG2STR(TM);
|
---|
618 |
|
---|
619 | TAG2STR(TRPM);
|
---|
620 |
|
---|
621 | TAG2STR(VM);
|
---|
622 | TAG2STR(VM_REQ);
|
---|
623 |
|
---|
624 | TAG2STR(VMM);
|
---|
625 |
|
---|
626 | TAG2STR(HWACCM);
|
---|
627 |
|
---|
628 | #undef TAG2STR
|
---|
629 |
|
---|
630 | default:
|
---|
631 | {
|
---|
632 | AssertMsgFailed(("Unknown tag %d! forgot to add it to the switch?\n", enmTag));
|
---|
633 | static char sz[48];
|
---|
634 | RTStrPrintf(sz, sizeof(sz), "%d", enmTag);
|
---|
635 | return sz;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|