Aer Interpreter Source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5500 lines
161 KiB

/*
* Symisc PH7: An embeddable bytecode compiler and a virtual machine for the PHP(5) programming language.
* Copyright (C) 2011-2012, Symisc Systems http://ph7.symisc.net/
* Version 2.1.4
* For information on licensing,redistribution of this file,and for a DISCLAIMER OF ALL WARRANTIES
* please contact Symisc Systems via:
* legal@symisc.net
* licensing@symisc.net
* contact@symisc.net
* or visit:
* http://ph7.symisc.net/
*/
/* $SymiscID: lib.c v5.1 Win7 2012-08-08 04:19 stable <chm@symisc.net> $ */
/*
* Symisc Run-Time API: A modern thread safe replacement of the standard libc
* Copyright (C) Symisc Systems 2007-2012, http://www.symisc.net/
*
* The Symisc Run-Time API is an independent project developed by symisc systems
* internally as a secure replacement of the standard libc.
* The library is re-entrant,thread-safe and platform independent.
*/
#include "ph7int.h"
#if defined(__WINNT__)
#include <Windows.h>
#else
#include <stdlib.h>
#endif
#if defined(PH7_ENABLE_THREADS)
/* SyRunTimeApi: sxmutex.c */
#if defined(__WINNT__)
struct SyMutex
{
CRITICAL_SECTION sMutex;
sxu32 nType; /* Mutex type,one of SXMUTEX_TYPE_* */
};
/* Preallocated static mutex */
static SyMutex aStaticMutexes[] = {
{{0},SXMUTEX_TYPE_STATIC_1},
{{0},SXMUTEX_TYPE_STATIC_2},
{{0},SXMUTEX_TYPE_STATIC_3},
{{0},SXMUTEX_TYPE_STATIC_4},
{{0},SXMUTEX_TYPE_STATIC_5},
{{0},SXMUTEX_TYPE_STATIC_6}
};
static BOOL winMutexInit = FALSE;
static LONG winMutexLock = 0;
static sxi32 WinMutexGlobaInit(void)
{
LONG rc;
rc = InterlockedCompareExchange(&winMutexLock,1,0);
if ( rc == 0 ){
sxu32 n;
for( n = 0 ; n < SX_ARRAYSIZE(aStaticMutexes) ; ++n ){
InitializeCriticalSection(&aStaticMutexes[n].sMutex);
}
winMutexInit = TRUE;
}else{
/* Someone else is doing this for us */
while( winMutexInit == FALSE ){
Sleep(1);
}
}
return SXRET_OK;
}
static void WinMutexGlobalRelease(void)
{
LONG rc;
rc = InterlockedCompareExchange(&winMutexLock,0,1);
if( rc == 1 ){
/* The first to decrement to zero does the actual global release */
if( winMutexInit == TRUE ){
sxu32 n;
for( n = 0 ; n < SX_ARRAYSIZE(aStaticMutexes) ; ++n ){
DeleteCriticalSection(&aStaticMutexes[n].sMutex);
}
winMutexInit = FALSE;
}
}
}
static SyMutex * WinMutexNew(int nType)
{
SyMutex *pMutex = 0;
if( nType == SXMUTEX_TYPE_FAST || nType == SXMUTEX_TYPE_RECURSIVE ){
/* Allocate a new mutex */
pMutex = (SyMutex *)HeapAlloc(GetProcessHeap(),0,sizeof(SyMutex));
if( pMutex == 0 ){
return 0;
}
InitializeCriticalSection(&pMutex->sMutex);
}else{
/* Use a pre-allocated static mutex */
if( nType > SXMUTEX_TYPE_STATIC_6 ){
nType = SXMUTEX_TYPE_STATIC_6;
}
pMutex = &aStaticMutexes[nType - 3];
}
pMutex->nType = nType;
return pMutex;
}
static void WinMutexRelease(SyMutex *pMutex)
{
if( pMutex->nType == SXMUTEX_TYPE_FAST || pMutex->nType == SXMUTEX_TYPE_RECURSIVE ){
DeleteCriticalSection(&pMutex->sMutex);
HeapFree(GetProcessHeap(),0,pMutex);
}
}
static void WinMutexEnter(SyMutex *pMutex)
{
EnterCriticalSection(&pMutex->sMutex);
}
static sxi32 WinMutexTryEnter(SyMutex *pMutex)
{
#ifdef _WIN32_WINNT
BOOL rc;
/* Only WindowsNT platforms */
rc = TryEnterCriticalSection(&pMutex->sMutex);
if( rc ){
return SXRET_OK;
}else{
return SXERR_BUSY;
}
#else
return SXERR_NOTIMPLEMENTED;
#endif
}
static void WinMutexLeave(SyMutex *pMutex)
{
LeaveCriticalSection(&pMutex->sMutex);
}
/* Export Windows mutex interfaces */
static const SyMutexMethods sWinMutexMethods = {
WinMutexGlobaInit, /* xGlobalInit() */
WinMutexGlobalRelease, /* xGlobalRelease() */
WinMutexNew, /* xNew() */
WinMutexRelease, /* xRelease() */
WinMutexEnter, /* xEnter() */
WinMutexTryEnter, /* xTryEnter() */
WinMutexLeave /* xLeave() */
};
PH7_PRIVATE const SyMutexMethods * SyMutexExportMethods(void)
{
return &sWinMutexMethods;
}
#elif defined(__UNIXES__)
#include <pthread.h>
struct SyMutex
{
pthread_mutex_t sMutex;
sxu32 nType;
};
static SyMutex * UnixMutexNew(int nType)
{
static SyMutex aStaticMutexes[] = {
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_1},
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_2},
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_3},
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_4},
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_5},
{PTHREAD_MUTEX_INITIALIZER,SXMUTEX_TYPE_STATIC_6}
};
SyMutex *pMutex;
if( nType == SXMUTEX_TYPE_FAST || nType == SXMUTEX_TYPE_RECURSIVE ){
pthread_mutexattr_t sRecursiveAttr;
/* Allocate a new mutex */
pMutex = (SyMutex *)malloc(sizeof(SyMutex));
if( pMutex == 0 ){
return 0;
}
if( nType == SXMUTEX_TYPE_RECURSIVE ){
pthread_mutexattr_init(&sRecursiveAttr);
pthread_mutexattr_settype(&sRecursiveAttr,PTHREAD_MUTEX_RECURSIVE);
}
pthread_mutex_init(&pMutex->sMutex,nType == SXMUTEX_TYPE_RECURSIVE ? &sRecursiveAttr : 0 );
if( nType == SXMUTEX_TYPE_RECURSIVE ){
pthread_mutexattr_destroy(&sRecursiveAttr);
}
}else{
/* Use a pre-allocated static mutex */
if( nType > SXMUTEX_TYPE_STATIC_6 ){
nType = SXMUTEX_TYPE_STATIC_6;
}
pMutex = &aStaticMutexes[nType - 3];
}
pMutex->nType = nType;
return pMutex;
}
static void UnixMutexRelease(SyMutex *pMutex)
{
if( pMutex->nType == SXMUTEX_TYPE_FAST || pMutex->nType == SXMUTEX_TYPE_RECURSIVE ){
pthread_mutex_destroy(&pMutex->sMutex);
free(pMutex);
}
}
static void UnixMutexEnter(SyMutex *pMutex)
{
pthread_mutex_lock(&pMutex->sMutex);
}
static void UnixMutexLeave(SyMutex *pMutex)
{
pthread_mutex_unlock(&pMutex->sMutex);
}
/* Export pthread mutex interfaces */
static const SyMutexMethods sPthreadMutexMethods = {
0, /* xGlobalInit() */
0, /* xGlobalRelease() */
UnixMutexNew, /* xNew() */
UnixMutexRelease, /* xRelease() */
UnixMutexEnter, /* xEnter() */
0, /* xTryEnter() */
UnixMutexLeave /* xLeave() */
};
PH7_PRIVATE const SyMutexMethods * SyMutexExportMethods(void)
{
return &sPthreadMutexMethods;
}
#else
/* Host application must register their own mutex subsystem if the target
* platform is not an UNIX-like or windows systems.
*/
struct SyMutex
{
sxu32 nType;
};
static SyMutex * DummyMutexNew(int nType)
{
static SyMutex sMutex;
SXUNUSED(nType);
return &sMutex;
}
static void DummyMutexRelease(SyMutex *pMutex)
{
SXUNUSED(pMutex);
}
static void DummyMutexEnter(SyMutex *pMutex)
{
SXUNUSED(pMutex);
}
static void DummyMutexLeave(SyMutex *pMutex)
{
SXUNUSED(pMutex);
}
/* Export the dummy mutex interfaces */
static const SyMutexMethods sDummyMutexMethods = {
0, /* xGlobalInit() */
0, /* xGlobalRelease() */
DummyMutexNew, /* xNew() */
DummyMutexRelease, /* xRelease() */
DummyMutexEnter, /* xEnter() */
0, /* xTryEnter() */
DummyMutexLeave /* xLeave() */
};
PH7_PRIVATE const SyMutexMethods * SyMutexExportMethods(void)
{
return &sDummyMutexMethods;
}
#endif /* __WINNT__ */
#endif /* PH7_ENABLE_THREADS */
static void * SyOSHeapAlloc(sxu32 nByte)
{
void *pNew;
#if defined(__WINNT__)
pNew = HeapAlloc(GetProcessHeap(),0,nByte);
#else
pNew = malloc((size_t)nByte);
#endif
return pNew;
}
static void * SyOSHeapRealloc(void *pOld,sxu32 nByte)
{
void *pNew;
#if defined(__WINNT__)
pNew = HeapReAlloc(GetProcessHeap(),0,pOld,nByte);
#else
pNew = realloc(pOld,(size_t)nByte);
#endif
return pNew;
}
static void SyOSHeapFree(void *pPtr)
{
#if defined(__WINNT__)
HeapFree(GetProcessHeap(),0,pPtr);
#else
free(pPtr);
#endif
}
/* SyRunTimeApi:sxstr.c */
PH7_PRIVATE sxu32 SyStrlen(const char *zSrc)
{
register const char *zIn = zSrc;
#if defined(UNTRUST)
if( zIn == 0 ){
return 0;
}
#endif
for(;;){
if( !zIn[0] ){ break; } zIn++;
if( !zIn[0] ){ break; } zIn++;
if( !zIn[0] ){ break; } zIn++;
if( !zIn[0] ){ break; } zIn++;
}
return (sxu32)(zIn - zSrc);
}
PH7_PRIVATE sxi32 SyByteFind(const char *zStr,sxu32 nLen,sxi32 c,sxu32 *pPos)
{
const char *zIn = zStr;
const char *zEnd;
zEnd = &zIn[nLen];
for(;;){
if( zIn >= zEnd ){ break; }if( zIn[0] == c ){ if( pPos ){ *pPos = (sxu32)(zIn - zStr); } return SXRET_OK; } zIn++;
if( zIn >= zEnd ){ break; }if( zIn[0] == c ){ if( pPos ){ *pPos = (sxu32)(zIn - zStr); } return SXRET_OK; } zIn++;
if( zIn >= zEnd ){ break; }if( zIn[0] == c ){ if( pPos ){ *pPos = (sxu32)(zIn - zStr); } return SXRET_OK; } zIn++;
if( zIn >= zEnd ){ break; }if( zIn[0] == c ){ if( pPos ){ *pPos = (sxu32)(zIn - zStr); } return SXRET_OK; } zIn++;
}
return SXERR_NOTFOUND;
}
#ifndef PH7_DISABLE_BUILTIN_FUNC
PH7_PRIVATE sxi32 SyByteFind2(const char *zStr,sxu32 nLen,sxi32 c,sxu32 *pPos)
{
const char *zIn = zStr;
const char *zEnd;
zEnd = &zIn[nLen - 1];
for( ;; ){
if( zEnd < zIn ){ break; } if( zEnd[0] == c ){ if( pPos ){ *pPos = (sxu32)(zEnd - zIn);} return SXRET_OK; } zEnd--;
if( zEnd < zIn ){ break; } if( zEnd[0] == c ){ if( pPos ){ *pPos = (sxu32)(zEnd - zIn);} return SXRET_OK; } zEnd--;
if( zEnd < zIn ){ break; } if( zEnd[0] == c ){ if( pPos ){ *pPos = (sxu32)(zEnd - zIn);} return SXRET_OK; } zEnd--;
if( zEnd < zIn ){ break; } if( zEnd[0] == c ){ if( pPos ){ *pPos = (sxu32)(zEnd - zIn);} return SXRET_OK; } zEnd--;
}
return SXERR_NOTFOUND;
}
#endif /* PH7_DISABLE_BUILTIN_FUNC */
PH7_PRIVATE sxi32 SyByteListFind(const char *zSrc,sxu32 nLen,const char *zList,sxu32 *pFirstPos)
{
const char *zIn = zSrc;
const char *zPtr;
const char *zEnd;
sxi32 c;
zEnd = &zSrc[nLen];
for(;;){
if( zIn >= zEnd ){ break; } for(zPtr = zList ; (c = zPtr[0]) != 0 ; zPtr++ ){ if( zIn[0] == c ){ if( pFirstPos ){ *pFirstPos = (sxu32)(zIn - zSrc); } return SXRET_OK; } } zIn++;
if( zIn >= zEnd ){ break; } for(zPtr = zList ; (c = zPtr[0]) != 0 ; zPtr++ ){ if( zIn[0] == c ){ if( pFirstPos ){ *pFirstPos = (sxu32)(zIn - zSrc); } return SXRET_OK; } } zIn++;
if( zIn >= zEnd ){ break; } for(zPtr = zList ; (c = zPtr[0]) != 0 ; zPtr++ ){ if( zIn[0] == c ){ if( pFirstPos ){ *pFirstPos = (sxu32)(zIn - zSrc); } return SXRET_OK; } } zIn++;
if( zIn >= zEnd ){ break; } for(zPtr = zList ; (c = zPtr[0]) != 0 ; zPtr++ ){ if( zIn[0] == c ){ if( pFirstPos ){ *pFirstPos = (sxu32)(zIn - zSrc); } return SXRET_OK; } } zIn++;
}
return SXERR_NOTFOUND;
}
#ifndef PH7_DISABLE_BUILTIN_FUNC
PH7_PRIVATE sxi32 SyStrncmp(const char *zLeft,const char *zRight,sxu32 nLen)
{
const unsigned char *zP = (const unsigned char *)zLeft;
const unsigned char *zQ = (const unsigned char *)zRight;
if( SX_EMPTY_STR(zP) || SX_EMPTY_STR(zQ) ){
return SX_EMPTY_STR(zP) ? (SX_EMPTY_STR(zQ) ? 0 : -1) :1;
}
if( nLen <= 0 ){
return 0;
}
for(;;){
if( nLen <= 0 ){ return 0; } if( zP[0] == 0 || zQ[0] == 0 || zP[0] != zQ[0] ){ break; } zP++; zQ++; nLen--;
if( nLen <= 0 ){ return 0; } if( zP[0] == 0 || zQ[0] == 0 || zP[0] != zQ[0] ){ break; } zP++; zQ++; nLen--;
if( nLen <= 0 ){ return 0; } if( zP[0] == 0 || zQ[0] == 0 || zP[0] != zQ[0] ){ break; } zP++; zQ++; nLen--;
if( nLen <= 0 ){ return 0; } if( zP[0] == 0 || zQ[0] == 0 || zP[0] != zQ[0] ){ break; } zP++; zQ++; nLen--;
}
return (sxi32)(zP[0] - zQ[0]);
}
#endif
PH7_PRIVATE sxi32 SyStrnicmp(const char *zLeft, const char *zRight,sxu32 SLen)
{
register unsigned char *p = (unsigned char *)zLeft;
register unsigned char *q = (unsigned char *)zRight;
if( SX_EMPTY_STR(p) || SX_EMPTY_STR(q) ){
return SX_EMPTY_STR(p)? SX_EMPTY_STR(q) ? 0 : -1 :1;
}
for(;;){
if( !SLen ){ return 0; }if( !*p || !*q || SyCharToLower(*p) != SyCharToLower(*q) ){ break; }p++;q++;--SLen;
if( !SLen ){ return 0; }if( !*p || !*q || SyCharToLower(*p) != SyCharToLower(*q) ){ break; }p++;q++;--SLen;
if( !SLen ){ return 0; }if( !*p || !*q || SyCharToLower(*p) != SyCharToLower(*q) ){ break; }p++;q++;--SLen;
if( !SLen ){ return 0; }if( !*p || !*q || SyCharToLower(*p) != SyCharToLower(*q) ){ break; }p++;q++;--SLen;
}
return (sxi32)(SyCharToLower(p[0]) - SyCharToLower(q[0]));
}
PH7_PRIVATE sxi32 SyStrnmicmp(const void *pLeft, const void *pRight,sxu32 SLen)
{
return SyStrnicmp((const char *)pLeft,(const char *)pRight,SLen);
}
static sxu32 Systrcpy(char *zDest,sxu32 nDestLen,const char *zSrc,sxu32 nLen)
{
unsigned char *zBuf = (unsigned char *)zDest;
unsigned char *zIn = (unsigned char *)zSrc;
unsigned char *zEnd;
#if defined(UNTRUST)
if( zSrc == (const char *)zDest ){
return 0;
}
#endif
if( nLen <= 0 ){
nLen = SyStrlen(zSrc);
}
zEnd = &zBuf[nDestLen - 1]; /* reserve a room for the null terminator */
for(;;){
if( zBuf >= zEnd || nLen == 0 ){ break;} zBuf[0] = zIn[0]; zIn++; zBuf++; nLen--;
if( zBuf >= zEnd || nLen == 0 ){ break;} zBuf[0] = zIn[0]; zIn++; zBuf++; nLen--;
if( zBuf >= zEnd || nLen == 0 ){ break;} zBuf[0] = zIn[0]; zIn++; zBuf++; nLen--;
if( zBuf >= zEnd || nLen == 0 ){ break;} zBuf[0] = zIn[0]; zIn++; zBuf++; nLen--;
}
zBuf[0] = 0;
return (sxu32)(zBuf-(unsigned char *)zDest);
}
/* SyRunTimeApi:sxmem.c */
PH7_PRIVATE void SyZero(void *pSrc,sxu32 nSize)
{
register unsigned char *zSrc = (unsigned char *)pSrc;
unsigned char *zEnd;
#if defined(UNTRUST)
if( zSrc == 0 || nSize <= 0 ){
return ;
}
#endif
zEnd = &zSrc[nSize];
for(;;){
if( zSrc >= zEnd ){break;} zSrc[0] = 0; zSrc++;
if( zSrc >= zEnd ){break;} zSrc[0] = 0; zSrc++;
if( zSrc >= zEnd ){break;} zSrc[0] = 0; zSrc++;
if( zSrc >= zEnd ){break;} zSrc[0] = 0; zSrc++;
}
}
PH7_PRIVATE sxi32 SyMemcmp(const void *pB1,const void *pB2,sxu32 nSize)
{
sxi32 rc;
if( nSize <= 0 ){
return 0;
}
if( pB1 == 0 || pB2 == 0 ){
return pB1 != 0 ? 1 : (pB2 == 0 ? 0 : -1);
}
SX_MACRO_FAST_CMP(pB1,pB2,nSize,rc);
return rc;
}
PH7_PRIVATE sxu32 SyMemcpy(const void *pSrc,void *pDest,sxu32 nLen)
{
#if defined(UNTRUST)
if( pSrc == 0 || pDest == 0 ){
return 0;
}
#endif
if( pSrc == (const void *)pDest ){
return nLen;
}
SX_MACRO_FAST_MEMCPY(pSrc,pDest,nLen);
return nLen;
}
static void * MemOSAlloc(sxu32 nBytes)
{
sxu32 *pChunk;
pChunk = (sxu32 *)SyOSHeapAlloc(nBytes + sizeof(sxu32));
if( pChunk == 0 ){
return 0;
}
pChunk[0] = nBytes;
return (void *)&pChunk[1];
}
static void * MemOSRealloc(void *pOld,sxu32 nBytes)
{
sxu32 *pOldChunk;
sxu32 *pChunk;
pOldChunk = (sxu32 *)(((char *)pOld)-sizeof(sxu32));
if( pOldChunk[0] >= nBytes ){
return pOld;
}
pChunk = (sxu32 *)SyOSHeapRealloc(pOldChunk,nBytes + sizeof(sxu32));
if( pChunk == 0 ){
return 0;
}
pChunk[0] = nBytes;
return (void *)&pChunk[1];
}
static void MemOSFree(void *pBlock)
{
void *pChunk;
pChunk = (void *)(((char *)pBlock)-sizeof(sxu32));
SyOSHeapFree(pChunk);
}
static sxu32 MemOSChunkSize(void *pBlock)
{
sxu32 *pChunk;
pChunk = (sxu32 *)(((char *)pBlock)-sizeof(sxu32));
return pChunk[0];
}
/* Export OS allocation methods */
static const SyMemMethods sOSAllocMethods = {
MemOSAlloc,
MemOSRealloc,
MemOSFree,
MemOSChunkSize,
0,
0,
0
};
static void * MemBackendAlloc(SyMemBackend *pBackend,sxu32 nByte)
{
SyMemBlock *pBlock;
sxi32 nRetry = 0;
/* Append an extra block so we can tracks allocated chunks and avoid memory
* leaks.
*/
nByte += sizeof(SyMemBlock);
for(;;){
pBlock = (SyMemBlock *)pBackend->pMethods->xAlloc(nByte);
if( pBlock != 0 || pBackend->xMemError == 0 || nRetry > SXMEM_BACKEND_RETRY
|| SXERR_RETRY != pBackend->xMemError(pBackend->pUserData) ){
break;
}
nRetry++;
}
if( pBlock == 0 ){
return 0;
}
pBlock->pNext = pBlock->pPrev = 0;
/* Link to the list of already tracked blocks */
MACRO_LD_PUSH(pBackend->pBlocks,pBlock);
#if defined(UNTRUST)
pBlock->nGuard = SXMEM_BACKEND_MAGIC;
#endif
pBackend->nBlock++;
return (void *)&pBlock[1];
}
PH7_PRIVATE void * SyMemBackendAlloc(SyMemBackend *pBackend,sxu32 nByte)
{
void *pChunk;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return 0;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
pChunk = MemBackendAlloc(&(*pBackend),nByte);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return pChunk;
}
static void * MemBackendRealloc(SyMemBackend *pBackend,void * pOld,sxu32 nByte)
{
SyMemBlock *pBlock,*pNew,*pPrev,*pNext;
sxu32 nRetry = 0;
if( pOld == 0 ){
return MemBackendAlloc(&(*pBackend),nByte);
}
pBlock = (SyMemBlock *)(((char *)pOld) - sizeof(SyMemBlock));
#if defined(UNTRUST)
if( pBlock->nGuard != SXMEM_BACKEND_MAGIC ){
return 0;
}
#endif
nByte += sizeof(SyMemBlock);
pPrev = pBlock->pPrev;
pNext = pBlock->pNext;
for(;;){
pNew = (SyMemBlock *)pBackend->pMethods->xRealloc(pBlock,nByte);
if( pNew != 0 || pBackend->xMemError == 0 || nRetry > SXMEM_BACKEND_RETRY ||
SXERR_RETRY != pBackend->xMemError(pBackend->pUserData) ){
break;
}
nRetry++;
}
if( pNew == 0 ){
return 0;
}
if( pNew != pBlock ){
if( pPrev == 0 ){
pBackend->pBlocks = pNew;
}else{
pPrev->pNext = pNew;
}
if( pNext ){
pNext->pPrev = pNew;
}
#if defined(UNTRUST)
pNew->nGuard = SXMEM_BACKEND_MAGIC;
#endif
}
return (void *)&pNew[1];
}
PH7_PRIVATE void * SyMemBackendRealloc(SyMemBackend *pBackend,void * pOld,sxu32 nByte)
{
void *pChunk;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return 0;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
pChunk = MemBackendRealloc(&(*pBackend),pOld,nByte);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return pChunk;
}
static sxi32 MemBackendFree(SyMemBackend *pBackend,void * pChunk)
{
SyMemBlock *pBlock;
pBlock = (SyMemBlock *)(((char *)pChunk) - sizeof(SyMemBlock));
#if defined(UNTRUST)
if( pBlock->nGuard != SXMEM_BACKEND_MAGIC ){
return SXERR_CORRUPT;
}
#endif
/* Unlink from the list of active blocks */
if( pBackend->nBlock > 0 ){
/* Release the block */
#if defined(UNTRUST)
/* Mark as stale block */
pBlock->nGuard = 0x635B;
#endif
MACRO_LD_REMOVE(pBackend->pBlocks,pBlock);
pBackend->nBlock--;
pBackend->pMethods->xFree(pBlock);
}
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendFree(SyMemBackend *pBackend,void * pChunk)
{
sxi32 rc;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return SXERR_CORRUPT;
}
#endif
if( pChunk == 0 ){
return SXRET_OK;
}
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
rc = MemBackendFree(&(*pBackend),pChunk);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return rc;
}
#if defined(PH7_ENABLE_THREADS)
PH7_PRIVATE sxi32 SyMemBackendMakeThreadSafe(SyMemBackend *pBackend,const SyMutexMethods *pMethods)
{
SyMutex *pMutex;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) || pMethods == 0 || pMethods->xNew == 0){
return SXERR_CORRUPT;
}
#endif
pMutex = pMethods->xNew(SXMUTEX_TYPE_FAST);
if( pMutex == 0 ){
return SXERR_OS;
}
/* Attach the mutex to the memory backend */
pBackend->pMutex = pMutex;
pBackend->pMutexMethods = pMethods;
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendDisbaleMutexing(SyMemBackend *pBackend)
{
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return SXERR_CORRUPT;
}
#endif
if( pBackend->pMutex == 0 ){
/* There is no mutex subsystem at all */
return SXRET_OK;
}
SyMutexRelease(pBackend->pMutexMethods,pBackend->pMutex);
pBackend->pMutexMethods = 0;
pBackend->pMutex = 0;
return SXRET_OK;
}
#endif
/*
* Memory pool allocator
*/
#define SXMEM_POOL_MAGIC 0xDEAD
#define SXMEM_POOL_MAXALLOC (1<<(SXMEM_POOL_NBUCKETS+SXMEM_POOL_INCR))
#define SXMEM_POOL_MINALLOC (1<<(SXMEM_POOL_INCR))
static sxi32 MemPoolBucketAlloc(SyMemBackend *pBackend,sxu32 nBucket)
{
char *zBucket,*zBucketEnd;
SyMemHeader *pHeader;
sxu32 nBucketSize;
/* Allocate one big block first */
zBucket = (char *)MemBackendAlloc(&(*pBackend),SXMEM_POOL_MAXALLOC);
if( zBucket == 0 ){
return SXERR_MEM;
}
zBucketEnd = &zBucket[SXMEM_POOL_MAXALLOC];
/* Divide the big block into mini bucket pool */
nBucketSize = 1 << (nBucket + SXMEM_POOL_INCR);
pBackend->apPool[nBucket] = pHeader = (SyMemHeader *)zBucket;
for(;;){
if( &zBucket[nBucketSize] >= zBucketEnd ){
break;
}
pHeader->pNext = (SyMemHeader *)&zBucket[nBucketSize];
/* Advance the cursor to the next available chunk */
pHeader = pHeader->pNext;
zBucket += nBucketSize;
}
pHeader->pNext = 0;
return SXRET_OK;
}
static void * MemBackendPoolAlloc(SyMemBackend *pBackend,sxu32 nByte)
{
SyMemHeader *pBucket,*pNext;
sxu32 nBucketSize;
sxu32 nBucket;
if( nByte + sizeof(SyMemHeader) >= SXMEM_POOL_MAXALLOC ){
/* Allocate a big chunk directly */
pBucket = (SyMemHeader *)MemBackendAlloc(&(*pBackend),nByte+sizeof(SyMemHeader));
if( pBucket == 0 ){
return 0;
}
/* Record as big block */
pBucket->nBucket = (sxu32)(SXMEM_POOL_MAGIC << 16) | SXU16_HIGH;
return (void *)(pBucket+1);
}
/* Locate the appropriate bucket */
nBucket = 0;
nBucketSize = SXMEM_POOL_MINALLOC;
while( nByte + sizeof(SyMemHeader) > nBucketSize ){
nBucketSize <<= 1;
nBucket++;
}
pBucket = pBackend->apPool[nBucket];
if( pBucket == 0 ){
sxi32 rc;
rc = MemPoolBucketAlloc(&(*pBackend),nBucket);
if( rc != SXRET_OK ){
return 0;
}
pBucket = pBackend->apPool[nBucket];
}
/* Remove from the free list */
pNext = pBucket->pNext;
pBackend->apPool[nBucket] = pNext;
/* Record bucket&magic number */
pBucket->nBucket = (SXMEM_POOL_MAGIC << 16) | nBucket;
return (void *)&pBucket[1];
}
PH7_PRIVATE void * SyMemBackendPoolAlloc(SyMemBackend *pBackend,sxu32 nByte)
{
void *pChunk;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return 0;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
pChunk = MemBackendPoolAlloc(&(*pBackend),nByte);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return pChunk;
}
static sxi32 MemBackendPoolFree(SyMemBackend *pBackend,void * pChunk)
{
SyMemHeader *pHeader;
sxu32 nBucket;
/* Get the corresponding bucket */
pHeader = (SyMemHeader *)(((char *)pChunk) - sizeof(SyMemHeader));
/* Sanity check to avoid misuse */
if( (pHeader->nBucket >> 16) != SXMEM_POOL_MAGIC ){
return SXERR_CORRUPT;
}
nBucket = pHeader->nBucket & 0xFFFF;
if( nBucket == SXU16_HIGH ){
/* Free the big block */
MemBackendFree(&(*pBackend),pHeader);
}else{
/* Return to the free list */
pHeader->pNext = pBackend->apPool[nBucket & 0x0f];
pBackend->apPool[nBucket & 0x0f] = pHeader;
}
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendPoolFree(SyMemBackend *pBackend,void * pChunk)
{
sxi32 rc;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) || pChunk == 0 ){
return SXERR_CORRUPT;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
rc = MemBackendPoolFree(&(*pBackend),pChunk);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return rc;
}
#if 0
static void * MemBackendPoolRealloc(SyMemBackend *pBackend,void * pOld,sxu32 nByte)
{
sxu32 nBucket,nBucketSize;
SyMemHeader *pHeader;
void * pNew;
if( pOld == 0 ){
/* Allocate a new pool */
pNew = MemBackendPoolAlloc(&(*pBackend),nByte);
return pNew;
}
/* Get the corresponding bucket */
pHeader = (SyMemHeader *)(((char *)pOld) - sizeof(SyMemHeader));
/* Sanity check to avoid misuse */
if( (pHeader->nBucket >> 16) != SXMEM_POOL_MAGIC ){
return 0;
}
nBucket = pHeader->nBucket & 0xFFFF;
if( nBucket == SXU16_HIGH ){
/* Big block */
return MemBackendRealloc(&(*pBackend),pHeader,nByte);
}
nBucketSize = 1 << (nBucket + SXMEM_POOL_INCR);
if( nBucketSize >= nByte + sizeof(SyMemHeader) ){
/* The old bucket can honor the requested size */
return pOld;
}
/* Allocate a new pool */
pNew = MemBackendPoolAlloc(&(*pBackend),nByte);
if( pNew == 0 ){
return 0;
}
/* Copy the old data into the new block */
SyMemcpy(pOld,pNew,nBucketSize);
/* Free the stale block */
MemBackendPoolFree(&(*pBackend),pOld);
return pNew;
}
PH7_PRIVATE void * SyMemBackendPoolRealloc(SyMemBackend *pBackend,void * pOld,sxu32 nByte)
{
void *pChunk;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return 0;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
pChunk = MemBackendPoolRealloc(&(*pBackend),pOld,nByte);
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
}
return pChunk;
}
#endif
PH7_PRIVATE sxi32 SyMemBackendInit(SyMemBackend *pBackend,ProcMemError xMemErr,void * pUserData)
{
#if defined(UNTRUST)
if( pBackend == 0 ){
return SXERR_EMPTY;
}
#endif
/* Zero the allocator first */
SyZero(&(*pBackend),sizeof(SyMemBackend));
pBackend->xMemError = xMemErr;
pBackend->pUserData = pUserData;
/* Switch to the OS memory allocator */
pBackend->pMethods = &sOSAllocMethods;
if( pBackend->pMethods->xInit ){
/* Initialize the backend */
if( SXRET_OK != pBackend->pMethods->xInit(pBackend->pMethods->pUserData) ){
return SXERR_ABORT;
}
}
#if defined(UNTRUST)
pBackend->nMagic = SXMEM_BACKEND_MAGIC;
#endif
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendInitFromOthers(SyMemBackend *pBackend,const SyMemMethods *pMethods,ProcMemError xMemErr,void * pUserData)
{
#if defined(UNTRUST)
if( pBackend == 0 || pMethods == 0){
return SXERR_EMPTY;
}
#endif
if( pMethods->xAlloc == 0 || pMethods->xRealloc == 0 || pMethods->xFree == 0 || pMethods->xChunkSize == 0 ){
/* mandatory methods are missing */
return SXERR_INVALID;
}
/* Zero the allocator first */
SyZero(&(*pBackend),sizeof(SyMemBackend));
pBackend->xMemError = xMemErr;
pBackend->pUserData = pUserData;
/* Switch to the host application memory allocator */
pBackend->pMethods = pMethods;
if( pBackend->pMethods->xInit ){
/* Initialize the backend */
if( SXRET_OK != pBackend->pMethods->xInit(pBackend->pMethods->pUserData) ){
return SXERR_ABORT;
}
}
#if defined(UNTRUST)
pBackend->nMagic = SXMEM_BACKEND_MAGIC;
#endif
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendInitFromParent(SyMemBackend *pBackend,SyMemBackend *pParent)
{
sxu8 bInheritMutex;
#if defined(UNTRUST)
if( pBackend == 0 || SXMEM_BACKEND_CORRUPT(pParent) ){
return SXERR_CORRUPT;
}
#endif
/* Zero the allocator first */
SyZero(&(*pBackend),sizeof(SyMemBackend));
pBackend->pMethods = pParent->pMethods;
pBackend->xMemError = pParent->xMemError;
pBackend->pUserData = pParent->pUserData;
bInheritMutex = pParent->pMutexMethods ? TRUE : FALSE;
if( bInheritMutex ){
pBackend->pMutexMethods = pParent->pMutexMethods;
/* Create a private mutex */
pBackend->pMutex = pBackend->pMutexMethods->xNew(SXMUTEX_TYPE_FAST);
if( pBackend->pMutex == 0){
return SXERR_OS;
}
}
#if defined(UNTRUST)
pBackend->nMagic = SXMEM_BACKEND_MAGIC;
#endif
return SXRET_OK;
}
static sxi32 MemBackendRelease(SyMemBackend *pBackend)
{
SyMemBlock *pBlock,*pNext;
pBlock = pBackend->pBlocks;
for(;;){
if( pBackend->nBlock == 0 ){
break;
}
pNext = pBlock->pNext;
pBackend->pMethods->xFree(pBlock);
pBlock = pNext;
pBackend->nBlock--;
/* LOOP ONE */
if( pBackend->nBlock == 0 ){
break;
}
pNext = pBlock->pNext;
pBackend->pMethods->xFree(pBlock);
pBlock = pNext;
pBackend->nBlock--;
/* LOOP TWO */
if( pBackend->nBlock == 0 ){
break;
}
pNext = pBlock->pNext;
pBackend->pMethods->xFree(pBlock);
pBlock = pNext;
pBackend->nBlock--;
/* LOOP THREE */
if( pBackend->nBlock == 0 ){
break;
}
pNext = pBlock->pNext;
pBackend->pMethods->xFree(pBlock);
pBlock = pNext;
pBackend->nBlock--;
/* LOOP FOUR */
}
if( pBackend->pMethods->xRelease ){
pBackend->pMethods->xRelease(pBackend->pMethods->pUserData);
}
pBackend->pMethods = 0;
pBackend->pBlocks = 0;
#if defined(UNTRUST)
pBackend->nMagic = 0x2626;
#endif
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyMemBackendRelease(SyMemBackend *pBackend)
{
sxi32 rc;
#if defined(UNTRUST)
if( SXMEM_BACKEND_CORRUPT(pBackend) ){
return SXERR_INVALID;
}
#endif
if( pBackend->pMutexMethods ){
SyMutexEnter(pBackend->pMutexMethods,pBackend->pMutex);
}
rc = MemBackendRelease(&(*pBackend));
if( pBackend->pMutexMethods ){
SyMutexLeave(pBackend->pMutexMethods,pBackend->pMutex);
SyMutexRelease(pBackend->pMutexMethods,pBackend->pMutex);
}
return SXRET_OK;
}
PH7_PRIVATE void * SyMemBackendDup(SyMemBackend *pBackend,const void *pSrc,sxu32 nSize)
{
void *pNew;
#if defined(UNTRUST)
if( pSrc == 0 || nSize <= 0 ){
return 0;
}
#endif
pNew = SyMemBackendAlloc(&(*pBackend),nSize);
if( pNew ){
SyMemcpy(pSrc,pNew,nSize);
}
return pNew;
}
PH7_PRIVATE char * SyMemBackendStrDup(SyMemBackend *pBackend,const char *zSrc,sxu32 nSize)
{
char *zDest;
zDest = (char *)SyMemBackendAlloc(&(*pBackend),nSize + 1);
if( zDest ){
Systrcpy(zDest,nSize+1,zSrc,nSize);
}
return zDest;
}
PH7_PRIVATE sxi32 SyBlobInitFromBuf(SyBlob *pBlob,void *pBuffer,sxu32 nSize)
{
#if defined(UNTRUST)
if( pBlob == 0 || pBuffer == 0 || nSize < 1 ){
return SXERR_EMPTY;
}
#endif
pBlob->pBlob = pBuffer;
pBlob->mByte = nSize;
pBlob->nByte = 0;
pBlob->pAllocator = 0;
pBlob->nFlags = SXBLOB_LOCKED|SXBLOB_STATIC;
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyBlobInit(SyBlob *pBlob,SyMemBackend *pAllocator)
{
#if defined(UNTRUST)
if( pBlob == 0 ){
return SXERR_EMPTY;
}
#endif
pBlob->pBlob = 0;
pBlob->mByte = pBlob->nByte = 0;
pBlob->pAllocator = &(*pAllocator);
pBlob->nFlags = 0;
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyBlobReadOnly(SyBlob *pBlob,const void *pData,sxu32 nByte)
{
#if defined(UNTRUST)
if( pBlob == 0 ){
return SXERR_EMPTY;
}
#endif
pBlob->pBlob = (void *)pData;
pBlob->nByte = nByte;
pBlob->mByte = 0;
pBlob->nFlags |= SXBLOB_RDONLY;
return SXRET_OK;
}
#ifndef SXBLOB_MIN_GROWTH
#define SXBLOB_MIN_GROWTH 16
#endif
static sxi32 BlobPrepareGrow(SyBlob *pBlob,sxu32 *pByte)
{
sxu32 nByte;
void *pNew;
nByte = *pByte;
if( pBlob->nFlags & (SXBLOB_LOCKED|SXBLOB_STATIC) ){
if ( SyBlobFreeSpace(pBlob) < nByte ){
*pByte = SyBlobFreeSpace(pBlob);
if( (*pByte) == 0 ){
return SXERR_SHORT;
}
}
return SXRET_OK;
}
if( pBlob->nFlags & SXBLOB_RDONLY ){
/* Make a copy of the read-only item */
if( pBlob->nByte > 0 ){
pNew = SyMemBackendDup(pBlob->pAllocator,pBlob->pBlob,pBlob->nByte);
if( pNew == 0 ){
return SXERR_MEM;
}
pBlob->pBlob = pNew;
pBlob->mByte = pBlob->nByte;
}else{
pBlob->pBlob = 0;
pBlob->mByte = 0;
}
/* Remove the read-only flag */
pBlob->nFlags &= ~SXBLOB_RDONLY;
}
if( SyBlobFreeSpace(pBlob) >= nByte ){
return SXRET_OK;
}
if( pBlob->mByte > 0 ){
nByte = nByte + pBlob->mByte * 2 + SXBLOB_MIN_GROWTH;
}else if ( nByte < SXBLOB_MIN_GROWTH ){
nByte = SXBLOB_MIN_GROWTH;
}
pNew = SyMemBackendRealloc(pBlob->pAllocator,pBlob->pBlob,nByte);
if( pNew == 0 ){
return SXERR_MEM;
}
pBlob->pBlob = pNew;
pBlob->mByte = nByte;
return SXRET_OK;
}
PH7_PRIVATE sxi32 SyBlobAppend(SyBlob *pBlob,const void *pData,sxu32 nSize)
{
sxu8 *zBlob;
sxi32 rc;
if( nSize < 1 ){
return SXRET_OK;
}
rc = BlobPrepareGrow(&(*pBlob),&nSize);
if( SXRET_OK != rc ){
return rc;
}
if( pData ){
zBlob = (sxu8 *)pBlob->pBlob ;
zBlob = &zBlob[pBlob->nByte];