Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local memory management #1880

Merged
merged 2 commits into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Library/Backends/ZendEngine2/Backend.php
Expand Up @@ -185,6 +185,11 @@ public function getTypeDefinition($type)
$code = 'zend_class_entry';
break;

case 'zephir_method_globals':
$pointer = '*';
$code = 'zephir_method_globals';
break;

case 'zephir_ce_guard':
$code = 'zend_bool';
break;
Expand Down
10 changes: 9 additions & 1 deletion Library/ClassMethod.php
Expand Up @@ -2042,7 +2042,7 @@ public function compile(CompilationContext $compilationContext)
if ($symbolTable->getMustGrownStack()) {
$code .= "\t".'zephir_fetch_params(1, '.$numberRequiredParams.', '.$numberOptionalParams.', '.implode(', ', $params).');'.PHP_EOL;
} else {
$code .= "\t".'zephir_fetch_params(0, '.$numberRequiredParams.', '.$numberOptionalParams.', '.implode(', ', $params).');'.PHP_EOL;
$code .= "\t".'zephir_fetch_params_without_memory_grow('.$numberRequiredParams.', '.$numberOptionalParams.', '.implode(', ', $params).');'.PHP_EOL;
}
} else {
foreach ($params as $param) {
Expand Down Expand Up @@ -2089,6 +2089,14 @@ public function compile(CompilationContext $compilationContext)
*/
if ($symbolTable->getMustGrownStack()) {
$compilationContext->headersManager->add('kernel/memory');
if (!$compilationContext->symbolTable->hasVariable('ZEPHIR_METHOD_GLOBALS_PTR')) {
$methodGlobals = new Variable('zephir_method_globals', 'ZEPHIR_METHOD_GLOBALS_PTR', $compilationContext->branchManager->getCurrentBranch());
$methodGlobals->setMustInitNull(true);
$methodGlobals->increaseUses();
$methodGlobals->setReusable(false);
$methodGlobals->setReadOnly(true);
$compilationContext->symbolTable->addRawVariable($methodGlobals);
}
$codePrinter->preOutput("\t".'ZEPHIR_MM_GROW();');
}

Expand Down
6 changes: 5 additions & 1 deletion Library/Expression/Constants.php
Expand Up @@ -221,7 +221,11 @@ public function compile(array $expression, CompilationContext $compilationContex
throw new CompilerException('Cannot use variable: '.$symbolVariable->getType().' to assign property value', $expression);
}

$compilationContext->codePrinter->output('ZEPHIR_GET_CONSTANT('.$compilationContext->backend->getVariableCode($symbolVariable).', "'.$expression['value'].'");');
if ($compilationContext->symbolTable->getMustGrownStack()) {
$compilationContext->codePrinter->output('ZEPHIR_MM_GET_CONSTANT('.$compilationContext->backend->getVariableCode($symbolVariable).', "'.$expression['value'].'");');
} else {
$compilationContext->codePrinter->output('ZEPHIR_GET_CONSTANT('.$compilationContext->backend->getVariableCode($symbolVariable).', "'.$expression['value'].'");');
}

return new CompiledExpression('variable', $symbolVariable->getName(), $expression);
}
Expand Down
Expand Up @@ -57,9 +57,9 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
$symbolVariable->initVariant($context);
}

$context->headersManager->add('kernel/memory');
$context->symbolTable->mustGrownStack(true);

$context->codePrinter->output('zephir_create_symbol_table(TSRMLS_C);');
$context->codePrinter->output('ZEPHIR_CREATE_SYMBOL_TABLE();');
$context->codePrinter->output('');

return new CompiledExpression('null', null, $expression);
Expand Down
7 changes: 4 additions & 3 deletions ext/kernel/exception.c
Expand Up @@ -40,6 +40,7 @@
void zephir_throw_exception_debug(zval *object, const char *file, zend_uint line)
{
zend_class_entry *default_exception_ce;
zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
int ZEPHIR_LAST_CALL_STATUS = 0;
zval curline;
zval object_copy;
Expand Down Expand Up @@ -86,7 +87,7 @@ void zephir_throw_exception_string_debug(zend_class_entry *ce, const char *messa

ZVAL_STRINGL(&msg, message, message_len);

ZEPHIR_CALL_METHOD(NULL, &object, "__construct", NULL, 0, &msg);
ZEPHIR_CALL_METHOD_WITHOUT_OBSERVE(NULL, &object, "__construct", NULL, 0, &msg);

if (line > 0) {
default_exception_ce = zend_exception_get_default();
Expand All @@ -113,7 +114,7 @@ void zephir_throw_exception_string(zend_class_entry *ce, const char *message, ze

ZVAL_STRINGL(&msg, message, message_len);

ZEPHIR_CALL_METHOD(NULL, &object, "__construct", NULL, 0, &msg);
ZEPHIR_CALL_METHOD_WITHOUT_OBSERVE(NULL, &object, "__construct", NULL, 0, &msg);

if (ZEPHIR_LAST_CALL_STATUS != FAILURE) {
zend_throw_exception_object(&object);
Expand Down Expand Up @@ -141,7 +142,7 @@ void zephir_throw_exception_format(zend_class_entry *ce, const char *format, ...
ZVAL_STRINGL(&msg, buffer, len);
efree(buffer);

ZEPHIR_CALL_METHOD(NULL, &object, "__construct", NULL, 0, &msg);
ZEPHIR_CALL_METHOD_WITHOUT_OBSERVE(NULL, &object, "__construct", NULL, 0, &msg);

if (ZEPHIR_LAST_CALL_STATUS != FAILURE) {
zend_throw_exception_object(&object);
Expand Down
24 changes: 0 additions & 24 deletions ext/kernel/globals.h
Expand Up @@ -23,32 +23,8 @@

#include <php.h>

#define ZEPHIR_MAX_MEMORY_STACK 48
#define ZEPHIR_MAX_CACHE_SLOTS 512

/** Memory frame */
typedef struct _zephir_memory_entry {
size_t pointer;
size_t capacity;
zval **addresses;
size_t hash_pointer;
size_t hash_capacity;
zval **hash_addresses;
struct _zephir_memory_entry *prev;
struct _zephir_memory_entry *next;
#ifndef ZEPHIR_RELEASE
int permanent;
const char *func;
#endif
} zephir_memory_entry;

/** Virtual Symbol Table */
typedef struct _zephir_symbol_table {
struct _zephir_memory_entry *scope;
zend_array *symbol_table;
struct _zephir_symbol_table *prev;
} zephir_symbol_table;

typedef struct _zephir_function_cache {
zend_class_entry *ce;
zend_function *func;
Expand Down
15 changes: 14 additions & 1 deletion ext/kernel/main.h
Expand Up @@ -266,6 +266,11 @@ int zephir_fetch_parameters(int num_args, int required_args, int optional_args,
} \
}

#define zephir_fetch_params_without_memory_grow(required_params, optional_params, ...) \
if (zephir_fetch_parameters(ZEND_NUM_ARGS(), required_params, optional_params, __VA_ARGS__) == FAILURE) { \
RETURN_NULL(); \
}

#define ZEPHIR_CREATE_OBJECT(obj, class_type) \
{ \
zend_object *object = zend_objects_new(class_type); \
Expand All @@ -277,7 +282,7 @@ int zephir_fetch_parameters(int num_args, int required_args, int optional_args,
#define ZEPHIR_MAKE_REF(obj) ZVAL_NEW_REF(obj, obj);
#define ZEPHIR_UNREF(obj) ZVAL_UNREF(obj);

#define ZEPHIR_GET_CONSTANT(return_value, const_name) do { \
#define ZEPHIR_MM_GET_CONSTANT(return_value, const_name) do { \
zval *_constant_ptr = zend_get_constant_str(SL(const_name)); \
if (_constant_ptr == NULL) { \
ZEPHIR_MM_RESTORE(); \
Expand All @@ -286,6 +291,14 @@ int zephir_fetch_parameters(int num_args, int required_args, int optional_args,
ZVAL_COPY(return_value, _constant_ptr); \
} while(0)

#define ZEPHIR_GET_CONSTANT(return_value, const_name) do { \
zval *_constant_ptr = zend_get_constant_str(SL(const_name)); \
if (_constant_ptr == NULL) { \
return; \
} \
ZVAL_COPY(return_value, _constant_ptr); \
} while(0)

#define ZEPHIR_GET_IMKEY(var, it) it->funcs->get_current_key(it, &var);

/* Declare class constants */
Expand Down