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