First version of #25 implementation, providing memory_get_usage/memory_get_peak_usage/memory_limit
All checks were successful
The build was successful.
All checks were successful
The build was successful.
This commit is contained in:
@@ -86,18 +86,39 @@ PH7_PRIVATE sxu32 SyMemcpy(const void *pSrc, void *pDest, sxu32 nLen) {
|
||||
SX_MACRO_FAST_MEMCPY(pSrc, pDest, nLen);
|
||||
return nLen;
|
||||
}
|
||||
static void *MemOSAlloc(sxu32 nBytes) {
|
||||
|
||||
#define CheckHeap(BACKEND, BYTES)\
|
||||
if(BACKEND->pHeap.iMax && BACKEND->pHeap.nBytes + BYTES > BACKEND->pHeap.iMax) {\
|
||||
if(BACKEND->xMemError) {\
|
||||
char buf[256] = {0};\
|
||||
snprintf(buf, sizeof(buf) - 1, "Out of memory, max allowed %llu bytes, allocated %llu bytes\n", BACKEND->pHeap.iMax, BACKEND->pHeap.nBytes);\
|
||||
BACKEND->xMemError(buf);\
|
||||
}\
|
||||
PH7_VmRelease(BACKEND->pHeap.pVm);\
|
||||
exit(255);\
|
||||
}\
|
||||
(void)BYTES
|
||||
#define AddToHeap(BACKEND, BYTES)\
|
||||
BACKEND->pHeap.nBytes += BYTES;\
|
||||
if(BACKEND->pHeap.nBytes > BACKEND->pHeap.iPeak) {\
|
||||
BACKEND->pHeap.iPeak = BACKEND->pHeap.nBytes;\
|
||||
}\
|
||||
(void)BYTES
|
||||
static void *MemOSAlloc(sxu32 nBytes, SyMemBackend *pBackend) {
|
||||
sxu32 *pChunk;
|
||||
CheckHeap(pBackend, nBytes);
|
||||
pChunk = (sxu32 *)SyOSHeapAlloc(nBytes + sizeof(sxu32));
|
||||
if(pChunk == 0) {
|
||||
return 0;
|
||||
}
|
||||
pChunk[0] = nBytes;
|
||||
AddToHeap(pBackend, nBytes);
|
||||
return (void *)&pChunk[1];
|
||||
}
|
||||
static void *MemOSRealloc(void *pOld, sxu32 nBytes) {
|
||||
static void *MemOSRealloc(void *pOld, sxu32 nBytes, SyMemBackend *pBackend) {
|
||||
sxu32 *pOldChunk;
|
||||
sxu32 *pChunk;
|
||||
CheckHeap(pBackend, nBytes);
|
||||
pOldChunk = (sxu32 *)(((char *)pOld) - sizeof(sxu32));
|
||||
if(pOldChunk[0] >= nBytes) {
|
||||
return pOld;
|
||||
@@ -107,11 +128,13 @@ static void *MemOSRealloc(void *pOld, sxu32 nBytes) {
|
||||
return 0;
|
||||
}
|
||||
pChunk[0] = nBytes;
|
||||
AddToHeap(pBackend, abs(nBytes - pOldChunk[0]));
|
||||
return (void *)&pChunk[1];
|
||||
}
|
||||
static void MemOSFree(void *pBlock) {
|
||||
void *pChunk;
|
||||
pChunk = (void *)(((char *)pBlock) - sizeof(sxu32));
|
||||
static void MemOSFree(void *pBlock, SyMemBackend *pBackend) {
|
||||
sxu32 *pChunk;
|
||||
pChunk = (sxu32 *)(((char *)pBlock) - sizeof(sxu32));
|
||||
pBackend->pHeap.nBytes -= pChunk[0];
|
||||
SyOSHeapFree(pChunk);
|
||||
}
|
||||
static sxu32 MemOSChunkSize(void *pBlock) {
|
||||
@@ -137,14 +160,14 @@ static void *MemBackendAlloc(SyMemBackend *pBackend, sxu32 nByte) {
|
||||
*/
|
||||
nByte += sizeof(SyMemBlock);
|
||||
for(;;) {
|
||||
pBlock = (SyMemBlock *)pBackend->pMethods->xAlloc(nByte);
|
||||
pBlock = (SyMemBlock *)pBackend->pMethods->xAlloc(nByte, pBackend);
|
||||
if(pBlock != 0 || pBackend->xMemError == 0 || nRetry > SXMEM_BACKEND_RETRY
|
||||
|| SXERR_RETRY != pBackend->xMemError(pBackend->pUserData)) {
|
||||
break;
|
||||
}
|
||||
nRetry++;
|
||||
}
|
||||
if(pBlock == 0) {
|
||||
if(pBlock == 0) {
|
||||
return 0;
|
||||
}
|
||||
pBlock->pNext = pBlock->pPrev = 0;
|
||||
@@ -188,7 +211,7 @@ static void *MemBackendRealloc(SyMemBackend *pBackend, void *pOld, sxu32 nByte)
|
||||
pPrev = pBlock->pPrev;
|
||||
pNext = pBlock->pNext;
|
||||
for(;;) {
|
||||
pNew = (SyMemBlock *)pBackend->pMethods->xRealloc(pBlock, nByte);
|
||||
pNew = (SyMemBlock *)pBackend->pMethods->xRealloc(pBlock, nByte, pBackend);
|
||||
if(pNew != 0 || pBackend->xMemError == 0 || nRetry > SXMEM_BACKEND_RETRY ||
|
||||
SXERR_RETRY != pBackend->xMemError(pBackend->pUserData)) {
|
||||
break;
|
||||
@@ -246,7 +269,7 @@ static sxi32 MemBackendFree(SyMemBackend *pBackend, void *pChunk) {
|
||||
#endif
|
||||
MACRO_LD_REMOVE(pBackend->pBlocks, pBlock);
|
||||
pBackend->nBlock--;
|
||||
pBackend->pMethods->xFree(pBlock);
|
||||
pBackend->pMethods->xFree(pBlock, pBackend);
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
@@ -538,6 +561,7 @@ PH7_PRIVATE sxi32 SyMemBackendInitFromParent(SyMemBackend *pBackend, SyMemBacken
|
||||
pBackend->pMethods = pParent->pMethods;
|
||||
pBackend->xMemError = pParent->xMemError;
|
||||
pBackend->pUserData = pParent->pUserData;
|
||||
pBackend->pHeap.iMax = pParent->pHeap.iMax;
|
||||
bInheritMutex = pParent->pMutexMethods ? TRUE : FALSE;
|
||||
if(bInheritMutex) {
|
||||
pBackend->pMutexMethods = pParent->pMutexMethods;
|
||||
@@ -560,7 +584,7 @@ static sxi32 MemBackendRelease(SyMemBackend *pBackend) {
|
||||
break;
|
||||
}
|
||||
pNext = pBlock->pNext;
|
||||
pBackend->pMethods->xFree(pBlock);
|
||||
pBackend->pMethods->xFree(pBlock, pBackend);
|
||||
pBlock = pNext;
|
||||
pBackend->nBlock--;
|
||||
/* LOOP ONE */
|
||||
@@ -568,7 +592,7 @@ static sxi32 MemBackendRelease(SyMemBackend *pBackend) {
|
||||
break;
|
||||
}
|
||||
pNext = pBlock->pNext;
|
||||
pBackend->pMethods->xFree(pBlock);
|
||||
pBackend->pMethods->xFree(pBlock, pBackend);
|
||||
pBlock = pNext;
|
||||
pBackend->nBlock--;
|
||||
/* LOOP TWO */
|
||||
@@ -576,7 +600,7 @@ static sxi32 MemBackendRelease(SyMemBackend *pBackend) {
|
||||
break;
|
||||
}
|
||||
pNext = pBlock->pNext;
|
||||
pBackend->pMethods->xFree(pBlock);
|
||||
pBackend->pMethods->xFree(pBlock, pBackend);
|
||||
pBlock = pNext;
|
||||
pBackend->nBlock--;
|
||||
/* LOOP THREE */
|
||||
@@ -584,7 +608,7 @@ static sxi32 MemBackendRelease(SyMemBackend *pBackend) {
|
||||
break;
|
||||
}
|
||||
pNext = pBlock->pNext;
|
||||
pBackend->pMethods->xFree(pBlock);
|
||||
pBackend->pMethods->xFree(pBlock, pBackend);
|
||||
pBlock = pNext;
|
||||
pBackend->nBlock--;
|
||||
/* LOOP FOUR */
|
||||
@@ -853,4 +877,4 @@ PH7_PRIVATE sxi32 SyBlobSearch(const void *pBlob, sxu32 nLen, const void *pPatte
|
||||
zIn++;
|
||||
}
|
||||
return SXERR_NOTFOUND;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user