This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.h
249 lines (215 loc) · 6.32 KB
/
memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
+------------------------------------------------------------------------+
| Zephir Language |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Zephir Team (http://www.zephir-lang.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
| Vladimir Kolesnikov <[email protected]> |
+------------------------------------------------------------------------+
*/
#ifndef ZEPHIR_KERNEL_MEMORY_H
#define ZEPHIR_KERNEL_MEMORY_H
/* Variable Tracking */
void zephir_init_nvar(zval **var TSRMLS_DC);
void zephir_cpy_wrt(zval **dest, zval *var TSRMLS_DC);
void zephir_cpy_wrt_ctor(zval **dest, zval *var TSRMLS_DC);
void zephir_value_dtor(zval *zvalue ZEND_FILE_LINE_DC);
/* Memory Frames */
#ifndef ZEPHIR_RELEASE
void ZEPHIR_FASTCALL zephir_memory_grow_stack(const char *func TSRMLS_DC);
int ZEPHIR_FASTCALL zephir_memory_restore_stack(const char *func TSRMLS_DC);
#define ZEPHIR_MM_GROW() zephir_memory_grow_stack(NULL TSRMLS_CC)
#define ZEPHIR_MM_RESTORE() zephir_memory_restore_stack(NULL TSRMLS_CC)
#else
void ZEPHIR_FASTCALL zephir_memory_grow_stack(TSRMLS_D);
int ZEPHIR_FASTCALL zephir_memory_restore_stack(TSRMLS_D);
#define ZEPHIR_MM_GROW() zephir_memory_grow_stack(TSRMLS_C)
#define ZEPHIR_MM_RESTORE() zephir_memory_restore_stack(TSRMLS_C)
#endif
void ZEPHIR_FASTCALL zephir_memory_observe(zval **var TSRMLS_DC);
void ZEPHIR_FASTCALL zephir_memory_remove(zval **var TSRMLS_DC);
void ZEPHIR_FASTCALL zephir_memory_alloc(zval **var TSRMLS_DC);
void ZEPHIR_FASTCALL zephir_memory_alloc_pnull(zval **var TSRMLS_DC);
int ZEPHIR_FASTCALL zephir_clean_restore_stack(TSRMLS_D);
/* Virtual symbol tables */
void zephir_create_symbol_table(TSRMLS_D);
/*void zephir_restore_symbol_table(TSRMLS_D);*/
void zephir_clean_symbol_tables(TSRMLS_D);
/** Export symbols to active symbol table */
int zephir_set_symbol(zval *key_name, zval *value TSRMLS_DC);
int zephir_set_symbol_str(char *key_name, unsigned int key_length, zval *value TSRMLS_DC);
void ZEPHIR_FASTCALL zephir_copy_ctor(zval *destiny, zval *origin);
/* Memory macros */
#define ZEPHIR_ALLOC_ZVAL(z) \
ALLOC_INIT_ZVAL(z)
#define ZEPHIR_SINIT_VAR(z) \
INIT_PZVAL(&z); \
ZVAL_NULL(&z);
#define ZEPHIR_SINIT_NVAR(z) Z_SET_REFCOUNT_P(&z, 1)
#define ZEPHIR_INIT_ZVAL_NREF(z) \
ALLOC_ZVAL(z); \
Z_SET_REFCOUNT_P(z, 0); \
Z_UNSET_ISREF_P(z);
#define ZEPHIR_INIT_VAR(z) \
zephir_memory_alloc(&z TSRMLS_CC)
#define ZEPHIR_INIT_NVAR(z)\
if (z) { \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
ALLOC_ZVAL(z); \
Z_SET_REFCOUNT_P(z, 1); \
Z_UNSET_ISREF_P(z); \
} else {\
zval_dtor(z); \
} \
ZVAL_NULL(z); \
} else { \
zephir_memory_alloc(&z TSRMLS_CC); \
}
/**
* Second allocation, assumes the variable was allocated for the first time in the branch zero
*/
#define ZEPHIR_INIT_BNVAR(z) \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
ALLOC_ZVAL(z); \
Z_SET_REFCOUNT_P(z, 1); \
Z_UNSET_ISREF_P(z); \
ZVAL_NULL(z); \
} else {\
zval_ptr_dtor(&z); \
ZEPHIR_ALLOC_ZVAL(z); \
}
#define ZEPHIR_INIT_NVAR_PNULL(z)\
if (z) { \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
if (Z_REFCOUNT_P(z) >= 1) { \
zval_copy_ctor(z); \
} \
ALLOC_ZVAL(z); \
Z_SET_REFCOUNT_P(z, 1); \
Z_UNSET_ISREF_P(z); \
} \
ZVAL_NULL(z); \
} else { \
zephir_memory_alloc_pnull(&z TSRMLS_CC); \
}
/* only removes the value body of the zval */
#define ZEPHIR_INIT_LNVAR(z)\
if (z) { \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
ALLOC_ZVAL(z); \
Z_SET_REFCOUNT_P(z, 1); \
Z_UNSET_ISREF_P(z); \
ZVAL_NULL(z); \
} else {\
zephir_value_dtor(z ZEND_FILE_LINE_CC); \
ZVAL_NULL(z); \
} \
} else { \
zephir_memory_alloc(&z TSRMLS_CC); \
}
#define ZEPHIR_CPY_WRT(d, v) \
if (d) { \
if (Z_REFCOUNT_P(d) > 0) { \
zval_ptr_dtor(&d); \
} \
} else { \
zephir_memory_observe(&d TSRMLS_CC); \
} \
Z_ADDREF_P(v); \
d = v;
#define ZEPHIR_CPY_WRT_CTOR(d, v) \
if (d) { \
if (Z_REFCOUNT_P(d) > 0) { \
zval_ptr_dtor(&d); \
} \
} else { \
zephir_memory_observe(&d TSRMLS_CC); \
} \
ALLOC_ZVAL(d); \
*d = *v; \
zval_copy_ctor(d); \
Z_SET_REFCOUNT_P(d, 1); \
Z_UNSET_ISREF_P(d);
/* */
#define ZEPHIR_OBS_VAR(z) \
zephir_memory_observe(&z TSRMLS_CC)
#define ZEPHIR_OBS_NVAR(z)\
if (z) { \
if (Z_REFCOUNT_P(z) > 1) { \
Z_DELREF_P(z); \
} else {\
zval_ptr_dtor(&z); \
z = NULL; \
} \
} else { \
zephir_memory_observe(&z TSRMLS_CC); \
}
#define ZEPHIR_OBSERVE_OR_NULLIFY_PPZV(ppzv) \
do { \
zval **tmp_ = (ppzv); \
if (tmp_ != NULL) { \
if (*tmp_) { \
zval_ptr_dtor(tmp_); \
*tmp_ = NULL; \
} \
else { \
zephir_memory_observe((ppzv) TSRMLS_CC); \
} \
} \
} while (0)
#define ZEPHIR_OBSERVE_OR_NULLIFY_VAR(z) \
do { \
if (z) { \
zval_ptr_dtor(&z); \
z = NULL; \
} \
else { \
zephir_memory_observe(&z TSRMLS_CC); \
} \
} while (0)
#define ZEPHIR_SEPARATE_ARRAY(a) \
{ \
if (Z_REFCOUNT_P(a) > 1) { \
zval *new_zv; \
Z_DELREF_P(a); \
ALLOC_ZVAL(new_zv); \
INIT_PZVAL_COPY(new_zv, a); \
a = new_zv; \
zval_copy_ctor(new_zv); \
} \
}
#define ZEPHIR_SEPARATE(z) SEPARATE_ZVAL(&z)
#define ZEPHIR_SEPARATE_PARAM(z) \
do { \
zval *orig_ptr = z;\
zephir_memory_observe(&z TSRMLS_CC);\
ALLOC_ZVAL(z);\
*z = *orig_ptr;\
zval_copy_ctor(z);\
Z_SET_REFCOUNT_P(z, 1);\
Z_UNSET_ISREF_P(z);\
} while (0)
#define ZEPHIR_SEPARATE_PARAM_NMO(z) { \
zval *orig_ptr = z; \
if (Z_REFCOUNT_P(orig_ptr) > 1) { \
ALLOC_ZVAL(z); \
*z = *orig_ptr; \
zval_copy_ctor(z); \
Z_SET_REFCOUNT_P(z, 1); \
Z_UNSET_ISREF_P(z); \
} \
}
#endif