php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_hash.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 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@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 | Dmitry Stogov <dmitry@php.net> |
18 +----------------------------------------------------------------------+
19*/
20
21#include "zend.h"
22#include "zend_globals.h"
23#include "zend_variables.h"
24
25#if defined(__aarch64__) || defined(_M_ARM64)
26# include <arm_neon.h>
27#endif
28
29/* Prefer to use AVX2 instructions for better latency and throughput */
30#if defined(__AVX2__)
31# include <immintrin.h>
32#elif defined( __SSE2__)
33# include <mmintrin.h>
34# include <emmintrin.h>
35#endif
36
37#if ZEND_DEBUG
38# define HT_ASSERT(ht, expr) \
39 ZEND_ASSERT((expr) || (HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION))
40#else
41# define HT_ASSERT(ht, expr)
42#endif
43
44#define HT_ASSERT_RC1(ht) HT_ASSERT(ht, GC_REFCOUNT(ht) == 1)
45
46#define HT_POISONED_PTR ((HashTable *) (intptr_t) -1)
47
48#if ZEND_DEBUG
49
50#define HT_OK 0x00
51#define HT_IS_DESTROYING 0x01
52#define HT_DESTROYED 0x02
53#define HT_CLEANING 0x03
54
55static void _zend_is_inconsistent(const HashTable *ht, const char *file, int line)
56{
57 if ((HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) == HT_OK) {
58 return;
59 }
61 case HT_IS_DESTROYING:
62 zend_output_debug_string(1, "%s(%d) : ht=%p is being destroyed", file, line, ht);
63 break;
64 case HT_DESTROYED:
65 zend_output_debug_string(1, "%s(%d) : ht=%p is already destroyed", file, line, ht);
66 break;
67 case HT_CLEANING:
68 zend_output_debug_string(1, "%s(%d) : ht=%p is being cleaned", file, line, ht);
69 break;
70 default:
71 zend_output_debug_string(1, "%s(%d) : ht=%p is inconsistent", file, line, ht);
72 break;
73 }
75}
76#define IS_CONSISTENT(a) _zend_is_inconsistent(a, __FILE__, __LINE__);
77#define SET_INCONSISTENT(n) do { \
78 HT_FLAGS(ht) = (HT_FLAGS(ht) & ~HASH_FLAG_CONSISTENCY) | (n); \
79 } while (0)
80#else
81#define IS_CONSISTENT(a)
82#define SET_INCONSISTENT(n)
83#endif
84
85#define ZEND_HASH_IF_FULL_DO_RESIZE(ht) \
86 if ((ht)->nNumUsed >= (ht)->nTableSize) { \
87 zend_hash_do_resize(ht); \
88 }
89
90ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len) {
91 void *result;
92 char *lc_str;
93
94 /* Stack allocate small strings to improve performance */
95 ALLOCA_FLAG(use_heap)
96
97 lc_str = zend_str_tolower_copy(do_alloca(len + 1, use_heap), str, len);
98 result = zend_hash_str_find_ptr(ht, lc_str, len);
99 free_alloca(lc_str, use_heap);
100
101 return result;
102}
103
105 void *result;
106 zend_string *lc_key = zend_string_tolower(key);
107 result = zend_hash_find_ptr(ht, lc_key);
108 zend_string_release(lc_key);
109 return result;
110}
111
112static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht);
113
114static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
115{
116#ifdef ZEND_WIN32
117 unsigned long index;
118#endif
119
120 /* Use big enough power of 2 */
121 /* size should be between HT_MIN_SIZE and HT_MAX_SIZE */
122 if (nSize <= HT_MIN_SIZE) {
123 return HT_MIN_SIZE;
124 } else if (UNEXPECTED(nSize > HT_MAX_SIZE)) {
125 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", nSize, sizeof(Bucket), sizeof(Bucket));
126 }
127
128#ifdef ZEND_WIN32
129 if (BitScanReverse(&index, nSize - 1)) {
130 return 0x2u << ((31 - index) ^ 0x1f);
131 } else {
132 /* nSize is ensured to be in the valid range, fall back to it
133 rather than using an undefined bis scan result. */
134 return nSize;
135 }
136#elif (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ)
137 return 0x2u << (__builtin_clz(nSize - 1) ^ 0x1f);
138#else
139 nSize -= 1;
140 nSize |= (nSize >> 1);
141 nSize |= (nSize >> 2);
142 nSize |= (nSize >> 4);
143 nSize |= (nSize >> 8);
144 nSize |= (nSize >> 16);
145 return nSize + 1;
146#endif
147}
148
149static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
150{
151 void *data;
152
154 data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), 1);
155 } else if (EXPECTED(ht->nTableSize == HT_MIN_SIZE)) {
156 /* Use specialized API with constant allocation amount for a particularly common case. */
158 } else {
160 }
162 /* Don't overwrite iterator count. */
165}
166
167static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
168{
169 void *data;
170 uint32_t nSize = ht->nTableSize;
171
173
175 data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), 1);
176 } else if (EXPECTED(nSize == HT_MIN_SIZE)) {
178 ht->nTableMask = HT_SIZE_TO_MASK(HT_MIN_SIZE);
180 /* Don't overwrite iterator count. */
181 ht->u.v.flags = HASH_FLAG_STATIC_KEYS;
182#if defined(__AVX2__)
183 do {
184 __m256i ymm0 = _mm256_setzero_si256();
185 ymm0 = _mm256_cmpeq_epi64(ymm0, ymm0);
186 _mm256_storeu_si256((__m256i*)&HT_HASH_EX(data, 0), ymm0);
187 _mm256_storeu_si256((__m256i*)&HT_HASH_EX(data, 8), ymm0);
188 } while(0);
189#elif defined (__SSE2__)
190 do {
191 __m128i xmm0 = _mm_setzero_si128();
192 xmm0 = _mm_cmpeq_epi8(xmm0, xmm0);
193 _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 0), xmm0);
194 _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 4), xmm0);
195 _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 8), xmm0);
196 _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 12), xmm0);
197 } while (0);
198#elif defined(__aarch64__) || defined(_M_ARM64)
199 do {
200 int32x4_t t = vdupq_n_s32(-1);
201 vst1q_s32((int32_t*)&HT_HASH_EX(data, 0), t);
202 vst1q_s32((int32_t*)&HT_HASH_EX(data, 4), t);
203 vst1q_s32((int32_t*)&HT_HASH_EX(data, 8), t);
204 vst1q_s32((int32_t*)&HT_HASH_EX(data, 12), t);
205 } while (0);
206#else
207 HT_HASH_EX(data, 0) = -1;
208 HT_HASH_EX(data, 1) = -1;
209 HT_HASH_EX(data, 2) = -1;
210 HT_HASH_EX(data, 3) = -1;
211 HT_HASH_EX(data, 4) = -1;
212 HT_HASH_EX(data, 5) = -1;
213 HT_HASH_EX(data, 6) = -1;
214 HT_HASH_EX(data, 7) = -1;
215 HT_HASH_EX(data, 8) = -1;
216 HT_HASH_EX(data, 9) = -1;
217 HT_HASH_EX(data, 10) = -1;
218 HT_HASH_EX(data, 11) = -1;
219 HT_HASH_EX(data, 12) = -1;
220 HT_HASH_EX(data, 13) = -1;
221 HT_HASH_EX(data, 14) = -1;
222 HT_HASH_EX(data, 15) = -1;
223#endif
224 return;
225 } else {
226 data = emalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)));
227 }
228 ht->nTableMask = HT_SIZE_TO_MASK(nSize);
232}
233
234static zend_always_inline void zend_hash_real_init_ex(HashTable *ht, bool packed)
235{
238 if (packed) {
239 zend_hash_real_init_packed_ex(ht);
240 } else {
241 zend_hash_real_init_mixed_ex(ht);
242 }
243}
244
245static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
247
249 .gc.refcount = 2,
250 .gc.u.type_info = IS_ARRAY | (GC_IMMUTABLE << GC_FLAGS_SHIFT),
251 .u.flags = HASH_FLAG_UNINITIALIZED,
252 .nTableMask = HT_MIN_MASK,
253 {.arData = (Bucket*)&uninitialized_bucket[2]},
254 .nNumUsed = 0,
255 .nNumOfElements = 0,
256 .nTableSize = HT_MIN_SIZE,
257 .nInternalPointer = 0,
258 .nNextFreeElement = ZEND_LONG_MIN,
259 .pDestructor = ZVAL_PTR_DTOR
260};
261
262static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
263{
267 ht->nTableMask = HT_MIN_MASK;
268 HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
269 ht->nNumUsed = 0;
270 ht->nNumOfElements = 0;
271 ht->nInternalPointer = 0;
272 ht->nNextFreeElement = ZEND_LONG_MIN;
273 ht->pDestructor = pDestructor;
274 ht->nTableSize = zend_hash_check_size(nSize);
275}
276
277ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
278{
279 _zend_hash_init_int(ht, nSize, pDestructor, persistent);
280}
281
283{
284 HashTable *ht = emalloc(sizeof(HashTable));
285 _zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, 0);
286 return ht;
287}
288
290{
291 HashTable *ht = emalloc(sizeof(HashTable));
292 _zend_hash_init_int(ht, nSize, ZVAL_PTR_DTOR, 0);
293 return ht;
294}
295
297{
298 zval *zv;
299 HashTable *ht = emalloc(sizeof(HashTable));
300 _zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, 0);
301 ht->nNumUsed = ht->nNumOfElements = ht->nNextFreeElement = 2;
302 zend_hash_real_init_packed_ex(ht);
303
304 zv = ht->arPacked;
305 ZVAL_COPY_VALUE(zv, val1);
306 zv++;
307 ZVAL_COPY_VALUE(zv, val2);
308 return ht;
309}
310
312{
314 if (ht->nTableSize >= HT_MAX_SIZE) {
315 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket), sizeof(Bucket));
316 }
317 uint32_t newTableSize = ht->nTableSize * 2;
319 ht->nTableSize = newTableSize;
320}
321
323{
325
327 zend_hash_real_init_ex(ht, packed);
328}
329
331{
333
335 zend_hash_real_init_packed_ex(ht);
336}
337
339{
341
343 zend_hash_real_init_mixed_ex(ht);
344}
345
347{
348 void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
349 zval *src = ht->arPacked;
350 Bucket *dst;
351 uint32_t i;
352 uint32_t nSize = ht->nTableSize;
353
355
357 // Alloc before assign to avoid inconsistencies on OOM
358 new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
360 ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
361 HT_SET_DATA_ADDR(ht, new_data);
362 dst = ht->arData;
363 for (i = 0; i < ht->nNumUsed; i++) {
364 ZVAL_COPY_VALUE(&dst->val, src);
365 dst->h = i;
366 dst->key = NULL;
367 dst++;
368 src++;
369 }
372}
373
375{
376 void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
377 Bucket *src = ht->arData;
378 zval *dst;
379 uint32_t i;
380
384 ht->nTableMask = HT_MIN_MASK;
385 HT_SET_DATA_ADDR(ht, new_data);
387 dst = ht->arPacked;
388 for (i = 0; i < ht->nNumUsed; i++) {
389 ZVAL_COPY_VALUE(dst, &src->val);
390 dst++;
391 src++;
392 }
394}
395
396ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, bool packed)
397{
399
400 if (nSize == 0) return;
401
403
405 if (nSize > ht->nTableSize) {
406 ht->nTableSize = zend_hash_check_size(nSize);
407 }
408 zend_hash_real_init(ht, packed);
409 } else {
410 if (packed) {
412 if (nSize > ht->nTableSize) {
413 uint32_t newTableSize = zend_hash_check_size(nSize);
415 ht->nTableSize = newTableSize;
416 }
417 } else {
419 if (nSize > ht->nTableSize) {
420 void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
421 Bucket *old_buckets = ht->arData;
422 nSize = zend_hash_check_size(nSize);
423 new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
424 ht->nTableSize = nSize;
425 ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
426 HT_SET_DATA_ADDR(ht, new_data);
427 memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
430 }
431 }
432 }
433}
434
436{
437 Bucket *p, *end, *arData;
438 uint32_t nIndex;
439
441 arData = ht->arData;
442 p = arData + ht->nNumUsed;
443 end = arData + nNumUsed;
444 ht->nNumUsed = nNumUsed;
445 while (p != end) {
446 p--;
447 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
448 ht->nNumOfElements--;
449 /* Collision pointers always directed from higher to lower buckets */
450#if 0
451 if (!(Z_NEXT(p->val) == HT_INVALID_IDX || HT_HASH_TO_BUCKET_EX(arData, Z_NEXT(p->val)) < p)) {
452 abort();
453 }
454#endif
455 nIndex = p->h | ht->nTableMask;
456 HT_HASH_EX(arData, nIndex) = Z_NEXT(p->val);
457 }
458}
459
460static uint32_t zend_array_recalc_elements(HashTable *ht)
461{
462 zval *val;
463 uint32_t num = ht->nNumOfElements;
464
466 if (Z_TYPE_P(val) == IS_INDIRECT) {
468 num--;
469 }
470 }
472 return num;
473}
474/* }}} */
475
477{
478 uint32_t num;
480 num = zend_array_recalc_elements(ht);
481 if (UNEXPECTED(ht->nNumOfElements == num)) {
483 }
484 } else if (UNEXPECTED(ht == &EG(symbol_table))) {
485 num = zend_array_recalc_elements(ht);
486 } else {
487 num = zend_hash_num_elements(ht);
488 }
489 return num;
490}
491/* }}} */
492
493static zend_always_inline HashPosition _zend_hash_get_valid_pos(const HashTable *ht, HashPosition pos)
494{
495 if (HT_IS_PACKED(ht)) {
496 while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arPacked[pos])) {
497 pos++;
498 }
499 } else {
500 while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arData[pos].val)) {
501 pos++;
502 }
503 }
504 return pos;
505}
506
507static zend_always_inline HashPosition _zend_hash_get_current_pos(const HashTable *ht)
508{
509 return _zend_hash_get_valid_pos(ht, ht->nInternalPointer);
510}
511
513{
514 return _zend_hash_get_current_pos(ht);
515}
516
518{
519 return _zend_hash_get_valid_pos(ht, pos);
520}
521
522static void zend_hash_remove_iterator_copies(uint32_t idx) {
523 HashTableIterator *iterators = EG(ht_iterators);
524
525 HashTableIterator *iter = iterators + idx;
526 uint32_t next_idx = iter->next_copy;
527 while (next_idx != idx) {
528 uint32_t cur_idx = next_idx;
529 HashTableIterator *cur_iter = iterators + cur_idx;
530 next_idx = cur_iter->next_copy;
531 cur_iter->next_copy = cur_idx; // avoid recursion in zend_hash_iterator_del
532 zend_hash_iterator_del(cur_idx);
533 }
534 iter->next_copy = idx;
535}
536
538{
539 HashTableIterator *iter = EG(ht_iterators);
540 HashTableIterator *end = iter + EG(ht_iterators_count);
541 uint32_t idx;
542
545 }
546 while (iter != end) {
547 if (iter->ht == NULL) {
548 iter->ht = ht;
549 iter->pos = pos;
550 idx = iter - EG(ht_iterators);
551 iter->next_copy = idx;
552 if (idx + 1 > EG(ht_iterators_used)) {
553 EG(ht_iterators_used) = idx + 1;
554 }
555 return idx;
556 }
557 iter++;
558 }
559 if (EG(ht_iterators) == EG(ht_iterators_slots)) {
560 EG(ht_iterators) = emalloc(sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
561 memcpy(EG(ht_iterators), EG(ht_iterators_slots), sizeof(HashTableIterator) * EG(ht_iterators_count));
562 } else {
563 EG(ht_iterators) = erealloc(EG(ht_iterators), sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
564 }
565 iter = EG(ht_iterators) + EG(ht_iterators_count);
566 EG(ht_iterators_count) += 8;
567 iter->ht = ht;
568 iter->pos = pos;
569 memset(iter + 1, 0, sizeof(HashTableIterator) * 7);
570 idx = iter - EG(ht_iterators);
571 iter->next_copy = idx;
572 EG(ht_iterators_used) = idx + 1;
573 return idx;
574}
575
576// To avoid losing track of the HashTable when separating arrays, we track all copies at once.
577static zend_always_inline bool zend_hash_iterator_find_copy_pos(uint32_t idx, HashTable *ht) {
578 HashTableIterator *iter = EG(ht_iterators) + idx;
579
580 uint32_t next_idx = iter->next_copy;
581 if (EXPECTED(next_idx != idx)) {
582 HashTableIterator *copy_iter;
583 while (next_idx != idx) {
584 copy_iter = EG(ht_iterators) + next_idx;
585 if (copy_iter->ht == ht) {
586 // We have found the hashtable we are actually iterating over
587 // Now clean any intermittent copies and replace the original index by the found one
588 if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
589 && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
591 }
594 }
595 iter->ht = copy_iter->ht;
596 iter->pos = copy_iter->pos;
597 zend_hash_remove_iterator_copies(idx);
598 return true;
599 }
600 next_idx = copy_iter->next_copy;
601 }
602 zend_hash_remove_iterator_copies(idx);
603 }
604
605 return false;
606}
607
609{
610 HashTableIterator *iter = EG(ht_iterators) + idx;
611
612 ZEND_ASSERT(idx != (uint32_t)-1);
613 if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
614 if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
615 && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
617 }
620 }
621 iter->ht = ht;
622 iter->pos = _zend_hash_get_current_pos(ht);
623 }
624 return iter->pos;
625}
626
628{
629 HashTable *ht = Z_ARRVAL_P(array);
630 HashTableIterator *iter = EG(ht_iterators) + idx;
631
632 ZEND_ASSERT(idx != (uint32_t)-1);
633 if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
634 if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
637 }
638 SEPARATE_ARRAY(array);
639 ht = Z_ARRVAL_P(array);
642 }
643 iter->ht = ht;
644 iter->pos = _zend_hash_get_current_pos(ht);
645 }
646 return iter->pos;
647}
648
650{
651 HashTableIterator *iter = EG(ht_iterators) + idx;
652
653 ZEND_ASSERT(idx != (uint32_t)-1);
654
655 if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
656 && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
657 ZEND_ASSERT(HT_ITERATORS_COUNT(iter->ht) != 0);
659 }
660 iter->ht = NULL;
661
662 if (UNEXPECTED(iter->next_copy != idx)) {
663 zend_hash_remove_iterator_copies(idx);
664 }
665
666 if (idx == EG(ht_iterators_used) - 1) {
667 while (idx > 0 && EG(ht_iterators)[idx - 1].ht == NULL) {
668 idx--;
669 }
670 EG(ht_iterators_used) = idx;
671 }
672}
673
674static zend_never_inline void ZEND_FASTCALL _zend_hash_iterators_remove(HashTable *ht)
675{
676 HashTableIterator *iter = EG(ht_iterators);
677 HashTableIterator *end = iter + EG(ht_iterators_used);
678
679 while (iter != end) {
680 if (iter->ht == ht) {
681 iter->ht = HT_POISONED_PTR;
682 }
683 iter++;
684 }
685}
686
687static zend_always_inline void zend_hash_iterators_remove(HashTable *ht)
688{
690 _zend_hash_iterators_remove(ht);
691 }
692}
693
695{
696 HashTableIterator *iter = EG(ht_iterators);
697 HashTableIterator *end = iter + EG(ht_iterators_used);
698 HashPosition res = ht->nNumUsed;
699
700 while (iter != end) {
701 if (iter->ht == ht) {
702 if (iter->pos >= start && iter->pos < res) {
703 res = iter->pos;
704 }
705 }
706 iter++;
707 }
708 return res;
709}
710
712{
713 HashTableIterator *iter = EG(ht_iterators);
714 HashTableIterator *end = iter + EG(ht_iterators_used);
715
716 while (iter != end) {
717 if (iter->ht == ht && iter->pos == from) {
718 iter->pos = to;
719 }
720 iter++;
721 }
722}
723
725{
726 HashTableIterator *iter = EG(ht_iterators);
727 HashTableIterator *end = iter + EG(ht_iterators_used);
728
729 while (iter != end) {
730 if (iter->ht == ht) {
731 iter->pos += step;
732 }
733 iter++;
734 }
735}
736
737/* Hash must be known and precomputed before */
738static zend_always_inline Bucket *zend_hash_find_bucket(const HashTable *ht, const zend_string *key)
739{
740 uint32_t nIndex;
741 uint32_t idx;
742 Bucket *p, *arData;
743
744 ZEND_ASSERT(ZSTR_H(key) != 0 && "Hash must be known");
745
746 arData = ht->arData;
747 nIndex = ZSTR_H(key) | ht->nTableMask;
748 idx = HT_HASH_EX(arData, nIndex);
749
750 if (UNEXPECTED(idx == HT_INVALID_IDX)) {
751 return NULL;
752 }
753 p = HT_HASH_TO_BUCKET_EX(arData, idx);
754 if (EXPECTED(p->key == key)) { /* check for the same interned string */
755 return p;
756 }
757
758 while (1) {
759 if (p->h == ZSTR_H(key) &&
760 EXPECTED(p->key) &&
761 zend_string_equal_content(p->key, key)) {
762 return p;
763 }
764 idx = Z_NEXT(p->val);
765 if (idx == HT_INVALID_IDX) {
766 return NULL;
767 }
768 p = HT_HASH_TO_BUCKET_EX(arData, idx);
769 if (p->key == key) { /* check for the same interned string */
770 return p;
771 }
772 }
773}
774
775static zend_always_inline Bucket *zend_hash_str_find_bucket(const HashTable *ht, const char *str, size_t len, zend_ulong h)
776{
777 uint32_t nIndex;
778 uint32_t idx;
779 Bucket *p, *arData;
780
781 arData = ht->arData;
782 nIndex = h | ht->nTableMask;
783 idx = HT_HASH_EX(arData, nIndex);
784 while (idx != HT_INVALID_IDX) {
785 ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
786 p = HT_HASH_TO_BUCKET_EX(arData, idx);
787 if ((p->h == h)
788 && p->key
789 && zend_string_equals_cstr(p->key, str, len)) {
790 return p;
791 }
792 idx = Z_NEXT(p->val);
793 }
794 return NULL;
795}
796
797static zend_always_inline Bucket *zend_hash_index_find_bucket(const HashTable *ht, zend_ulong h)
798{
799 uint32_t nIndex;
800 uint32_t idx;
801 Bucket *p, *arData;
802
803 arData = ht->arData;
804 nIndex = h | ht->nTableMask;
805 idx = HT_HASH_EX(arData, nIndex);
806 while (idx != HT_INVALID_IDX) {
807 ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
808 p = HT_HASH_TO_BUCKET_EX(arData, idx);
809 if (p->h == h && !p->key) {
810 return p;
811 }
812 idx = Z_NEXT(p->val);
813 }
814 return NULL;
815}
816
817static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_string *key, zval *pData, uint32_t flag)
818{
819 zend_ulong h;
820 uint32_t nIndex;
821 uint32_t idx;
822 Bucket *p, *arData;
823
826 zend_string_hash_val(key);
827
831 goto add_to_hash;
832 } else {
834 }
835 } else if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
836 p = zend_hash_find_bucket(ht, key);
837
838 if (p) {
839 zval *data;
840
841 ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
842 if (flag & HASH_LOOKUP) {
843 return &p->val;
844 } else if (flag & HASH_ADD) {
845 if (!(flag & HASH_UPDATE_INDIRECT)) {
846 return NULL;
847 }
848 ZEND_ASSERT(&p->val != pData);
849 data = &p->val;
850 if (Z_TYPE_P(data) == IS_INDIRECT) {
852 if (Z_TYPE_P(data) != IS_UNDEF) {
853 return NULL;
854 }
855 } else {
856 return NULL;
857 }
858 } else {
859 ZEND_ASSERT(&p->val != pData);
860 data = &p->val;
861 if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
863 }
864 }
865 if (ht->pDestructor) {
866 ht->pDestructor(data);
867 }
868 ZVAL_COPY_VALUE(data, pData);
869 return data;
870 }
871 }
872
873 ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
874
875add_to_hash:
876 if (!ZSTR_IS_INTERNED(key)) {
877 zend_string_addref(key);
879 }
880 idx = ht->nNumUsed++;
881 ht->nNumOfElements++;
882 arData = ht->arData;
883 p = arData + idx;
884 p->key = key;
885 p->h = h = ZSTR_H(key);
886 nIndex = h | ht->nTableMask;
887 Z_NEXT(p->val) = HT_HASH_EX(arData, nIndex);
888 HT_HASH_EX(arData, nIndex) = HT_IDX_TO_HASH(idx);
889 if (flag & HASH_LOOKUP) {
890 ZVAL_NULL(&p->val);
891 } else {
892 ZVAL_COPY_VALUE(&p->val, pData);
893 }
894
895 return &p->val;
896}
897
898static zend_always_inline zval *_zend_hash_str_add_or_update_i(HashTable *ht, const char *str, size_t len, zend_ulong h, zval *pData, uint32_t flag)
899{
901 uint32_t nIndex;
902 uint32_t idx;
903 Bucket *p;
904
907
911 goto add_to_hash;
912 } else {
914 }
915 } else if ((flag & HASH_ADD_NEW) == 0) {
916 p = zend_hash_str_find_bucket(ht, str, len, h);
917
918 if (p) {
919 zval *data;
920
921 if (flag & HASH_LOOKUP) {
922 return &p->val;
923 } else if (flag & HASH_ADD) {
924 if (!(flag & HASH_UPDATE_INDIRECT)) {
925 return NULL;
926 }
927 ZEND_ASSERT(&p->val != pData);
928 data = &p->val;
929 if (Z_TYPE_P(data) == IS_INDIRECT) {
931 if (Z_TYPE_P(data) != IS_UNDEF) {
932 return NULL;
933 }
934 } else {
935 return NULL;
936 }
937 } else {
938 ZEND_ASSERT(&p->val != pData);
939 data = &p->val;
940 if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
942 }
943 }
944 if (ht->pDestructor) {
945 ht->pDestructor(data);
946 }
947 ZVAL_COPY_VALUE(data, pData);
948 return data;
949 }
950 }
951
952 ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
953
954add_to_hash:
955 idx = ht->nNumUsed++;
956 ht->nNumOfElements++;
957 p = ht->arData + idx;
958 p->key = key = zend_string_init(str, len, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
959#if ZEND_RC_DEBUG
962 }
963#endif
964 p->h = ZSTR_H(key) = h;
966 if (flag & HASH_LOOKUP) {
967 ZVAL_NULL(&p->val);
968 } else {
969 ZVAL_COPY_VALUE(&p->val, pData);
970 }
971 nIndex = h | ht->nTableMask;
972 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
973 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
974
975 return &p->val;
976}
977
979{
980 if (flag == HASH_ADD) {
981 return zend_hash_add(ht, key, pData);
982 } else if (flag == HASH_ADD_NEW) {
983 return zend_hash_add_new(ht, key, pData);
984 } else if (flag == HASH_UPDATE) {
985 return zend_hash_update(ht, key, pData);
986 } else {
988 return zend_hash_update_ind(ht, key, pData);
989 }
990}
991
993{
994 return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD);
995}
996
998{
999 return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE);
1000}
1001
1003{
1004 return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT);
1005}
1006
1008{
1009 return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD_NEW);
1010}
1011
1013{
1014 return _zend_hash_add_or_update_i(ht, key, NULL, HASH_LOOKUP);
1015}
1016
1017ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, uint32_t flag)
1018{
1019 if (flag == HASH_ADD) {
1020 return zend_hash_str_add(ht, str, len, pData);
1021 } else if (flag == HASH_ADD_NEW) {
1022 return zend_hash_str_add_new(ht, str, len, pData);
1023 } else if (flag == HASH_UPDATE) {
1024 return zend_hash_str_update(ht, str, len, pData);
1025 } else {
1027 return zend_hash_str_update_ind(ht, str, len, pData);
1028 }
1029}
1030
1032{
1033 zend_ulong h = zend_hash_func(str, len);
1034
1035 return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE);
1036}
1037
1039{
1040 zend_ulong h = zend_hash_func(str, len);
1041
1042 return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT);
1043}
1044
1046{
1047 zend_ulong h = zend_hash_func(str, len);
1048
1049 return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD);
1050}
1051
1053{
1054 zend_ulong h = zend_hash_func(str, len);
1055
1056 return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD_NEW);
1057}
1058
1060{
1061 zval dummy;
1062
1063 ZVAL_NULL(&dummy);
1064 return zend_hash_index_add(ht, h, &dummy);
1065}
1066
1068{
1069 zval dummy;
1070
1071 ZVAL_NULL(&dummy);
1072 return zend_hash_add(ht, key, &dummy);
1073}
1074
1076{
1077 zval dummy;
1078
1079 ZVAL_NULL(&dummy);
1080 return zend_hash_str_add(ht, str, len, &dummy);
1081}
1082
1083static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag)
1084{
1085 uint32_t nIndex;
1086 uint32_t idx;
1087 Bucket *p;
1088 zval *zv;
1089
1092
1093 if ((flag & HASH_ADD_NEXT) && h == ZEND_LONG_MIN) {
1094 h = 0;
1095 }
1096
1097 if (HT_IS_PACKED(ht)) {
1099 && h < ht->nNumUsed) {
1100 zv = ht->arPacked + h;
1101 if (Z_TYPE_P(zv) != IS_UNDEF) {
1102 if (flag & HASH_LOOKUP) {
1103 return zv;
1104 }
1105replace:
1106 if (flag & HASH_ADD) {
1107 return NULL;
1108 }
1109 if (ht->pDestructor) {
1110 ht->pDestructor(zv);
1111 }
1112 ZVAL_COPY_VALUE(zv, pData);
1113 return zv;
1114 } else { /* we have to keep the order :( */
1115 goto convert_to_hash;
1116 }
1117 } else if (EXPECTED(h < ht->nTableSize)) {
1118add_to_packed:
1119 zv = ht->arPacked + h;
1120 /* incremental initialization of empty Buckets */
1122 if (h > ht->nNumUsed) {
1123 zval *q = ht->arPacked + ht->nNumUsed;
1124 while (q != zv) {
1125 ZVAL_UNDEF(q);
1126 q++;
1127 }
1128 }
1129 }
1130 ht->nNextFreeElement = ht->nNumUsed = h + 1;
1131 ht->nNumOfElements++;
1132 if (flag & HASH_LOOKUP) {
1133 ZVAL_NULL(zv);
1134 } else {
1135 ZVAL_COPY_VALUE(zv, pData);
1136 }
1137
1138 return zv;
1139 } else if ((h >> 1) < ht->nTableSize &&
1140 (ht->nTableSize >> 1) < ht->nNumOfElements) {
1142 goto add_to_packed;
1143 } else {
1144 if (ht->nNumUsed >= ht->nTableSize) {
1145 ht->nTableSize += ht->nTableSize;
1146 }
1147convert_to_hash:
1149 }
1150 } else if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
1151 if (h < ht->nTableSize) {
1152 zend_hash_real_init_packed_ex(ht);
1153 goto add_to_packed;
1154 }
1156 } else {
1157 if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
1158 p = zend_hash_index_find_bucket(ht, h);
1159 if (p) {
1160 if (flag & HASH_LOOKUP) {
1161 return &p->val;
1162 }
1163 ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
1164 zv = &p->val;
1165 goto replace;
1166 }
1167 }
1168 ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
1169 }
1170
1171 idx = ht->nNumUsed++;
1172 nIndex = h | ht->nTableMask;
1173 p = ht->arData + idx;
1174 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1175 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1176 if ((zend_long)h >= ht->nNextFreeElement) {
1177 ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
1178 }
1179 ht->nNumOfElements++;
1180 p->h = h;
1181 p->key = NULL;
1182 if (flag & HASH_LOOKUP) {
1183 ZVAL_NULL(&p->val);
1184 } else {
1185 ZVAL_COPY_VALUE(&p->val, pData);
1186 }
1187
1188 return &p->val;
1189}
1190
1192{
1193 if (flag == HASH_ADD) {
1194 return zend_hash_index_add(ht, h, pData);
1195 } else if (flag == (HASH_ADD|HASH_ADD_NEW)) {
1196 return zend_hash_index_add_new(ht, h, pData);
1197 } else if (flag == (HASH_ADD|HASH_ADD_NEXT)) {
1198 ZEND_ASSERT(h == ht->nNextFreeElement);
1199 return zend_hash_next_index_insert(ht, pData);
1200 } else if (flag == (HASH_ADD|HASH_ADD_NEW|HASH_ADD_NEXT)) {
1201 ZEND_ASSERT(h == ht->nNextFreeElement);
1202 return zend_hash_next_index_insert_new(ht, pData);
1203 } else {
1204 ZEND_ASSERT(flag == HASH_UPDATE);
1205 return zend_hash_index_update(ht, h, pData);
1206 }
1207}
1208
1210{
1211 return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD);
1212}
1213
1215{
1216 return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD | HASH_ADD_NEW);
1217}
1218
1220{
1221 return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_UPDATE);
1222}
1223
1225{
1226 return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEXT);
1227}
1228
1230{
1231 return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEW | HASH_ADD_NEXT);
1232}
1233
1235{
1236 return _zend_hash_index_add_or_update_i(ht, h, NULL, HASH_LOOKUP);
1237}
1238
1240{
1241 uint32_t nIndex;
1242 uint32_t idx, i;
1243 Bucket *p, *arData;
1244
1248
1249 (void)zend_string_hash_val(key);
1250 p = zend_hash_find_bucket(ht, key);
1251 if (UNEXPECTED(p)) {
1252 return (p == b) ? &p->val : NULL;
1253 }
1254
1255 if (!ZSTR_IS_INTERNED(key)) {
1256 zend_string_addref(key);
1258 }
1259
1260 arData = ht->arData;
1261
1262 /* del from hash */
1263 idx = HT_IDX_TO_HASH(b - arData);
1264 nIndex = b->h | ht->nTableMask;
1265 i = HT_HASH_EX(arData, nIndex);
1266 if (i == idx) {
1267 HT_HASH_EX(arData, nIndex) = Z_NEXT(b->val);
1268 } else {
1269 p = HT_HASH_TO_BUCKET_EX(arData, i);
1270 while (Z_NEXT(p->val) != idx) {
1271 i = Z_NEXT(p->val);
1272 p = HT_HASH_TO_BUCKET_EX(arData, i);
1273 }
1274 Z_NEXT(p->val) = Z_NEXT(b->val);
1275 }
1276 zend_string_release(b->key);
1277
1278 /* add to hash */
1279 idx = b - arData;
1280 b->key = key;
1281 b->h = ZSTR_H(key);
1282 nIndex = b->h | ht->nTableMask;
1283 idx = HT_IDX_TO_HASH(idx);
1284 i = HT_HASH_EX(arData, nIndex);
1285 if (i == HT_INVALID_IDX || i < idx) {
1286 Z_NEXT(b->val) = i;
1287 HT_HASH_EX(arData, nIndex) = idx;
1288 } else {
1289 p = HT_HASH_TO_BUCKET_EX(arData, i);
1290 while (Z_NEXT(p->val) != HT_INVALID_IDX && Z_NEXT(p->val) > idx) {
1291 i = Z_NEXT(p->val);
1292 p = HT_HASH_TO_BUCKET_EX(arData, i);
1293 }
1294 Z_NEXT(b->val) = Z_NEXT(p->val);
1295 Z_NEXT(p->val) = idx;
1296 }
1297 return &b->val;
1298}
1299
1300static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
1301{
1302
1305
1307 if (ht->nNumUsed > ht->nNumOfElements + (ht->nNumOfElements >> 5)) { /* additional term is there to amortize the cost of compaction */
1309 } else if (ht->nTableSize < HT_MAX_SIZE) { /* Let's double the table size */
1310 void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
1311 uint32_t nSize = ht->nTableSize + ht->nTableSize;
1312 Bucket *old_buckets = ht->arData;
1313
1315
1316 new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1317 ht->nTableSize = nSize;
1318 ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
1319 HT_SET_DATA_ADDR(ht, new_data);
1320 memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
1321 pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1323 } else {
1324 zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket) + sizeof(uint32_t), sizeof(Bucket));
1325 }
1326}
1327
1329{
1330 Bucket *p;
1331 uint32_t nIndex, i;
1332
1334
1335 if (UNEXPECTED(ht->nNumOfElements == 0)) {
1337 ht->nNumUsed = 0;
1339 /* Even if the array is empty, we still need to reset the iterator positions. */
1340 ht->nInternalPointer = 0;
1342 HashTableIterator *iter = EG(ht_iterators);
1343 HashTableIterator *end = iter + EG(ht_iterators_used);
1344 while (iter != end) {
1345 if (iter->ht == ht) {
1346 iter->pos = 0;
1347 }
1348 iter++;
1349 }
1350 }
1351 }
1352 return;
1353 }
1354
1356 i = 0;
1357 p = ht->arData;
1358 if (HT_IS_WITHOUT_HOLES(ht)) {
1359 do {
1360 nIndex = p->h | ht->nTableMask;
1361 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1362 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1363 p++;
1364 } while (++i < ht->nNumUsed);
1365 } else {
1366 uint32_t old_num_used = ht->nNumUsed;
1367 do {
1368 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) {
1369 uint32_t j = i;
1370 Bucket *q = p;
1371
1372 if (EXPECTED(!HT_HAS_ITERATORS(ht))) {
1373 while (++i < ht->nNumUsed) {
1374 p++;
1375 if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1376 ZVAL_COPY_VALUE(&q->val, &p->val);
1377 q->h = p->h;
1378 nIndex = q->h | ht->nTableMask;
1379 q->key = p->key;
1380 Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1381 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1382 if (UNEXPECTED(ht->nInternalPointer == i)) {
1383 ht->nInternalPointer = j;
1384 }
1385 q++;
1386 j++;
1387 }
1388 }
1389 } else {
1390 uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, i + 1);
1391
1392 while (++i < ht->nNumUsed) {
1393 p++;
1394 if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1395 ZVAL_COPY_VALUE(&q->val, &p->val);
1396 q->h = p->h;
1397 nIndex = q->h | ht->nTableMask;
1398 q->key = p->key;
1399 Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1400 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1401 if (UNEXPECTED(ht->nInternalPointer == i)) {
1402 ht->nInternalPointer = j;
1403 }
1404 if (UNEXPECTED(i >= iter_pos)) {
1405 do {
1406 zend_hash_iterators_update(ht, iter_pos, j);
1407 iter_pos = zend_hash_iterators_lower_pos(ht, iter_pos + 1);
1408 } while (iter_pos < i);
1409 }
1410 q++;
1411 j++;
1412 }
1413 }
1414 }
1415 ht->nNumUsed = j;
1416 break;
1417 }
1418 nIndex = p->h | ht->nTableMask;
1419 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1420 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1421 p++;
1422 } while (++i < ht->nNumUsed);
1423
1424 /* Migrate pointer to one past the end of the array to the new one past the end, so that
1425 * newly inserted elements are picked up correctly. */
1427 _zend_hash_iterators_update(ht, old_num_used, ht->nNumUsed);
1428 }
1429 }
1430}
1431
1432static zend_always_inline void zend_hash_iterators_clamp_max(HashTable *ht, uint32_t max)
1433{
1435 HashTableIterator *iter = EG(ht_iterators);
1436 HashTableIterator *end = iter + EG(ht_iterators_used);
1437 while (iter != end) {
1438 if (iter->ht == ht) {
1439 iter->pos = MIN(iter->pos, max);
1440 }
1441 iter++;
1442 }
1443 }
1444}
1445
1446static zend_always_inline void _zend_hash_packed_del_val(HashTable *ht, uint32_t idx, zval *zv)
1447{
1448 idx = HT_HASH_TO_IDX(idx);
1449 ht->nNumOfElements--;
1450 if (ht->nNumUsed - 1 == idx) {
1451 do {
1452 ht->nNumUsed--;
1453 } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arPacked[ht->nNumUsed-1]) == IS_UNDEF)));
1454 ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1455 zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1456 }
1457 if (ht->pDestructor) {
1458 zval tmp;
1459 ZVAL_COPY_VALUE(&tmp, zv);
1460 ZVAL_UNDEF(zv);
1461 ht->pDestructor(&tmp);
1462 } else {
1463 ZVAL_UNDEF(zv);
1464 }
1465}
1466
1467static zend_always_inline void _zend_hash_del_el_ex(HashTable *ht, uint32_t idx, Bucket *p, Bucket *prev)
1468{
1469 if (prev) {
1470 Z_NEXT(prev->val) = Z_NEXT(p->val);
1471 } else {
1472 HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
1473 }
1474 idx = HT_HASH_TO_IDX(idx);
1475 ht->nNumOfElements--;
1476 if (ht->nNumUsed - 1 == idx) {
1477 do {
1478 ht->nNumUsed--;
1479 } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arData[ht->nNumUsed-1].val) == IS_UNDEF)));
1480 ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1481 zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1482 }
1483 if (ht->pDestructor) {
1484 zval tmp;
1485 ZVAL_COPY_VALUE(&tmp, &p->val);
1486 ZVAL_UNDEF(&p->val);
1487 ht->pDestructor(&tmp);
1488 } else {
1489 ZVAL_UNDEF(&p->val);
1490 }
1491}
1492
1493static zend_always_inline void _zend_hash_del_el(HashTable *ht, uint32_t idx, Bucket *p)
1494{
1495 Bucket *prev = NULL;
1496 uint32_t nIndex;
1497 uint32_t i;
1498
1499 nIndex = p->h | ht->nTableMask;
1500 i = HT_HASH(ht, nIndex);
1501
1502 if (i != idx) {
1504 while (Z_NEXT(prev->val) != idx) {
1505 i = Z_NEXT(prev->val);
1507 }
1508 }
1509
1510 if (p->key) {
1511 zend_string_release(p->key);
1512 p->key = NULL;
1513 }
1514 _zend_hash_del_el_ex(ht, idx, p, prev);
1515}
1516
1518{
1522 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(zv - ht->arPacked), zv);
1523}
1524
1525
1527{
1531 _zend_hash_del_el(ht, HT_IDX_TO_HASH(p - ht->arData), p);
1532}
1533
1535{
1536 zend_ulong h;
1537 uint32_t nIndex;
1538 uint32_t idx;
1539 Bucket *p;
1540 Bucket *prev = NULL;
1541
1544
1545 h = zend_string_hash_val(key);
1546 nIndex = h | ht->nTableMask;
1547
1548 idx = HT_HASH(ht, nIndex);
1549 while (idx != HT_INVALID_IDX) {
1550 p = HT_HASH_TO_BUCKET(ht, idx);
1551 if ((p->key == key) ||
1552 (p->h == h &&
1553 p->key &&
1554 zend_string_equal_content(p->key, key))) {
1555 zend_string_release(p->key);
1556 p->key = NULL;
1557 _zend_hash_del_el_ex(ht, idx, p, prev);
1558 return SUCCESS;
1559 }
1560 prev = p;
1561 idx = Z_NEXT(p->val);
1562 }
1563 return FAILURE;
1564}
1565
1567{
1568 zend_ulong h;
1569 uint32_t nIndex;
1570 uint32_t idx;
1571 Bucket *p;
1572 Bucket *prev = NULL;
1573
1576
1577 h = zend_string_hash_val(key);
1578 nIndex = h | ht->nTableMask;
1579
1580 idx = HT_HASH(ht, nIndex);
1581 while (idx != HT_INVALID_IDX) {
1582 p = HT_HASH_TO_BUCKET(ht, idx);
1583 if ((p->key == key) ||
1584 (p->h == h &&
1585 p->key &&
1586 zend_string_equal_content(p->key, key))) {
1587 if (Z_TYPE(p->val) == IS_INDIRECT) {
1588 zval *data = Z_INDIRECT(p->val);
1589
1590 if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
1591 return FAILURE;
1592 } else {
1593 if (ht->pDestructor) {
1594 zval tmp;
1595 ZVAL_COPY_VALUE(&tmp, data);
1597 ht->pDestructor(&tmp);
1598 } else {
1600 }
1602 }
1603 } else {
1604 zend_string_release(p->key);
1605 p->key = NULL;
1606 _zend_hash_del_el_ex(ht, idx, p, prev);
1607 }
1608 return SUCCESS;
1609 }
1610 prev = p;
1611 idx = Z_NEXT(p->val);
1612 }
1613 return FAILURE;
1614}
1615
1617{
1618 zend_ulong h;
1619 uint32_t nIndex;
1620 uint32_t idx;
1621 Bucket *p;
1622 Bucket *prev = NULL;
1623
1626
1627 h = zend_inline_hash_func(str, len);
1628 nIndex = h | ht->nTableMask;
1629
1630 idx = HT_HASH(ht, nIndex);
1631 while (idx != HT_INVALID_IDX) {
1632 p = HT_HASH_TO_BUCKET(ht, idx);
1633 if ((p->h == h)
1634 && p->key
1635 && zend_string_equals_cstr(p->key, str, len)) {
1636 if (Z_TYPE(p->val) == IS_INDIRECT) {
1637 zval *data = Z_INDIRECT(p->val);
1638
1639 if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
1640 return FAILURE;
1641 } else {
1642 if (ht->pDestructor) {
1643 ht->pDestructor(data);
1644 }
1647 }
1648 } else {
1649 zend_string_release(p->key);
1650 p->key = NULL;
1651 _zend_hash_del_el_ex(ht, idx, p, prev);
1652 }
1653 return SUCCESS;
1654 }
1655 prev = p;
1656 idx = Z_NEXT(p->val);
1657 }
1658 return FAILURE;
1659}
1660
1662{
1663 zend_ulong h;
1664 uint32_t nIndex;
1665 uint32_t idx;
1666 Bucket *p;
1667 Bucket *prev = NULL;
1668
1671
1672 h = zend_inline_hash_func(str, len);
1673 nIndex = h | ht->nTableMask;
1674
1675 idx = HT_HASH(ht, nIndex);
1676 while (idx != HT_INVALID_IDX) {
1677 p = HT_HASH_TO_BUCKET(ht, idx);
1678 if ((p->h == h)
1679 && p->key
1680 && zend_string_equals_cstr(p->key, str, len)) {
1681 zend_string_release(p->key);
1682 p->key = NULL;
1683 _zend_hash_del_el_ex(ht, idx, p, prev);
1684 return SUCCESS;
1685 }
1686 prev = p;
1687 idx = Z_NEXT(p->val);
1688 }
1689 return FAILURE;
1690}
1691
1693{
1694 uint32_t nIndex;
1695 uint32_t idx;
1696 Bucket *p;
1697 Bucket *prev = NULL;
1698
1701
1702 if (HT_IS_PACKED(ht)) {
1703 if (h < ht->nNumUsed) {
1704 zval *zv = ht->arPacked + h;
1705 if (Z_TYPE_P(zv) != IS_UNDEF) {
1706 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(h), zv);
1707 return SUCCESS;
1708 }
1709 }
1710 return FAILURE;
1711 }
1712 nIndex = h | ht->nTableMask;
1713
1714 idx = HT_HASH(ht, nIndex);
1715 while (idx != HT_INVALID_IDX) {
1716 p = HT_HASH_TO_BUCKET(ht, idx);
1717 if ((p->h == h) && (p->key == NULL)) {
1718 _zend_hash_del_el_ex(ht, idx, p, prev);
1719 return SUCCESS;
1720 }
1721 prev = p;
1722 idx = Z_NEXT(p->val);
1723 }
1724 return FAILURE;
1725}
1726
1728{
1730 HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1731
1732 if (ht->nNumUsed) {
1733 if (HT_IS_PACKED(ht)) {
1734 if (ht->pDestructor) {
1735 zval *zv = ht->arPacked;
1736 zval *end = zv + ht->nNumUsed;
1737
1738 SET_INCONSISTENT(HT_IS_DESTROYING);
1739 if (HT_IS_WITHOUT_HOLES(ht)) {
1740 do {
1741 ht->pDestructor(zv);
1742 } while (++zv != end);
1743 } else {
1744 do {
1745 if (EXPECTED(Z_TYPE_P(zv) != IS_UNDEF)) {
1746 ht->pDestructor(zv);
1747 }
1748 } while (++zv != end);
1749 }
1750 SET_INCONSISTENT(HT_DESTROYED);
1751 }
1752 zend_hash_iterators_remove(ht);
1753 } else {
1754 Bucket *p = ht->arData;
1755 Bucket *end = p + ht->nNumUsed;
1756
1757 if (ht->pDestructor) {
1758 SET_INCONSISTENT(HT_IS_DESTROYING);
1759
1761 if (HT_IS_WITHOUT_HOLES(ht)) {
1762 do {
1763 ht->pDestructor(&p->val);
1764 } while (++p != end);
1765 } else {
1766 do {
1767 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1768 ht->pDestructor(&p->val);
1769 }
1770 } while (++p != end);
1771 }
1772 } else if (HT_IS_WITHOUT_HOLES(ht)) {
1773 do {
1774 ht->pDestructor(&p->val);
1775 if (EXPECTED(p->key)) {
1776 zend_string_release(p->key);
1777 }
1778 } while (++p != end);
1779 } else {
1780 do {
1781 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1782 ht->pDestructor(&p->val);
1783 if (EXPECTED(p->key)) {
1784 zend_string_release(p->key);
1785 }
1786 }
1787 } while (++p != end);
1788 }
1789
1790 SET_INCONSISTENT(HT_DESTROYED);
1791 } else {
1793 do {
1794 if (EXPECTED(p->key)) {
1795 zend_string_release(p->key);
1796 }
1797 } while (++p != end);
1798 }
1799 }
1800 zend_hash_iterators_remove(ht);
1801 }
1803 return;
1804 }
1806}
1807
1809{
1811 HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1812
1813 /* break possible cycles */
1815 GC_TYPE_INFO(ht) = GC_NULL /*???| (GC_WHITE << 16)*/;
1816
1817 if (ht->nNumUsed) {
1818 /* In some rare cases destructors of regular arrays may be changed */
1819 if (UNEXPECTED(ht->pDestructor != ZVAL_PTR_DTOR)) {
1821 goto free_ht;
1822 }
1823
1824 SET_INCONSISTENT(HT_IS_DESTROYING);
1825
1826 if (HT_IS_PACKED(ht)) {
1827 zval *zv = ht->arPacked;
1828 zval *end = zv + ht->nNumUsed;
1829
1830 do {
1831 i_zval_ptr_dtor(zv);
1832 } while (++zv != end);
1833 } else {
1834 Bucket *p = ht->arData;
1835 Bucket *end = p + ht->nNumUsed;
1836
1838 do {
1839 i_zval_ptr_dtor(&p->val);
1840 } while (++p != end);
1841 } else if (HT_IS_WITHOUT_HOLES(ht)) {
1842 do {
1843 i_zval_ptr_dtor(&p->val);
1844 if (EXPECTED(p->key)) {
1845 zend_string_release_ex(p->key, 0);
1846 }
1847 } while (++p != end);
1848 } else {
1849 do {
1850 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1851 i_zval_ptr_dtor(&p->val);
1852 if (EXPECTED(p->key)) {
1853 zend_string_release_ex(p->key, 0);
1854 }
1855 }
1856 } while (++p != end);
1857 }
1858 }
1860 goto free_ht;
1861 }
1862 SET_INCONSISTENT(HT_DESTROYED);
1864free_ht:
1865 zend_hash_iterators_remove(ht);
1867}
1868
1870{
1873
1874 if (ht->nNumUsed) {
1875 if (HT_IS_PACKED(ht)) {
1876 zval *zv = ht->arPacked;
1877 zval *end = zv + ht->nNumUsed;
1878
1879 if (ht->pDestructor) {
1881 if (HT_IS_WITHOUT_HOLES(ht)) {
1882 do {
1883 ht->pDestructor(zv);
1884 } while (++zv != end);
1885 } else {
1886 do {
1887 if (EXPECTED(Z_TYPE_P(zv) != IS_UNDEF)) {
1888 ht->pDestructor(zv);
1889 }
1890 } while (++zv != end);
1891 }
1892 }
1893 }
1894 } else {
1895 Bucket *p = ht->arData;
1896 Bucket *end = p + ht->nNumUsed;
1897
1898 if (ht->pDestructor) {
1900 if (HT_IS_WITHOUT_HOLES(ht)) {
1901 do {
1902 ht->pDestructor(&p->val);
1903 } while (++p != end);
1904 } else {
1905 do {
1906 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1907 ht->pDestructor(&p->val);
1908 }
1909 } while (++p != end);
1910 }
1911 } else if (HT_IS_WITHOUT_HOLES(ht)) {
1912 do {
1913 ht->pDestructor(&p->val);
1914 if (EXPECTED(p->key)) {
1915 zend_string_release(p->key);
1916 }
1917 } while (++p != end);
1918 } else {
1919 do {
1920 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1921 ht->pDestructor(&p->val);
1922 if (EXPECTED(p->key)) {
1923 zend_string_release(p->key);
1924 }
1925 }
1926 } while (++p != end);
1927 }
1928 } else {
1930 do {
1931 if (EXPECTED(p->key)) {
1932 zend_string_release(p->key);
1933 }
1934 } while (++p != end);
1935 }
1936 }
1938 }
1939 }
1940 ht->nNumUsed = 0;
1941 ht->nNumOfElements = 0;
1942 ht->nNextFreeElement = ZEND_LONG_MIN;
1943 ht->nInternalPointer = 0;
1944}
1945
1947{
1948 Bucket *p, *end;
1949
1952
1953 if (ht->nNumUsed) {
1955 p = ht->arData;
1956 end = p + ht->nNumUsed;
1958 do {
1959 i_zval_ptr_dtor(&p->val);
1960 } while (++p != end);
1961 } else if (HT_IS_WITHOUT_HOLES(ht)) {
1962 do {
1963 i_zval_ptr_dtor(&p->val);
1964 if (EXPECTED(p->key)) {
1965 zend_string_release(p->key);
1966 }
1967 } while (++p != end);
1968 } else {
1969 do {
1970 if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1971 i_zval_ptr_dtor(&p->val);
1972 if (EXPECTED(p->key)) {
1973 zend_string_release(p->key);
1974 }
1975 }
1976 } while (++p != end);
1977 }
1979 }
1980 ht->nNumUsed = 0;
1981 ht->nNumOfElements = 0;
1982 ht->nNextFreeElement = ZEND_LONG_MIN;
1983 ht->nInternalPointer = 0;
1984}
1985
1987{
1988 uint32_t idx;
1989
1992
1993 if (HT_IS_PACKED(ht)) {
1994 zval *zv = ht->arPacked;
1995
1996 for (idx = 0; idx < ht->nNumUsed; idx++, zv++) {
1997 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
1998 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
1999 }
2000 } else {
2001 Bucket *p = ht->arData;
2002
2003 for (idx = 0; idx < ht->nNumUsed; idx++, p++) {
2004 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2005 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2006 }
2007 }
2010 }
2011
2012 SET_INCONSISTENT(HT_DESTROYED);
2013}
2014
2016{
2017 uint32_t idx;
2018
2021
2022 idx = ht->nNumUsed;
2023 if (HT_IS_PACKED(ht)) {
2024 zval *zv = ht->arPacked + ht->nNumUsed;
2025
2026 while (idx > 0) {
2027 idx--;
2028 zv--;
2029 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2030 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2031 }
2032 } else {
2033 Bucket *p = ht->arData + ht->nNumUsed;
2034
2035 while (idx > 0) {
2036 idx--;
2037 p--;
2038 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2039 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2040 }
2041 }
2042
2045 }
2046
2047 SET_INCONSISTENT(HT_DESTROYED);
2048}
2049
2050/* This is used to recurse elements and selectively delete certain entries
2051 * from a hashtable. apply_func() receives the data and decides if the entry
2052 * should be deleted or recursion should be stopped. The following three
2053 * return codes are possible:
2054 * ZEND_HASH_APPLY_KEEP - continue
2055 * ZEND_HASH_APPLY_STOP - stop iteration
2056 * ZEND_HASH_APPLY_REMOVE - delete the element, combinable with the former
2057 */
2058
2060{
2061 uint32_t idx;
2062 int result;
2063
2065 if (HT_IS_PACKED(ht)) {
2066 for (idx = 0; idx < ht->nNumUsed; idx++) {
2067 zval *zv = ht->arPacked + idx;
2068
2069 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2070 result = apply_func(zv);
2071
2074 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2075 }
2077 break;
2078 }
2079 }
2080 } else {
2081 for (idx = 0; idx < ht->nNumUsed; idx++) {
2082 Bucket *p = ht->arData + idx;
2083
2084 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2085 result = apply_func(&p->val);
2086
2089 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2090 }
2092 break;
2093 }
2094 }
2095 }
2096}
2097
2098
2100{
2101 uint32_t idx;
2102 int result;
2103
2105 if (HT_IS_PACKED(ht)) {
2106 for (idx = 0; idx < ht->nNumUsed; idx++) {
2107 zval *zv = ht->arPacked + idx;
2108 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2109 result = apply_func(zv, argument);
2110
2113 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2114 }
2116 break;
2117 }
2118 }
2119 } else {
2120 for (idx = 0; idx < ht->nNumUsed; idx++) {
2121 Bucket *p = ht->arData + idx;
2122 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2123 result = apply_func(&p->val, argument);
2124
2127 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2128 }
2130 break;
2131 }
2132 }
2133 }
2134}
2135
2136
2138{
2139 uint32_t idx;
2140 va_list args;
2141 zend_hash_key hash_key;
2142 int result;
2143
2145
2146 if (HT_IS_PACKED(ht)) {
2147 for (idx = 0; idx < ht->nNumUsed; idx++) {
2148 zval *zv = ht->arPacked + idx;
2149
2150 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2151 va_start(args, num_args);
2152 hash_key.h = idx;
2153 hash_key.key = NULL;
2154
2155 result = apply_func(zv, num_args, args, &hash_key);
2156
2159 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2160 }
2162 va_end(args);
2163 break;
2164 }
2165 va_end(args);
2166 }
2167 } else {
2168 for (idx = 0; idx < ht->nNumUsed; idx++) {
2169 Bucket *p = ht->arData + idx;
2170
2171 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2172 va_start(args, num_args);
2173 hash_key.h = p->h;
2174 hash_key.key = p->key;
2175
2176 result = apply_func(&p->val, num_args, args, &hash_key);
2177
2180 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2181 }
2183 va_end(args);
2184 break;
2185 }
2186 va_end(args);
2187 }
2188 }
2189}
2190
2191
2193{
2194 uint32_t idx;
2195 int result;
2196
2198
2199 idx = ht->nNumUsed;
2200 if (HT_IS_PACKED(ht)) {
2201 zval *zv;
2202
2203 while (idx > 0) {
2204 idx--;
2205 zv = ht->arPacked + idx;
2206 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2207
2208 result = apply_func(zv);
2209
2212 _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2213 }
2215 break;
2216 }
2217 }
2218 } else {
2219 Bucket *p;
2220
2221 while (idx > 0) {
2222 idx--;
2223 p = ht->arData + idx;
2224 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2225
2226 result = apply_func(&p->val);
2227
2230 _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2231 }
2233 break;
2234 }
2235 }
2236 }
2237}
2238
2239
2241{
2242 uint32_t idx;
2243 zval *new_entry, *data;
2244
2245 IS_CONSISTENT(source);
2246 IS_CONSISTENT(target);
2247 HT_ASSERT_RC1(target);
2248
2249 if (HT_IS_PACKED(source)) {
2250 for (idx = 0; idx < source->nNumUsed; idx++) {
2251 zval *zv = source->arPacked + idx;
2252 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2253
2254 new_entry = zend_hash_index_update(target, idx, zv);
2255 if (pCopyConstructor) {
2256 pCopyConstructor(new_entry);
2257 }
2258 }
2259 return;
2260 }
2261
2262 for (idx = 0; idx < source->nNumUsed; idx++) {
2263 Bucket *p = source->arData + idx;
2264
2265 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2266
2267 /* INDIRECT element may point to UNDEF-ined slots */
2268 data = &p->val;
2269 if (Z_TYPE_P(data) == IS_INDIRECT) {
2271 if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
2272 continue;
2273 }
2274 }
2275 if (p->key) {
2276 new_entry = zend_hash_update(target, p->key, data);
2277 } else {
2278 new_entry = zend_hash_index_update(target, p->h, data);
2279 }
2280 if (pCopyConstructor) {
2281 pCopyConstructor(new_entry);
2282 }
2283 }
2284}
2285
2286
2287static zend_always_inline bool zend_array_dup_value(HashTable *source, HashTable *target, zval *data, zval *dest, bool packed, bool with_holes)
2288{
2289 if (with_holes) {
2290 if (!packed && Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2292 }
2294 return 0;
2295 }
2296 } else if (!packed) {
2297 /* INDIRECT element may point to UNDEF-ined slots */
2298 if (Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2301 return 0;
2302 }
2303 }
2304 }
2305
2306 do {
2307 if (Z_OPT_REFCOUNTED_P(data)) {
2308 if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1 &&
2310 Z_ARRVAL_P(Z_REFVAL_P(data)) != source)) {
2311 data = Z_REFVAL_P(data);
2312 if (!Z_OPT_REFCOUNTED_P(data)) {
2313 break;
2314 }
2315 }
2317 }
2318 } while (0);
2319 ZVAL_COPY_VALUE(dest, data);
2320
2321 return 1;
2322}
2323
2324static zend_always_inline bool zend_array_dup_element(HashTable *source, HashTable *target, uint32_t idx, Bucket *p, Bucket *q, bool packed, bool static_keys, bool with_holes)
2325{
2326 if (!zend_array_dup_value(source, target, &p->val, &q->val, packed, with_holes)) {
2327 return 0;
2328 }
2329
2330 if (!packed) {
2331 uint32_t nIndex;
2332
2333 q->h = p->h;
2334 q->key = p->key;
2335 if (!static_keys && q->key) {
2336 zend_string_addref(q->key);
2337 }
2338
2339 nIndex = q->h | target->nTableMask;
2340 Z_NEXT(q->val) = HT_HASH(target, nIndex);
2341 HT_HASH(target, nIndex) = HT_IDX_TO_HASH(idx);
2342 }
2343 return 1;
2344}
2345
2346// We need to duplicate iterators to be able to search through all copy-on-write copies to find the actually iterated HashTable and position back
2347static void zend_array_dup_ht_iterators(HashTable *source, HashTable *target) {
2348 uint32_t iter_index = 0;
2349 uint32_t end_index = EG(ht_iterators_used);
2350
2351 while (iter_index != end_index) {
2352 HashTableIterator *iter = &EG(ht_iterators)[iter_index];
2353 if (iter->ht == source) {
2354 uint32_t copy_idx = zend_hash_iterator_add(target, iter->pos);
2355 /* Refetch iter because the memory may be reallocated. */
2356 iter = &EG(ht_iterators)[iter_index];
2357 HashTableIterator *copy_iter = EG(ht_iterators) + copy_idx;
2358 copy_iter->next_copy = iter->next_copy;
2359 iter->next_copy = copy_idx;
2360 }
2361 iter_index++;
2362 }
2363}
2364
2365static zend_always_inline void zend_array_dup_packed_elements(HashTable *source, HashTable *target, bool with_holes)
2366{
2367 zval *p = source->arPacked;
2368 zval *q = target->arPacked;
2369 zval *end = p + source->nNumUsed;
2370
2371 do {
2372 if (!zend_array_dup_value(source, target, p, q, 1, with_holes)) {
2373 if (with_holes) {
2374 ZVAL_UNDEF(q);
2375 }
2376 }
2377 p++; q++;
2378 } while (p != end);
2379
2380 if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2381 zend_array_dup_ht_iterators(source, target);
2382 }
2383}
2384
2385static zend_always_inline uint32_t zend_array_dup_elements(HashTable *source, HashTable *target, bool static_keys, bool with_holes)
2386{
2387 uint32_t idx = 0;
2388 Bucket *p = source->arData;
2389 Bucket *q = target->arData;
2390 Bucket *end = p + source->nNumUsed;
2391
2392 if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2393 zend_array_dup_ht_iterators(source, target);
2394 }
2395
2396 do {
2397 if (!zend_array_dup_element(source, target, idx, p, q, 0, static_keys, with_holes)) {
2398 uint32_t target_idx = idx;
2399
2400 idx++; p++;
2401 if (EXPECTED(!HT_HAS_ITERATORS(target))) {
2402 while (p != end) {
2403 if (zend_array_dup_element(source, target, target_idx, p, q, 0, static_keys, with_holes)) {
2404 if (source->nInternalPointer == idx) {
2405 target->nInternalPointer = target_idx;
2406 }
2407 target_idx++; q++;
2408 }
2409 idx++; p++;
2410 }
2411 } else {
2412 target->nNumUsed = source->nNumUsed;
2413 uint32_t iter_pos = zend_hash_iterators_lower_pos(target, idx);
2414
2415 while (p != end) {
2416 if (zend_array_dup_element(source, target, target_idx, p, q, 0, static_keys, with_holes)) {
2417 if (source->nInternalPointer == idx) {
2418 target->nInternalPointer = target_idx;
2419 }
2420 if (UNEXPECTED(idx >= iter_pos)) {
2421 do {
2422 zend_hash_iterators_update(target, iter_pos, target_idx);
2423 iter_pos = zend_hash_iterators_lower_pos(target, iter_pos + 1);
2424 } while (iter_pos < idx);
2425 }
2426 target_idx++; q++;
2427 }
2428 idx++; p++;
2429 }
2430 }
2431 return target_idx;
2432 }
2433 idx++; p++; q++;
2434 } while (p != end);
2435 return idx;
2436}
2437
2439{
2440 uint32_t idx;
2441 HashTable *target;
2442
2443 IS_CONSISTENT(source);
2444
2445 ALLOC_HASHTABLE(target);
2446 GC_SET_REFCOUNT(target, 1);
2447 GC_TYPE_INFO(target) = GC_ARRAY;
2448
2449 target->pDestructor = ZVAL_PTR_DTOR;
2450
2451 if (source->nNumOfElements == 0) {
2453 target->nTableMask = HT_MIN_MASK;
2454 target->nNumUsed = 0;
2455 target->nNumOfElements = 0;
2456 target->nNextFreeElement = source->nNextFreeElement;
2457 target->nInternalPointer = 0;
2458 target->nTableSize = HT_MIN_SIZE;
2459 HT_SET_DATA_ADDR(target, &uninitialized_bucket);
2460 } else if (GC_FLAGS(source) & IS_ARRAY_IMMUTABLE) {
2461 HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2462 target->nTableMask = source->nTableMask;
2463 target->nNumUsed = source->nNumUsed;
2464 target->nNumOfElements = source->nNumOfElements;
2465 target->nNextFreeElement = source->nNextFreeElement;
2466 target->nTableSize = source->nTableSize;
2467 if (HT_IS_PACKED(source)) {
2468 HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE(target)));
2469 target->nInternalPointer = source->nInternalPointer;
2471 } else {
2472 HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2473 target->nInternalPointer = source->nInternalPointer;
2474 memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_USED_SIZE(source));
2475 }
2476 } else if (HT_IS_PACKED(source)) {
2477 HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2478 target->nTableMask = HT_MIN_MASK;
2479 target->nNumUsed = source->nNumUsed;
2480 target->nNumOfElements = source->nNumOfElements;
2481 target->nNextFreeElement = source->nNextFreeElement;
2482 target->nTableSize = source->nTableSize;
2483 HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE_EX(target->nTableSize, HT_MIN_MASK)));
2484 target->nInternalPointer =
2485 (source->nInternalPointer < source->nNumUsed) ?
2486 source->nInternalPointer : 0;
2487
2488 HT_HASH_RESET_PACKED(target);
2489
2490 if (HT_IS_WITHOUT_HOLES(target)) {
2491 zend_array_dup_packed_elements(source, target, 0);
2492 } else {
2493 zend_array_dup_packed_elements(source, target, 1);
2494 }
2495 } else {
2496 HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2497 target->nTableMask = source->nTableMask;
2498 target->nNextFreeElement = source->nNextFreeElement;
2499 target->nInternalPointer =
2500 (source->nInternalPointer < source->nNumUsed) ?
2501 source->nInternalPointer : 0;
2502
2503 target->nTableSize = source->nTableSize;
2504 HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2505 HT_HASH_RESET(target);
2506
2507 if (HT_HAS_STATIC_KEYS_ONLY(target)) {
2508 if (HT_IS_WITHOUT_HOLES(source)) {
2509 idx = zend_array_dup_elements(source, target, 1, 0);
2510 } else {
2511 idx = zend_array_dup_elements(source, target, 1, 1);
2512 }
2513 } else {
2514 if (HT_IS_WITHOUT_HOLES(source)) {
2515 idx = zend_array_dup_elements(source, target, 0, 0);
2516 } else {
2517 idx = zend_array_dup_elements(source, target, 0, 1);
2518 }
2519 }
2520 target->nNumUsed = idx;
2521 target->nNumOfElements = idx;
2522 }
2523 return target;
2524}
2525
2527{
2528 HashTable *result = _zend_new_array(zend_hash_num_elements(source));
2530
2532 zval *entry;
2533
2534 ZEND_HASH_FOREACH_VAL(source, entry) {
2535 if (UNEXPECTED(Z_ISREF_P(entry) && Z_REFCOUNT_P(entry) == 1)) {
2536 entry = Z_REFVAL_P(entry);
2537 }
2538 Z_TRY_ADDREF_P(entry);
2539 ZEND_HASH_FILL_ADD(entry);
2542
2543 return result;
2544}
2545
2546
2547ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite)
2548{
2549 uint32_t idx;
2550 Bucket *p;
2551 zval *t, *s;
2552
2553 IS_CONSISTENT(source);
2554 IS_CONSISTENT(target);
2555 HT_ASSERT_RC1(target);
2556
2557 if (overwrite) {
2558 if (HT_IS_PACKED(source)) {
2559 for (idx = 0; idx < source->nNumUsed; idx++) {
2560 s = source->arPacked + idx;
2561 if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2562 continue;
2563 }
2564 t = zend_hash_index_update(target, idx, s);
2565 if (pCopyConstructor) {
2566 pCopyConstructor(t);
2567 }
2568 }
2569 return;
2570 }
2571
2572 for (idx = 0; idx < source->nNumUsed; idx++) {
2573 p = source->arData + idx;
2574 s = &p->val;
2575 if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2576 s = Z_INDIRECT_P(s);
2577 }
2578 if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2579 continue;
2580 }
2581 if (p->key) {
2582 t = _zend_hash_add_or_update_i(target, p->key, s, HASH_UPDATE | HASH_UPDATE_INDIRECT);
2583 if (pCopyConstructor) {
2584 pCopyConstructor(t);
2585 }
2586 } else {
2587 t = zend_hash_index_update(target, p->h, s);
2588 if (pCopyConstructor) {
2589 pCopyConstructor(t);
2590 }
2591 }
2592 }
2593 } else {
2594 if (HT_IS_PACKED(source)) {
2595 for (idx = 0; idx < source->nNumUsed; idx++) {
2596 s = source->arPacked + idx;
2597 if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2598 continue;
2599 }
2600 t = zend_hash_index_add(target, idx, s);
2601 if (t && pCopyConstructor) {
2602 pCopyConstructor(t);
2603 }
2604 }
2605 return;
2606 }
2607
2608 for (idx = 0; idx < source->nNumUsed; idx++) {
2609 p = source->arData + idx;
2610 s = &p->val;
2611 if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2612 s = Z_INDIRECT_P(s);
2613 }
2614 if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2615 continue;
2616 }
2617 if (p->key) {
2618 t = _zend_hash_add_or_update_i(target, p->key, s, HASH_ADD | HASH_UPDATE_INDIRECT);
2619 if (t && pCopyConstructor) {
2620 pCopyConstructor(t);
2621 }
2622 } else {
2623 t = zend_hash_index_add(target, p->h, s);
2624 if (t && pCopyConstructor) {
2625 pCopyConstructor(t);
2626 }
2627 }
2628 }
2629 }
2630}
2631
2632
2633static bool ZEND_FASTCALL zend_hash_replace_checker_wrapper(HashTable *target, zval *source_data, zend_ulong h, zend_string *key, void *pParam, merge_checker_func_t merge_checker_func)
2634{
2635 zend_hash_key hash_key;
2636
2637 hash_key.h = h;
2638 hash_key.key = key;
2639 return merge_checker_func(target, source_data, &hash_key, pParam);
2640}
2641
2642
2643ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam)
2644{
2645 uint32_t idx;
2646 Bucket *p;
2647 zval *t;
2648
2649 IS_CONSISTENT(source);
2650 IS_CONSISTENT(target);
2651 HT_ASSERT_RC1(target);
2652
2653 ZEND_ASSERT(!HT_IS_PACKED(source));
2654 for (idx = 0; idx < source->nNumUsed; idx++) {
2655 p = source->arData + idx;
2656 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2657 if (zend_hash_replace_checker_wrapper(target, &p->val, p->h, p->key, pParam, pMergeSource)) {
2658 t = zend_hash_update(target, p->key, &p->val);
2659 if (pCopyConstructor) {
2660 pCopyConstructor(t);
2661 }
2662 }
2663 }
2664}
2665
2666
2667/* Returns the hash table data if found and NULL if not. */
2669{
2670 Bucket *p;
2671
2673
2674 (void)zend_string_hash_val(key);
2675 p = zend_hash_find_bucket(ht, key);
2676 return p ? &p->val : NULL;
2677}
2678
2680{
2681 Bucket *p;
2682
2684
2685 p = zend_hash_find_bucket(ht, key);
2686 return p ? &p->val : NULL;
2687}
2688
2690{
2691 zend_ulong h;
2692 Bucket *p;
2693
2695
2696 h = zend_inline_hash_func(str, len);
2697 p = zend_hash_str_find_bucket(ht, str, len, h);
2698 return p ? &p->val : NULL;
2699}
2700
2702{
2703 Bucket *p;
2704
2706
2707 if (HT_IS_PACKED(ht)) {
2708 if (h < ht->nNumUsed) {
2709 zval *zv = ht->arPacked + h;
2710
2711 if (Z_TYPE_P(zv) != IS_UNDEF) {
2712 return zv;
2713 }
2714 }
2715 return NULL;
2716 }
2717
2718 p = zend_hash_index_find_bucket(ht, h);
2719 return p ? &p->val : NULL;
2720}
2721
2723{
2724 Bucket *p;
2725
2728
2729 p = zend_hash_index_find_bucket(ht, h);
2730 return p ? &p->val : NULL;
2731}
2732
2734{
2736 HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2737 *pos = _zend_hash_get_valid_pos(ht, 0);
2738}
2739
2740
2741/* This function will be extremely optimized by remembering
2742 * the end of the list
2743 */
2745{
2746 uint32_t idx;
2747
2749 HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2750
2751 idx = ht->nNumUsed;
2752 if (HT_IS_PACKED(ht)) {
2753 while (idx > 0) {
2754 idx--;
2755 if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2756 *pos = idx;
2757 return;
2758 }
2759 }
2760 } else {
2761 while (idx > 0) {
2762 idx--;
2763 if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2764 *pos = idx;
2765 return;
2766 }
2767 }
2768 }
2769 *pos = ht->nNumUsed;
2770}
2771
2772
2774{
2775 uint32_t idx;
2776
2778 HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2779
2780 idx = _zend_hash_get_valid_pos(ht, *pos);
2781 if (idx < ht->nNumUsed) {
2782 if (HT_IS_PACKED(ht)) {
2783 while (1) {
2784 idx++;
2785 if (idx >= ht->nNumUsed) {
2786 *pos = ht->nNumUsed;
2787 return SUCCESS;
2788 }
2789 if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2790 *pos = idx;
2791 return SUCCESS;
2792 }
2793 }
2794 } else {
2795 while (1) {
2796 idx++;
2797 if (idx >= ht->nNumUsed) {
2798 *pos = ht->nNumUsed;
2799 return SUCCESS;
2800 }
2801 if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2802 *pos = idx;
2803 return SUCCESS;
2804 }
2805 }
2806 }
2807 } else {
2808 return FAILURE;
2809 }
2810}
2811
2813{
2814 uint32_t idx = *pos;
2815
2817 HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2818
2819 if (idx < ht->nNumUsed) {
2820 if (HT_IS_PACKED(ht)) {
2821 while (idx > 0) {
2822 idx--;
2823 if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2824 *pos = idx;
2825 return SUCCESS;
2826 }
2827 }
2828 } else {
2829 while (idx > 0) {
2830 idx--;
2831 if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2832 *pos = idx;
2833 return SUCCESS;
2834 }
2835 }
2836 }
2837 *pos = ht->nNumUsed;
2838 return SUCCESS;
2839 } else {
2840 return FAILURE;
2841 }
2842}
2843
2844
2845/* This function should be made binary safe */
2847{
2848 uint32_t idx;
2849 Bucket *p;
2850
2852 idx = _zend_hash_get_valid_pos(ht, *pos);
2853 if (idx < ht->nNumUsed) {
2854 if (HT_IS_PACKED(ht)) {
2855 *num_index = idx;
2856 return HASH_KEY_IS_LONG;
2857 }
2858 p = ht->arData + idx;
2859 if (p->key) {
2860 *str_index = p->key;
2861 return HASH_KEY_IS_STRING;
2862 } else {
2863 *num_index = p->h;
2864 return HASH_KEY_IS_LONG;
2865 }
2866 }
2867 return HASH_KEY_NON_EXISTENT;
2868}
2869
2871{
2872 uint32_t idx;
2873 Bucket *p;
2874
2876 idx = _zend_hash_get_valid_pos(ht, *pos);
2877 if (idx >= ht->nNumUsed) {
2878 ZVAL_NULL(key);
2879 } else {
2880 if (HT_IS_PACKED(ht)) {
2881 ZVAL_LONG(key, idx);
2882 return;
2883 }
2884 p = ht->arData + idx;
2885 if (p->key) {
2886 ZVAL_STR_COPY(key, p->key);
2887 } else {
2888 ZVAL_LONG(key, p->h);
2889 }
2890 }
2891}
2892
2894{
2895 uint32_t idx;
2896 Bucket *p;
2897
2899 idx = _zend_hash_get_valid_pos(ht, *pos);
2900 if (idx < ht->nNumUsed) {
2901 if (HT_IS_PACKED(ht)) {
2902 return HASH_KEY_IS_LONG;
2903 }
2904 p = ht->arData + idx;
2905 if (p->key) {
2906 return HASH_KEY_IS_STRING;
2907 } else {
2908 return HASH_KEY_IS_LONG;
2909 }
2910 }
2911 return HASH_KEY_NON_EXISTENT;
2912}
2913
2914
2916{
2917 uint32_t idx;
2918 Bucket *p;
2919
2921 idx = _zend_hash_get_valid_pos(ht, *pos);
2922 if (idx < ht->nNumUsed) {
2923 if (HT_IS_PACKED(ht)) {
2924 return &ht->arPacked[idx];
2925 }
2926 p = ht->arData + idx;
2927 return &p->val;
2928 } else {
2929 return NULL;
2930 }
2931}
2932
2934{
2935 zval val;
2936 zend_ulong h;
2938
2939 val = p->val;
2940 h = p->h;
2941 key = p->key;
2942
2943 p->val = q->val;
2944 p->h = q->h;
2945 p->key = q->key;
2946
2947 q->val = val;
2948 q->h = h;
2949 q->key = key;
2950}
2951
2953{
2954 zval val;
2955
2956 val = p->val;
2957 p->val = q->val;
2958 q->val = val;
2959}
2960
2962{
2963 zval val;
2964 zend_ulong h;
2965
2966 val = p->val;
2967 h = p->h;
2968
2969 p->val = q->val;
2970 p->h = q->h;
2971
2972 q->val = val;
2973 q->h = h;
2974}
2975
2976static void zend_hash_sort_internal(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
2977{
2978 Bucket *p;
2979 uint32_t i, j;
2980
2982
2983 if (!(ht->nNumOfElements>1) && !(renumber && ht->nNumOfElements>0)) {
2984 /* Doesn't require sorting */
2985 return;
2986 }
2987
2988 if (HT_IS_PACKED(ht)) {
2989 zend_hash_packed_to_hash(ht); // TODO: ???
2990 }
2991
2992 if (HT_IS_WITHOUT_HOLES(ht)) {
2993 /* Store original order of elements in extra space to allow stable sorting. */
2994 for (i = 0; i < ht->nNumUsed; i++) {
2995 Z_EXTRA(ht->arData[i].val) = i;
2996 }
2997 } else {
2998 /* Remove holes and store original order. */
2999 for (j = 0, i = 0; j < ht->nNumUsed; j++) {
3000 p = ht->arData + j;
3001 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
3002 if (i != j) {
3003 ht->arData[i] = *p;
3004 }
3005 Z_EXTRA(ht->arData[i].val) = i;
3006 i++;
3007 }
3008 ht->nNumUsed = i;
3009 }
3010
3011 if (!HT_IS_PACKED(ht)) {
3012 /* We broke the hash collisions chains overriding Z_NEXT() by Z_EXTRA().
3013 * Reset the hash headers table as well to avoid possible inconsistent
3014 * access on recursive data structures.
3015 *
3016 * See Zend/tests/bug63882_2.phpt
3017 */
3019 }
3020
3021 sort((void *)ht->arData, ht->nNumUsed, sizeof(Bucket), (compare_func_t) compar,
3024
3025 ht->nInternalPointer = 0;
3026
3027 if (renumber) {
3028 for (j = 0; j < i; j++) {
3029 p = ht->arData + j;
3030 p->h = j;
3031 if (p->key) {
3032 zend_string_release(p->key);
3033 p->key = NULL;
3034 }
3035 }
3036
3037 ht->nNextFreeElement = i;
3038 }
3039 if (HT_IS_PACKED(ht)) {
3040 if (!renumber) {
3042 }
3043 } else {
3044 if (renumber) {
3045 void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
3046 Bucket *old_buckets = ht->arData;
3047 zval *zv;
3048
3049 new_data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), (GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
3051 ht->nTableMask = HT_MIN_MASK;
3052 HT_SET_DATA_ADDR(ht, new_data);
3053 p = old_buckets;
3054 zv = ht->arPacked;
3055 for (i = 0; i < ht->nTableSize; i++) {
3056 ZVAL_COPY_VALUE(zv, &p->val);
3057 zv++;
3058 p++;
3059 }
3060 pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
3062 } else {
3064 }
3065 }
3066}
3067
3069{
3071 zend_hash_sort_internal(ht, sort, compar, renumber);
3072}
3073
3075{
3077
3078 /* Unpack the array early to avoid RCn assertion failures. */
3079 if (HT_IS_PACKED(ht)) {
3081 }
3082
3083 /* Adding a refcount prevents the array from going away. */
3084 GC_ADDREF(ht);
3085
3086 zend_hash_sort_internal(ht, sort, compar, renumber);
3087
3088 if (UNEXPECTED(GC_DELREF(ht) == 0)) {
3090 } else {
3091 gc_check_possible_root((zend_refcounted *)ht);
3092 }
3093}
3094
3095static zend_always_inline int zend_hash_compare_impl(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered) {
3096 uint32_t idx1, idx2;
3097 zend_string *key1, *key2;
3098 zend_ulong h1, h2;
3099 zval *pData1, *pData2;;
3100 int result;
3101
3102 if (ht1->nNumOfElements != ht2->nNumOfElements) {
3103 return ht1->nNumOfElements > ht2->nNumOfElements ? 1 : -1;
3104 }
3105
3106 for (idx1 = 0, idx2 = 0; idx1 < ht1->nNumUsed; idx1++) {
3107 if (HT_IS_PACKED(ht1)) {
3108 pData1 = ht1->arPacked + idx1;
3109 h1 = idx1;
3110 key1 = NULL;
3111 } else {
3112 Bucket *p = ht1->arData + idx1;
3113 pData1 = &p->val;
3114 h1 = p->h;
3115 key1 = p->key;
3116 }
3117
3118 if (Z_TYPE_P(pData1) == IS_UNDEF) continue;
3119 if (ordered) {
3120 if (HT_IS_PACKED(ht2)) {
3121 while (1) {
3122 ZEND_ASSERT(idx2 != ht2->nNumUsed);
3123 pData2 = ht2->arPacked + idx2;
3124 h2 = idx2;
3125 key2 = NULL;
3126 if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3127 idx2++;
3128 }
3129 } else {
3130 while (1) {
3131 Bucket *p;
3132 ZEND_ASSERT(idx2 != ht2->nNumUsed);
3133 p = ht2->arData + idx2;
3134 pData2 = &p->val;
3135 h2 = p->h;
3136 key2 = p->key;
3137 if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3138 idx2++;
3139 }
3140 }
3141 if (key1 == NULL && key2 == NULL) { /* numeric indices */
3142 if (h1 != h2) {
3143 return h1 > h2 ? 1 : -1;
3144 }
3145 } else if (key1 != NULL && key2 != NULL) { /* string indices */
3146 if (ZSTR_LEN(key1) != ZSTR_LEN(key2)) {
3147 return ZSTR_LEN(key1) > ZSTR_LEN(key2) ? 1 : -1;
3148 }
3149
3150 result = memcmp(ZSTR_VAL(key1), ZSTR_VAL(key2), ZSTR_LEN(key1));
3151 if (result != 0) {
3152 return result;
3153 }
3154 } else {
3155 /* Mixed key types: A string key is considered as larger */
3156 return key1 != NULL ? 1 : -1;
3157 }
3158 idx2++;
3159 } else {
3160 if (key1 == NULL) { /* numeric index */
3161 pData2 = zend_hash_index_find(ht2, h1);
3162 if (pData2 == NULL) {
3163 return 1;
3164 }
3165 } else { /* string index */
3166 pData2 = zend_hash_find(ht2, key1);
3167 if (pData2 == NULL) {
3168 return 1;
3169 }
3170 }
3171 }
3172
3173 if (Z_TYPE_P(pData1) == IS_INDIRECT) {
3174 pData1 = Z_INDIRECT_P(pData1);
3175 }
3176 if (Z_TYPE_P(pData2) == IS_INDIRECT) {
3177 pData2 = Z_INDIRECT_P(pData2);
3178 }
3179
3180 if (Z_TYPE_P(pData1) == IS_UNDEF) {
3181 if (Z_TYPE_P(pData2) != IS_UNDEF) {
3182 return -1;
3183 }
3184 } else if (Z_TYPE_P(pData2) == IS_UNDEF) {
3185 return 1;
3186 } else {
3187 result = compar(pData1, pData2);
3188 if (result != 0) {
3189 return result;
3190 }
3191 }
3192 }
3193
3194 return 0;
3195}
3196
3197ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered)
3198{
3199 int result;
3200 IS_CONSISTENT(ht1);
3201 IS_CONSISTENT(ht2);
3202
3203 if (ht1 == ht2) {
3204 return 0;
3205 }
3206
3207 /* It's enough to protect only one of the arrays.
3208 * The second one may be referenced from the first and this may cause
3209 * false recursion detection.
3210 */
3211 if (UNEXPECTED(GC_IS_RECURSIVE(ht1))) {
3212 zend_throw_error(NULL, "Nesting level too deep - recursive dependency?");
3213 return ZEND_UNCOMPARABLE;
3214 }
3215
3217 result = zend_hash_compare_impl(ht1, ht2, compar, ordered);
3219
3220 return result;
3221}
3222
3223
3225{
3226 uint32_t idx;
3227 zval *res;
3228
3230
3231 if (ht->nNumOfElements == 0 ) {
3232 return NULL;
3233 }
3234
3235 if (HT_IS_PACKED(ht)) {
3236 zval *zv;
3237
3238 idx = 0;
3239 while (1) {
3240 if (idx == ht->nNumUsed) {
3241 return NULL;
3242 }
3243 if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) break;
3244 idx++;
3245 }
3246 res = ht->arPacked + idx;
3247 for (; idx < ht->nNumUsed; idx++) {
3248 zv = ht->arPacked + idx;
3249 if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
3250
3251 if (flag) {
3252 if (compar(res, zv) < 0) { /* max */
3253 res = zv;
3254 }
3255 } else {
3256 if (compar(res, zv) > 0) { /* min */
3257 res = zv;
3258 }
3259 }
3260 }
3261 } else {
3262 Bucket *p;
3263
3264 idx = 0;
3265 while (1) {
3266 if (idx == ht->nNumUsed) {
3267 return NULL;
3268 }
3269 if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) break;
3270 idx++;
3271 }
3272 res = &ht->arData[idx].val;
3273 for (; idx < ht->nNumUsed; idx++) {
3274 p = ht->arData + idx;
3275 if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
3276
3277 if (flag) {
3278 if (compar(res, &p->val) < 0) { /* max */
3279 res = &p->val;
3280 }
3281 } else {
3282 if (compar(res, &p->val) > 0) { /* min */
3283 res = &p->val;
3284 }
3285 }
3286 }
3287 }
3288 return res;
3289}
3290
3292{
3293 const char *tmp = key;
3294
3295 const char *end = key + length;
3296
3297 if (*tmp == '-') {
3298 tmp++;
3299 }
3300
3301 if ((*tmp == '0' && length > 1) /* numbers with leading zeros */
3302 || (end - tmp > MAX_LENGTH_OF_LONG - 1) /* number too long */
3303 || (SIZEOF_ZEND_LONG == 4 &&
3304 end - tmp == MAX_LENGTH_OF_LONG - 1 &&
3305 *tmp > '2')) { /* overflow */
3306 return 0;
3307 }
3308 *idx = (*tmp - '0');
3309 while (1) {
3310 ++tmp;
3311 if (tmp == end) {
3312 if (*key == '-') {
3313 if (*idx-1 > ZEND_LONG_MAX) { /* overflow */
3314 return 0;
3315 }
3316 *idx = 0 - *idx;
3317 } else if (*idx > ZEND_LONG_MAX) { /* overflow */
3318 return 0;
3319 }
3320 return 1;
3321 }
3322 if (*tmp <= '9' && *tmp >= '0') {
3323 *idx = (*idx * 10) + (*tmp - '0');
3324 } else {
3325 return 0;
3326 }
3327 }
3328}
3329
3330/* Takes a "symtable" hashtable (contains integer and non-numeric string keys)
3331 * and converts it to a "proptable" (contains only string keys).
3332 * If the symtable didn't need duplicating, its refcount is incremented.
3333 */
3335{
3336 zend_ulong num_key;
3337 zend_string *str_key;
3338 zval *zv;
3339
3340 if (UNEXPECTED(HT_IS_PACKED(ht))) {
3341 goto convert;
3342 }
3343
3345 if (!str_key) {
3346 goto convert;
3347 }
3349
3350 if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
3351 GC_ADDREF(ht);
3352 }
3353
3354 return ht;
3355
3356convert:
3357 {
3358 HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3359
3360 ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
3361 if (!str_key) {
3362 str_key = zend_long_to_str(num_key);
3363 zend_string_delref(str_key);
3364 }
3365 do {
3366 if (Z_OPT_REFCOUNTED_P(zv)) {
3367 if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
3368 zv = Z_REFVAL_P(zv);
3369 if (!Z_OPT_REFCOUNTED_P(zv)) {
3370 break;
3371 }
3372 }
3373 Z_ADDREF_P(zv);
3374 }
3375 } while (0);
3376 zend_hash_update(new_ht, str_key, zv);
3378
3379 return new_ht;
3380 }
3381}
3382
3383/* Takes a "proptable" hashtable (contains only string keys) and converts it to
3384 * a "symtable" (contains integer and non-numeric string keys).
3385 * If the proptable didn't need duplicating, its refcount is incremented.
3386 */
3388{
3389 zend_ulong num_key;
3390 zend_string *str_key;
3391 zval *zv;
3392
3393 if (!HT_IS_PACKED(ht)) {
3395 /* The `str_key &&` here might seem redundant: property tables should
3396 * only have string keys. Unfortunately, this isn't true, at the very
3397 * least because of ArrayObject, which stores a symtable where the
3398 * property table should be.
3399 */
3400 if (str_key && ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3401 goto convert;
3402 }
3404 }
3405
3406 if (always_duplicate) {
3407 return zend_array_dup(ht);
3408 }
3409
3411 GC_ADDREF(ht);
3412 }
3413
3414 return ht;
3415
3416convert:
3417 {
3418 HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3419
3420 ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, num_key, str_key, zv) {
3421 do {
3422 if (Z_OPT_REFCOUNTED_P(zv)) {
3423 if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
3424 zv = Z_REFVAL_P(zv);
3425 if (!Z_OPT_REFCOUNTED_P(zv)) {
3426 break;
3427 }
3428 }
3429 Z_ADDREF_P(zv);
3430 }
3431 } while (0);
3432 /* Again, thank ArrayObject for `!str_key ||`. */
3433 if (!str_key || ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3434 zend_hash_index_update(new_ht, num_key, zv);
3435 } else {
3436 zend_hash_update(new_ht, str_key, zv);
3437 }
3439
3440 return new_ht;
3441 }
3442}
size_t len
Definition apprentice.c:174
file(string $filename, int $flags=0, $context=null)
prev(array|object &$array)
char s[4]
Definition cdf.c:77
uint32_t u
Definition cdf.c:78
#define max(a, b)
Definition exif.c:60
zval * zv
Definition ffi.c:3975
zend_string * res
Definition ffi.c:4692
memcpy(ptr1, ptr2, size)
memset(ptr, 0, type->size)
zval * val
Definition ffi.c:4262
HashTable * ht
Definition ffi.c:4838
buf start
Definition ffi.c:4687
ffi persistent
Definition ffi.c:3633
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
again j
int line
Definition php_ffi.h:54
unsigned const char * end
Definition php_ffi.h:51
unsigned const char * pos
Definition php_ffi.h:52
#define abort()
unsigned char key[REFLECTION_KEY_LEN]
#define zend_hash_str_add(...)
Definition phpdbg.h:77
zend_constant * data
p
Definition session.c:1105
zend_ulong h
Definition zend_types.h:382
zend_string * key
Definition zend_types.h:383
zval val
Definition zend_types.h:381
HashPosition pos
Definition zend_types.h:552
zend_long nNextFreeElement
Definition zend_types.h:410
uint32_t nNumUsed
Definition zend_types.h:406
uint32_t nInternalPointer
Definition zend_types.h:409
uint32_t nTableSize
Definition zend_types.h:408
Bucket * arData
Definition zend_types.h:403
zval * arPacked
Definition zend_types.h:404
uint32_t nTableMask
Definition zend_types.h:400
uint32_t nNumOfElements
Definition zend_types.h:407
zend_ulong h
Definition zend_hash.h:95
zend_string * key
Definition zend_hash.h:96
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format,...)
Definition zend.c:1703
ZEND_API ZEND_COLD void zend_output_debug_string(bool trigger_break, const char *format,...)
Definition zend.c:1861
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
#define perealloc2(ptr, size, copy_size, persistent)
Definition zend_alloc.h:202
#define efree(ptr)
Definition zend_alloc.h:155
#define pefree(ptr, persistent)
Definition zend_alloc.h:191
#define pemalloc(size, persistent)
Definition zend_alloc.h:189
#define FREE_HASHTABLE(ht)
Definition zend_alloc.h:234
#define erealloc(ptr, size)
Definition zend_alloc.h:159
#define ALLOC_HASHTABLE(ht)
Definition zend_alloc.h:231
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
uint32_t num_args
zend_string_release_ex(func->internal_function.function_name, 0)
zval * args
#define ZEND_API
#define E_ERROR
Definition zend_errors.h:23
ZEND_API void(ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data)
#define GC_REMOVE_FROM_BUFFER(p)
Definition zend_gc.h:77
#define EG(v)
#define HT_ASSERT(ht, expr)
Definition zend_hash.c:41
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q)
Definition zend_hash.c:2952
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2744
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func)
Definition zend_hash.c:2059
ZEND_API HashTable *ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate)
Definition zend_hash.c:3387
ZEND_API HashTable *ZEND_FASTCALL _zend_new_array_0(void)
Definition zend_hash.c:282
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q)
Definition zend_hash.c:2933
ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht)
Definition zend_hash.c:346
ZEND_API HashTable *ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht)
Definition zend_hash.c:3334
ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func)
Definition zend_hash.c:2192
ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht)
Definition zend_hash.c:330
ZEND_API zval *ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h)
Definition zend_hash.c:1059
ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int num_args,...)
Definition zend_hash.c:2137
ZEND_API HashTable * zend_array_to_list(HashTable *source)
Definition zend_hash.c:2526
ZEND_API uint32_t zend_array_count(HashTable *ht)
Definition zend_hash.c:476
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:2689
ZEND_API zval *ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *b, zend_string *key)
Definition zend_hash.c:1239
ZEND_API zend_result ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2773
#define HT_POISONED_PTR
Definition zend_hash.c:46
ZEND_API zval *ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
Definition zend_hash.c:1038
ZEND_API zval *ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, uint32_t flag)
Definition zend_hash.c:1017
ZEND_API HashTable *ZEND_FASTCALL _zend_new_array(uint32_t nSize)
Definition zend_hash.c:289
ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht)
Definition zend_hash.c:1328
ZEND_API zval *ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag)
Definition zend_hash.c:3224
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData)
Definition zend_hash.c:1229
ZEND_API zval *ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1209
ZEND_API void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht)
Definition zend_hash.c:311
ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos)
Definition zend_hash.c:537
ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, const HashPosition *pos)
Definition zend_hash.c:2846
ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q)
Definition zend_hash.c:2961
ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht)
Definition zend_hash.c:1946
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
Definition zend_hash.c:1224
ZEND_API void * zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:90
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2733
ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, bool packed)
Definition zend_hash.c:322
ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(HashTable *ht, HashPosition step)
Definition zend_hash.c:724
ZEND_API zval *ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h)
Definition zend_hash.c:1234
ZEND_API zval *ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2915
ZEND_API zval *ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1214
ZEND_API zval *ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key)
Definition zend_hash.c:1012
ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht)
Definition zend_hash.c:1986
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument)
Definition zend_hash.c:2099
ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
Definition zend_hash.c:277
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
Definition zend_hash.c:1869
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered)
Definition zend_hash.c:3197
ZEND_API void ZEND_FASTCALL zend_hash_packed_del_val(HashTable *ht, zval *zv)
Definition zend_hash.c:1517
ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite)
Definition zend_hash.c:2547
ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p)
Definition zend_hash.c:1526
ZEND_API zval *ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:1007
ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h)
Definition zend_hash.c:1692
ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2893
ZEND_API zval *ZEND_FASTCALL zend_hash_find_known_hash(const HashTable *ht, const zend_string *key)
Definition zend_hash.c:2679
ZEND_API zval *ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:1002
ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht)
Definition zend_hash.c:2015
ZEND_API zend_result ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos)
Definition zend_hash.c:2812
ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, const HashPosition *pos)
Definition zend_hash.c:2870
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht)
Definition zend_hash.c:608
#define ZEND_HASH_IF_FULL_DO_RESIZE(ht)
Definition zend_hash.c:85
ZEND_API zval *ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
Definition zend_hash.c:1219
ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht)
Definition zend_hash.c:374
ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, bool packed)
Definition zend_hash.c:396
ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key)
Definition zend_hash.c:1566
ZEND_API zval *ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:1075
ZEND_API bool ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx)
Definition zend_hash.c:3291
ZEND_API zval *ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
Definition zend_hash.c:1031
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
Definition zend_hash.c:2240
#define SET_INCONSISTENT(n)
Definition zend_hash.c:82
ZEND_API HashTable *ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2)
Definition zend_hash.c:296
ZEND_API zval *ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag)
Definition zend_hash.c:978
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
Definition zend_hash.c:1808
#define HT_ASSERT_RC1(ht)
Definition zend_hash.c:44
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array)
Definition zend_hash.c:627
ZEND_API zval *ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData)
Definition zend_hash.c:1052
void ZEND_FASTCALL zend_array_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
Definition zend_hash.c:3074
ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam)
Definition zend_hash.c:2643
ZEND_API HashTable *ZEND_FASTCALL zend_array_dup(HashTable *source)
Definition zend_hash.c:2438
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos_ex(const HashTable *ht, HashPosition pos)
Definition zend_hash.c:517
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:997
ZEND_API void * zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key)
Definition zend_hash.c:104
ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht)
Definition zend_hash.c:338
ZEND_API zval *ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key)
Definition zend_hash.c:1067
ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
Definition zend_hash.c:1534
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:1661
ZEND_API zval *ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
Definition zend_hash.c:2668
ZEND_API zval *ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2722
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:1616
ZEND_API zval *ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key, zval *pData)
Definition zend_hash.c:992
ZEND_API zval *ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2701
ZEND_API const HashTable zend_empty_array
Definition zend_hash.c:248
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start)
Definition zend_hash.c:694
#define IS_CONSISTENT(a)
Definition zend_hash.c:81
ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed)
Definition zend_hash.c:435
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
Definition zend_hash.c:3068
ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx)
Definition zend_hash.c:649
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht)
Definition zend_hash.c:512
ZEND_API zval *ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag)
Definition zend_hash.c:1191
ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
Definition zend_hash.c:711
#define ZEND_HASH_FILL_ADD(_val)
Definition zend_hash.h:1570
int(* apply_func_arg_t)(zval *pDest, void *argument)
Definition zend_hash.h:151
#define ZEND_HASH_MAP_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1310
#define HASH_KEY_NON_EXISTENT
Definition zend_hash.h:31
#define HASH_LOOKUP
Definition zend_hash.h:38
struct _zend_hash_key zend_hash_key
#define ZEND_HASH_APPLY_STOP
Definition zend_hash.h:148
#define HASH_KEY_IS_STRING
Definition zend_hash.h:29
#define HT_IS_WITHOUT_HOLES(ht)
Definition zend_hash.h:62
#define ZEND_HASH_FILL_PACKED(ht)
Definition zend_hash.h:1528
bool(* merge_checker_func_t)(HashTable *target_ht, zval *source_data, zend_hash_key *hash_key, void *pParam)
Definition zend_hash.h:99
int(* apply_func_args_t)(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key)
Definition zend_hash.h:152
int(* bucket_compare_func_t)(Bucket *a, Bucket *b)
Definition zend_hash.h:299
#define ZEND_HASH_APPLY_REMOVE
Definition zend_hash.h:147
#define ZEND_HASH_FILL_END()
Definition zend_hash.h:1582
#define HT_DEC_ITERATORS_COUNT(ht)
Definition zend_hash.h:82
#define HASH_FLAG_STATIC_KEYS
Definition zend_hash.h:43
#define HASH_ADD_NEW
Definition zend_hash.h:36
#define HT_HAS_STATIC_KEYS_ONLY(ht)
Definition zend_hash.h:65
#define HT_IS_PACKED(ht)
Definition zend_hash.h:59
#define HT_ITERATORS_COUNT(ht)
Definition zend_hash.h:74
#define HT_HAS_ITERATORS(ht)
Definition zend_hash.h:76
#define HT_INC_ITERATORS_COUNT(ht)
Definition zend_hash.h:80
#define zend_new_array(size)
Definition zend_hash.h:338
#define HASH_FLAG_PACKED
Definition zend_hash.h:41
#define ZEND_HANDLE_NUMERIC(key, idx)
Definition zend_hash.h:420
#define ZEND_HASH_FOREACH_KEY_VAL(ht, _h, _key, _val)
Definition zend_hash.h:1181
#define HASH_FLAG_UNINITIALIZED
Definition zend_hash.h:42
#define ZEND_HASH_MAP_FOREACH_STR_KEY(ht, _key)
Definition zend_hash.h:1346
#define HASH_FLAG_MASK
Definition zend_hash.h:48
#define HASH_ADD
Definition zend_hash.h:34
#define HT_ITERATORS_OVERFLOW(ht)
Definition zend_hash.h:75
int(* apply_func_t)(zval *pDest)
Definition zend_hash.h:150
#define HT_FLAGS(ht)
Definition zend_hash.h:50
#define HASH_UPDATE_INDIRECT
Definition zend_hash.h:35
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define HASH_FLAG_CONSISTENCY
Definition zend_hash.h:40
#define HASH_ADD_NEXT
Definition zend_hash.h:37
#define HASH_FLAG_HAS_EMPTY_IND
Definition zend_hash.h:44
#define ZEND_HASH_FOREACH_VAL(ht, _val)
Definition zend_hash.h:1102
#define HASH_KEY_IS_LONG
Definition zend_hash.h:30
#define ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, _h, _key, _val)
Definition zend_hash.h:1411
#define HASH_UPDATE
Definition zend_hash.h:33
int32_t zend_long
Definition zend_long.h:42
#define MAX_LENGTH_OF_LONG
Definition zend_long.h:109
#define ZEND_LONG_MIN
Definition zend_long.h:46
uint32_t zend_ulong
Definition zend_long.h:43
#define SIZEOF_ZEND_LONG
Definition zend_long.h:50
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_string zend_string
ZEND_API zend_string *ZEND_FASTCALL zend_long_to_str(zend_long num)
ZEND_API char *ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length)
#define ZEND_UNCOMPARABLE
#define ALLOCA_FLAG(name)
#define zend_never_inline
#define MIN(a, b)
#define EXPECTED(condition)
#define do_alloca(p, use_heap)
#define zend_always_inline
#define ZEND_FASTCALL
#define ZEND_ASSERT(c)
#define ZEND_UNREACHABLE()
#define free_alloca(p, use_heap)
#define UNEXPECTED(condition)
ZEND_API zend_ulong ZEND_FASTCALL zend_hash_func(const char *str, size_t len)
Definition zend_string.c:60
#define ZSTR_H(zstr)
Definition zend_string.h:70
#define ZSTR_IS_INTERNED(s)
Definition zend_string.h:84
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
int(* compare_func_t)(const void *, const void *)
Definition zend_types.h:104
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define HT_MIN_SIZE
Definition zend_types.h:432
#define HT_PACKED_SIZE_EX(nTableSize, nTableMask)
Definition zend_types.h:480
#define HT_HASH(ht, idx)
Definition zend_types.h:463
#define Z_ISREF_P(zval_p)
Definition zend_types.h:954
#define Z_TRY_ADDREF_P(pz)
#define ZVAL_UNDEF(z)
#define GC_SET_REFCOUNT(p, rc)
Definition zend_types.h:708
#define Z_REFVAL_P(zval_p)
#define IS_UNDEF
Definition zend_types.h:600
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_NULL(z)
#define HT_USED_SIZE(ht)
Definition zend_types.h:476
#define GC_NULL
Definition zend_types.h:784
#define ZVAL_LONG(z, l)
#define Z_OPT_REFCOUNTED_P(zval_p)
Definition zend_types.h:944
#define HT_HASH_TO_BUCKET(ht, idx)
Definition zend_types.h:539
#define ZVAL_STR_COPY(z, s)
struct _zend_array HashTable
Definition zend_types.h:386
#define HT_INVALID_IDX
Definition zend_types.h:429
#define IS_ARRAY
Definition zend_types.h:607
void(* sort_func_t)(void *, size_t, size_t, compare_func_t, swap_func_t)
Definition zend_types.h:106
#define Z_TYPE_INFO_P(zval_p)
Definition zend_types.h:669
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define HT_SIZE(ht)
Definition zend_types.h:474
#define Z_NEXT(zval)
Definition zend_types.h:671
void(* dtor_func_t)(zval *pDest)
Definition zend_types.h:107
#define GC_DELREF(p)
Definition zend_types.h:710
#define GC_FLAGS(p)
Definition zend_types.h:756
#define GC_ADDREF(p)
Definition zend_types.h:709
#define GC_MAKE_PERSISTENT_LOCAL(p)
#define Z_ADDREF_P(pz)
#define HT_PACKED_SIZE(ht)
Definition zend_types.h:482
#define GC_TRY_UNPROTECT_RECURSION(p)
Definition zend_types.h:880
@ FAILURE
Definition zend_types.h:61
#define HT_SIZE_EX(nTableSize, nTableMask)
Definition zend_types.h:472
#define GC_TRY_PROTECT_RECURSION(p)
Definition zend_types.h:876
#define Z_EXTRA(zval)
Definition zend_types.h:695
void(* copy_ctor_func_t)(zval *pElement)
Definition zend_types.h:108
#define GC_PERSISTENT_LOCAL
Definition zend_types.h:782
#define HT_HASH_RESET(ht)
Definition zend_types.h:532
uint32_t HashPosition
Definition zend_types.h:548
#define HT_GET_DATA_ADDR(ht)
Definition zend_types.h:545
struct _zend_refcounted zend_refcounted
Definition zend_types.h:95
#define HT_SIZE_TO_MASK(nTableSize)
Definition zend_types.h:466
#define Z_TYPE_INFO(zval)
Definition zend_types.h:668
#define GC_ARRAY
Definition zend_types.h:786
struct _Bucket Bucket
void(* swap_func_t)(void *, void *)
Definition zend_types.h:105
#define HT_HASH_EX(data, idx)
Definition zend_types.h:461
#define Z_INDIRECT_P(zval_p)
#define HT_SET_DATA_ADDR(ht, ptr)
Definition zend_types.h:542
#define Z_REFCOUNT_P(pz)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
#define IS_ARRAY_IMMUTABLE
Definition zend_types.h:823
struct _HashTableIterator HashTableIterator
#define IS_ARRAY_PERSISTENT
Definition zend_types.h:824
#define GC_NOT_COLLECTABLE
Definition zend_types.h:778
#define SEPARATE_ARRAY(zv)
#define GC_PERSISTENT
Definition zend_types.h:781
#define HT_HASH_RESET_PACKED(ht)
Definition zend_types.h:535
#define GC_REFCOUNT(p)
Definition zend_types.h:707
#define GC_IS_RECURSIVE(p)
Definition zend_types.h:865
#define Z_TYPE(zval)
Definition zend_types.h:659
#define IS_INDIRECT
Definition zend_types.h:623
#define Z_INDIRECT(zval)
#define HT_PACKED_USED_SIZE(ht)
Definition zend_types.h:484
#define HT_MIN_MASK
Definition zend_types.h:431
#define ZVAL_COPY_VALUE(z, v)
#define GC_IMMUTABLE
Definition zend_types.h:780
#define GC_FLAGS_SHIFT
Definition zend_types.h:739
#define GC_TYPE_INFO(p)
Definition zend_types.h:754
#define ZVAL_PTR_DTOR
bool result