This is a new memory subsystem implementing heap calculations as well as new builtin functions: * get_memory_usage() * get_memory_peak_usage() * get_memory_limit() It also allows to set an upper memory limit, ensuring that processed script will not be able to allocate more memory from OS. New subsystem is based on work done in 'memory_limit' branch. Big thanks to devnexen! This finally fixes #25.
This commit is contained in:
@@ -249,7 +249,7 @@ struct SyMemMethods {
|
||||
void *(*xAlloc)(unsigned int); /* [Required:] Allocate a memory chunk */
|
||||
void *(*xRealloc)(void *, unsigned int); /* [Required:] Re-allocate a memory chunk */
|
||||
void (*xFree)(void *); /* [Required:] Release a memory chunk */
|
||||
unsigned int (*xChunkSize)(void *); /* [Optional:] Return chunk size */
|
||||
unsigned int (*xChunkSize)(void *); /* [Required:] Return chunk size */
|
||||
int (*xInit)(void *); /* [Optional:] Initialization callback */
|
||||
void (*xRelease)(void *); /* [Optional:] Release callback */
|
||||
void *pUserData; /* [Optional:] First argument to xInit() and xRelease() */
|
||||
@@ -351,6 +351,7 @@ typedef sxi64 ph7_int64;
|
||||
#define PH7_CONFIG_ERR_OUTPUT 1 /* TWO ARGUMENTS: int (*xConsumer)(const void *pOut,unsigned int nLen,void *pUserData),void *pUserData */
|
||||
#define PH7_CONFIG_ERR_ABORT 2 /* RESERVED FOR FUTURE USE */
|
||||
#define PH7_CONFIG_ERR_LOG 3 /* TWO ARGUMENTS: const char **pzBuf,int *pLen */
|
||||
#define PH7_CONFIG_MEM_LIMIT 4 /* ONE ARGUMENT: char *nMemLimit */
|
||||
/*
|
||||
* Virtual Machine Configuration Commands.
|
||||
*
|
||||
|
@@ -239,9 +239,18 @@ union SyMemHeader {
|
||||
SyMemHeader *pNext; /* Next chunk of size 1 << (nBucket + SXMEM_POOL_INCR) in the list */
|
||||
sxu32 nBucket; /* Bucket index in aPool[] */
|
||||
};
|
||||
/* Heap allocation control structure */
|
||||
typedef struct SyMemHeap SyMemHeap;
|
||||
struct SyMemHeap {
|
||||
sxu64 nSize; /* Current memory usage */
|
||||
sxu64 nPeak; /* Peak memory usage */
|
||||
sxu64 nLimit; /* Memory limit */
|
||||
};
|
||||
/* Memory allocation backend container */
|
||||
struct SyMemBackend {
|
||||
const SyMutexMethods *pMutexMethods; /* Mutex methods */
|
||||
const SyMemMethods *pMethods; /* Memory allocation methods */
|
||||
SyMemHeap *pHeap; /* Heap allocation */
|
||||
SyMemBlock *pBlocks; /* List of valid memory blocks */
|
||||
sxu32 nBlock; /* Total number of memory blocks allocated so far */
|
||||
ProcMemError xMemError; /* Out-of memory callback */
|
||||
@@ -1799,10 +1808,10 @@ PH7_PRIVATE sxi32 SyMemBackendInitFromParent(SyMemBackend *pBackend, SyMemBacken
|
||||
PH7_PRIVATE void *SyMemBackendPoolRealloc(SyMemBackend *pBackend, void *pOld, sxu32 nByte);
|
||||
#endif
|
||||
PH7_PRIVATE sxi32 SyMemBackendPoolFree(SyMemBackend *pBackend, void *pChunk);
|
||||
PH7_PRIVATE void *SyMemBackendPoolAlloc(SyMemBackend *pBackend, sxu32 nByte);
|
||||
PH7_PRIVATE void *SyMemBackendPoolAlloc(SyMemBackend *pBackend, sxu32 nBytes);
|
||||
PH7_PRIVATE sxi32 SyMemBackendFree(SyMemBackend *pBackend, void *pChunk);
|
||||
PH7_PRIVATE void *SyMemBackendRealloc(SyMemBackend *pBackend, void *pOld, sxu32 nByte);
|
||||
PH7_PRIVATE void *SyMemBackendAlloc(SyMemBackend *pBackend, sxu32 nByte);
|
||||
PH7_PRIVATE void *SyMemBackendRealloc(SyMemBackend *pBackend, void *pOld, sxu32 nBytes);
|
||||
PH7_PRIVATE void *SyMemBackendAlloc(SyMemBackend *pBackend, sxu32 nBytes);
|
||||
#if defined(PH7_ENABLE_THREADS)
|
||||
PH7_PRIVATE sxi32 SyMemBackendMakeThreadSafe(SyMemBackend *pBackend, const SyMutexMethods *pMethods);
|
||||
PH7_PRIVATE sxi32 SyMemBackendDisbaleMutexing(SyMemBackend *pBackend);
|
||||
|
Reference in New Issue
Block a user