Skip to content

Commit deb2052

Browse files
author
phalcon
committed
Adding ZTS support
1 parent e8011f5 commit deb2052

25 files changed

+205
-124
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
- Added Phalcon_Db_Profiler to profile database operations and determine posible bottlenecks
1313
- Added methods to query table metadata: Phalcon_Db::describeTable, Phalcon_Db::describeIndexes and Phalcon_Db::describeReferences
1414
- Fixed segmentation fault in Phalcon_Db::tableExists
15+
- Fixed memory leak in Phalcon_Paginator
16+
- Adding Zend Thread Safety (ZTS) to Phalcon Memory Manager
1517

1618
0.3.3
1719
- Added Phalcon_Tag::setDefault as an alias for Phalcon_Tag::displayTo

dev/acl.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ PHP_METHOD(Phalcon_Acl, __construct){
7777
if (!v1) {
7878
PHALCON_INIT_VAR(a0);
7979
array_init(a0);
80-
PHALCON_CPY_WRT(v1, a0);
80+
PHALCON_CPY_WRT(v1, a0);
8181
}
8282

8383
PHALCON_ALLOC_ZVAL_MM(r0);
@@ -138,7 +138,7 @@ PHP_METHOD(Phalcon_Acl, __call){
138138
if (!v1) {
139139
PHALCON_INIT_VAR(a0);
140140
array_init(a0);
141-
PHALCON_CPY_WRT(v1, a0);
141+
PHALCON_CPY_WRT(v1, a0);
142142
}
143143

144144
PHALCON_ALLOC_ZVAL_MM(r0);

dev/acl/adapter/memory.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResource){
504504
if (!v1) {
505505
PHALCON_INIT_VAR(a0);
506506
array_init(a0);
507-
PHALCON_CPY_WRT(v1, a0);
507+
PHALCON_CPY_WRT(v1, a0);
508508
}
509509

510510
if (Z_TYPE_P(v0) == IS_OBJECT) {

dev/cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ PHP_METHOD(Phalcon_Cache, factory){
7676
if (!v2) {
7777
PHALCON_INIT_VAR(a0);
7878
array_init(a0);
79-
PHALCON_CPY_WRT(v2, a0);
79+
PHALCON_CPY_WRT(v2, a0);
8080
}
8181
if (!v3) {
8282
PHALCON_INIT_VAR(a1);
8383
array_init(a1);
84-
PHALCON_CPY_WRT(v3, a1);
84+
PHALCON_CPY_WRT(v3, a1);
8585
}
8686

8787
PHALCON_ALLOC_ZVAL_MM(r0);

dev/config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PHP_METHOD(Phalcon_Config, __construct){
7979
if (!v0) {
8080
PHALCON_INIT_VAR(a0);
8181
array_init(a0);
82-
PHALCON_CPY_WRT(v0, a0);
82+
PHALCON_CPY_WRT(v0, a0);
8383
}
8484

8585
if (Z_TYPE_P(v0) != IS_ARRAY) {

dev/db/adapter/mysql.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ PHP_METHOD(Phalcon_Db_Adapter_Mysql, connect){
389389

390390
/**
391391
* Sends SQL statements to the MySQL database server returning success state.
392+
* When the SQL sent have returned any row, the result is a PHP resource.
392393
*
393394
*
394395
*
@@ -523,7 +524,8 @@ PHP_METHOD(Phalcon_Db_Adapter_Mysql, close){
523524
}
524525

525526
/**
526-
* Fetches a row from active resultset. This method is affected by active fetch flag set using Phalcon_Db_Mysql::setFetchMode
527+
* Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
528+
* This method is affected by the active fetch flag set using Phalcon_Db_Mysql::setFetchMode
527529
*
528530
*
529531
*

dev/exception.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ PHP_METHOD(Phalcon_Exception, __construct){
7878
if (!v3) {
7979
PHALCON_INIT_VAR(a0);
8080
array_init(a0);
81-
PHALCON_CPY_WRT(v3, a0);
81+
PHALCON_CPY_WRT(v3, a0);
8282
}
8383

8484
PHALCON_ALLOC_ZVAL_MM(r0);

dev/kernel/main.c

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
#include "php_main.h"
2727
#include "kernel/main.h"
2828

29+
/**
30+
* Initialize globals on each request or each thread started
31+
*/
32+
void php_phalcon_init_globals(zend_phalcon_globals *phalcon_globals TSRMLS_DC){
33+
phalcon_globals->phalcon_memory_stack = 0;
34+
phalcon_globals->start_memory = NULL;
35+
phalcon_globals->active_memory = NULL;
36+
}
37+
2938
/**
3039
* Initilializes super global variables if doesn't
3140
*/
@@ -427,3 +436,11 @@ int phalcon_filter_alphanum(zval *result, zval *param){
427436

428437
return SUCCESS;
429438
}
439+
440+
/**
441+
* Generates error when inherited class isn't found
442+
*/
443+
int phalcon_inherit_not_found(char *class_name, char *inherit_name){
444+
fprintf(stderr, "Phalcon Error: Extended class '%s' not found when registering class '%s'", class_name, inherit_name);
445+
return SUCCESS;
446+
}

dev/kernel/main.h

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extern PHPAPI zend_class_entry *spl_ce_Countable;
4141
extern PHPAPI zend_class_entry *spl_ce_SeekableIterator;
4242
#endif
4343

44+
/** Startup functions */
45+
extern void php_phalcon_init_globals(zend_phalcon_globals *phalcon_globals TSRMLS_DC);
46+
4447
/** Globals functions */
4548
extern int phalcon_init_global(char *global TSRMLS_DC);
4649
extern int phalcon_get_global(zval *arr, char *global, int global_type TSRMLS_DC);
@@ -164,3 +167,5 @@ extern int phalcon_filter_alphanum(zval *result, zval *param);
164167
PHALCON_MM_RESTORE();\
165168
return;
166169

170+
/* Utils functions */
171+
int phalcon_inherit_not_found(char *class_name, char *inherit_name);

dev/kernel/memory.c

+37-28
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,42 @@
2525
#include "php_phalcon.h"
2626
#include "kernel/memory.h"
2727

28-
int phalcon_memory_stack = 0;
28+
/**
29+
* Initializes memory stack for the active function
30+
*/
31+
int phalcon_memory_grow_stack(TSRMLS_D){
2932

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;
3634
phalcon_memory_entry *entry;
3735

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);
4442
}
4543

4644
entry = (phalcon_memory_entry *) emalloc(sizeof(phalcon_memory_entry));
4745
for(i=0;i<PHALCON_MAX_MEMORY_STACK;i++){
4846
entry->addresses[i] = NULL;
4947
}
5048
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;
5452

5553
return SUCCESS;
5654
}
5755

58-
int phalcon_memory_restore_stack(){
56+
/**
57+
* Finishes memory stack by releasing allocated memory
58+
*/
59+
int phalcon_memory_restore_stack(TSRMLS_D){
5960

6061
register int i;
6162
phalcon_memory_entry *prev;
63+
phalcon_memory_entry *active_memory = PHALCON_GLOBAL(active_memory);
6264

6365
if(active_memory){
6466

@@ -82,18 +84,18 @@ int phalcon_memory_restore_stack(){
8284
}
8385

8486
prev = active_memory->prev;
85-
efree(active_memory);
86-
active_memory = prev;
87+
efree(PHALCON_GLOBAL(active_memory));
88+
PHALCON_GLOBAL(active_memory) = prev;
8789
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;
9395
}
9496
} else {
95-
start_memory = NULL;
96-
active_memory = NULL;
97+
PHALCON_GLOBAL(start_memory) = NULL;
98+
PHALCON_GLOBAL(active_memory) = NULL;
9799
}
98100

99101
} else {
@@ -103,13 +105,20 @@ int phalcon_memory_restore_stack(){
103105
return SUCCESS;
104106
}
105107

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);
107113
active_memory->pointer++;
108114
active_memory->addresses[active_memory->pointer] = var;
109115
return SUCCESS;
110116
}
111117

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){
113122
zval_ptr_dtor(var);
114123
*var = NULL;
115124
return SUCCESS;

dev/kernel/memory.h

+12-21
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,14 @@
1717
+------------------------------------------------------------------------+
1818
*/
1919

20-
#define PHALCON_MAX_MEMORY_STACK 96
20+
extern int phalcon_memory_grow_stack(TSRMLS_D);
21+
extern int phalcon_memory_restore_stack(TSRMLS_D);
2122

22-
typedef struct _phalcon_memory_entry {
23-
int pointer;
24-
zval **addresses[PHALCON_MAX_MEMORY_STACK];
25-
struct _phalcon_memory_entry *prev;
26-
struct _phalcon_memory_entry *next;
27-
} phalcon_memory_entry;
23+
extern int phalcon_memory_observe(zval **var TSRMLS_DC);
24+
extern int phalcon_memory_remove(zval **var TSRMLS_DC);
2825

29-
extern int phalcon_memory_grow_stack();
30-
extern int phalcon_memory_restore_stack();
31-
32-
extern int phalcon_memory_observe(zval **var);
33-
extern int phalcon_memory_remove(zval **var);
34-
35-
#define PHALCON_MM_GROW() phalcon_memory_grow_stack()
36-
#define PHALCON_MM_RESTORE() phalcon_memory_restore_stack()
26+
#define PHALCON_MM_GROW() phalcon_memory_grow_stack(TSRMLS_C)
27+
#define PHALCON_MM_RESTORE() phalcon_memory_restore_stack(TSRMLS_C)
3728

3829
/** Memory macros */
3930
#define PHALCON_ALLOC_ZVAL(z) \
@@ -51,7 +42,7 @@ extern int phalcon_memory_remove(zval **var);
5142
PHALCON_ALLOC_ZVAL(z);\
5243
}\
5344
} else {\
54-
phalcon_memory_observe(&z);\
45+
phalcon_memory_observe(&z TSRMLS_CC);\
5546
PHALCON_ALLOC_ZVAL(z);\
5647
}
5748

@@ -67,21 +58,21 @@ extern int phalcon_memory_remove(zval **var);
6758
PHALCON_ALLOC_ZVAL(z);\
6859
}\
6960
} else {\
70-
phalcon_memory_observe(&z);\
61+
phalcon_memory_observe(&z TSRMLS_CC);\
7162
PHALCON_ALLOC_ZVAL(z);\
7263
}
7364

7465
#define PHALCON_ALLOC_ZVAL_MM(z) \
7566
PHALCON_ALLOC_ZVAL(z); \
76-
phalcon_memory_observe(&z);
67+
phalcon_memory_observe(&z TSRMLS_CC);
7768

7869
#define PHALCON_CPY_WRT(d, v) \
7970
if (d) { \
8071
if (Z_REFCOUNT_P(d) > 0) { \
8172
zval_ptr_dtor(&d); \
8273
} \
8374
} else { \
84-
phalcon_memory_observe(&d); \
75+
phalcon_memory_observe(&d TSRMLS_CC); \
8576
} \
8677
Z_ADDREF_P(v); \
8778
d = v;
@@ -104,7 +95,7 @@ extern int phalcon_memory_remove(zval **var);
10495
if (Z_REFCOUNT_P(orig_ptr) > 1) {\
10596
Z_DELREF_P(orig_ptr);\
10697
ALLOC_ZVAL(z);\
107-
phalcon_memory_observe(&z);\
98+
phalcon_memory_observe(&z TSRMLS_CC);\
10899
*z = *orig_ptr;\
109100
zval_copy_ctor(z);\
110101
Z_SET_REFCOUNT_P(z, 1);\
@@ -130,7 +121,7 @@ extern int phalcon_memory_remove(zval **var);
130121
zval *orig_ptr = z;\
131122
if (Z_REFCOUNT_P(orig_ptr) > 1) {\
132123
ALLOC_ZVAL(z);\
133-
phalcon_memory_observe(&z);\
124+
phalcon_memory_observe(&z TSRMLS_CC);\
134125
*z = *orig_ptr;\
135126
zval_copy_ctor(z);\
136127
Z_SET_REFCOUNT_P(z, 1);\

dev/logger.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PHP_METHOD(Phalcon_Logger, __construct){
7979
if (!v2) {
8080
PHALCON_INIT_VAR(a0);
8181
array_init(a0);
82-
PHALCON_CPY_WRT(v2, a0);
82+
PHALCON_CPY_WRT(v2, a0);
8383
}
8484

8585
PHALCON_ALLOC_ZVAL_MM(r0);
@@ -391,7 +391,7 @@ PHP_METHOD(Phalcon_Logger, __call){
391391
if (!v1) {
392392
PHALCON_INIT_VAR(a0);
393393
array_init(a0);
394-
PHALCON_CPY_WRT(v1, a0);
394+
PHALCON_CPY_WRT(v1, a0);
395395
}
396396

397397
PHALCON_ALLOC_ZVAL_MM(r0);

dev/logger/adapter/file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ PHP_METHOD(Phalcon_Logger_Adapter_File, __construct){
7676
if (!v1) {
7777
PHALCON_INIT_VAR(a1);
7878
array_init(a1);
79-
PHALCON_CPY_WRT(v1, a1);
79+
PHALCON_CPY_WRT(v1, a1);
8080
}
8181

8282
eval_int = phalcon_array_isset_string(v1, "mode", strlen("mode")+1);

dev/model/base.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3612,7 +3612,7 @@ PHP_METHOD(Phalcon_Model_Base, __call){
36123612
if (!v1) {
36133613
PHALCON_INIT_VAR(a0);
36143614
array_init(a0);
3615-
PHALCON_CPY_WRT(v1, a0);
3615+
PHALCON_CPY_WRT(v1, a0);
36163616
}
36173617

36183618
PHALCON_ALLOC_ZVAL_MM(r0);

dev/model/metadata.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
/**
4242
* Phalcon_Model_MetaData
4343
*
44-
* Because Phalcon_Model requires meta-data as field names, data types, primary keys, etc.
44+
* Because Phalcon_Model requires meta-data like field names, data types, primary keys, etc.
4545
* Phalcon_Model_MetaData recopiles them and store for further querying by Phalcon_Model_Base.
46-
* This component can also be used to store metadata adapters temporarily or permanently.
46+
* This component can also use adapters to store temporarily or permanently the meta-data.
4747
*
4848
* A standard Phalcon_Model_MetaData can be used to query model attributes:
4949
*
@@ -397,7 +397,7 @@ PHP_METHOD(Phalcon_Model_MetaData, _initializeMetaData){
397397
}
398398

399399
/**
400-
* Returns table attributes name (fields)
400+
* Returns table attributes names (fields)
401401
*
402402
* @paramPhalcon_Model_Base $model
403403
* @return array
@@ -495,7 +495,7 @@ PHP_METHOD(Phalcon_Model_MetaData, getPrimaryKeyAttributes){
495495
}
496496

497497
/**
498-
* Returns table attributes which are non part of primary key
498+
* Returns table attributes which are not part of primary key
499499
*
500500
* @paramPhalcon_Model_Base $model
501501
* @return array

0 commit comments

Comments
 (0)