First version of #25 implementation, providing memory_get_usage/memory_get_peak_usage/memory_limit

This commit is contained in:
2018-08-09 07:37:56 +00:00
parent 08c47b7528
commit 6a43e654b6
10 changed files with 152 additions and 26 deletions

View File

@@ -86,7 +86,10 @@ typedef struct ph7_context ph7_context;
typedef struct ph7_value ph7_value;
typedef struct ph7_vfs ph7_vfs;
typedef struct ph7_vm ph7_vm;
typedef struct ph7_heap ph7_heap;
typedef struct ph7 ph7;
typedef struct SyMemBackend SyMemBackend;
/*
* ------------------------------
* Compile time directives
@@ -246,9 +249,9 @@ struct Sytm {
/* Dynamic memory allocation methods. */
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 */
void *(*xAlloc)(unsigned int, SyMemBackend *); /* [Required:] Allocate a memory chunk */
void *(*xRealloc)(void *, unsigned int, SyMemBackend *); /* [Required:] Re-allocate a memory chunk */
void (*xFree)(void *, SyMemBackend *); /* [Required:] Release a memory chunk */
unsigned int (*xChunkSize)(void *); /* [Optional:] Return chunk size */
int (*xInit)(void *); /* [Optional:] Initialization callback */
void (*xRelease)(void *); /* [Optional:] Release callback */
@@ -351,6 +354,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 *iMaxLimit */
/*
* Virtual Machine Configuration Commands.
*
@@ -580,6 +584,13 @@ struct ph7_io_stream {
int (*xSync)(void *); /* Flush open stream data */
int (*xStat)(void *, ph7_value *, ph7_value *); /* Stat an open stream handle */
};
struct ph7_heap {
ph7_vm *pVm; /* Reference to active VM */
sxu64 nBytes; /* Actually allocated */
sxu64 iPeak; /* As its peak */
sxu64 iMax; /* Max allowed */
};
/*
* C-API-REF: Please refer to the official documentation for interfaces
* purpose and expected parameters.

View File

@@ -146,7 +146,6 @@ typedef sxi32(*ProcRawStrCmp)(const SyString *, const SyString *);
#define SXUNUSED(P) (P = 0)
#define SX_EMPTY(PTR) (PTR == 0)
#define SX_EMPTY_STR(STR) (STR == 0 || STR[0] == 0 )
typedef struct SyMemBackend SyMemBackend;
typedef struct SyBlob SyBlob;
typedef struct SySet SySet;
/* Standard function signatures */
@@ -242,6 +241,7 @@ union SyMemHeader {
struct SyMemBackend {
const SyMutexMethods *pMutexMethods; /* Mutex methods */
const SyMemMethods *pMethods; /* Memory allocation methods */
ph7_heap pHeap; /* Heap infos */
SyMemBlock *pBlocks; /* List of valid memory blocks */
sxu32 nBlock; /* Total number of memory blocks allocated so far */
ProcMemError xMemError; /* Out-of memory callback */
@@ -1026,7 +1026,7 @@ typedef struct ph7_class_method ph7_class_method;
typedef struct ph7_class_attr ph7_class_attr;
/*
* Information about class/interface inheritance and interface implementation
* is stored in an instance of the following structure.
* is stored in an instance of the following structure.
*/
struct ph7_class_info {
SyString sName; /* Class full qualified name */
@@ -1810,6 +1810,7 @@ PH7_PRIVATE void *SyMemBackendAlloc(SyMemBackend *pBackend, sxu32 nByte);
PH7_PRIVATE sxu32 SyMemcpy(const void *pSrc, void *pDest, sxu32 nLen);
PH7_PRIVATE sxi32 SyMemcmp(const void *pB1, const void *pB2, sxu32 nSize);
PH7_PRIVATE void SyZero(void *pSrc, sxu32 nSize);
PH7_PRIVATE sxu32 Systrcpy(char *zDest, sxu32 nDestLen, const char *zSrc, sxu32 nLen);
PH7_PRIVATE sxi32 SyStrnicmp(const char *zLeft, const char *zRight, sxu32 SLen);
PH7_PRIVATE sxi32 SyStrnmicmp(const void *pLeft, const void *pRight, sxu32 SLen);
#ifndef PH7_DISABLE_BUILTIN_FUNC