/** Replacement for free(). */ static void rtMemReplacementFree(void *pv) { if (pv) { /* We're not strict about where the memory was allocated. */ PRTMEMBLOCK pBlock = rtmemBlockGet(pv); if (pBlock) rtR3MemFree("r-free", RTMEMTYPE_RTMEMFREE, pv, ASMReturnAddress(), RT_SRC_POS); else g_pfnOrgFree(pv); } }
/** Replacement for realloc. */ static void *rtMemReplacementRealloc(void *pvOld, size_t cbNew) { if (pvOld) { /* We're not strict about where the memory was allocated. */ PRTMEMBLOCK pBlock = rtmemBlockGet(pvOld); if (pBlock) { size_t cbAligned = RTMEM_REPLACMENT_ALIGN(cbNew); return rtR3MemRealloc("r-realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbAligned, "heap", ASMReturnAddress(), RT_SRC_POS); } return g_pfnOrgRealloc(pvOld, cbNew); } return rtMemReplacementMalloc(cbNew); }
/** Replacement for malloc. */ static size_t rtMemReplacementMallocSize(void *pv) { size_t cb; if (pv) { /* We're not strict about where the memory was allocated. */ PRTMEMBLOCK pBlock = rtmemBlockGet(pv); if (pBlock) cb = pBlock->cbUnaligned; else cb = g_pfnOrgMallocSize(pv); } else cb = 0; return cb; }
/** * Internal realloc. */ RTDECL(void *) rtR3MemRealloc(const char *pszOp, RTMEMTYPE enmType, void *pvOld, size_t cbNew, const char *pszTag, void *pvCaller, RT_SRC_POS_DECL) { /* * Allocate new and copy. */ if (!pvOld) return rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pszTag, pvCaller, RT_SRC_POS_ARGS); if (!cbNew) { rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, RT_SRC_POS_ARGS); return NULL; } #ifdef RTALLOC_EFENCE_TRACE /* * Get the block, allocate the new, copy the data, free the old one. */ PRTMEMBLOCK pBlock = rtmemBlockGet(pvOld); if (pBlock) { void *pvRet = rtR3MemAlloc(pszOp, enmType, cbNew, cbNew, pszTag, pvCaller, RT_SRC_POS_ARGS); if (pvRet) { memcpy(pvRet, pvOld, RT_MIN(cbNew, pBlock->cbUnaligned)); rtR3MemFree(pszOp, RTMEMTYPE_RTMEMREALLOC, pvOld, pvCaller, RT_SRC_POS_ARGS); } return pvRet; } else rtmemComplain(pszOp, "pvOld=%p was not found!\n", pvOld); return NULL; #else /* !RTALLOC_EFENCE_TRACE */ rtmemComplain(pszOp, "Not supported if RTALLOC_EFENCE_TRACE isn't defined!\n"); return NULL; #endif /* !RTALLOC_EFENCE_TRACE */ }