From e18a27cad67556b54fc1137a4658528a03830f5c Mon Sep 17 00:00:00 2001 From: Vladimir Kolesnikov Date: Wed, 24 Jul 2013 23:55:13 +0300 Subject: [PATCH] The root memory frame survives across requests --- ext/kernel/main.c | 1 - ext/kernel/memory.c | 38 +++++--------------------------------- ext/kernel/memory.h | 2 -- ext/mvc/url.c | 1 + ext/phalcon.c | 30 ++++++++++++++++++++++++++++-- 5 files changed, 34 insertions(+), 38 deletions(-) diff --git a/ext/kernel/main.c b/ext/kernel/main.c index 6937b386728..08cbecd517a 100644 --- a/ext/kernel/main.c +++ b/ext/kernel/main.c @@ -40,7 +40,6 @@ void php_phalcon_init_globals(zend_phalcon_globals *phalcon_globals TSRMLS_DC) { /* Memory options */ - phalcon_globals->start_memory = NULL; phalcon_globals->active_memory = NULL; /* Virtual Symbol Tables */ diff --git a/ext/kernel/memory.c b/ext/kernel/memory.c index 0fb35bb0dd2..de0d4417c8f 100644 --- a/ext/kernel/memory.c +++ b/ext/kernel/memory.c @@ -108,22 +108,8 @@ void PHALCON_FASTCALL phalcon_memory_grow_stack(TSRMLS_D) { zend_phalcon_globals *phalcon_globals_ptr = PHALCON_VGLOBAL; - if (!phalcon_globals_ptr->start_memory) { - phalcon_memory_entry *start = (phalcon_memory_entry *) ecalloc(1, sizeof(phalcon_memory_entry)); - /* ecalloc() will take care of these members - start->pointer = 0; - start->capacity = 0; - start->addresses = NULL; - start->hash_pointer = 0; - start->hash_capacity = 0; - start->hash_addresses = NULL; - start->prev = NULL; - start->next = NULL; - */ - phalcon_globals_ptr->start_memory = start; - phalcon_globals_ptr->active_memory = start; - } - else if (!phalcon_globals_ptr->active_memory) { + assert(phalcon_globals_ptr->start_memory != NULL); + if (!phalcon_globals_ptr->active_memory) { phalcon_globals_ptr->active_memory = phalcon_globals_ptr->start_memory; } else { @@ -195,7 +181,6 @@ int PHALCON_FASTCALL phalcon_memory_restore_stack(TSRMLS_D) { Z_DELREF_PP(active_memory->addresses[i]); } } - } prev = active_memory->prev; @@ -237,7 +222,7 @@ int PHALCON_FASTCALL phalcon_clean_shutdown_stack(TSRMLS_D) static void phalcon_reallocate_memory(phalcon_memory_entry *frame) { - void *buf = erealloc(frame->addresses, sizeof(zval **) * (frame->capacity + 16)); + void *buf = perealloc(frame->addresses, sizeof(zval **) * (frame->capacity + 16), unlikely(frame->prev == NULL)); if (likely(buf != NULL)) { frame->capacity += 16; frame->addresses = buf; @@ -249,7 +234,7 @@ static void phalcon_reallocate_memory(phalcon_memory_entry *frame) static void phalcon_reallocate_hmemory(phalcon_memory_entry *frame) { - void *buf = erealloc(frame->hash_addresses, sizeof(zval **) * (frame->hash_capacity + 4)); + void *buf = perealloc(frame->hash_addresses, sizeof(zval **) * (frame->hash_capacity + 4), unlikely(frame->prev == NULL)); if (likely(buf != NULL)) { frame->hash_capacity += 4; frame->hash_addresses = buf; @@ -278,7 +263,7 @@ void PHALCON_FASTCALL phalcon_memory_observe(zval **var TSRMLS_DC) { } /** - * Observe a variable and allocates memory for it + * Observes a variable and allocates memory for it */ void PHALCON_FASTCALL phalcon_memory_alloc(zval **var TSRMLS_DC) { @@ -324,19 +309,6 @@ int PHALCON_FASTCALL phalcon_clean_restore_stack(TSRMLS_D) { phalcon_memory_restore_stack(TSRMLS_C); } - if (likely(phalcon_globals_ptr->start_memory != NULL)) { - if (phalcon_globals_ptr->start_memory->hash_addresses != NULL) { - efree(phalcon_globals_ptr->start_memory->hash_addresses); - } - - if (likely(phalcon_globals_ptr->start_memory->addresses != NULL)) { - efree(phalcon_globals_ptr->start_memory->addresses); - } - - efree(phalcon_globals_ptr->start_memory); - phalcon_globals_ptr->start_memory = NULL; - } - return SUCCESS; } diff --git a/ext/kernel/memory.h b/ext/kernel/memory.h index d8b2a2a5ef7..c42d0f9c3de 100644 --- a/ext/kernel/memory.h +++ b/ext/kernel/memory.h @@ -17,8 +17,6 @@ +------------------------------------------------------------------------+ */ -#define PHALCON_MEMORY_FRAME_CHUNK 16 - /* Variable Tracking */ extern void phalcon_init_nvar(zval **var TSRMLS_DC); extern void phalcon_cpy_wrt(zval **dest, zval *var TSRMLS_DC); diff --git a/ext/mvc/url.c b/ext/mvc/url.c index db62e34a4d4..b232f11dea5 100644 --- a/ext/mvc/url.c +++ b/ext/mvc/url.c @@ -38,6 +38,7 @@ #include "kernel/fcall.h" #include "kernel/framework/url.h" #include "kernel/concat.h" +#include "kernel/string.h" #include "kernel/framework/router.h" /** diff --git a/ext/phalcon.c b/ext/phalcon.c index f04e8f83088..9093eb7c6dd 100644 --- a/ext/phalcon.c +++ b/ext/phalcon.c @@ -682,7 +682,6 @@ static PHP_MINIT_FUNCTION(phalcon){ static PHP_MSHUTDOWN_FUNCTION(phalcon){ - assert(PHALCON_GLOBAL(start_memory) == NULL); assert(PHALCON_GLOBAL(function_cache) == NULL); assert(PHALCON_GLOBAL(orm).parser_cache == NULL); assert(PHALCON_GLOBAL(orm).ast_cache == NULL); @@ -724,7 +723,34 @@ static PHP_MINFO_FUNCTION(phalcon) static PHP_GINIT_FUNCTION(phalcon) { + phalcon_memory_entry *start; + int i; + php_phalcon_init_globals(phalcon_globals TSRMLS_CC); + + start = (phalcon_memory_entry *) pecalloc(1, sizeof(phalcon_memory_entry), 1); +/* pecalloc() will take care of these members + start->pointer = 0; + start->hash_pointer = 0; + start->prev = NULL; + start->next = NULL; +*/ + start->addresses = pecalloc(24, sizeof(zval*), 1); + start->capacity = 24; + start->hash_addresses = pecalloc(8, sizeof(zval*), 1); + start->hash_capacity = 8; + + phalcon_globals->start_memory = start; +} + +static PHP_GSHUTDOWN_FUNCTION(phalcon) +{ + assert(phalcon_globals->start_memory != NULL); + + pefree(phalcon_globals->start_memory->hash_addresses, 1); + pefree(phalcon_globals->start_memory->addresses, 1); + pefree(phalcon_globals->start_memory, 1); + phalcon_globals->start_memory = NULL; } static @@ -759,7 +785,7 @@ zend_module_entry phalcon_module_entry = { PHP_PHALCON_VERSION, ZEND_MODULE_GLOBALS(phalcon), PHP_GINIT(phalcon), - NULL, + PHP_GSHUTDOWN(phalcon), NULL, STANDARD_MODULE_PROPERTIES_EX };