Skip to content

Commit

Permalink
The root memory frame survives across requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jul 25, 2013
1 parent 4beb87a commit e18a27c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
1 change: 0 additions & 1 deletion ext/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
38 changes: 5 additions & 33 deletions ext/kernel/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -195,7 +181,6 @@ int PHALCON_FASTCALL phalcon_memory_restore_stack(TSRMLS_D) {
Z_DELREF_PP(active_memory->addresses[i]);
}
}

}

prev = active_memory->prev;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions ext/kernel/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ext/mvc/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/**
Expand Down
30 changes: 28 additions & 2 deletions ext/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
};
Expand Down

0 comments on commit e18a27c

Please sign in to comment.