1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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 INVALID_IDX ((uint32_t) -1)
38
39#define HASH_FLAG_PERSISTENT       (1<<0)
40#define HASH_FLAG_APPLY_PROTECTION (1<<1)
41#define HASH_FLAG_PACKED           (1<<2)
42
43#define HASH_MASK_CONSISTENCY      0x60
44
45typedef struct _zend_hash_key {
46    zend_ulong h;
47    zend_string *key;
48} zend_hash_key;
49
50typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
51
52typedef uint32_t HashPosition;
53
54BEGIN_EXTERN_C()
55
56/* startup/shutdown */
57ZEND_API void _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
58ZEND_API void _zend_hash_init_ex(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC);
59ZEND_API void zend_hash_destroy(HashTable *ht);
60ZEND_API void zend_hash_clean(HashTable *ht);
61#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)                       _zend_hash_init((ht), (nSize), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
62#define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)      _zend_hash_init_ex((ht), (nSize), (pDestructor), (persistent), (bApplyProtection) ZEND_FILE_LINE_CC)
63
64ZEND_API void zend_hash_real_init(HashTable *ht, zend_bool packed);
65ZEND_API void zend_hash_packed_to_hash(HashTable *ht);
66ZEND_API void zend_hash_to_packed(HashTable *ht);
67
68/* additions/updates/changes */
69ZEND_API zval *_zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
70ZEND_API zval *_zend_hash_update(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
71ZEND_API zval *_zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
72ZEND_API zval *_zend_hash_add(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
73ZEND_API zval *_zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
74
75#define zend_hash_update(ht, key, pData) \
76        _zend_hash_update(ht, key, pData ZEND_FILE_LINE_CC)
77#define zend_hash_update_ind(ht, key, pData) \
78        _zend_hash_update_ind(ht, key, pData ZEND_FILE_LINE_CC)
79#define zend_hash_add(ht, key, pData) \
80        _zend_hash_add(ht, key, pData ZEND_FILE_LINE_CC)
81#define zend_hash_add_new(ht, key, pData) \
82        _zend_hash_add_new(ht, key, pData ZEND_FILE_LINE_CC)
83
84ZEND_API zval *_zend_hash_str_add_or_update(HashTable *ht, const char *key, size_t len, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
85ZEND_API zval *_zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
86ZEND_API zval *_zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
87ZEND_API zval *_zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
88ZEND_API zval *_zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
89
90#define zend_hash_str_update(ht, key, len, pData) \
91        _zend_hash_str_update(ht, key, len, pData ZEND_FILE_LINE_CC)
92#define zend_hash_str_update_ind(ht, key, len, pData) \
93        _zend_hash_str_update_ind(ht, key, len, pData ZEND_FILE_LINE_CC)
94#define zend_hash_str_add(ht, key, len, pData) \
95        _zend_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
96#define zend_hash_str_add_new(ht, key, len, pData) \
97        _zend_hash_str_add_new(ht, key, len, pData ZEND_FILE_LINE_CC)
98
99ZEND_API zval *_zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
100ZEND_API zval *_zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
101ZEND_API zval *_zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
102ZEND_API zval *_zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
103ZEND_API zval *_zend_hash_next_index_insert(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
104ZEND_API zval *_zend_hash_next_index_insert_new(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
105
106#define zend_hash_index_add(ht, h, pData) \
107        _zend_hash_index_add(ht, h, pData ZEND_FILE_LINE_CC)
108#define zend_hash_index_add_new(ht, h, pData) \
109        _zend_hash_index_add_new(ht, h, pData ZEND_FILE_LINE_CC)
110#define zend_hash_index_update(ht, h, pData) \
111        _zend_hash_index_update(ht, h, pData ZEND_FILE_LINE_CC)
112#define zend_hash_next_index_insert(ht, pData) \
113        _zend_hash_next_index_insert(ht, pData ZEND_FILE_LINE_CC)
114#define zend_hash_next_index_insert_new(ht, pData) \
115        _zend_hash_next_index_insert_new(ht, pData ZEND_FILE_LINE_CC)
116
117ZEND_API zval *zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
118ZEND_API zval *zend_hash_add_empty_element(HashTable *ht, zend_string *key);
119ZEND_API zval *zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
120
121#define ZEND_HASH_APPLY_KEEP                0
122#define ZEND_HASH_APPLY_REMOVE              1<<0
123#define ZEND_HASH_APPLY_STOP                1<<1
124
125typedef int (*apply_func_t)(zval *pDest TSRMLS_DC);
126typedef int (*apply_func_arg_t)(zval *pDest, void *argument TSRMLS_DC);
127typedef int (*apply_func_args_t)(zval *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
128
129ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
130ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
131ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
132ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void * TSRMLS_DC);
133ZEND_API void zend_hash_apply_with_arguments(HashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int, ...);
134
135/* This function should be used with special care (in other words,
136 * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
137 * return value, it assumes things about the order of the elements in the hash.
138 * Also, it does not provide the same kind of reentrancy protection that
139 * the standard apply functions do.
140 */
141ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
142
143
144/* Deletes */
145ZEND_API int zend_hash_del(HashTable *ht, zend_string *key);
146ZEND_API int zend_hash_del_ind(HashTable *ht, zend_string *key);
147ZEND_API int zend_hash_str_del(HashTable *ht, const char *key, size_t len);
148ZEND_API int zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
149ZEND_API int zend_hash_index_del(HashTable *ht, zend_ulong h);
150
151/* Data retreival */
152ZEND_API zval *zend_hash_find(const HashTable *ht, zend_string *key);
153ZEND_API zval *zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
154ZEND_API zval *zend_hash_index_find(const HashTable *ht, zend_ulong h);
155
156/* Misc */
157ZEND_API zend_bool zend_hash_exists(const HashTable *ht, zend_string *key);
158ZEND_API zend_bool zend_hash_str_exists(const HashTable *ht, const char *str, size_t len);
159ZEND_API zend_bool zend_hash_index_exists(const HashTable *ht, zend_ulong h);
160
161/* traversing */
162#define zend_hash_has_more_elements_ex(ht, pos) \
163    (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
164ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
165ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
166ZEND_API int zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, zend_bool duplicate, HashPosition *pos);
167ZEND_API void zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
168ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
169ZEND_API zval *zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
170ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
171ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
172
173typedef struct _HashPointer {
174    HashPosition  pos;
175    HashTable    *ht;
176    zend_ulong    h;
177    zend_string  *key;
178} HashPointer;
179
180#define zend_hash_has_more_elements(ht) \
181    zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
182#define zend_hash_move_forward(ht) \
183    zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
184#define zend_hash_move_backwards(ht) \
185    zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
186#define zend_hash_get_current_key(ht, str_index, num_index, duplicate) \
187    zend_hash_get_current_key_ex(ht, str_index, num_index, duplicate, &(ht)->nInternalPointer)
188#define zend_hash_get_current_key_zval(ht, key) \
189    zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
190#define zend_hash_get_current_key_type(ht) \
191    zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
192#define zend_hash_get_current_data(ht) \
193    zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
194#define zend_hash_internal_pointer_reset(ht) \
195    zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
196#define zend_hash_internal_pointer_end(ht) \
197    zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
198
199/* Copying, merging and sorting */
200ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
201ZEND_API void _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite ZEND_FILE_LINE_DC);
202ZEND_API void zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
203ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber TSRMLS_DC);
204ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
205ZEND_API zval *zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag TSRMLS_DC);
206
207#define zend_hash_merge(target, source, pCopyConstructor, overwrite)                    \
208    _zend_hash_merge(target, source, pCopyConstructor, overwrite ZEND_FILE_LINE_CC)
209
210#define zend_hash_num_elements(ht) \
211    (ht)->nNumOfElements
212
213#define zend_hash_next_free_element(ht) \
214    (ht)->nNextFreeElement
215
216ZEND_API int zend_hash_rehash(HashTable *ht);
217
218ZEND_API void zend_array_dup(HashTable *target, HashTable *source);
219
220#if ZEND_DEBUG
221/* debug */
222void zend_hash_display_pListTail(const HashTable *ht);
223void zend_hash_display(const HashTable *ht);
224#endif
225
226ZEND_API int _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
227
228END_EXTERN_C()
229
230#define ZEND_INIT_SYMTABLE(ht)                              \
231    ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
232
233#define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)            \
234    zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
235
236static zend_always_inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
237{
238    register const char *tmp = key;
239
240    if (*tmp > '9') {
241        return 0;
242    } else if (*tmp < '0') {
243        if (*tmp != '-') {
244            return 0;
245        }
246        tmp++;
247        if (*tmp > '9' || *tmp < '0') {
248            return 0;
249        }
250    }
251    return _zend_handle_numeric_str_ex(key, length, idx);
252}
253
254#define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
255    _zend_handle_numeric_str(key, length, &idx)
256
257#define ZEND_HANDLE_NUMERIC(key, idx) \
258    ZEND_HANDLE_NUMERIC_STR((key)->val, (key)->len, idx)
259
260
261static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
262{
263    zval *zv;
264
265    zv = zend_hash_find(ht, key);
266    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
267}
268
269
270static zend_always_inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
271{
272    zval *zv;
273
274    zv = zend_hash_find(ht, key);
275    return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
276            Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
277}
278
279
280static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
281{
282    zval *zv;
283
284    zv = zend_hash_str_find(ht, str, len);
285    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
286}
287
288
289static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
290{
291    zend_ulong idx;
292
293    if (ZEND_HANDLE_NUMERIC(key, idx)) {
294        return zend_hash_index_update(ht, idx, pData);
295    } else {
296        return zend_hash_update(ht, key, pData);
297    }
298}
299
300
301static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
302{
303    zend_ulong idx;
304
305    if (ZEND_HANDLE_NUMERIC(key, idx)) {
306        return zend_hash_index_update(ht, idx, pData);
307    } else {
308        return zend_hash_update_ind(ht, key, pData);
309    }
310}
311
312
313static zend_always_inline int zend_symtable_del(HashTable *ht, zend_string *key)
314{
315    zend_ulong idx;
316
317    if (ZEND_HANDLE_NUMERIC(key, idx)) {
318        return zend_hash_index_del(ht, idx);
319    } else {
320        return zend_hash_del(ht, key);
321    }
322}
323
324
325static zend_always_inline int zend_symtable_del_ind(HashTable *ht, zend_string *key)
326{
327    zend_ulong idx;
328
329    if (ZEND_HANDLE_NUMERIC(key, idx)) {
330        return zend_hash_index_del(ht, idx);
331    } else {
332        return zend_hash_del_ind(ht, key);
333    }
334}
335
336
337static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
338{
339    zend_ulong idx;
340
341    if (ZEND_HANDLE_NUMERIC(key, idx)) {
342        return zend_hash_index_find(ht, idx);
343    } else {
344        return zend_hash_find(ht, key);
345    }
346}
347
348
349static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
350{
351    zend_ulong idx;
352
353    if (ZEND_HANDLE_NUMERIC(key, idx)) {
354        return zend_hash_index_find(ht, idx);
355    } else {
356        return zend_hash_find_ind(ht, key);
357    }
358}
359
360
361static zend_always_inline int zend_symtable_exists(HashTable *ht, zend_string *key)
362{
363    zend_ulong idx;
364
365    if (ZEND_HANDLE_NUMERIC(key, idx)) {
366        return zend_hash_index_exists(ht, idx);
367    } else {
368        return zend_hash_exists(ht, key);
369    }
370}
371
372
373static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
374{
375    zend_ulong idx;
376
377    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
378        return zend_hash_index_update(ht, idx, pData);
379    } else {
380        return zend_hash_str_update(ht, str, len, pData);
381    }
382}
383
384
385static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
386{
387    zend_ulong idx;
388
389    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
390        return zend_hash_index_update(ht, idx, pData);
391    } else {
392        return zend_hash_str_update_ind(ht, str, len, pData);
393    }
394}
395
396
397static zend_always_inline int zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
398{
399    zend_ulong idx;
400
401    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
402        return zend_hash_index_del(ht, idx);
403    } else {
404        return zend_hash_str_del(ht, str, len);
405    }
406}
407
408
409static zend_always_inline int zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
410{
411    zend_ulong idx;
412
413    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
414        return zend_hash_index_del(ht, idx);
415    } else {
416        return zend_hash_str_del_ind(ht, str, len);
417    }
418}
419
420
421static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
422{
423    zend_ulong idx;
424
425    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
426        return zend_hash_index_find(ht, idx);
427    } else {
428        return zend_hash_str_find(ht, str, len);
429    }
430}
431
432
433static zend_always_inline int zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
434{
435    zend_ulong idx;
436
437    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
438        return zend_hash_index_exists(ht, idx);
439    } else {
440        return zend_hash_str_exists(ht, str, len);
441    }
442}
443
444static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
445{
446    zval tmp, *zv;
447
448    ZVAL_PTR(&tmp, pData);
449    zv = zend_hash_add(ht, key, &tmp);
450    return zv ? Z_PTR_P(zv) : NULL;
451}
452
453static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
454{
455    zval tmp, *zv;
456
457    ZVAL_PTR(&tmp, pData);
458    zv = zend_hash_add_new(ht, key, &tmp);
459    return zv ? Z_PTR_P(zv) : NULL;
460}
461
462static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
463{
464    zval tmp, *zv;
465
466    ZVAL_PTR(&tmp, pData);
467    zv = zend_hash_str_add(ht, str, len, &tmp);
468    return zv ? Z_PTR_P(zv) : NULL;
469}
470
471static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
472{
473    zval tmp, *zv;
474
475    ZVAL_PTR(&tmp, pData);
476    zv = zend_hash_update(ht, key, &tmp);
477    return zv ? Z_PTR_P(zv) : NULL;
478}
479
480static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
481{
482    zval tmp, *zv;
483
484    ZVAL_PTR(&tmp, pData);
485    zv = zend_hash_str_update(ht, str, len, &tmp);
486    return zv ? Z_PTR_P(zv) : NULL;
487}
488
489static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
490{
491    zval tmp, *zv;
492
493    ZVAL_PTR(&tmp, NULL);
494    if ((zv = zend_hash_add(ht, key, &tmp))) {
495        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
496        memcpy(Z_PTR_P(zv), pData, size);
497        return Z_PTR_P(zv);
498    }
499    return NULL;
500}
501
502static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
503{
504    zval tmp, *zv;
505
506    ZVAL_PTR(&tmp, NULL);
507    if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
508        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
509        memcpy(Z_PTR_P(zv), pData, size);
510        return Z_PTR_P(zv);
511    }
512    return NULL;
513}
514
515static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
516{
517    void *p;
518
519    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
520    memcpy(p, pData, size);
521    return zend_hash_update_ptr(ht, key, p);
522}
523
524static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
525{
526    void *p;
527
528    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
529    memcpy(p, pData, size);
530    return zend_hash_str_update_ptr(ht, str, len, p);
531}
532
533static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
534{
535    zval tmp, *zv;
536
537    ZVAL_PTR(&tmp, pData);
538    zv = zend_hash_index_update(ht, h, &tmp);
539    return zv ? Z_PTR_P(zv) : NULL;
540}
541
542static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
543{
544    zval tmp, *zv;
545
546    ZVAL_PTR(&tmp, pData);
547    zv = zend_hash_next_index_insert(ht, &tmp);
548    return zv ? Z_PTR_P(zv) : NULL;
549}
550
551static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
552{
553    void *p;
554
555    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
556    memcpy(p, pData, size);
557    return zend_hash_index_update_ptr(ht, h, p);
558}
559
560static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
561{
562    zval tmp, *zv;
563
564    ZVAL_PTR(&tmp, NULL);
565    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
566        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
567        memcpy(Z_PTR_P(zv), pData, size);
568        return Z_PTR_P(zv);
569    }
570    return NULL;
571}
572
573static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
574{
575    zval *zv;
576
577    zv = zend_hash_find(ht, key);
578    return zv ? Z_PTR_P(zv) : NULL;
579}
580
581static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
582{
583    zval *zv;
584
585    zv = zend_hash_str_find(ht, str, len);
586    return zv ? Z_PTR_P(zv) : NULL;
587}
588
589static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
590{
591    zval *zv;
592
593    zv = zend_hash_index_find(ht, h);
594    return zv ? Z_PTR_P(zv) : NULL;
595}
596
597static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
598{
599    zend_ulong idx;
600
601    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
602        return zend_hash_index_find_ptr(ht, idx);
603    } else {
604        return zend_hash_str_find_ptr(ht, str, len);
605    }
606}
607
608static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
609{
610    zval *zv;
611
612    zv = zend_hash_get_current_data_ex(ht, pos);
613    return zv ? Z_PTR_P(zv) : NULL;
614}
615
616#define zend_hash_get_current_data_ptr(ht) \
617    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
618
619#define ZEND_HASH_FOREACH(_ht, indirect) do { \
620        uint _idx; \
621        for (_idx = 0; _idx < (_ht)->nNumUsed; _idx++) { \
622            Bucket *_p = (_ht)->arData + _idx; \
623            zval *_z = &_p->val; \
624            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
625                _z = Z_INDIRECT_P(_z); \
626            } \
627            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
628
629#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
630        uint _idx; \
631        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
632            Bucket *_p = (_ht)->arData + _idx - 1; \
633            zval *_z = &_p->val; \
634            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
635                _z = Z_INDIRECT_P(_z); \
636            } \
637            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
638
639#define ZEND_HASH_FOREACH_END() \
640        } \
641    } while (0)
642
643#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
644    ZEND_HASH_FOREACH(ht, 0); \
645    _bucket = _p;
646
647#define ZEND_HASH_FOREACH_VAL(ht, _val) \
648    ZEND_HASH_FOREACH(ht, 0); \
649    _val = _z;
650
651#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
652    ZEND_HASH_FOREACH(ht, 1); \
653    _val = _z;
654
655#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
656    ZEND_HASH_FOREACH(ht, 0); \
657    _ptr = Z_PTR_P(_z);
658
659#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
660    ZEND_HASH_FOREACH(ht, 0); \
661    _h = _p->h;
662
663#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
664    ZEND_HASH_FOREACH(ht, 0); \
665    _key = _p->key;
666
667#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
668    ZEND_HASH_FOREACH(ht, 0); \
669    _h = _p->h; \
670    _key = _p->key;
671
672#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
673    ZEND_HASH_FOREACH(ht, 0); \
674    _h = _p->h; \
675    _val = _z;
676
677#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
678    ZEND_HASH_FOREACH(ht, 0); \
679    _key = _p->key; \
680    _val = _z;
681
682#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
683    ZEND_HASH_FOREACH(ht, 0); \
684    _h = _p->h; \
685    _key = _p->key; \
686    _val = _z;
687
688#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
689    ZEND_HASH_FOREACH(ht, 1); \
690    _key = _p->key; \
691    _val = _z;
692
693#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
694    ZEND_HASH_FOREACH(ht, 1); \
695    _h = _p->h; \
696    _key = _p->key; \
697    _val = _z;
698
699#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
700    ZEND_HASH_FOREACH(ht, 0); \
701    _key = _p->key; \
702    _ptr = Z_PTR_P(_z);
703
704#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
705    ZEND_HASH_FOREACH(ht, 0); \
706    _h = _p->h; \
707    _key = _p->key; \
708    _ptr = Z_PTR_P(_z);
709
710#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
711    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
712    _val = _z;
713
714#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
715    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
716    _ptr = Z_PTR_P(_z);
717
718#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
719    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
720    _val = _z;
721
722#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
723    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
724    _h = _p->h; \
725    _key = _p->key; \
726    _val = _z;
727
728#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
729    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
730    _h = _p->h; \
731    _key = _p->key; \
732    _val = _z;
733
734#define ZEND_HASH_APPLY_PROTECTION(ht) \
735    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
736
737#define ZEND_HASH_APPLY_SHIFT 8
738#define ZEND_HASH_GET_APPLY_COUNT(ht) ((ht)->u.flags >> ZEND_HASH_APPLY_SHIFT)
739#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
740#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
741
742#endif                          /* ZEND_HASH_H */
743
744/*
745 * Local variables:
746 * tab-width: 4
747 * c-basic-offset: 4
748 * indent-tabs-mode: t
749 * End:
750 */
751