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