Skip to content

Commit

Permalink
Remove memory management logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Nov 26, 2023
1 parent 777b991 commit e9436ee
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 52 deletions.
5 changes: 0 additions & 5 deletions kernel/exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@
void zephir_throw_exception_debug(zval *object, const char *file, uint32_t 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;

ZVAL_UNDEF(&curline);

ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);
zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);

if (Z_TYPE_P(object) != IS_OBJECT) {
ZVAL_COPY_VALUE(&object_copy, object);
object_init_ex(object, zend_exception_get_default());
Expand All @@ -63,7 +59,6 @@ void zephir_throw_exception_debug(zval *object, const char *file, uint32_t line)
if (ZEPHIR_LAST_CALL_STATUS != FAILURE) {
zend_throw_exception_object(object);
}
ZEPHIR_MM_RESTORE();
}

/**
Expand Down
4 changes: 0 additions & 4 deletions kernel/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@
#define ZEPHIR_THROW_EXCEPTION_STR(class_entry, message) \
do { \
zephir_throw_exception_string(class_entry, message, strlen(message)); \
ZEPHIR_MM_RESTORE(); \
} while (0)

#define ZEPHIR_THROW_EXCEPTION_DEBUG_STR(class_entry, message, file, line) \
do { \
zephir_throw_exception_string_debug(class_entry, message, strlen(message), file, line); \
ZEPHIR_MM_RESTORE(); \
} while (0)

#define ZEPHIR_THROW_EXCEPTION_ZVAL(class_entry, message) \
do { \
zephir_throw_exception_zval(class_entry, message); \
ZEPHIR_MM_RESTORE(); \
} while (0)

#define ZEPHIR_THROW_EXCEPTION_DEBUG_ZVAL(class_entry, message, file, line) \
do { \
zephir_throw_exception_zval(class_entry, message, file, line); \
ZEPHIR_MM_RESTORE(); \
} while (0)


Expand Down
4 changes: 0 additions & 4 deletions kernel/fcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ ZEPHIR_ATTR_WARN_UNUSED_RESULT ZEPHIR_ATTR_NONNULL static inline int zephir_has_
#define zephir_check_call_status() \
do { \
if (ZEPHIR_LAST_CALL_STATUS == FAILURE) { \
ZEPHIR_MM_RESTORE(); \
return; \
} \
} while(0)
Expand All @@ -404,9 +403,6 @@ ZEPHIR_ATTR_WARN_UNUSED_RESULT ZEPHIR_ATTR_NONNULL static inline int zephir_has_
if (ZEPHIR_LAST_CALL_STATUS == FAILURE) { \
if (EG(exception)) { \
goto label; \
} else { \
ZEPHIR_MM_RESTORE(); \
return; \
} \
} \
} while (0)
Expand Down
19 changes: 7 additions & 12 deletions kernel/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ extern zend_string* i_self;
#define RETURN_CTOR(var) \
do { \
RETVAL_ZVAL(var, 1, 0); \
ZEPHIR_MM_RESTORE(); \
return; \
} while (0)

Expand All @@ -127,7 +126,6 @@ extern zend_string* i_self;
#define RETURN_CCTOR(v) \
do { \
ZVAL_DUP(return_value, v); \
ZEPHIR_MM_RESTORE(); \
return; \
} while (0)

Expand All @@ -146,7 +144,6 @@ extern zend_string* i_self;
#define RETURN_THIS() { \
RETVAL_ZVAL(getThis(), 1, 0); \
} \
ZEPHIR_MM_RESTORE(); \
return;

#define RETURN_LCTORW(var) RETURN_CCTORW(var);
Expand All @@ -161,20 +158,20 @@ extern zend_string* i_self;
return;

/** Return without change return_value */
#define RETURN_MM() { ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM() { return; }

/** Return null restoring memory frame */
#define RETURN_MM_BOOL(value) { RETVAL_BOOL(value); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_BOOL(value) { RETVAL_BOOL(value); return; }

/** Return string restoring memory frame */
#define RETURN_MM_STRING(str) { RETVAL_STRING(str); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_EMPTY_STRING() { RETVAL_EMPTY_STRING(); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_STRING(str) { RETVAL_STRING(str); return; }
#define RETURN_MM_EMPTY_STRING() { RETVAL_EMPTY_STRING(); return; }

/* Return long */
#define RETURN_MM_LONG(value) { RETVAL_LONG(value); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_LONG(value) { RETVAL_LONG(value); return; }

/* Return double */
#define RETURN_MM_DOUBLE(value) { RETVAL_DOUBLE(value); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_DOUBLE(value) { RETVAL_DOUBLE(value); return; }

/**
* Returns a zval in an object member
Expand All @@ -193,13 +190,12 @@ extern zend_string* i_self;
#define RETURN_MM_ON_FAILURE(what) \
do { \
if (what == FAILURE) { \
ZEPHIR_MM_RESTORE(); \
return; \
} \
} while (0)

/** Return null restoring memory frame */
#define RETURN_MM_NULL() { RETVAL_NULL(); ZEPHIR_MM_RESTORE(); return; }
#define RETURN_MM_NULL() { RETVAL_NULL(); return; }

/* Globals functions */
int zephir_get_global(zval *arr, const char *global, unsigned int global_length);
Expand Down Expand Up @@ -245,7 +241,6 @@ int zephir_is_iterable_ex(zval *arr, int duplicate);
#define zephir_is_iterable(var, duplicate, file, line) \
if (!zephir_is_iterable_ex(var, duplicate)) { \
ZEPHIR_THROW_EXCEPTION_DEBUG_STRW(zend_exception_get_default(), "The argument is not initialized or iterable()", file, line); \
ZEPHIR_MM_RESTORE(); \
return; \
}

Expand Down
2 changes: 0 additions & 2 deletions kernel/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ void ZEPHIR_FASTCALL zephir_memory_grow_stack(zephir_method_globals *g, const ch
{
if (g->active_memory == NULL) {
zephir_memory_entry *active_memory;

active_memory = (zephir_memory_entry *) pecalloc(1, sizeof(zephir_memory_entry), 0);

active_memory->addresses = pecalloc(24, sizeof(zval*), 0);
active_memory->capacity = 24;

Expand Down
23 changes: 4 additions & 19 deletions kernel/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,28 @@ typedef struct _zephir_method_globals {
void ZEPHIR_FASTCALL zephir_memory_grow_stack(zephir_method_globals *g, const char *func);
void ZEPHIR_FASTCALL zephir_memory_restore_stack(zephir_method_globals *g, const char *func);

#define ZEPHIR_MM_RESTORE() \
zephir_memory_restore_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__); \
pefree(ZEPHIR_METHOD_GLOBALS_PTR, 0); \
ZEPHIR_METHOD_GLOBALS_PTR = NULL;
#define ZEPHIR_MM_RESTORE() do { /* dummy code */ } while (0)

void zephir_initialize_memory(zend_zephir_globals_def *zephir_globals_ptr);
void zephir_deinitialize_memory();

void ZEPHIR_FASTCALL zephir_do_memory_observe(zval *var, const zephir_method_globals *g);
#define zephir_memory_observe(var) zephir_do_memory_observe(var, ZEPHIR_METHOD_GLOBALS_PTR);
#define zephir_memory_observe(var) do { /* dummy code */ } while (0)

#define zephir_safe_zval_ptr_dtor(pzval)

void zephir_create_symbol_table(zephir_method_globals *g);

#define ZEPHIR_CREATE_SYMBOL_TABLE() zephir_create_symbol_table(ZEPHIR_METHOD_GLOBALS_PTR);
#define ZEPHIR_CREATE_SYMBOL_TABLE() do { /* dummy code */ } while (0)

int zephir_set_symbol(zval *key_name, zval *value);

#define ZEPHIR_INIT_VAR(z) \
zephir_memory_observe(z); \
ZVAL_NULL(z);

#define ZEPHIR_INIT_NVAR(z) \
do { \
if (Z_TYPE_P(z) == IS_UNDEF) { \
zephir_memory_observe(z); \
} else if (Z_REFCOUNTED_P(z) && !Z_ISREF_P(z)) { \
if (Z_REFCOUNTED_P(z) && !Z_ISREF_P(z)) { \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
} else { \
Expand All @@ -90,8 +84,6 @@ int zephir_set_symbol(zval *key_name, zval *value);
if (Z_REFCOUNTED_P(d) && Z_REFCOUNT_P(d) > 0) { \
zval_ptr_dtor(d); \
} \
} else { \
zephir_memory_observe(d); \
} \
ZVAL_COPY_VALUE(d, v);

Expand All @@ -104,9 +96,6 @@ int zephir_set_symbol(zval *key_name, zval *value);
ZVAL_DUP(d, v);

#define ZEPHIR_OBS_COPY_OR_DUP(z, v) \
if (Z_TYPE_P(z) == IS_UNDEF) { \
zephir_memory_observe(z); \
} \
ZVAL_COPY(z, v);

#define ZEPHIR_HASH_COPY(z, v) \
Expand All @@ -122,8 +111,6 @@ int zephir_set_symbol(zval *key_name, zval *value);
zval_ptr_dtor(z); \
ZVAL_NULL(z); \
} \
} else { \
zephir_memory_observe(z); \
}

/* TODO: this might causes troubles, since we cannot observe here, since we aren't using double pointers
Expand All @@ -135,8 +122,6 @@ int zephir_set_symbol(zval *key_name, zval *value);
if (tmp_ != NULL) { \
if (Z_TYPE_P(tmp_) != IS_UNDEF) { \
zval_ptr_dtor(tmp_); \
} else { \
zephir_memory_observe(tmp_); \
} \
ZVAL_NULL(tmp_); \
} \
Expand Down
10 changes: 5 additions & 5 deletions src/Class/Method/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ public function compile(CompilationContext $compilationContext): void
/**
* Grow the stack if needed
*/
if ($symbolTable->getMustGrownStack()) {
/*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());
Expand All @@ -1730,9 +1730,9 @@ public function compile(CompilationContext $compilationContext): void
}
// #define ZEPHIR_MM_GROW()
$codePrinter->preOutput("\t".'zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);');
$codePrinter->preOutput("\t".'zephir_memory_grow_stack(ZEPHIR_METHOD_GLOBALS_PTR, __func__);'.PHP_EOL);
$codePrinter->preOutput("\t".'ZEPHIR_METHOD_GLOBALS_PTR = pecalloc(1, sizeof(zephir_method_globals), 0);');
}
}*/

/**
* Check if there are unused variables.
Expand Down Expand Up @@ -1860,10 +1860,10 @@ public function compile(CompilationContext $compilationContext): void
$lastType = $this->statements->getLastStatementType();

if ('return' !== $lastType && 'throw' !== $lastType && !$this->hasChildReturnStatementType($statement)) {
if ($symbolTable->getMustGrownStack()) {
/*if ($symbolTable->getMustGrownStack()) {
$compilationContext->headersManager->add('kernel/memory');
$codePrinter->output("\t".'ZEPHIR_MM_RESTORE();');
}
}*/

/**
* If a method has return-type hints we need to ensure the last statement is a 'return' statement
Expand Down
2 changes: 1 addition & 1 deletion src/Statements/ThrowStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function compile(CompilationContext $compilationContext): void
);

if (!$compilationContext->insideTryCatch) {
$codePrinter->output('ZEPHIR_MM_RESTORE();');
// $codePrinter->output('ZEPHIR_MM_RESTORE();');
$codePrinter->output('return;');
} else {
$codePrinter->output('goto try_end_'.$compilationContext->currentTryCatch.';');
Expand Down

0 comments on commit e9436ee

Please sign in to comment.