1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Dmitry Stogov <dmitry@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #ifndef ZEND_HASH_H
22 #define ZEND_HASH_H
23
24 #include "zend.h"
25
26 #define HASH_KEY_IS_STRING 1
27 #define HASH_KEY_IS_LONG 2
28 #define HASH_KEY_NON_EXISTENT 3
29
30 #define HASH_UPDATE (1<<0)
31 #define HASH_ADD (1<<1)
32 #define HASH_UPDATE_INDIRECT (1<<2)
33 #define HASH_ADD_NEW (1<<3)
34 #define HASH_ADD_NEXT (1<<4)
35
36 #define HASH_FLAG_CONSISTENCY ((1<<0) | (1<<1))
37 #define HASH_FLAG_PACKED (1<<2)
38 #define HASH_FLAG_INITIALIZED (1<<3)
39 #define HASH_FLAG_STATIC_KEYS (1<<4) /* long and interned strings */
40 #define HASH_FLAG_HAS_EMPTY_IND (1<<5)
41 #define HASH_FLAG_ALLOW_COW_VIOLATION (1<<6)
42
43 /* Only the low byte are real flags */
44 #define HASH_FLAG_MASK 0xff
45
46 #define HT_FLAGS(ht) (ht)->u.flags
47
48 #define HT_IS_PACKED(ht) \
49 ((HT_FLAGS(ht) & HASH_FLAG_PACKED) != 0)
50
51 #define HT_IS_WITHOUT_HOLES(ht) \
52 ((ht)->nNumUsed == (ht)->nNumOfElements)
53
54 #define HT_HAS_STATIC_KEYS_ONLY(ht) \
55 ((HT_FLAGS(ht) & (HASH_FLAG_PACKED|HASH_FLAG_STATIC_KEYS)) != 0)
56
57 #if ZEND_DEBUG
58 # define HT_ALLOW_COW_VIOLATION(ht) HT_FLAGS(ht) |= HASH_FLAG_ALLOW_COW_VIOLATION
59 #else
60 # define HT_ALLOW_COW_VIOLATION(ht)
61 #endif
62
63 #define HT_ITERATORS_COUNT(ht) (ht)->u.v.nIteratorsCount
64 #define HT_ITERATORS_OVERFLOW(ht) (HT_ITERATORS_COUNT(ht) == 0xff)
65 #define HT_HAS_ITERATORS(ht) (HT_ITERATORS_COUNT(ht) != 0)
66
67 #define HT_SET_ITERATORS_COUNT(ht, iters) \
68 do { HT_ITERATORS_COUNT(ht) = (iters); } while (0)
69 #define HT_INC_ITERATORS_COUNT(ht) \
70 HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) + 1)
71 #define HT_DEC_ITERATORS_COUNT(ht) \
72 HT_SET_ITERATORS_COUNT(ht, HT_ITERATORS_COUNT(ht) - 1)
73
74 extern ZEND_API const HashTable zend_empty_array;
75
76 #define ZVAL_EMPTY_ARRAY(z) do { \
77 zval *__z = (z); \
78 Z_ARR_P(__z) = (zend_array*)&zend_empty_array; \
79 Z_TYPE_INFO_P(__z) = IS_ARRAY; \
80 } while (0)
81
82
83 typedef struct _zend_hash_key {
84 zend_ulong h;
85 zend_string *key;
86 } zend_hash_key;
87
88 typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
89
90 BEGIN_EXTERN_C()
91
92 /* startup/shutdown */
93 ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent);
94 ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht);
95 ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht);
96
97 #define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent) \
98 _zend_hash_init((ht), (nSize), (pDestructor), (persistent))
99 #define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection) \
100 _zend_hash_init((ht), (nSize), (pDestructor), (persistent))
101
102 ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, zend_bool packed);
103 ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht);
104 ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht);
105 ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht);
106 ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht);
107 ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, zend_bool packed);
108 ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed);
109
110 /* additions/updates/changes */
111 ZEND_API zval* ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag);
112 ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key,zval *pData);
113 ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData);
114 ZEND_API zval* ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key,zval *pData);
115 ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData);
116
117 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag);
118 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
119 ZEND_API zval* ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData);
120 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData);
121 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData);
122
123 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag);
124 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData);
125 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData);
126 ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData);
127 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData);
128 ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData);
129
130 ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
131 ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key);
132 ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
133
134 #define ZEND_HASH_APPLY_KEEP 0
135 #define ZEND_HASH_APPLY_REMOVE 1<<0
136 #define ZEND_HASH_APPLY_STOP 1<<1
137
138 typedef int (*apply_func_t)(zval *pDest);
139 typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
140 typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
141
142 ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht);
143 ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht);
144 ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func);
145 ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
146 ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
147
148 /* This function should be used with special care (in other words,
149 * it should usually not be used). When used with the ZEND_HASH_APPLY_STOP
150 * return value, it assumes things about the order of the elements in the hash.
151 * Also, it does not provide the same kind of reentrancy protection that
152 * the standard apply functions do.
153 */
154 ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
155
156
157 /* Deletes */
158 ZEND_API int ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key);
159 ZEND_API int ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key);
160 ZEND_API int ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *key, size_t len);
161 ZEND_API int ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
162 ZEND_API int ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h);
163 ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p);
164
165 /* Data retreival */
166 ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key);
167 ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
168 ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h);
169 ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h);
170
171 /* The same as zend_hash_find(), but hash value of the key must be already calculated */
172 ZEND_API zval* ZEND_FASTCALL _zend_hash_find_known_hash(const HashTable *ht, zend_string *key);
173
zend_hash_find_ex(const HashTable * ht,zend_string * key,zend_bool known_hash)174 static zend_always_inline zval *zend_hash_find_ex(const HashTable *ht, zend_string *key, zend_bool known_hash)
175 {
176 if (known_hash) {
177 return _zend_hash_find_known_hash(ht, key);
178 } else {
179 return zend_hash_find(ht, key);
180 }
181 }
182
183 #define ZEND_HASH_INDEX_FIND(_ht, _h, _ret, _not_found) do { \
184 if (EXPECTED(HT_FLAGS(_ht) & HASH_FLAG_PACKED)) { \
185 if (EXPECTED((zend_ulong)(_h) < (zend_ulong)(_ht)->nNumUsed)) { \
186 _ret = &_ht->arData[_h].val; \
187 if (UNEXPECTED(Z_TYPE_P(_ret) == IS_UNDEF)) { \
188 goto _not_found; \
189 } \
190 } else { \
191 goto _not_found; \
192 } \
193 } else { \
194 _ret = _zend_hash_index_find(_ht, _h); \
195 if (UNEXPECTED(_ret == NULL)) { \
196 goto _not_found; \
197 } \
198 } \
199 } while (0)
200
201
202 /* Misc */
203 ZEND_API zend_bool ZEND_FASTCALL zend_hash_exists(const HashTable *ht, zend_string *key);
204 ZEND_API zend_bool ZEND_FASTCALL zend_hash_str_exists(const HashTable *ht, const char *str, size_t len);
205 ZEND_API zend_bool ZEND_FASTCALL zend_hash_index_exists(const HashTable *ht, zend_ulong h);
206
207 /* traversing */
208 ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht);
209
210 #define zend_hash_has_more_elements_ex(ht, pos) \
211 (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
212 ZEND_API int ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
213 ZEND_API int ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
214 ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos);
215 ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
216 ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
217 ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
218 ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
219 ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
220
221 #define zend_hash_has_more_elements(ht) \
222 zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
223 #define zend_hash_move_forward(ht) \
224 zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
225 #define zend_hash_move_backwards(ht) \
226 zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
227 #define zend_hash_get_current_key(ht, str_index, num_index) \
228 zend_hash_get_current_key_ex(ht, str_index, num_index, &(ht)->nInternalPointer)
229 #define zend_hash_get_current_key_zval(ht, key) \
230 zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
231 #define zend_hash_get_current_key_type(ht) \
232 zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
233 #define zend_hash_get_current_data(ht) \
234 zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
235 #define zend_hash_internal_pointer_reset(ht) \
236 zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
237 #define zend_hash_internal_pointer_end(ht) \
238 zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
239
240 /* Copying, merging and sorting */
241 ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
242 ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite);
243 ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
244 ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q);
245 ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
246 ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
247 ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
248 ZEND_API int ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber);
249 ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
250
251 #define zend_hash_sort(ht, compare_func, renumber) \
252 zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
253
254 #define zend_hash_num_elements(ht) \
255 (ht)->nNumOfElements
256
257 #define zend_hash_next_free_element(ht) \
258 (ht)->nNextFreeElement
259
260 ZEND_API int ZEND_FASTCALL zend_hash_rehash(HashTable *ht);
261
262 #if !ZEND_DEBUG && defined(HAVE_BUILTIN_CONSTANT_P)
263 # define zend_new_array(size) \
264 (__builtin_constant_p(size) ? \
265 ((((uint32_t)(size)) <= HT_MIN_SIZE) ? \
266 _zend_new_array_0() \
267 : \
268 _zend_new_array((size)) \
269 ) \
270 : \
271 _zend_new_array((size)) \
272 )
273 #else
274 # define zend_new_array(size) \
275 _zend_new_array(size)
276 #endif
277
278 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void);
279 ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t size);
280 ZEND_API uint32_t zend_array_count(HashTable *ht);
281 ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source);
282 ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht);
283 ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht);
284 ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht);
285 ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, zend_bool always_duplicate);
286
287 ZEND_API int ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
288
289 ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos);
290 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht);
291 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array);
292 ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx);
293 ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start);
294 ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to);
295 ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(HashTable *ht, HashPosition step);
296
zend_hash_iterators_update(HashTable * ht,HashPosition from,HashPosition to)297 static zend_always_inline void zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
298 {
299 if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
300 _zend_hash_iterators_update(ht, from, to);
301 }
302 }
303
304
END_EXTERN_C()305 END_EXTERN_C()
306
307 #define ZEND_INIT_SYMTABLE(ht) \
308 ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
309
310 #define ZEND_INIT_SYMTABLE_EX(ht, n, persistent) \
311 zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
312
313 static zend_always_inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
314 {
315 const char *tmp = key;
316
317 if (EXPECTED(*tmp > '9')) {
318 return 0;
319 } else if (*tmp < '0') {
320 if (*tmp != '-') {
321 return 0;
322 }
323 tmp++;
324 if (*tmp > '9' || *tmp < '0') {
325 return 0;
326 }
327 }
328 return _zend_handle_numeric_str_ex(key, length, idx);
329 }
330
331 #define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
332 _zend_handle_numeric_str(key, length, &idx)
333
334 #define ZEND_HANDLE_NUMERIC(key, idx) \
335 ZEND_HANDLE_NUMERIC_STR(ZSTR_VAL(key), ZSTR_LEN(key), idx)
336
337
zend_hash_find_ind(const HashTable * ht,zend_string * key)338 static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
339 {
340 zval *zv;
341
342 zv = zend_hash_find(ht, key);
343 return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
344 ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
345 }
346
347
zend_hash_find_ex_ind(const HashTable * ht,zend_string * key,zend_bool known_hash)348 static zend_always_inline zval *zend_hash_find_ex_ind(const HashTable *ht, zend_string *key, zend_bool known_hash)
349 {
350 zval *zv;
351
352 zv = zend_hash_find_ex(ht, key, known_hash);
353 return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
354 ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
355 }
356
357
zend_hash_exists_ind(const HashTable * ht,zend_string * key)358 static zend_always_inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
359 {
360 zval *zv;
361
362 zv = zend_hash_find(ht, key);
363 return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
364 Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
365 }
366
367
zend_hash_str_find_ind(const HashTable * ht,const char * str,size_t len)368 static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
369 {
370 zval *zv;
371
372 zv = zend_hash_str_find(ht, str, len);
373 return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ?
374 ((Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF) ? Z_INDIRECT_P(zv) : NULL) : zv;
375 }
376
377
zend_hash_str_exists_ind(const HashTable * ht,const char * str,size_t len)378 static zend_always_inline int zend_hash_str_exists_ind(const HashTable *ht, const char *str, size_t len)
379 {
380 zval *zv;
381
382 zv = zend_hash_str_find(ht, str, len);
383 return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
384 Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
385 }
386
zend_symtable_add_new(HashTable * ht,zend_string * key,zval * pData)387 static zend_always_inline zval *zend_symtable_add_new(HashTable *ht, zend_string *key, zval *pData)
388 {
389 zend_ulong idx;
390
391 if (ZEND_HANDLE_NUMERIC(key, idx)) {
392 return zend_hash_index_add_new(ht, idx, pData);
393 } else {
394 return zend_hash_add_new(ht, key, pData);
395 }
396 }
397
zend_symtable_update(HashTable * ht,zend_string * key,zval * pData)398 static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
399 {
400 zend_ulong idx;
401
402 if (ZEND_HANDLE_NUMERIC(key, idx)) {
403 return zend_hash_index_update(ht, idx, pData);
404 } else {
405 return zend_hash_update(ht, key, pData);
406 }
407 }
408
409
zend_symtable_update_ind(HashTable * ht,zend_string * key,zval * pData)410 static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
411 {
412 zend_ulong idx;
413
414 if (ZEND_HANDLE_NUMERIC(key, idx)) {
415 return zend_hash_index_update(ht, idx, pData);
416 } else {
417 return zend_hash_update_ind(ht, key, pData);
418 }
419 }
420
421
zend_symtable_del(HashTable * ht,zend_string * key)422 static zend_always_inline int zend_symtable_del(HashTable *ht, zend_string *key)
423 {
424 zend_ulong idx;
425
426 if (ZEND_HANDLE_NUMERIC(key, idx)) {
427 return zend_hash_index_del(ht, idx);
428 } else {
429 return zend_hash_del(ht, key);
430 }
431 }
432
433
zend_symtable_del_ind(HashTable * ht,zend_string * key)434 static zend_always_inline int zend_symtable_del_ind(HashTable *ht, zend_string *key)
435 {
436 zend_ulong idx;
437
438 if (ZEND_HANDLE_NUMERIC(key, idx)) {
439 return zend_hash_index_del(ht, idx);
440 } else {
441 return zend_hash_del_ind(ht, key);
442 }
443 }
444
445
zend_symtable_find(const HashTable * ht,zend_string * key)446 static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
447 {
448 zend_ulong idx;
449
450 if (ZEND_HANDLE_NUMERIC(key, idx)) {
451 return zend_hash_index_find(ht, idx);
452 } else {
453 return zend_hash_find(ht, key);
454 }
455 }
456
457
zend_symtable_find_ind(const HashTable * ht,zend_string * key)458 static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
459 {
460 zend_ulong idx;
461
462 if (ZEND_HANDLE_NUMERIC(key, idx)) {
463 return zend_hash_index_find(ht, idx);
464 } else {
465 return zend_hash_find_ind(ht, key);
466 }
467 }
468
469
zend_symtable_exists(HashTable * ht,zend_string * key)470 static zend_always_inline int zend_symtable_exists(HashTable *ht, zend_string *key)
471 {
472 zend_ulong idx;
473
474 if (ZEND_HANDLE_NUMERIC(key, idx)) {
475 return zend_hash_index_exists(ht, idx);
476 } else {
477 return zend_hash_exists(ht, key);
478 }
479 }
480
481
zend_symtable_exists_ind(HashTable * ht,zend_string * key)482 static zend_always_inline int zend_symtable_exists_ind(HashTable *ht, zend_string *key)
483 {
484 zend_ulong idx;
485
486 if (ZEND_HANDLE_NUMERIC(key, idx)) {
487 return zend_hash_index_exists(ht, idx);
488 } else {
489 return zend_hash_exists_ind(ht, key);
490 }
491 }
492
493
zend_symtable_str_update(HashTable * ht,const char * str,size_t len,zval * pData)494 static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
495 {
496 zend_ulong idx;
497
498 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
499 return zend_hash_index_update(ht, idx, pData);
500 } else {
501 return zend_hash_str_update(ht, str, len, pData);
502 }
503 }
504
505
zend_symtable_str_update_ind(HashTable * ht,const char * str,size_t len,zval * pData)506 static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
507 {
508 zend_ulong idx;
509
510 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
511 return zend_hash_index_update(ht, idx, pData);
512 } else {
513 return zend_hash_str_update_ind(ht, str, len, pData);
514 }
515 }
516
517
zend_symtable_str_del(HashTable * ht,const char * str,size_t len)518 static zend_always_inline int zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
519 {
520 zend_ulong idx;
521
522 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
523 return zend_hash_index_del(ht, idx);
524 } else {
525 return zend_hash_str_del(ht, str, len);
526 }
527 }
528
529
zend_symtable_str_del_ind(HashTable * ht,const char * str,size_t len)530 static zend_always_inline int zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
531 {
532 zend_ulong idx;
533
534 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
535 return zend_hash_index_del(ht, idx);
536 } else {
537 return zend_hash_str_del_ind(ht, str, len);
538 }
539 }
540
541
zend_symtable_str_find(HashTable * ht,const char * str,size_t len)542 static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
543 {
544 zend_ulong idx;
545
546 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
547 return zend_hash_index_find(ht, idx);
548 } else {
549 return zend_hash_str_find(ht, str, len);
550 }
551 }
552
553
zend_symtable_str_exists(HashTable * ht,const char * str,size_t len)554 static zend_always_inline int zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
555 {
556 zend_ulong idx;
557
558 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
559 return zend_hash_index_exists(ht, idx);
560 } else {
561 return zend_hash_str_exists(ht, str, len);
562 }
563 }
564
zend_hash_add_ptr(HashTable * ht,zend_string * key,void * pData)565 static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
566 {
567 zval tmp, *zv;
568
569 ZVAL_PTR(&tmp, pData);
570 zv = zend_hash_add(ht, key, &tmp);
571 if (zv) {
572 ZEND_ASSUME(Z_PTR_P(zv));
573 return Z_PTR_P(zv);
574 } else {
575 return NULL;
576 }
577 }
578
zend_hash_add_new_ptr(HashTable * ht,zend_string * key,void * pData)579 static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
580 {
581 zval tmp, *zv;
582
583 ZVAL_PTR(&tmp, pData);
584 zv = zend_hash_add_new(ht, key, &tmp);
585 if (zv) {
586 ZEND_ASSUME(Z_PTR_P(zv));
587 return Z_PTR_P(zv);
588 } else {
589 return NULL;
590 }
591 }
592
zend_hash_str_add_ptr(HashTable * ht,const char * str,size_t len,void * pData)593 static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
594 {
595 zval tmp, *zv;
596
597 ZVAL_PTR(&tmp, pData);
598 zv = zend_hash_str_add(ht, str, len, &tmp);
599 if (zv) {
600 ZEND_ASSUME(Z_PTR_P(zv));
601 return Z_PTR_P(zv);
602 } else {
603 return NULL;
604 }
605 }
606
zend_hash_str_add_new_ptr(HashTable * ht,const char * str,size_t len,void * pData)607 static zend_always_inline void *zend_hash_str_add_new_ptr(HashTable *ht, const char *str, size_t len, void *pData)
608 {
609 zval tmp, *zv;
610
611 ZVAL_PTR(&tmp, pData);
612 zv = zend_hash_str_add_new(ht, str, len, &tmp);
613 if (zv) {
614 ZEND_ASSUME(Z_PTR_P(zv));
615 return Z_PTR_P(zv);
616 } else {
617 return NULL;
618 }
619 }
620
zend_hash_update_ptr(HashTable * ht,zend_string * key,void * pData)621 static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
622 {
623 zval tmp, *zv;
624
625 ZVAL_PTR(&tmp, pData);
626 zv = zend_hash_update(ht, key, &tmp);
627 ZEND_ASSUME(Z_PTR_P(zv));
628 return Z_PTR_P(zv);
629 }
630
zend_hash_str_update_ptr(HashTable * ht,const char * str,size_t len,void * pData)631 static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
632 {
633 zval tmp, *zv;
634
635 ZVAL_PTR(&tmp, pData);
636 zv = zend_hash_str_update(ht, str, len, &tmp);
637 ZEND_ASSUME(Z_PTR_P(zv));
638 return Z_PTR_P(zv);
639 }
640
zend_hash_add_mem(HashTable * ht,zend_string * key,void * pData,size_t size)641 static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
642 {
643 zval tmp, *zv;
644
645 ZVAL_PTR(&tmp, NULL);
646 if ((zv = zend_hash_add(ht, key, &tmp))) {
647 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
648 memcpy(Z_PTR_P(zv), pData, size);
649 return Z_PTR_P(zv);
650 }
651 return NULL;
652 }
653
zend_hash_add_new_mem(HashTable * ht,zend_string * key,void * pData,size_t size)654 static zend_always_inline void *zend_hash_add_new_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
655 {
656 zval tmp, *zv;
657
658 ZVAL_PTR(&tmp, NULL);
659 if ((zv = zend_hash_add_new(ht, key, &tmp))) {
660 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
661 memcpy(Z_PTR_P(zv), pData, size);
662 return Z_PTR_P(zv);
663 }
664 return NULL;
665 }
666
zend_hash_str_add_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)667 static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
668 {
669 zval tmp, *zv;
670
671 ZVAL_PTR(&tmp, NULL);
672 if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
673 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
674 memcpy(Z_PTR_P(zv), pData, size);
675 return Z_PTR_P(zv);
676 }
677 return NULL;
678 }
679
zend_hash_str_add_new_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)680 static zend_always_inline void *zend_hash_str_add_new_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
681 {
682 zval tmp, *zv;
683
684 ZVAL_PTR(&tmp, NULL);
685 if ((zv = zend_hash_str_add_new(ht, str, len, &tmp))) {
686 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
687 memcpy(Z_PTR_P(zv), pData, size);
688 return Z_PTR_P(zv);
689 }
690 return NULL;
691 }
692
zend_hash_update_mem(HashTable * ht,zend_string * key,void * pData,size_t size)693 static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
694 {
695 void *p;
696
697 p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
698 memcpy(p, pData, size);
699 return zend_hash_update_ptr(ht, key, p);
700 }
701
zend_hash_str_update_mem(HashTable * ht,const char * str,size_t len,void * pData,size_t size)702 static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
703 {
704 void *p;
705
706 p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
707 memcpy(p, pData, size);
708 return zend_hash_str_update_ptr(ht, str, len, p);
709 }
710
zend_hash_index_add_ptr(HashTable * ht,zend_ulong h,void * pData)711 static zend_always_inline void *zend_hash_index_add_ptr(HashTable *ht, zend_ulong h, void *pData)
712 {
713 zval tmp, *zv;
714
715 ZVAL_PTR(&tmp, pData);
716 zv = zend_hash_index_add(ht, h, &tmp);
717 return zv ? Z_PTR_P(zv) : NULL;
718 }
719
zend_hash_index_add_new_ptr(HashTable * ht,zend_ulong h,void * pData)720 static zend_always_inline void *zend_hash_index_add_new_ptr(HashTable *ht, zend_ulong h, void *pData)
721 {
722 zval tmp, *zv;
723
724 ZVAL_PTR(&tmp, pData);
725 zv = zend_hash_index_add_new(ht, h, &tmp);
726 return zv ? Z_PTR_P(zv) : NULL;
727 }
728
zend_hash_index_update_ptr(HashTable * ht,zend_ulong h,void * pData)729 static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
730 {
731 zval tmp, *zv;
732
733 ZVAL_PTR(&tmp, pData);
734 zv = zend_hash_index_update(ht, h, &tmp);
735 ZEND_ASSUME(Z_PTR_P(zv));
736 return Z_PTR_P(zv);
737 }
738
zend_hash_index_add_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)739 static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
740 {
741 zval tmp, *zv;
742
743 ZVAL_PTR(&tmp, NULL);
744 if ((zv = zend_hash_index_add(ht, h, &tmp))) {
745 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
746 memcpy(Z_PTR_P(zv), pData, size);
747 return Z_PTR_P(zv);
748 }
749 return NULL;
750 }
751
zend_hash_next_index_insert_ptr(HashTable * ht,void * pData)752 static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
753 {
754 zval tmp, *zv;
755
756 ZVAL_PTR(&tmp, pData);
757 zv = zend_hash_next_index_insert(ht, &tmp);
758 if (zv) {
759 ZEND_ASSUME(Z_PTR_P(zv));
760 return Z_PTR_P(zv);
761 } else {
762 return NULL;
763 }
764 }
765
zend_hash_index_update_mem(HashTable * ht,zend_ulong h,void * pData,size_t size)766 static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
767 {
768 void *p;
769
770 p = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
771 memcpy(p, pData, size);
772 return zend_hash_index_update_ptr(ht, h, p);
773 }
774
zend_hash_next_index_insert_mem(HashTable * ht,void * pData,size_t size)775 static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
776 {
777 zval tmp, *zv;
778
779 ZVAL_PTR(&tmp, NULL);
780 if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
781 Z_PTR_P(zv) = pemalloc(size, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
782 memcpy(Z_PTR_P(zv), pData, size);
783 return Z_PTR_P(zv);
784 }
785 return NULL;
786 }
787
zend_hash_find_ptr(const HashTable * ht,zend_string * key)788 static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
789 {
790 zval *zv;
791
792 zv = zend_hash_find(ht, key);
793 if (zv) {
794 ZEND_ASSUME(Z_PTR_P(zv));
795 return Z_PTR_P(zv);
796 } else {
797 return NULL;
798 }
799 }
800
zend_hash_find_ex_ptr(const HashTable * ht,zend_string * key,zend_bool known_hash)801 static zend_always_inline void *zend_hash_find_ex_ptr(const HashTable *ht, zend_string *key, zend_bool known_hash)
802 {
803 zval *zv;
804
805 zv = zend_hash_find_ex(ht, key, known_hash);
806 if (zv) {
807 ZEND_ASSUME(Z_PTR_P(zv));
808 return Z_PTR_P(zv);
809 } else {
810 return NULL;
811 }
812 }
813
zend_hash_str_find_ptr(const HashTable * ht,const char * str,size_t len)814 static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
815 {
816 zval *zv;
817
818 zv = zend_hash_str_find(ht, str, len);
819 if (zv) {
820 ZEND_ASSUME(Z_PTR_P(zv));
821 return Z_PTR_P(zv);
822 } else {
823 return NULL;
824 }
825 }
826
zend_hash_index_find_ptr(const HashTable * ht,zend_ulong h)827 static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
828 {
829 zval *zv;
830
831 zv = zend_hash_index_find(ht, h);
832 if (zv) {
833 ZEND_ASSUME(Z_PTR_P(zv));
834 return Z_PTR_P(zv);
835 } else {
836 return NULL;
837 }
838 }
839
zend_hash_index_find_deref(HashTable * ht,zend_ulong h)840 static zend_always_inline zval *zend_hash_index_find_deref(HashTable *ht, zend_ulong h)
841 {
842 zval *zv = zend_hash_index_find(ht, h);
843 if (zv) {
844 ZVAL_DEREF(zv);
845 }
846 return zv;
847 }
848
zend_hash_find_deref(HashTable * ht,zend_string * str)849 static zend_always_inline zval *zend_hash_find_deref(HashTable *ht, zend_string *str)
850 {
851 zval *zv = zend_hash_find(ht, str);
852 if (zv) {
853 ZVAL_DEREF(zv);
854 }
855 return zv;
856 }
857
zend_hash_str_find_deref(HashTable * ht,const char * str,size_t len)858 static zend_always_inline zval *zend_hash_str_find_deref(HashTable *ht, const char *str, size_t len)
859 {
860 zval *zv = zend_hash_str_find(ht, str, len);
861 if (zv) {
862 ZVAL_DEREF(zv);
863 }
864 return zv;
865 }
866
zend_symtable_str_find_ptr(HashTable * ht,const char * str,size_t len)867 static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
868 {
869 zend_ulong idx;
870
871 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
872 return zend_hash_index_find_ptr(ht, idx);
873 } else {
874 return zend_hash_str_find_ptr(ht, str, len);
875 }
876 }
877
zend_hash_get_current_data_ptr_ex(HashTable * ht,HashPosition * pos)878 static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
879 {
880 zval *zv;
881
882 zv = zend_hash_get_current_data_ex(ht, pos);
883 if (zv) {
884 ZEND_ASSUME(Z_PTR_P(zv));
885 return Z_PTR_P(zv);
886 } else {
887 return NULL;
888 }
889 }
890
891 #define zend_hash_get_current_data_ptr(ht) \
892 zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
893
894 #define ZEND_HASH_FOREACH(_ht, indirect) do { \
895 HashTable *__ht = (_ht); \
896 Bucket *_p = __ht->arData; \
897 Bucket *_end = _p + __ht->nNumUsed; \
898 for (; _p != _end; _p++) { \
899 zval *_z = &_p->val; \
900 if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
901 _z = Z_INDIRECT_P(_z); \
902 } \
903 if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
904
905 #define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
906 HashTable *__ht = (_ht); \
907 uint32_t _idx = __ht->nNumUsed; \
908 Bucket *_p = __ht->arData + _idx; \
909 zval *_z; \
910 for (_idx = __ht->nNumUsed; _idx > 0; _idx--) { \
911 _p--; \
912 _z = &_p->val; \
913 if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
914 _z = Z_INDIRECT_P(_z); \
915 } \
916 if (UNEXPECTED(Z_TYPE_P(_z) == IS_UNDEF)) continue;
917
918 #define ZEND_HASH_FOREACH_END() \
919 } \
920 } while (0)
921
922 #define ZEND_HASH_FOREACH_END_DEL() \
923 __ht->nNumOfElements--; \
924 do { \
925 uint32_t j = HT_IDX_TO_HASH(_idx - 1); \
926 uint32_t nIndex = _p->h | __ht->nTableMask; \
927 uint32_t i = HT_HASH(__ht, nIndex); \
928 if (UNEXPECTED(j != i)) { \
929 Bucket *prev = HT_HASH_TO_BUCKET(__ht, i); \
930 while (Z_NEXT(prev->val) != j) { \
931 i = Z_NEXT(prev->val); \
932 prev = HT_HASH_TO_BUCKET(__ht, i); \
933 } \
934 Z_NEXT(prev->val) = Z_NEXT(_p->val); \
935 } else { \
936 HT_HASH(__ht, nIndex) = Z_NEXT(_p->val); \
937 } \
938 } while (0); \
939 } \
940 __ht->nNumUsed = _idx; \
941 } while (0)
942
943 #define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
944 ZEND_HASH_FOREACH(ht, 0); \
945 _bucket = _p;
946
947 #define ZEND_HASH_FOREACH_VAL(ht, _val) \
948 ZEND_HASH_FOREACH(ht, 0); \
949 _val = _z;
950
951 #define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
952 ZEND_HASH_FOREACH(ht, 1); \
953 _val = _z;
954
955 #define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
956 ZEND_HASH_FOREACH(ht, 0); \
957 _ptr = Z_PTR_P(_z);
958
959 #define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
960 ZEND_HASH_FOREACH(ht, 0); \
961 _h = _p->h;
962
963 #define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
964 ZEND_HASH_FOREACH(ht, 0); \
965 _key = _p->key;
966
967 #define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
968 ZEND_HASH_FOREACH(ht, 0); \
969 _h = _p->h; \
970 _key = _p->key;
971
972 #define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
973 ZEND_HASH_FOREACH(ht, 0); \
974 _h = _p->h; \
975 _val = _z;
976
977 #define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
978 ZEND_HASH_FOREACH(ht, 0); \
979 _key = _p->key; \
980 _val = _z;
981
982 #define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
983 ZEND_HASH_FOREACH(ht, 0); \
984 _h = _p->h; \
985 _key = _p->key; \
986 _val = _z;
987
988 #define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
989 ZEND_HASH_FOREACH(ht, 1); \
990 _key = _p->key; \
991 _val = _z;
992
993 #define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
994 ZEND_HASH_FOREACH(ht, 1); \
995 _h = _p->h; \
996 _key = _p->key; \
997 _val = _z;
998
999 #define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
1000 ZEND_HASH_FOREACH(ht, 0); \
1001 _h = _p->h; \
1002 _ptr = Z_PTR_P(_z);
1003
1004 #define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
1005 ZEND_HASH_FOREACH(ht, 0); \
1006 _key = _p->key; \
1007 _ptr = Z_PTR_P(_z);
1008
1009 #define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
1010 ZEND_HASH_FOREACH(ht, 0); \
1011 _h = _p->h; \
1012 _key = _p->key; \
1013 _ptr = Z_PTR_P(_z);
1014
1015 #define ZEND_HASH_REVERSE_FOREACH_BUCKET(ht, _bucket) \
1016 ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1017 _bucket = _p;
1018
1019 #define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
1020 ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1021 _val = _z;
1022
1023 #define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
1024 ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1025 _ptr = Z_PTR_P(_z);
1026
1027 #define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
1028 ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1029 _val = _z;
1030
1031 #define ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(ht, _key, _val) \
1032 ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1033 _key = _p->key; \
1034 _val = _z;
1035
1036 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
1037 ZEND_HASH_REVERSE_FOREACH(ht, 0); \
1038 _h = _p->h; \
1039 _key = _p->key; \
1040 _val = _z;
1041
1042 #define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
1043 ZEND_HASH_REVERSE_FOREACH(ht, 1); \
1044 _h = _p->h; \
1045 _key = _p->key; \
1046 _val = _z;
1047
1048 /* The following macros are useful to insert a sequence of new elements
1049 * of packed array. They may be use insted of series of
1050 * zend_hash_next_index_insert_new()
1051 * (HashTable must have enough free buckets).
1052 */
1053 #define ZEND_HASH_FILL_PACKED(ht) do { \
1054 HashTable *__fill_ht = (ht); \
1055 Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
1056 uint32_t __fill_idx = __fill_ht->nNumUsed; \
1057 ZEND_ASSERT(HT_FLAGS(__fill_ht) & HASH_FLAG_PACKED);
1058
1059 #define ZEND_HASH_FILL_ADD(_val) do { \
1060 ZVAL_COPY_VALUE(&__fill_bkt->val, _val); \
1061 __fill_bkt->h = (__fill_idx); \
1062 __fill_bkt->key = NULL; \
1063 __fill_bkt++; \
1064 __fill_idx++; \
1065 } while (0)
1066
1067 #define ZEND_HASH_FILL_END() \
1068 __fill_ht->nNumUsed = __fill_idx; \
1069 __fill_ht->nNumOfElements = __fill_idx; \
1070 __fill_ht->nNextFreeElement = __fill_idx; \
1071 __fill_ht->nInternalPointer = 0; \
1072 } while (0)
1073
_zend_hash_append_ex(HashTable * ht,zend_string * key,zval * zv,int interned)1074 static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, int interned)
1075 {
1076 uint32_t idx = ht->nNumUsed++;
1077 uint32_t nIndex;
1078 Bucket *p = ht->arData + idx;
1079
1080 ZVAL_COPY_VALUE(&p->val, zv);
1081 if (!interned && !ZSTR_IS_INTERNED(key)) {
1082 HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1083 zend_string_addref(key);
1084 zend_string_hash_val(key);
1085 }
1086 p->key = key;
1087 p->h = ZSTR_H(key);
1088 nIndex = (uint32_t)p->h | ht->nTableMask;
1089 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1090 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1091 ht->nNumOfElements++;
1092 return &p->val;
1093 }
1094
_zend_hash_append(HashTable * ht,zend_string * key,zval * zv)1095 static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
1096 {
1097 return _zend_hash_append_ex(ht, key, zv, 0);
1098 }
1099
_zend_hash_append_ptr_ex(HashTable * ht,zend_string * key,void * ptr,int interned)1100 static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, int interned)
1101 {
1102 uint32_t idx = ht->nNumUsed++;
1103 uint32_t nIndex;
1104 Bucket *p = ht->arData + idx;
1105
1106 ZVAL_PTR(&p->val, ptr);
1107 if (!interned && !ZSTR_IS_INTERNED(key)) {
1108 HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1109 zend_string_addref(key);
1110 zend_string_hash_val(key);
1111 }
1112 p->key = key;
1113 p->h = ZSTR_H(key);
1114 nIndex = (uint32_t)p->h | ht->nTableMask;
1115 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1116 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1117 ht->nNumOfElements++;
1118 return &p->val;
1119 }
1120
_zend_hash_append_ptr(HashTable * ht,zend_string * key,void * ptr)1121 static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
1122 {
1123 return _zend_hash_append_ptr_ex(ht, key, ptr, 0);
1124 }
1125
_zend_hash_append_ind(HashTable * ht,zend_string * key,zval * ptr)1126 static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
1127 {
1128 uint32_t idx = ht->nNumUsed++;
1129 uint32_t nIndex;
1130 Bucket *p = ht->arData + idx;
1131
1132 ZVAL_INDIRECT(&p->val, ptr);
1133 if (!ZSTR_IS_INTERNED(key)) {
1134 HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1135 zend_string_addref(key);
1136 zend_string_hash_val(key);
1137 }
1138 p->key = key;
1139 p->h = ZSTR_H(key);
1140 nIndex = (uint32_t)p->h | ht->nTableMask;
1141 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1142 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1143 ht->nNumOfElements++;
1144 }
1145
1146 #endif /* ZEND_HASH_H */
1147
1148 /*
1149 * Local variables:
1150 * tab-width: 4
1151 * c-basic-offset: 4
1152 * indent-tabs-mode: t
1153 * End:
1154 * vim600: sw=4 ts=4 fdm=marker
1155 * vim<600: sw=4 ts=4
1156 */
1157