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_update_ptr(HashTable *ht, zend_ulong h, void *pData)
582{
583    zval tmp, *zv;
584
585    ZVAL_PTR(&tmp, pData);
586    zv = zend_hash_index_update(ht, h, &tmp);
587    if (zv) {
588        ZEND_ASSUME(Z_PTR_P(zv));
589        return Z_PTR_P(zv);
590    } else {
591        return NULL;
592    }
593}
594
595static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
596{
597    zval tmp, *zv;
598
599    ZVAL_PTR(&tmp, NULL);
600    if ((zv = zend_hash_index_add(ht, h, &tmp))) {
601        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
602        memcpy(Z_PTR_P(zv), pData, size);
603        return Z_PTR_P(zv);
604    }
605    return NULL;
606}
607
608static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
609{
610    zval tmp, *zv;
611
612    ZVAL_PTR(&tmp, pData);
613    zv = zend_hash_next_index_insert(ht, &tmp);
614    if (zv) {
615        ZEND_ASSUME(Z_PTR_P(zv));
616        return Z_PTR_P(zv);
617    } else {
618        return NULL;
619    }
620}
621
622static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
623{
624    void *p;
625
626    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
627    memcpy(p, pData, size);
628    return zend_hash_index_update_ptr(ht, h, p);
629}
630
631static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
632{
633    zval tmp, *zv;
634
635    ZVAL_PTR(&tmp, NULL);
636    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
637        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
638        memcpy(Z_PTR_P(zv), pData, size);
639        return Z_PTR_P(zv);
640    }
641    return NULL;
642}
643
644static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
645{
646    zval *zv;
647
648    zv = zend_hash_find(ht, key);
649    if (zv) {
650        ZEND_ASSUME(Z_PTR_P(zv));
651        return Z_PTR_P(zv);
652    } else {
653        return NULL;
654    }
655}
656
657static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
658{
659    zval *zv;
660
661    zv = zend_hash_str_find(ht, str, len);
662    if (zv) {
663        ZEND_ASSUME(Z_PTR_P(zv));
664        return Z_PTR_P(zv);
665    } else {
666        return NULL;
667    }
668}
669
670static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
671{
672    zval *zv;
673
674    zv = zend_hash_index_find(ht, h);
675    if (zv) {
676        ZEND_ASSUME(Z_PTR_P(zv));
677        return Z_PTR_P(zv);
678    } else {
679        return NULL;
680    }
681}
682
683static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
684{
685    zend_ulong idx;
686
687    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
688        return zend_hash_index_find_ptr(ht, idx);
689    } else {
690        return zend_hash_str_find_ptr(ht, str, len);
691    }
692}
693
694static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
695{
696    zval *zv;
697
698    zv = zend_hash_get_current_data_ex(ht, pos);
699    if (zv) {
700        ZEND_ASSUME(Z_PTR_P(zv));
701        return Z_PTR_P(zv);
702    } else {
703        return NULL;
704    }
705}
706
707#define zend_hash_get_current_data_ptr(ht) \
708    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
709
710#define ZEND_HASH_FOREACH(_ht, indirect) do { \
711        Bucket *_p = (_ht)->arData; \
712        Bucket *_end = _p + (_ht)->nNumUsed; \
713        for (; _p != _end; _p++) { \
714            zval *_z = &_p->val; \
715            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
716                _z = Z_INDIRECT_P(_z); \
717            } \
718            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
719
720#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
721        uint _idx; \
722        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
723            Bucket *_p = (_ht)->arData + _idx - 1; \
724            zval *_z = &_p->val; \
725            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
726                _z = Z_INDIRECT_P(_z); \
727            } \
728            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
729
730#define ZEND_HASH_FOREACH_END() \
731        } \
732    } while (0)
733
734#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
735    ZEND_HASH_FOREACH(ht, 0); \
736    _bucket = _p;
737
738#define ZEND_HASH_FOREACH_VAL(ht, _val) \
739    ZEND_HASH_FOREACH(ht, 0); \
740    _val = _z;
741
742#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
743    ZEND_HASH_FOREACH(ht, 1); \
744    _val = _z;
745
746#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
747    ZEND_HASH_FOREACH(ht, 0); \
748    _ptr = Z_PTR_P(_z);
749
750#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
751    ZEND_HASH_FOREACH(ht, 0); \
752    _h = _p->h;
753
754#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
755    ZEND_HASH_FOREACH(ht, 0); \
756    _key = _p->key;
757
758#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
759    ZEND_HASH_FOREACH(ht, 0); \
760    _h = _p->h; \
761    _key = _p->key;
762
763#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
764    ZEND_HASH_FOREACH(ht, 0); \
765    _h = _p->h; \
766    _val = _z;
767
768#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
769    ZEND_HASH_FOREACH(ht, 0); \
770    _key = _p->key; \
771    _val = _z;
772
773#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
774    ZEND_HASH_FOREACH(ht, 0); \
775    _h = _p->h; \
776    _key = _p->key; \
777    _val = _z;
778
779#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
780    ZEND_HASH_FOREACH(ht, 1); \
781    _key = _p->key; \
782    _val = _z;
783
784#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
785    ZEND_HASH_FOREACH(ht, 1); \
786    _h = _p->h; \
787    _key = _p->key; \
788    _val = _z;
789
790#define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
791    ZEND_HASH_FOREACH(ht, 0); \
792    _h = _p->h; \
793    _ptr = Z_PTR_P(_z);
794
795#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
796    ZEND_HASH_FOREACH(ht, 0); \
797    _key = _p->key; \
798    _ptr = Z_PTR_P(_z);
799
800#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
801    ZEND_HASH_FOREACH(ht, 0); \
802    _h = _p->h; \
803    _key = _p->key; \
804    _ptr = Z_PTR_P(_z);
805
806#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
807    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
808    _val = _z;
809
810#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
811    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
812    _ptr = Z_PTR_P(_z);
813
814#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
815    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
816    _val = _z;
817
818#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
819    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
820    _h = _p->h; \
821    _key = _p->key; \
822    _val = _z;
823
824#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
825    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
826    _h = _p->h; \
827    _key = _p->key; \
828    _val = _z;
829
830#define ZEND_HASH_APPLY_PROTECTION(ht) \
831    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
832
833#define ZEND_HASH_APPLY_SHIFT 8
834#define ZEND_HASH_GET_APPLY_COUNT(ht) ((ht)->u.flags >> ZEND_HASH_APPLY_SHIFT)
835#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
836#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
837
838
839/* The following macros are useful to insert a sequence of new elements
840 * of packed array. They may be use insted of series of
841 * zend_hash_next_index_insert_new()
842 * (HashTable must have enough free buckets).
843 */
844#define ZEND_HASH_FILL_PACKED(ht) do { \
845        HashTable *__fill_ht = (ht); \
846        Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
847        uint32_t __fill_idx = __fill_ht->nNumUsed; \
848        ZEND_ASSERT(__fill_ht->u.flags & HASH_FLAG_PACKED);
849
850#define ZEND_HASH_FILL_ADD(_val) do { \
851        ZVAL_COPY_VALUE(&__fill_bkt->val, _val); \
852        __fill_bkt->h = (__fill_idx); \
853        __fill_bkt->key = NULL; \
854        __fill_bkt++; \
855        __fill_idx++; \
856    } while (0)
857
858#define ZEND_HASH_FILL_END() \
859        __fill_ht->nNumUsed = __fill_idx; \
860        __fill_ht->nNumOfElements = __fill_idx; \
861        __fill_ht->nNextFreeElement = __fill_idx; \
862        __fill_ht->nInternalPointer = 0; \
863    } while (0)
864
865static zend_always_inline void _zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
866{
867    uint32_t idx = ht->nNumUsed++;
868    uint32_t nIndex;
869    Bucket *p = ht->arData + idx;
870
871    ZVAL_COPY_VALUE(&p->val, zv);
872    p->key = zend_string_copy(key);
873    p->h = zend_string_hash_val(key);
874    nIndex = (uint32_t)p->h | ht->nTableMask;
875    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
876    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
877    ht->nNumUsed = idx + 1;
878    ht->nNumOfElements++;
879}
880
881static zend_always_inline void _zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
882{
883    uint32_t idx = ht->nNumUsed++;
884    uint32_t nIndex;
885    Bucket *p = ht->arData + idx;
886
887    ZVAL_PTR(&p->val, ptr);
888    p->key = zend_string_copy(key);
889    p->h = zend_string_hash_val(key);
890    nIndex = (uint32_t)p->h | ht->nTableMask;
891    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
892    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
893    ht->nNumUsed = idx + 1;
894    ht->nNumOfElements++;
895}
896
897static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
898{
899    uint32_t idx = ht->nNumUsed++;
900    uint32_t nIndex;
901    Bucket *p = ht->arData + idx;
902
903    ZVAL_INDIRECT(&p->val, ptr);
904    p->key = zend_string_copy(key);
905    p->h = zend_string_hash_val(key);
906    nIndex = (uint32_t)p->h | ht->nTableMask;
907    Z_NEXT(p->val) = HT_HASH(ht, nIndex);
908    HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
909    ht->nNumUsed = idx + 1;
910    ht->nNumOfElements++;
911}
912
913#endif                          /* ZEND_HASH_H */
914
915/*
916 * Local variables:
917 * tab-width: 4
918 * c-basic-offset: 4
919 * indent-tabs-mode: t
920 * End:
921 */
922