First version of #25 implementation, providing memory_get_usage/memory_get_peak_usage/memory_limit
This commit is contained in:
38
engine/vm.c
38
engine/vm.c
@@ -12,6 +12,7 @@
|
||||
*/
|
||||
/* $SymiscID: vm.c v1.4 FreeBSD 2012-09-10 00:06 stable <chm@symisc.net> $ */
|
||||
#include "ph7int.h"
|
||||
#include <stdlib.h>
|
||||
/*
|
||||
* The code in this file implements execution method of the PH7 Virtual Machine.
|
||||
* The PH7 compiler (implemented in 'compiler.c' and 'parse.c') generates a bytecode program
|
||||
@@ -1075,6 +1076,7 @@ PH7_PRIVATE sxi32 PH7_VmInit(
|
||||
}
|
||||
/* VM correctly initialized,set the magic number */
|
||||
pVm->nMagic = PH7_VM_INIT;
|
||||
pVm->sAllocator.pHeap.pVm = pVm;
|
||||
SyStringInitFromBuf(&sBuiltin, PH7_BUILTIN_LIB, sizeof(PH7_BUILTIN_LIB) - 1);
|
||||
/* Precompile the built-in library */
|
||||
VmEvalChunk(&(*pVm), 0, &sBuiltin, PH7_AERSCRIPT_CODE);
|
||||
@@ -4217,8 +4219,7 @@ static sxi32 VmByteCodeExec(
|
||||
* Perform additional class initialization, by adding base classes
|
||||
* and interfaces to its definition.
|
||||
*/
|
||||
case PH7_OP_CLASS_INIT:
|
||||
{
|
||||
case PH7_OP_CLASS_INIT: {
|
||||
ph7_class_info *pClassInfo = (ph7_class_info *)pInstr->p3;
|
||||
ph7_class *pClass = PH7_VmExtractClass(pVm, pClassInfo->sName.zString, pClassInfo->sName.nByte, FALSE, 0);
|
||||
ph7_class *pBase = 0;
|
||||
@@ -4268,8 +4269,7 @@ static sxi32 VmByteCodeExec(
|
||||
* Perform additional interface initialization, by adding base interfaces
|
||||
* to its definition.
|
||||
*/
|
||||
case PH7_OP_INTERFACE_INIT:
|
||||
{
|
||||
case PH7_OP_INTERFACE_INIT: {
|
||||
ph7_class_info *pClassInfo = (ph7_class_info *)pInstr->p3;
|
||||
ph7_class *pClass = PH7_VmExtractClass(pVm, pClassInfo->sName.zString, pClassInfo->sName.nByte, FALSE, 0);
|
||||
ph7_class *pBase = 0;
|
||||
@@ -10677,6 +10677,33 @@ static int vm_builtin_require(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
|
||||
static int vm_builtin_memory_get_usage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
if(nArg != 0) {
|
||||
ph7_result_bool(pCtx, 0);
|
||||
} else {
|
||||
ph7_result_int64(pCtx, pCtx->pVm->sAllocator.pHeap.nBytes);
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
|
||||
static int vm_builtin_memory_get_peak_usage(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
if(nArg != 0) {
|
||||
ph7_result_bool(pCtx, 0);
|
||||
} else {
|
||||
ph7_result_int64(pCtx, pCtx->pVm->sAllocator.pHeap.iPeak);
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
|
||||
static int vm_builtin_memory_limit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
|
||||
if(nArg != 0) {
|
||||
ph7_result_bool(pCtx, 0);
|
||||
} else {
|
||||
ph7_result_int64(pCtx, pCtx->pVm->sAllocator.pHeap.iMax);
|
||||
}
|
||||
return SXRET_OK;
|
||||
}
|
||||
/*
|
||||
* Section:
|
||||
* Command line arguments processing.
|
||||
@@ -11285,6 +11312,9 @@ static const ph7_builtin_func aVmFunc[] = {
|
||||
{ "get_included_files", vm_builtin_get_included_files},
|
||||
{ "include", vm_builtin_include },
|
||||
{ "require", vm_builtin_require },
|
||||
{ "memory_get_usage", vm_builtin_memory_get_usage },
|
||||
{ "memory_get_peak_usage", vm_builtin_memory_get_peak_usage },
|
||||
{ "memory_limit", vm_builtin_memory_limit },
|
||||
};
|
||||
/*
|
||||
* Register the built-in VM functions defined above.
|
||||
|
Reference in New Issue
Block a user