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