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);
126typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
127typedef int (*apply_func_args_t)(zval *pDest, 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);
132ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
133ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, 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);
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);
204ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
205ZEND_API zval *zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
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);
219ZEND_API void zend_array_destroy(HashTable *ht);
220ZEND_API void zend_symtable_clean(HashTable *ht);
221
222#if ZEND_DEBUG
223/* debug */
224void zend_hash_display_pListTail(const HashTable *ht);
225void zend_hash_display(const HashTable *ht);
226#endif
227
228ZEND_API int _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
229
230END_EXTERN_C()
231
232#define ZEND_INIT_SYMTABLE(ht)                              \
233    ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
234
235#define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)            \
236    zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
237
238static zend_always_inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
239{
240    register const char *tmp = key;
241
242    if (*tmp > '9') {
243        return 0;
244    } else if (*tmp < '0') {
245        if (*tmp != '-') {
246            return 0;
247        }
248        tmp++;
249        if (*tmp > '9' || *tmp < '0') {
250            return 0;
251        }
252    }
253    return _zend_handle_numeric_str_ex(key, length, idx);
254}
255
256#define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
257    _zend_handle_numeric_str(key, length, &idx)
258
259#define ZEND_HANDLE_NUMERIC(key, idx) \
260    ZEND_HANDLE_NUMERIC_STR((key)->val, (key)->len, idx)
261
262
263static zend_always_inline zval *zend_hash_find_ind(const HashTable *ht, zend_string *key)
264{
265    zval *zv;
266
267    zv = zend_hash_find(ht, key);
268    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
269}
270
271
272static zend_always_inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
273{
274    zval *zv;
275
276    zv = zend_hash_find(ht, key);
277    return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
278            Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
279}
280
281
282static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
283{
284    zval *zv;
285
286    zv = zend_hash_str_find(ht, str, len);
287    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : zv;
288}
289
290
291static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
292{
293    zend_ulong idx;
294
295    if (ZEND_HANDLE_NUMERIC(key, idx)) {
296        return zend_hash_index_update(ht, idx, pData);
297    } else {
298        return zend_hash_update(ht, key, pData);
299    }
300}
301
302
303static zend_always_inline zval *zend_symtable_update_ind(HashTable *ht, zend_string *key, zval *pData)
304{
305    zend_ulong idx;
306
307    if (ZEND_HANDLE_NUMERIC(key, idx)) {
308        return zend_hash_index_update(ht, idx, pData);
309    } else {
310        return zend_hash_update_ind(ht, key, pData);
311    }
312}
313
314
315static zend_always_inline int zend_symtable_del(HashTable *ht, zend_string *key)
316{
317    zend_ulong idx;
318
319    if (ZEND_HANDLE_NUMERIC(key, idx)) {
320        return zend_hash_index_del(ht, idx);
321    } else {
322        return zend_hash_del(ht, key);
323    }
324}
325
326
327static zend_always_inline int zend_symtable_del_ind(HashTable *ht, zend_string *key)
328{
329    zend_ulong idx;
330
331    if (ZEND_HANDLE_NUMERIC(key, idx)) {
332        return zend_hash_index_del(ht, idx);
333    } else {
334        return zend_hash_del_ind(ht, key);
335    }
336}
337
338
339static zend_always_inline zval *zend_symtable_find(const HashTable *ht, zend_string *key)
340{
341    zend_ulong idx;
342
343    if (ZEND_HANDLE_NUMERIC(key, idx)) {
344        return zend_hash_index_find(ht, idx);
345    } else {
346        return zend_hash_find(ht, key);
347    }
348}
349
350
351static zend_always_inline zval *zend_symtable_find_ind(const HashTable *ht, zend_string *key)
352{
353    zend_ulong idx;
354
355    if (ZEND_HANDLE_NUMERIC(key, idx)) {
356        return zend_hash_index_find(ht, idx);
357    } else {
358        return zend_hash_find_ind(ht, key);
359    }
360}
361
362
363static zend_always_inline int zend_symtable_exists(HashTable *ht, zend_string *key)
364{
365    zend_ulong idx;
366
367    if (ZEND_HANDLE_NUMERIC(key, idx)) {
368        return zend_hash_index_exists(ht, idx);
369    } else {
370        return zend_hash_exists(ht, key);
371    }
372}
373
374
375static zend_always_inline zval *zend_symtable_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
376{
377    zend_ulong idx;
378
379    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
380        return zend_hash_index_update(ht, idx, pData);
381    } else {
382        return zend_hash_str_update(ht, str, len, pData);
383    }
384}
385
386
387static zend_always_inline zval *zend_symtable_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
388{
389    zend_ulong idx;
390
391    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
392        return zend_hash_index_update(ht, idx, pData);
393    } else {
394        return zend_hash_str_update_ind(ht, str, len, pData);
395    }
396}
397
398
399static zend_always_inline int zend_symtable_str_del(HashTable *ht, const char *str, size_t len)
400{
401    zend_ulong idx;
402
403    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
404        return zend_hash_index_del(ht, idx);
405    } else {
406        return zend_hash_str_del(ht, str, len);
407    }
408}
409
410
411static zend_always_inline int zend_symtable_str_del_ind(HashTable *ht, const char *str, size_t len)
412{
413    zend_ulong idx;
414
415    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
416        return zend_hash_index_del(ht, idx);
417    } else {
418        return zend_hash_str_del_ind(ht, str, len);
419    }
420}
421
422
423static zend_always_inline zval *zend_symtable_str_find(HashTable *ht, const char *str, size_t len)
424{
425    zend_ulong idx;
426
427    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
428        return zend_hash_index_find(ht, idx);
429    } else {
430        return zend_hash_str_find(ht, str, len);
431    }
432}
433
434
435static zend_always_inline int zend_symtable_str_exists(HashTable *ht, const char *str, size_t len)
436{
437    zend_ulong idx;
438
439    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
440        return zend_hash_index_exists(ht, idx);
441    } else {
442        return zend_hash_str_exists(ht, str, len);
443    }
444}
445
446static zend_always_inline void *zend_hash_add_ptr(HashTable *ht, zend_string *key, void *pData)
447{
448    zval tmp, *zv;
449
450    ZVAL_PTR(&tmp, pData);
451    zv = zend_hash_add(ht, key, &tmp);
452    return zv ? Z_PTR_P(zv) : NULL;
453}
454
455static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
456{
457    zval tmp, *zv;
458
459    ZVAL_PTR(&tmp, pData);
460    zv = zend_hash_add_new(ht, key, &tmp);
461    return zv ? Z_PTR_P(zv) : NULL;
462}
463
464static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
465{
466    zval tmp, *zv;
467
468    ZVAL_PTR(&tmp, pData);
469    zv = zend_hash_str_add(ht, str, len, &tmp);
470    return zv ? Z_PTR_P(zv) : NULL;
471}
472
473static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
474{
475    zval tmp, *zv;
476
477    ZVAL_PTR(&tmp, pData);
478    zv = zend_hash_update(ht, key, &tmp);
479    return zv ? Z_PTR_P(zv) : NULL;
480}
481
482static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
483{
484    zval tmp, *zv;
485
486    ZVAL_PTR(&tmp, pData);
487    zv = zend_hash_str_update(ht, str, len, &tmp);
488    return zv ? Z_PTR_P(zv) : NULL;
489}
490
491static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
492{
493    zval tmp, *zv;
494
495    ZVAL_PTR(&tmp, NULL);
496    if ((zv = zend_hash_add(ht, key, &tmp))) {
497        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
498        memcpy(Z_PTR_P(zv), pData, size);
499        return Z_PTR_P(zv);
500    }
501    return NULL;
502}
503
504static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
505{
506    zval tmp, *zv;
507
508    ZVAL_PTR(&tmp, NULL);
509    if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
510        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
511        memcpy(Z_PTR_P(zv), pData, size);
512        return Z_PTR_P(zv);
513    }
514    return NULL;
515}
516
517static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
518{
519    void *p;
520
521    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
522    memcpy(p, pData, size);
523    return zend_hash_update_ptr(ht, key, p);
524}
525
526static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
527{
528    void *p;
529
530    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
531    memcpy(p, pData, size);
532    return zend_hash_str_update_ptr(ht, str, len, p);
533}
534
535static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
536{
537    zval tmp, *zv;
538
539    ZVAL_PTR(&tmp, pData);
540    zv = zend_hash_index_update(ht, h, &tmp);
541    return zv ? Z_PTR_P(zv) : NULL;
542}
543
544static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
545{
546    zval tmp, *zv;
547
548    ZVAL_PTR(&tmp, NULL);
549    if ((zv = zend_hash_index_add(ht, h, &tmp))) {
550        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
551        memcpy(Z_PTR_P(zv), pData, size);
552        return Z_PTR_P(zv);
553    }
554    return NULL;
555}
556
557static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
558{
559    zval tmp, *zv;
560
561    ZVAL_PTR(&tmp, pData);
562    zv = zend_hash_next_index_insert(ht, &tmp);
563    return zv ? Z_PTR_P(zv) : NULL;
564}
565
566static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
567{
568    void *p;
569
570    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
571    memcpy(p, pData, size);
572    return zend_hash_index_update_ptr(ht, h, p);
573}
574
575static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
576{
577    zval tmp, *zv;
578
579    ZVAL_PTR(&tmp, NULL);
580    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
581        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
582        memcpy(Z_PTR_P(zv), pData, size);
583        return Z_PTR_P(zv);
584    }
585    return NULL;
586}
587
588static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
589{
590    zval *zv;
591
592    zv = zend_hash_find(ht, key);
593    return zv ? Z_PTR_P(zv) : NULL;
594}
595
596static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
597{
598    zval *zv;
599
600    zv = zend_hash_str_find(ht, str, len);
601    return zv ? Z_PTR_P(zv) : NULL;
602}
603
604static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
605{
606    zval *zv;
607
608    zv = zend_hash_index_find(ht, h);
609    return zv ? Z_PTR_P(zv) : NULL;
610}
611
612static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
613{
614    zend_ulong idx;
615
616    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
617        return zend_hash_index_find_ptr(ht, idx);
618    } else {
619        return zend_hash_str_find_ptr(ht, str, len);
620    }
621}
622
623static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
624{
625    zval *zv;
626
627    zv = zend_hash_get_current_data_ex(ht, pos);
628    return zv ? Z_PTR_P(zv) : NULL;
629}
630
631#define zend_hash_get_current_data_ptr(ht) \
632    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
633
634#define ZEND_HASH_FOREACH(_ht, indirect) do { \
635        uint _idx; \
636        for (_idx = 0; _idx < (_ht)->nNumUsed; _idx++) { \
637            Bucket *_p = (_ht)->arData + _idx; \
638            zval *_z = &_p->val; \
639            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
640                _z = Z_INDIRECT_P(_z); \
641            } \
642            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
643
644#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
645        uint _idx; \
646        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
647            Bucket *_p = (_ht)->arData + _idx - 1; \
648            zval *_z = &_p->val; \
649            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
650                _z = Z_INDIRECT_P(_z); \
651            } \
652            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
653
654#define ZEND_HASH_FOREACH_END() \
655        } \
656    } while (0)
657
658#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
659    ZEND_HASH_FOREACH(ht, 0); \
660    _bucket = _p;
661
662#define ZEND_HASH_FOREACH_VAL(ht, _val) \
663    ZEND_HASH_FOREACH(ht, 0); \
664    _val = _z;
665
666#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
667    ZEND_HASH_FOREACH(ht, 1); \
668    _val = _z;
669
670#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
671    ZEND_HASH_FOREACH(ht, 0); \
672    _ptr = Z_PTR_P(_z);
673
674#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
675    ZEND_HASH_FOREACH(ht, 0); \
676    _h = _p->h;
677
678#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
679    ZEND_HASH_FOREACH(ht, 0); \
680    _key = _p->key;
681
682#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
683    ZEND_HASH_FOREACH(ht, 0); \
684    _h = _p->h; \
685    _key = _p->key;
686
687#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
688    ZEND_HASH_FOREACH(ht, 0); \
689    _h = _p->h; \
690    _val = _z;
691
692#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
693    ZEND_HASH_FOREACH(ht, 0); \
694    _key = _p->key; \
695    _val = _z;
696
697#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
698    ZEND_HASH_FOREACH(ht, 0); \
699    _h = _p->h; \
700    _key = _p->key; \
701    _val = _z;
702
703#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
704    ZEND_HASH_FOREACH(ht, 1); \
705    _key = _p->key; \
706    _val = _z;
707
708#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
709    ZEND_HASH_FOREACH(ht, 1); \
710    _h = _p->h; \
711    _key = _p->key; \
712    _val = _z;
713
714#define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
715    ZEND_HASH_FOREACH(ht, 0); \
716    _h = _p->h; \
717    _ptr = Z_PTR_P(_z);
718
719#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
720    ZEND_HASH_FOREACH(ht, 0); \
721    _key = _p->key; \
722    _ptr = Z_PTR_P(_z);
723
724#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
725    ZEND_HASH_FOREACH(ht, 0); \
726    _h = _p->h; \
727    _key = _p->key; \
728    _ptr = Z_PTR_P(_z);
729
730#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
731    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
732    _val = _z;
733
734#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
735    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
736    _ptr = Z_PTR_P(_z);
737
738#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
739    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
740    _val = _z;
741
742#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
743    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
744    _h = _p->h; \
745    _key = _p->key; \
746    _val = _z;
747
748#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
749    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
750    _h = _p->h; \
751    _key = _p->key; \
752    _val = _z;
753
754#define ZEND_HASH_APPLY_PROTECTION(ht) \
755    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
756
757#define ZEND_HASH_APPLY_SHIFT 8
758#define ZEND_HASH_GET_APPLY_COUNT(ht) ((ht)->u.flags >> ZEND_HASH_APPLY_SHIFT)
759#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
760#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
761
762#endif                          /* ZEND_HASH_H */
763
764/*
765 * Local variables:
766 * tab-width: 4
767 * c-basic-offset: 4
768 * indent-tabs-mode: t
769 * End:
770 */
771