From 0a31211d9604cab581e484cf35f4bd98a265fafc Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Tue, 4 Jun 2013 23:05:02 -0600 Subject: [PATCH] added the IMallocSpy calls to the top-level IMalloc functions; also fixed a compiler warning in qdivrem --- include/comrogue/compiler_macros.h | 7 ++ kernel/lib/heap_toplevel.c | 117 ++++++++++++++++++++++++++++- kernel/lib/qdivrem.c | 2 +- 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/include/comrogue/compiler_macros.h b/include/comrogue/compiler_macros.h index 3c0d357..e1767c6 100644 --- a/include/comrogue/compiler_macros.h +++ b/include/comrogue/compiler_macros.h @@ -53,6 +53,13 @@ #endif /* __cplusplus */ +/*---------------------------- + * Additional compiler macros + *---------------------------- + */ + +#define UNUSED(var) ((void)var) + #endif /* __ASM__ */ #endif /* __COMPILER_MACROS_H_INCLUDED */ diff --git a/kernel/lib/heap_toplevel.c b/kernel/lib/heap_toplevel.c index a4ae160..8c88e6c 100644 --- a/kernel/lib/heap_toplevel.c +++ b/kernel/lib/heap_toplevel.c @@ -32,6 +32,7 @@ /* * This code is based on/inspired by jemalloc-3.3.1. Please see LICENSE.jemalloc for further details. */ +#include #include #include #include @@ -125,32 +126,140 @@ static UINT32 malloc_Release(IUnknown *pThis) static PVOID malloc_Alloc(IMalloc *pThis, SIZE_T cb) { - return NULL; /* TODO */ + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + SIZE_T cbActual = cb; /* actual number of bytes requested */ + PVOID rc; /* return from this function */ + + /* handle PreAlloc call */ + if (phd->pMallocSpy) + { + cbActual = IMallocSpy_PreAlloc(phd->pMallocSpy, cb); + if ((cbActual == 0) && (cb != 0)) + return NULL; /* simulated memory failure */ + } + + rc = NULL; /* TODO */ + + /* handle PostAlloc call */ + if (phd->pMallocSpy) + rc = IMallocSpy_PostAlloc(phd->pMallocSpy, rc); + + return rc; +} + +static BOOL isSpyedByCurrentSpy(PHEAPDATA phd, PVOID pBlock) +{ + return FALSE; /* TODO */ } static PVOID malloc_Realloc(IMalloc *pThis, PVOID pv, SIZE_T cb) { - return NULL; /* TODO */ + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + PVOID pvActual = pv; /* actual heap block pointer */ + SIZE_T cbActual = cb; /* actual number of bytes requested */ + PVOID rc; /* return from this function */ + BOOL fSpyed; /* were we allocated while currently spyed on? */ + + /* handle PreRealloc call */ + if (phd->pMallocSpy) + { + fSpyed = isSpyedByCurrentSpy(phd, pv); + cbActual = IMallocSpy_PreRealloc(phd->pMallocSpy, pv, cb, &pvActual, fSpyed); + if ((cbActual == 0) && (cb != 0)) + return NULL; /* simulated memory failure */ + } + + rc = NULL; /* TODO */ + + /* handle PostRealloc call */ + if (phd->pMallocSpy) + rc = IMallocSpy_PostRealloc(phd->pMallocSpy, rc, fSpyed); + + return rc; } static void malloc_Free(IMalloc *pThis, PVOID pv) { + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + PVOID pvActual = pv; /* actual heap block pointer */ + BOOL fSpyed; /* were we allocated while currently spyed on? */ + + /* handle PreFree call */ + if (phd->pMallocSpy) + { + fSpyed = isSpyedByCurrentSpy(phd, pv); + pvActual = IMallocSpy_PreFree(phd->pMallocSpy, pv, fSpyed); + } + /* TODO */ + UNUSED(pvActual); + + /* handle PostFree call */ + if (phd->pMallocSpy) + IMallocSpy_PostFree(phd->pMallocSpy, fSpyed); } static SIZE_T malloc_GetSize(IMalloc *pThis, PVOID pv) { - return -1; /* TODO */ + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + PVOID pvActual = pv; /* actual heap block pointer */ + BOOL fSpyed; /* were we allocated while currently spyed on? */ + SIZE_T rc; /* return from this function */ + + /* handle PreGetSize call */ + if (phd->pMallocSpy) + { + fSpyed = isSpyedByCurrentSpy(phd, pv); + pvActual = IMallocSpy_PreGetSize(phd->pMallocSpy, pv, fSpyed); + } + + rc = -1; /* TODO */ + UNUSED(pvActual); + + /* handle PostGetSize call */ + if (phd->pMallocSpy) + rc = IMallocSpy_PostGetSize(phd->pMallocSpy, rc, fSpyed); + + return rc; } static INT32 malloc_DidAlloc(IMalloc *pThis, PVOID pv) { - return -1; /* TODO */ + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + PVOID pvActual = pv; /* actual heap block pointer */ + BOOL fSpyed; /* were we allocated while currently spyed on? */ + INT32 rc; /* return from this function */ + + /* handle PreDidAlloc call */ + if (phd->pMallocSpy) + { + fSpyed = isSpyedByCurrentSpy(phd, pv); + pvActual = IMallocSpy_PreDidAlloc(phd->pMallocSpy, pv, fSpyed); + } + + rc = -1; /* TODO */ + UNUSED(pvActual); + + /* handle PostDidAlloc call */ + if (phd->pMallocSpy) + rc = IMallocSpy_PostDidAlloc(phd->pMallocSpy, pv, fSpyed, rc); + + return rc; } static void malloc_HeapMinimize(IMalloc *pThis) { + PHEAPDATA phd = (PHEAPDATA)pThis; /* pointer to heap data */ + + /* handle PreHeapMinimize call */ + if (phd->pMallocSpy) + IMallocSpy_PreHeapMinimize(phd->pMallocSpy); + /* TODO */ + + /* handle PostHeapMinimize call */ + if (phd->pMallocSpy) + IMallocSpy_PostHeapMinimize(phd->pMallocSpy); } /* The IMalloc vtable. */ diff --git a/kernel/lib/qdivrem.c b/kernel/lib/qdivrem.c index f00c630..1fb5713 100644 --- a/kernel/lib/qdivrem.c +++ b/kernel/lib/qdivrem.c @@ -84,7 +84,7 @@ UINT64 __qdivrem(UINT64 uq, UINT64 vq, PUINT64 pRem) */ if (vq == 0) { /* divide by zero. */ - SEG_RODATA static volatile const unsigned int zero = 0; + static const SEG_RODATA unsigned int zero = 0; tmp.ul[H] = tmp.ul[L] = 1 / zero; if (pRem)