From 5464cbf4e40299410d4f6fd078c02211e31a0917 Mon Sep 17 00:00:00 2001 From: kpl Date: Sat, 12 Oct 2013 20:20:37 -0700 Subject: [PATCH] cmLinkHeap.h/c : Added cmLHeapIsPtrInHeap(). --- cmLinkedHeap.c | 30 +++++++++++++++++++++++++++--- cmLinkedHeap.h | 3 +++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/cmLinkedHeap.c b/cmLinkedHeap.c index 178f1df..f16d97b 100644 --- a/cmLinkedHeap.c +++ b/cmLinkedHeap.c @@ -100,10 +100,10 @@ void* _cmLHeapAllocCb(void* funcArgPtr, unsigned byteCnt) return _cmLHeapAlloc(p,byteCnt); } -bool _cmLHeapFree( cmLHeap_t* lhp, void* dataPtr ) -{ +cmLhBlock_t* _cmLHeapPtrToBlock( cmLHeap_t* lhp, const void* dataPtr ) +{ if( dataPtr == NULL ) - return true; + return NULL; cmLhBlock_t* lbp = lhp->first; @@ -111,6 +111,24 @@ bool _cmLHeapFree( cmLHeap_t* lhp, void* dataPtr ) while( (lbp != NULL ) && (((char*)dataPtr < lbp->basePtr) || ((char*)dataPtr >= lbp->endPtr))) lbp = lbp->nextBlkPtr; + return lbp; +} + +bool _cmLHeapFree( cmLHeap_t* lhp, void* dataPtr ) +{ + if( dataPtr == NULL ) + return true; + + /* + cmLhBlock_t* lbp = lhp->first; + + // locate the block containing the area to free + while( (lbp != NULL ) && (((char*)dataPtr < lbp->basePtr) || ((char*)dataPtr >= lbp->endPtr))) + lbp = lbp->nextBlkPtr; + */ + + cmLhBlock_t* lbp = _cmLHeapPtrToBlock(lhp,dataPtr); + // the pointer must be in one of the blocks if( lbp == NULL ) return false; @@ -296,6 +314,12 @@ void cmLHeapClear( cmLHeapH_t h, bool releaseFl ) } } +bool cmLHeapIsPtrInHeap( cmLHeapH_t h, const void* ptr ) +{ + cmLHeap_t* lhp = _cmLHeapHandleToPtr(h); + return _cmLHeapPtrToBlock(lhp,ptr) != NULL; +} + cmMmRC_t cmLHeapReportErrors( cmLHeapH_t h, unsigned mmFlags ) { cmLHeap_t* lhp = _cmLHeapHandleToPtr(h); diff --git a/cmLinkedHeap.h b/cmLinkedHeap.h index 7271151..84753f7 100644 --- a/cmLinkedHeap.h +++ b/cmLinkedHeap.h @@ -37,6 +37,9 @@ extern "C" { // release the associated memory, otherwise releases all memory blocks. void cmLHeapClear( cmLHeapH_t h, bool releaseFl ); + // Return true if 'ptr' points into a linked heap block. + bool cmLHeapIsPtrInHeap( cmLHeapH_t h, const void* ptr ); + // mmFlags take the same values as the flags parameter to cmMmReport(). cmMmRC_t cmLHeapReportErrors( cmLHeapH_t h, unsigned mmFlags ); void cmLHeapReport( cmLHeapH_t h );