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 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#define HASH_FLAG_INITIALIZED      (1<<3)
43
44#define HASH_MASK_CONSISTENCY      0x60
45
46typedef struct _zend_hash_key {
47    zend_ulong h;
48    zend_string *key;
49} zend_hash_key;
50
51typedef zend_bool (*merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam);
52
53typedef uint32_t HashPosition;
54
55BEGIN_EXTERN_C()
56
57/* startup/shutdown */
58ZEND_API void _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC);
59ZEND_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);
60ZEND_API void zend_hash_destroy(HashTable *ht);
61ZEND_API void zend_hash_clean(HashTable *ht);
62#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)                       _zend_hash_init((ht), (nSize), (pDestructor), (persistent) ZEND_FILE_LINE_CC)
63#define zend_hash_init_ex(ht, nSize, pHashFunction, pDestructor, persistent, bApplyProtection)      _zend_hash_init_ex((ht), (nSize), (pDestructor), (persistent), (bApplyProtection) ZEND_FILE_LINE_CC)
64
65ZEND_API void zend_hash_real_init(HashTable *ht, zend_bool packed);
66ZEND_API void zend_hash_packed_to_hash(HashTable *ht);
67ZEND_API void zend_hash_to_packed(HashTable *ht);
68
69/* additions/updates/changes */
70ZEND_API zval *_zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
71ZEND_API zval *_zend_hash_update(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
72ZEND_API zval *_zend_hash_update_ind(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
73ZEND_API zval *_zend_hash_add(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
74ZEND_API zval *_zend_hash_add_new(HashTable *ht, zend_string *key,zval *pData ZEND_FILE_LINE_DC);
75
76#define zend_hash_update(ht, key, pData) \
77        _zend_hash_update(ht, key, pData ZEND_FILE_LINE_CC)
78#define zend_hash_update_ind(ht, key, pData) \
79        _zend_hash_update_ind(ht, key, pData ZEND_FILE_LINE_CC)
80#define zend_hash_add(ht, key, pData) \
81        _zend_hash_add(ht, key, pData ZEND_FILE_LINE_CC)
82#define zend_hash_add_new(ht, key, pData) \
83        _zend_hash_add_new(ht, key, pData ZEND_FILE_LINE_CC)
84
85ZEND_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);
86ZEND_API zval *_zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
87ZEND_API zval *_zend_hash_str_update_ind(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
88ZEND_API zval *_zend_hash_str_add(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
89ZEND_API zval *_zend_hash_str_add_new(HashTable *ht, const char *key, size_t len, zval *pData ZEND_FILE_LINE_DC);
90
91#define zend_hash_str_update(ht, key, len, pData) \
92        _zend_hash_str_update(ht, key, len, pData ZEND_FILE_LINE_CC)
93#define zend_hash_str_update_ind(ht, key, len, pData) \
94        _zend_hash_str_update_ind(ht, key, len, pData ZEND_FILE_LINE_CC)
95#define zend_hash_str_add(ht, key, len, pData) \
96        _zend_hash_str_add(ht, key, len, pData ZEND_FILE_LINE_CC)
97#define zend_hash_str_add_new(ht, key, len, pData) \
98        _zend_hash_str_add_new(ht, key, len, pData ZEND_FILE_LINE_CC)
99
100ZEND_API zval *_zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag ZEND_FILE_LINE_DC);
101ZEND_API zval *_zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
102ZEND_API zval *_zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
103ZEND_API zval *_zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC);
104ZEND_API zval *_zend_hash_next_index_insert(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
105ZEND_API zval *_zend_hash_next_index_insert_new(HashTable *ht, zval *pData ZEND_FILE_LINE_DC);
106
107#define zend_hash_index_add(ht, h, pData) \
108        _zend_hash_index_add(ht, h, pData ZEND_FILE_LINE_CC)
109#define zend_hash_index_add_new(ht, h, pData) \
110        _zend_hash_index_add_new(ht, h, pData ZEND_FILE_LINE_CC)
111#define zend_hash_index_update(ht, h, pData) \
112        _zend_hash_index_update(ht, h, pData ZEND_FILE_LINE_CC)
113#define zend_hash_next_index_insert(ht, pData) \
114        _zend_hash_next_index_insert(ht, pData ZEND_FILE_LINE_CC)
115#define zend_hash_next_index_insert_new(ht, pData) \
116        _zend_hash_next_index_insert_new(ht, pData ZEND_FILE_LINE_CC)
117
118ZEND_API zval *zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h);
119ZEND_API zval *zend_hash_add_empty_element(HashTable *ht, zend_string *key);
120ZEND_API zval *zend_hash_str_add_empty_element(HashTable *ht, const char *key, size_t len);
121
122#define ZEND_HASH_APPLY_KEEP                0
123#define ZEND_HASH_APPLY_REMOVE              1<<0
124#define ZEND_HASH_APPLY_STOP                1<<1
125
126typedef int (*apply_func_t)(zval *pDest);
127typedef int (*apply_func_arg_t)(zval *pDest, void *argument);
128typedef int (*apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
129
130ZEND_API void zend_hash_graceful_destroy(HashTable *ht);
131ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht);
132ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func);
133ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *);
134ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int, ...);
135
136/* This function should be used with special care (in other words,
137 * it should usually not be used).  When used with the ZEND_HASH_APPLY_STOP
138 * return value, it assumes things about the order of the elements in the hash.
139 * Also, it does not provide the same kind of reentrancy protection that
140 * the standard apply functions do.
141 */
142ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func);
143
144
145/* Deletes */
146ZEND_API int zend_hash_del(HashTable *ht, zend_string *key);
147ZEND_API int zend_hash_del_ind(HashTable *ht, zend_string *key);
148ZEND_API int zend_hash_str_del(HashTable *ht, const char *key, size_t len);
149ZEND_API int zend_hash_str_del_ind(HashTable *ht, const char *key, size_t len);
150ZEND_API int zend_hash_index_del(HashTable *ht, zend_ulong h);
151
152/* Data retreival */
153ZEND_API zval *zend_hash_find(const HashTable *ht, zend_string *key);
154ZEND_API zval *zend_hash_str_find(const HashTable *ht, const char *key, size_t len);
155ZEND_API zval *zend_hash_index_find(const HashTable *ht, zend_ulong h);
156
157/* Misc */
158ZEND_API zend_bool zend_hash_exists(const HashTable *ht, zend_string *key);
159ZEND_API zend_bool zend_hash_str_exists(const HashTable *ht, const char *str, size_t len);
160ZEND_API zend_bool zend_hash_index_exists(const HashTable *ht, zend_ulong h);
161
162/* traversing */
163#define zend_hash_has_more_elements_ex(ht, pos) \
164    (zend_hash_get_current_key_type_ex(ht, pos) == HASH_KEY_NON_EXISTENT ? FAILURE : SUCCESS)
165ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
166ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
167ZEND_API int zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos);
168ZEND_API void zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos);
169ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
170ZEND_API zval *zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos);
171ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos);
172ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos);
173
174typedef struct _HashPointer {
175    HashPosition  pos;
176    HashTable    *ht;
177    zend_ulong    h;
178    zend_string  *key;
179} HashPointer;
180
181#define zend_hash_has_more_elements(ht) \
182    zend_hash_has_more_elements_ex(ht, &(ht)->nInternalPointer)
183#define zend_hash_move_forward(ht) \
184    zend_hash_move_forward_ex(ht, &(ht)->nInternalPointer)
185#define zend_hash_move_backwards(ht) \
186    zend_hash_move_backwards_ex(ht, &(ht)->nInternalPointer)
187#define zend_hash_get_current_key(ht, str_index, num_index) \
188    zend_hash_get_current_key_ex(ht, str_index, num_index, &(ht)->nInternalPointer)
189#define zend_hash_get_current_key_zval(ht, key) \
190    zend_hash_get_current_key_zval_ex(ht, key, &(ht)->nInternalPointer)
191#define zend_hash_get_current_key_type(ht) \
192    zend_hash_get_current_key_type_ex(ht, &(ht)->nInternalPointer)
193#define zend_hash_get_current_data(ht) \
194    zend_hash_get_current_data_ex(ht, &(ht)->nInternalPointer)
195#define zend_hash_internal_pointer_reset(ht) \
196    zend_hash_internal_pointer_reset_ex(ht, &(ht)->nInternalPointer)
197#define zend_hash_internal_pointer_end(ht) \
198    zend_hash_internal_pointer_end_ex(ht, &(ht)->nInternalPointer)
199
200/* Copying, merging and sorting */
201ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor);
202ZEND_API void _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite ZEND_FILE_LINE_DC);
203ZEND_API void zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
204ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q);
205ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
206ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
207ZEND_API int zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber);
208ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
209ZEND_API zval *zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
210
211#define zend_hash_merge(target, source, pCopyConstructor, overwrite)                    \
212    _zend_hash_merge(target, source, pCopyConstructor, overwrite ZEND_FILE_LINE_CC)
213
214#define zend_hash_sort(ht, compare_func, renumber) \
215    zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
216
217#define zend_hash_num_elements(ht) \
218    (ht)->nNumOfElements
219
220#define zend_hash_next_free_element(ht) \
221    (ht)->nNextFreeElement
222
223ZEND_API int zend_hash_rehash(HashTable *ht);
224
225ZEND_API void zend_array_dup(HashTable *target, HashTable *source);
226ZEND_API void zend_array_destroy(HashTable *ht);
227ZEND_API void zend_symtable_clean(HashTable *ht);
228
229#if ZEND_DEBUG
230/* debug */
231void zend_hash_display_pListTail(const HashTable *ht);
232void zend_hash_display(const HashTable *ht);
233#endif
234
235ZEND_API int _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx);
236
237END_EXTERN_C()
238
239#define ZEND_INIT_SYMTABLE(ht)                              \
240    ZEND_INIT_SYMTABLE_EX(ht, 8, 0)
241
242#define ZEND_INIT_SYMTABLE_EX(ht, n, persistent)            \
243    zend_hash_init(ht, n, NULL, ZVAL_PTR_DTOR, persistent)
244
245static zend_always_inline int _zend_handle_numeric_str(const char *key, size_t length, zend_ulong *idx)
246{
247    register const char *tmp = key;
248
249    if (*tmp > '9') {
250        return 0;
251    } else if (*tmp < '0') {
252        if (*tmp != '-') {
253            return 0;
254        }
255        tmp++;
256        if (*tmp > '9' || *tmp < '0') {
257            return 0;
258        }
259    }
260    return _zend_handle_numeric_str_ex(key, length, idx);
261}
262
263#define ZEND_HANDLE_NUMERIC_STR(key, length, idx) \
264    _zend_handle_numeric_str(key, length, &idx)
265
266#define ZEND_HANDLE_NUMERIC(key, idx) \
267    ZEND_HANDLE_NUMERIC_STR((key)->val, (key)->len, idx)
268
269
270static zend_always_inline zval *zend_hash_find_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) ? Z_INDIRECT_P(zv) : zv;
276}
277
278
279static zend_always_inline int zend_hash_exists_ind(const HashTable *ht, zend_string *key)
280{
281    zval *zv;
282
283    zv = zend_hash_find(ht, key);
284    return zv && (Z_TYPE_P(zv) != IS_INDIRECT ||
285            Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
286}
287
288
289static zend_always_inline zval *zend_hash_str_find_ind(const HashTable *ht, const char *str, size_t len)
290{
291    zval *zv;
292
293    zv = zend_hash_str_find(ht, str, len);
294    return (zv && Z_TYPE_P(zv) == IS_INDIRECT) ? Z_INDIRECT_P(zv) : 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    return zv ? Z_PTR_P(zv) : NULL;
460}
461
462static zend_always_inline void *zend_hash_add_new_ptr(HashTable *ht, zend_string *key, void *pData)
463{
464    zval tmp, *zv;
465
466    ZVAL_PTR(&tmp, pData);
467    zv = zend_hash_add_new(ht, key, &tmp);
468    return zv ? Z_PTR_P(zv) : NULL;
469}
470
471static zend_always_inline void *zend_hash_str_add_ptr(HashTable *ht, const char *str, size_t len, void *pData)
472{
473    zval tmp, *zv;
474
475    ZVAL_PTR(&tmp, pData);
476    zv = zend_hash_str_add(ht, str, len, &tmp);
477    return zv ? Z_PTR_P(zv) : NULL;
478}
479
480static zend_always_inline void *zend_hash_update_ptr(HashTable *ht, zend_string *key, void *pData)
481{
482    zval tmp, *zv;
483
484    ZVAL_PTR(&tmp, pData);
485    zv = zend_hash_update(ht, key, &tmp);
486    return zv ? Z_PTR_P(zv) : NULL;
487}
488
489static zend_always_inline void *zend_hash_str_update_ptr(HashTable *ht, const char *str, size_t len, void *pData)
490{
491    zval tmp, *zv;
492
493    ZVAL_PTR(&tmp, pData);
494    zv = zend_hash_str_update(ht, str, len, &tmp);
495    return zv ? Z_PTR_P(zv) : NULL;
496}
497
498static zend_always_inline void *zend_hash_add_mem(HashTable *ht, zend_string *key, void *pData, size_t size)
499{
500    zval tmp, *zv;
501
502    ZVAL_PTR(&tmp, NULL);
503    if ((zv = zend_hash_add(ht, key, &tmp))) {
504        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
505        memcpy(Z_PTR_P(zv), pData, size);
506        return Z_PTR_P(zv);
507    }
508    return NULL;
509}
510
511static zend_always_inline void *zend_hash_str_add_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
512{
513    zval tmp, *zv;
514
515    ZVAL_PTR(&tmp, NULL);
516    if ((zv = zend_hash_str_add(ht, str, len, &tmp))) {
517        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
518        memcpy(Z_PTR_P(zv), pData, size);
519        return Z_PTR_P(zv);
520    }
521    return NULL;
522}
523
524static zend_always_inline void *zend_hash_update_mem(HashTable *ht, zend_string *key, 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_update_ptr(ht, key, p);
531}
532
533static zend_always_inline void *zend_hash_str_update_mem(HashTable *ht, const char *str, size_t len, void *pData, size_t size)
534{
535    void *p;
536
537    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
538    memcpy(p, pData, size);
539    return zend_hash_str_update_ptr(ht, str, len, p);
540}
541
542static zend_always_inline void *zend_hash_index_update_ptr(HashTable *ht, zend_ulong h, void *pData)
543{
544    zval tmp, *zv;
545
546    ZVAL_PTR(&tmp, pData);
547    zv = zend_hash_index_update(ht, h, &tmp);
548    return zv ? Z_PTR_P(zv) : NULL;
549}
550
551static zend_always_inline void *zend_hash_index_add_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
552{
553    zval tmp, *zv;
554
555    ZVAL_PTR(&tmp, NULL);
556    if ((zv = zend_hash_index_add(ht, h, &tmp))) {
557        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
558        memcpy(Z_PTR_P(zv), pData, size);
559        return Z_PTR_P(zv);
560    }
561    return NULL;
562}
563
564static zend_always_inline void *zend_hash_next_index_insert_ptr(HashTable *ht, void *pData)
565{
566    zval tmp, *zv;
567
568    ZVAL_PTR(&tmp, pData);
569    zv = zend_hash_next_index_insert(ht, &tmp);
570    return zv ? Z_PTR_P(zv) : NULL;
571}
572
573static zend_always_inline void *zend_hash_index_update_mem(HashTable *ht, zend_ulong h, void *pData, size_t size)
574{
575    void *p;
576
577    p = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
578    memcpy(p, pData, size);
579    return zend_hash_index_update_ptr(ht, h, p);
580}
581
582static zend_always_inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, size_t size)
583{
584    zval tmp, *zv;
585
586    ZVAL_PTR(&tmp, NULL);
587    if ((zv = zend_hash_next_index_insert(ht, &tmp))) {
588        Z_PTR_P(zv) = pemalloc(size, ht->u.flags & HASH_FLAG_PERSISTENT);
589        memcpy(Z_PTR_P(zv), pData, size);
590        return Z_PTR_P(zv);
591    }
592    return NULL;
593}
594
595static zend_always_inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key)
596{
597    zval *zv;
598
599    zv = zend_hash_find(ht, key);
600    return zv ? Z_PTR_P(zv) : NULL;
601}
602
603static zend_always_inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len)
604{
605    zval *zv;
606
607    zv = zend_hash_str_find(ht, str, len);
608    return zv ? Z_PTR_P(zv) : NULL;
609}
610
611static zend_always_inline void *zend_hash_index_find_ptr(const HashTable *ht, zend_ulong h)
612{
613    zval *zv;
614
615    zv = zend_hash_index_find(ht, h);
616    return zv ? Z_PTR_P(zv) : NULL;
617}
618
619static zend_always_inline void *zend_symtable_str_find_ptr(HashTable *ht, const char *str, size_t len)
620{
621    zend_ulong idx;
622
623    if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
624        return zend_hash_index_find_ptr(ht, idx);
625    } else {
626        return zend_hash_str_find_ptr(ht, str, len);
627    }
628}
629
630static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht, HashPosition *pos)
631{
632    zval *zv;
633
634    zv = zend_hash_get_current_data_ex(ht, pos);
635    return zv ? Z_PTR_P(zv) : NULL;
636}
637
638#define zend_hash_get_current_data_ptr(ht) \
639    zend_hash_get_current_data_ptr_ex(ht, &(ht)->nInternalPointer)
640
641#define ZEND_HASH_FOREACH(_ht, indirect) do { \
642        uint _idx; \
643        for (_idx = 0; _idx < (_ht)->nNumUsed; _idx++) { \
644            Bucket *_p = (_ht)->arData + _idx; \
645            zval *_z = &_p->val; \
646            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
647                _z = Z_INDIRECT_P(_z); \
648            } \
649            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
650
651#define ZEND_HASH_REVERSE_FOREACH(_ht, indirect) do { \
652        uint _idx; \
653        for (_idx = (_ht)->nNumUsed; _idx > 0; _idx--) { \
654            Bucket *_p = (_ht)->arData + _idx - 1; \
655            zval *_z = &_p->val; \
656            if (indirect && Z_TYPE_P(_z) == IS_INDIRECT) { \
657                _z = Z_INDIRECT_P(_z); \
658            } \
659            if (Z_TYPE_P(_z) == IS_UNDEF) continue;
660
661#define ZEND_HASH_FOREACH_END() \
662        } \
663    } while (0)
664
665#define ZEND_HASH_FOREACH_BUCKET(ht, _bucket) \
666    ZEND_HASH_FOREACH(ht, 0); \
667    _bucket = _p;
668
669#define ZEND_HASH_FOREACH_VAL(ht, _val) \
670    ZEND_HASH_FOREACH(ht, 0); \
671    _val = _z;
672
673#define ZEND_HASH_FOREACH_VAL_IND(ht, _val) \
674    ZEND_HASH_FOREACH(ht, 1); \
675    _val = _z;
676
677#define ZEND_HASH_FOREACH_PTR(ht, _ptr) \
678    ZEND_HASH_FOREACH(ht, 0); \
679    _ptr = Z_PTR_P(_z);
680
681#define ZEND_HASH_FOREACH_NUM_KEY(ht, _h) \
682    ZEND_HASH_FOREACH(ht, 0); \
683    _h = _p->h;
684
685#define ZEND_HASH_FOREACH_STR_KEY(ht, _key) \
686    ZEND_HASH_FOREACH(ht, 0); \
687    _key = _p->key;
688
689#define ZEND_HASH_FOREACH_KEY(ht, _h, _key) \
690    ZEND_HASH_FOREACH(ht, 0); \
691    _h = _p->h; \
692    _key = _p->key;
693
694#define ZEND_HASH_FOREACH_NUM_KEY_VAL(ht, _h, _val) \
695    ZEND_HASH_FOREACH(ht, 0); \
696    _h = _p->h; \
697    _val = _z;
698
699#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val) \
700    ZEND_HASH_FOREACH(ht, 0); \
701    _key = _p->key; \
702    _val = _z;
703
704#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val) \
705    ZEND_HASH_FOREACH(ht, 0); \
706    _h = _p->h; \
707    _key = _p->key; \
708    _val = _z;
709
710#define ZEND_HASH_FOREACH_STR_KEY_VAL_IND(ht, _key, _val) \
711    ZEND_HASH_FOREACH(ht, 1); \
712    _key = _p->key; \
713    _val = _z;
714
715#define ZEND_HASH_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
716    ZEND_HASH_FOREACH(ht, 1); \
717    _h = _p->h; \
718    _key = _p->key; \
719    _val = _z;
720
721#define ZEND_HASH_FOREACH_NUM_KEY_PTR(ht, _h, _ptr) \
722    ZEND_HASH_FOREACH(ht, 0); \
723    _h = _p->h; \
724    _ptr = Z_PTR_P(_z);
725
726#define ZEND_HASH_FOREACH_STR_KEY_PTR(ht, _key, _ptr) \
727    ZEND_HASH_FOREACH(ht, 0); \
728    _key = _p->key; \
729    _ptr = Z_PTR_P(_z);
730
731#define ZEND_HASH_FOREACH_KEY_PTR(ht, _h, _key, _ptr) \
732    ZEND_HASH_FOREACH(ht, 0); \
733    _h = _p->h; \
734    _key = _p->key; \
735    _ptr = Z_PTR_P(_z);
736
737#define ZEND_HASH_REVERSE_FOREACH_VAL(ht, _val) \
738    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
739    _val = _z;
740
741#define ZEND_HASH_REVERSE_FOREACH_PTR(ht, _ptr) \
742    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
743    _ptr = Z_PTR_P(_z);
744
745#define ZEND_HASH_REVERSE_FOREACH_VAL_IND(ht, _val) \
746    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
747    _val = _z;
748
749#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL(ht, _h, _key, _val) \
750    ZEND_HASH_REVERSE_FOREACH(ht, 0); \
751    _h = _p->h; \
752    _key = _p->key; \
753    _val = _z;
754
755#define ZEND_HASH_REVERSE_FOREACH_KEY_VAL_IND(ht, _h, _key, _val) \
756    ZEND_HASH_REVERSE_FOREACH(ht, 1); \
757    _h = _p->h; \
758    _key = _p->key; \
759    _val = _z;
760
761#define ZEND_HASH_APPLY_PROTECTION(ht) \
762    ((ht)->u.flags & HASH_FLAG_APPLY_PROTECTION)
763
764#define ZEND_HASH_APPLY_SHIFT 8
765#define ZEND_HASH_GET_APPLY_COUNT(ht) ((ht)->u.flags >> ZEND_HASH_APPLY_SHIFT)
766#define ZEND_HASH_INC_APPLY_COUNT(ht) ((ht)->u.flags += (1 << ZEND_HASH_APPLY_SHIFT))
767#define ZEND_HASH_DEC_APPLY_COUNT(ht) ((ht)->u.flags -= (1 << ZEND_HASH_APPLY_SHIFT))
768
769
770/* The following macros are useful to insert a sequence of new elements
771 * of packed array. They may be use insted of series of
772 * zend_hash_next_index_insert_new()
773 * (HashTable must have enough free buckets).
774 */
775#define ZEND_HASH_FILL_PACKED(ht) do { \
776        HashTable *__fill_ht = (ht); \
777        Bucket *__fill_bkt = __fill_ht->arData + __fill_ht->nNumUsed; \
778        uint32_t __fill_idx = __fill_ht->nNumUsed; \
779        ZEND_ASSERT(__fill_ht->u.flags & HASH_FLAG_PACKED);
780
781#define ZEND_HASH_FILL_ADD(_val) do { \
782        ZVAL_COPY_VALUE(&__fill_bkt->val, _val); \
783        __fill_bkt->h = (__fill_idx); \
784        __fill_bkt->key = NULL; \
785        __fill_bkt++; \
786        __fill_idx++; \
787    } while (0)
788
789#define ZEND_HASH_FILL_END() \
790        __fill_ht->nNumUsed = __fill_idx; \
791        __fill_ht->nNumOfElements = __fill_idx; \
792        __fill_ht->nNextFreeElement = __fill_idx + 1; \
793        __fill_ht->nInternalPointer = 0; \
794    } while (0)
795
796#endif                          /* ZEND_HASH_H */
797
798/*
799 * Local variables:
800 * tab-width: 4
801 * c-basic-offset: 4
802 * indent-tabs-mode: t
803 * End:
804 */
805