25
25
#include "php_phalcon.h"
26
26
#include "kernel/memory.h"
27
27
28
- int phalcon_memory_stack = 0 ;
28
+ /**
29
+ * Initializes memory stack for the active function
30
+ */
31
+ int phalcon_memory_grow_stack (TSRMLS_D ){
29
32
30
- phalcon_memory_entry * start_memory = NULL ;
31
- phalcon_memory_entry * active_memory = NULL ;
32
-
33
- int phalcon_memory_grow_stack (){
34
-
35
- int i ;
33
+ register int i ;
36
34
phalcon_memory_entry * entry ;
37
35
38
- if (!start_memory ){
39
- start_memory = (phalcon_memory_entry * ) emalloc (sizeof (phalcon_memory_entry ));
40
- start_memory -> pointer = -1 ;
41
- start_memory -> prev = NULL ;
42
- start_memory -> next = NULL ;
43
- active_memory = start_memory ;
36
+ if (!PHALCON_GLOBAL ( start_memory ) ){
37
+ PHALCON_GLOBAL ( start_memory ) = (phalcon_memory_entry * ) emalloc (sizeof (phalcon_memory_entry ));
38
+ PHALCON_GLOBAL ( start_memory ) -> pointer = -1 ;
39
+ PHALCON_GLOBAL ( start_memory ) -> prev = NULL ;
40
+ PHALCON_GLOBAL ( start_memory ) -> next = NULL ;
41
+ PHALCON_GLOBAL ( active_memory ) = PHALCON_GLOBAL ( start_memory ) ;
44
42
}
45
43
46
44
entry = (phalcon_memory_entry * ) emalloc (sizeof (phalcon_memory_entry ));
47
45
for (i = 0 ;i < PHALCON_MAX_MEMORY_STACK ;i ++ ){
48
46
entry -> addresses [i ] = NULL ;
49
47
}
50
48
entry -> pointer = -1 ;
51
- entry -> prev = active_memory ;
52
- active_memory -> next = entry ;
53
- active_memory = entry ;
49
+ entry -> prev = PHALCON_GLOBAL ( active_memory ) ;
50
+ PHALCON_GLOBAL ( active_memory ) -> next = entry ;
51
+ PHALCON_GLOBAL ( active_memory ) = entry ;
54
52
55
53
return SUCCESS ;
56
54
}
57
55
58
- int phalcon_memory_restore_stack (){
56
+ /**
57
+ * Finishes memory stack by releasing allocated memory
58
+ */
59
+ int phalcon_memory_restore_stack (TSRMLS_D ){
59
60
60
61
register int i ;
61
62
phalcon_memory_entry * prev ;
63
+ phalcon_memory_entry * active_memory = PHALCON_GLOBAL (active_memory );
62
64
63
65
if (active_memory ){
64
66
@@ -82,18 +84,18 @@ int phalcon_memory_restore_stack(){
82
84
}
83
85
84
86
prev = active_memory -> prev ;
85
- efree (active_memory );
86
- active_memory = prev ;
87
+ efree (PHALCON_GLOBAL ( active_memory ) );
88
+ PHALCON_GLOBAL ( active_memory ) = prev ;
87
89
if (prev != NULL ){
88
- active_memory -> next = NULL ;
89
- if (active_memory == start_memory ){
90
- efree (active_memory );
91
- start_memory = NULL ;
92
- active_memory = NULL ;
90
+ PHALCON_GLOBAL ( active_memory ) -> next = NULL ;
91
+ if (PHALCON_GLOBAL ( active_memory ) == PHALCON_GLOBAL ( start_memory ) ){
92
+ efree (PHALCON_GLOBAL ( active_memory ) );
93
+ PHALCON_GLOBAL ( start_memory ) = NULL ;
94
+ PHALCON_GLOBAL ( active_memory ) = NULL ;
93
95
}
94
96
} else {
95
- start_memory = NULL ;
96
- active_memory = NULL ;
97
+ PHALCON_GLOBAL ( start_memory ) = NULL ;
98
+ PHALCON_GLOBAL ( active_memory ) = NULL ;
97
99
}
98
100
99
101
} else {
@@ -103,13 +105,20 @@ int phalcon_memory_restore_stack(){
103
105
return SUCCESS ;
104
106
}
105
107
106
- int phalcon_memory_observe (zval * * var ){
108
+ /**
109
+ * Observes a memory pointer to release its memory at the end of the request
110
+ */
111
+ int phalcon_memory_observe (zval * * var TSRMLS_DC ){
112
+ phalcon_memory_entry * active_memory = PHALCON_GLOBAL (active_memory );
107
113
active_memory -> pointer ++ ;
108
114
active_memory -> addresses [active_memory -> pointer ] = var ;
109
115
return SUCCESS ;
110
116
}
111
117
112
- int phalcon_memory_remove (zval * * var ){
118
+ /**
119
+ * Removes a memory pointer from the active memory pool
120
+ */
121
+ int phalcon_memory_remove (zval * * var TSRMLS_DC ){
113
122
zval_ptr_dtor (var );
114
123
* var = NULL ;
115
124
return SUCCESS ;
0 commit comments