1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-01-02 12:57:58 +08:00
|
|
|
| Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:16:21 +08:00
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
2015-01-03 17:22:58 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2001-12-11 23:16:21 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
1999-07-16 22:58:16 +08:00
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
2015-08-31 16:38:16 +08:00
|
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-01 09:49:15 +08:00
|
|
|
/* $Id$ */
|
1999-07-16 22:58:16 +08:00
|
|
|
|
1999-09-07 00:14:08 +08:00
|
|
|
#include "zend.h"
|
2010-04-20 19:05:54 +08:00
|
|
|
#include "zend_globals.h"
|
2014-11-25 17:58:29 +08:00
|
|
|
#include "zend_variables.h"
|
1999-09-07 00:14:08 +08:00
|
|
|
|
2018-05-04 07:41:35 +08:00
|
|
|
#ifdef __SSE2__
|
|
|
|
# include <mmintrin.h>
|
|
|
|
# include <emmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-05 20:49:08 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
# define HT_ASSERT(ht, expr) \
|
2018-01-22 18:36:15 +08:00
|
|
|
ZEND_ASSERT((expr) || (HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION))
|
2015-02-14 03:20:39 +08:00
|
|
|
#else
|
2016-05-05 20:49:08 +08:00
|
|
|
# define HT_ASSERT(ht, expr)
|
2015-06-01 10:37:56 +08:00
|
|
|
#endif
|
2015-02-14 03:20:39 +08:00
|
|
|
|
2016-05-05 20:49:08 +08:00
|
|
|
#define HT_ASSERT_RC1(ht) HT_ASSERT(ht, GC_REFCOUNT(ht) == 1)
|
|
|
|
|
2015-12-18 05:11:08 +08:00
|
|
|
#define HT_POISONED_PTR ((HashTable *) (intptr_t) -1)
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
#if ZEND_DEBUG
|
2016-10-20 04:10:42 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
#define HT_OK 0x00
|
2018-01-22 20:32:34 +08:00
|
|
|
#define HT_IS_DESTROYING 0x01
|
|
|
|
#define HT_DESTROYED 0x02
|
|
|
|
#define HT_CLEANING 0x03
|
2000-01-18 03:17:58 +08:00
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
static void _zend_is_inconsistent(const HashTable *ht, const char *file, int line)
|
2000-02-20 03:21:45 +08:00
|
|
|
{
|
2018-01-22 20:32:34 +08:00
|
|
|
if ((HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) == HT_OK) {
|
2000-06-03 09:49:49 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-01-22 20:32:34 +08:00
|
|
|
switch (HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) {
|
2000-06-03 09:49:49 +08:00
|
|
|
case HT_IS_DESTROYING:
|
2003-08-29 15:34:37 +08:00
|
|
|
zend_output_debug_string(1, "%s(%d) : ht=%p is being destroyed", file, line, ht);
|
2000-06-03 09:49:49 +08:00
|
|
|
break;
|
|
|
|
case HT_DESTROYED:
|
2003-08-29 15:34:37 +08:00
|
|
|
zend_output_debug_string(1, "%s(%d) : ht=%p is already destroyed", file, line, ht);
|
2000-06-03 09:49:49 +08:00
|
|
|
break;
|
|
|
|
case HT_CLEANING:
|
2003-08-29 15:34:37 +08:00
|
|
|
zend_output_debug_string(1, "%s(%d) : ht=%p is being cleaned", file, line, ht);
|
2000-06-03 09:49:49 +08:00
|
|
|
break;
|
2010-09-14 22:26:37 +08:00
|
|
|
default:
|
|
|
|
zend_output_debug_string(1, "%s(%d) : ht=%p is inconsistent", file, line, ht);
|
|
|
|
break;
|
2002-07-24 03:29:02 +08:00
|
|
|
}
|
2000-06-03 09:49:49 +08:00
|
|
|
zend_bailout();
|
2000-01-15 21:40:17 +08:00
|
|
|
}
|
2001-08-11 23:56:40 +08:00
|
|
|
#define IS_CONSISTENT(a) _zend_is_inconsistent(a, __FILE__, __LINE__);
|
2014-02-10 14:04:30 +08:00
|
|
|
#define SET_INCONSISTENT(n) do { \
|
2018-01-22 20:32:34 +08:00
|
|
|
HT_FLAGS(ht) = (HT_FLAGS(ht) & ~HASH_FLAG_CONSISTENCY) | (n); \
|
2014-02-10 14:04:30 +08:00
|
|
|
} while (0)
|
2000-01-15 21:40:17 +08:00
|
|
|
#else
|
|
|
|
#define IS_CONSISTENT(a)
|
2000-01-16 17:45:10 +08:00
|
|
|
#define SET_INCONSISTENT(n)
|
2000-01-15 21:40:17 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-13 21:31:29 +08:00
|
|
|
#define ZEND_HASH_IF_FULL_DO_RESIZE(ht) \
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((ht)->nNumUsed >= (ht)->nTableSize) { \
|
2000-02-13 21:31:29 +08:00
|
|
|
zend_hash_do_resize(ht); \
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2016-04-29 19:44:56 +08:00
|
|
|
static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2015-01-14 19:29:50 +08:00
|
|
|
#if defined(ZEND_WIN32)
|
|
|
|
unsigned long index;
|
|
|
|
#endif
|
2015-02-13 07:12:42 +08:00
|
|
|
|
2014-10-23 04:26:32 +08:00
|
|
|
/* Use big enough power of 2 */
|
2015-02-13 07:12:42 +08:00
|
|
|
/* size should be between HT_MIN_SIZE and HT_MAX_SIZE */
|
2018-05-31 06:17:55 +08:00
|
|
|
if (nSize <= HT_MIN_SIZE) {
|
|
|
|
return HT_MIN_SIZE;
|
2015-08-12 16:44:51 +08:00
|
|
|
} else if (UNEXPECTED(nSize >= HT_MAX_SIZE)) {
|
2016-06-21 21:00:37 +08:00
|
|
|
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", nSize, sizeof(Bucket), sizeof(Bucket));
|
2015-08-12 16:44:51 +08:00
|
|
|
}
|
2015-01-14 19:29:50 +08:00
|
|
|
|
|
|
|
#if defined(ZEND_WIN32)
|
|
|
|
if (BitScanReverse(&index, nSize - 1)) {
|
2015-02-13 07:12:42 +08:00
|
|
|
return 0x2 << ((31 - index) ^ 0x1f);
|
2015-01-14 19:29:50 +08:00
|
|
|
} else {
|
|
|
|
/* nSize is ensured to be in the valid range, fall back to it
|
|
|
|
rather than using an undefined bis scan result. */
|
2015-02-13 07:12:42 +08:00
|
|
|
return nSize;
|
2015-01-14 19:29:50 +08:00
|
|
|
}
|
2015-12-03 18:28:41 +08:00
|
|
|
#elif (defined(__GNUC__) || __has_builtin(__builtin_clz)) && defined(PHP_HAVE_BUILTIN_CLZ)
|
2015-02-13 07:12:42 +08:00
|
|
|
return 0x2 << (__builtin_clz(nSize - 1) ^ 0x1f);
|
2015-01-14 19:29:50 +08:00
|
|
|
#else
|
2014-10-23 04:26:32 +08:00
|
|
|
nSize -= 1;
|
|
|
|
nSize |= (nSize >> 1);
|
|
|
|
nSize |= (nSize >> 2);
|
|
|
|
nSize |= (nSize >> 4);
|
|
|
|
nSize |= (nSize >> 8);
|
|
|
|
nSize |= (nSize >> 16);
|
2015-02-13 07:12:42 +08:00
|
|
|
return nSize + 1;
|
2014-10-23 01:23:29 +08:00
|
|
|
#endif
|
2015-02-13 07:12:42 +08:00
|
|
|
}
|
|
|
|
|
2018-03-23 05:13:45 +08:00
|
|
|
static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
|
2015-04-27 20:54:41 +08:00
|
|
|
{
|
2018-05-04 07:41:35 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, pemalloc(HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
|
2018-03-23 05:13:45 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_INITIALIZED | HASH_FLAG_PACKED;
|
|
|
|
HT_HASH_RESET_PACKED(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
|
|
|
|
{
|
2018-05-04 07:41:35 +08:00
|
|
|
uint32_t nSize = ht->nTableSize;
|
|
|
|
|
2018-05-08 17:32:35 +08:00
|
|
|
ht->nTableMask = HT_SIZE_TO_MASK(nSize);
|
2018-05-04 07:41:35 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
|
2018-03-23 05:13:45 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_INITIALIZED;
|
2018-05-04 07:41:35 +08:00
|
|
|
if (EXPECTED(ht->nTableMask == HT_SIZE_TO_MASK(HT_MIN_SIZE))) {
|
2018-03-23 05:13:45 +08:00
|
|
|
Bucket *arData = ht->arData;
|
|
|
|
|
2018-05-04 07:41:35 +08:00
|
|
|
#ifdef __SSE2__
|
|
|
|
__m128i xmm0 = _mm_setzero_si128();
|
|
|
|
xmm0 = _mm_cmpeq_epi8(xmm0, xmm0);
|
|
|
|
_mm_storeu_si128((__m128i*)&HT_HASH_EX(arData, -16), xmm0);
|
|
|
|
_mm_storeu_si128((__m128i*)&HT_HASH_EX(arData, -12), xmm0);
|
|
|
|
_mm_storeu_si128((__m128i*)&HT_HASH_EX(arData, -8), xmm0);
|
|
|
|
_mm_storeu_si128((__m128i*)&HT_HASH_EX(arData, -4), xmm0);
|
|
|
|
#else
|
|
|
|
HT_HASH_EX(arData, -16) = -1;
|
|
|
|
HT_HASH_EX(arData, -15) = -1;
|
|
|
|
HT_HASH_EX(arData, -14) = -1;
|
|
|
|
HT_HASH_EX(arData, -13) = -1;
|
|
|
|
HT_HASH_EX(arData, -12) = -1;
|
|
|
|
HT_HASH_EX(arData, -11) = -1;
|
|
|
|
HT_HASH_EX(arData, -10) = -1;
|
|
|
|
HT_HASH_EX(arData, -9) = -1;
|
2018-03-23 05:13:45 +08:00
|
|
|
HT_HASH_EX(arData, -8) = -1;
|
|
|
|
HT_HASH_EX(arData, -7) = -1;
|
|
|
|
HT_HASH_EX(arData, -6) = -1;
|
|
|
|
HT_HASH_EX(arData, -5) = -1;
|
|
|
|
HT_HASH_EX(arData, -4) = -1;
|
|
|
|
HT_HASH_EX(arData, -3) = -1;
|
|
|
|
HT_HASH_EX(arData, -2) = -1;
|
|
|
|
HT_HASH_EX(arData, -1) = -1;
|
2018-05-04 07:41:35 +08:00
|
|
|
#endif
|
2015-04-27 20:54:41 +08:00
|
|
|
} else {
|
2018-03-23 05:13:45 +08:00
|
|
|
HT_HASH_RESET(ht);
|
2015-04-27 20:54:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 05:13:45 +08:00
|
|
|
static zend_always_inline void zend_hash_real_init_ex(HashTable *ht, int packed)
|
2015-02-13 07:12:42 +08:00
|
|
|
{
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2018-03-23 05:13:45 +08:00
|
|
|
ZEND_ASSERT(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED));
|
|
|
|
if (packed) {
|
|
|
|
zend_hash_real_init_packed_ex(ht);
|
|
|
|
} else {
|
|
|
|
zend_hash_real_init_mixed_ex(ht);
|
2015-02-13 07:12:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
|
|
|
|
{HT_INVALID_IDX, HT_INVALID_IDX};
|
2001-07-11 04:31:42 +08:00
|
|
|
|
2017-10-24 22:27:31 +08:00
|
|
|
ZEND_API const HashTable zend_empty_array = {
|
|
|
|
.gc.refcount = 2,
|
|
|
|
.gc.u.type_info = IS_ARRAY | (GC_IMMUTABLE << GC_FLAGS_SHIFT),
|
|
|
|
.u.flags = HASH_FLAG_STATIC_KEYS,
|
|
|
|
.nTableMask = HT_MIN_MASK,
|
|
|
|
.arData = (Bucket*)&uninitialized_bucket[2],
|
|
|
|
.nNumUsed = 0,
|
|
|
|
.nNumOfElements = 0,
|
|
|
|
.nTableSize = HT_MIN_SIZE,
|
2018-03-22 07:41:49 +08:00
|
|
|
.nInternalPointer = 0,
|
2017-10-24 22:27:31 +08:00
|
|
|
.nNextFreeElement = 0,
|
|
|
|
.pDestructor = ZVAL_PTR_DTOR
|
|
|
|
};
|
|
|
|
|
2017-10-06 06:34:50 +08:00
|
|
|
static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent)
|
2015-02-13 07:12:42 +08:00
|
|
|
{
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_SET_REFCOUNT(ht, 1);
|
2017-10-06 07:54:14 +08:00
|
|
|
GC_TYPE_INFO(ht) = IS_ARRAY | (persistent ? (GC_PERSISTENT << GC_FLAGS_SHIFT) : (GC_COLLECTABLE << GC_FLAGS_SHIFT));
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) = HASH_FLAG_STATIC_KEYS;
|
2015-03-13 22:13:19 +08:00
|
|
|
ht->nTableMask = HT_MIN_MASK;
|
|
|
|
HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->nNumOfElements = 0;
|
2018-03-22 07:41:49 +08:00
|
|
|
ht->nInternalPointer = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->nNextFreeElement = 0;
|
2014-04-21 22:25:34 +08:00
|
|
|
ht->pDestructor = pDestructor;
|
2017-01-03 12:14:05 +08:00
|
|
|
ht->nTableSize = zend_hash_check_size(nSize);
|
2015-03-05 22:22:10 +08:00
|
|
|
}
|
|
|
|
|
2017-09-20 07:25:56 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent)
|
|
|
|
{
|
2017-10-06 06:34:50 +08:00
|
|
|
_zend_hash_init_int(ht, nSize, pDestructor, persistent);
|
2017-09-20 07:25:56 +08:00
|
|
|
}
|
|
|
|
|
2018-05-31 06:17:55 +08:00
|
|
|
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(ZEND_FILE_LINE_D)
|
|
|
|
{
|
|
|
|
HashTable *ht = emalloc(sizeof(HashTable));
|
|
|
|
_zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, 0);
|
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
|
2017-09-20 07:25:56 +08:00
|
|
|
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t nSize ZEND_FILE_LINE_DC)
|
|
|
|
{
|
|
|
|
HashTable *ht = emalloc(sizeof(HashTable));
|
2017-10-06 06:34:50 +08:00
|
|
|
_zend_hash_init_int(ht, nSize, ZVAL_PTR_DTOR, 0);
|
2017-09-20 07:25:56 +08:00
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
static void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-02-13 07:12:42 +08:00
|
|
|
if (ht->nTableSize >= HT_MAX_SIZE) {
|
2016-06-21 21:00:37 +08:00
|
|
|
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket), sizeof(Bucket));
|
2015-02-13 07:12:42 +08:00
|
|
|
}
|
|
|
|
ht->nTableSize += ht->nTableSize;
|
2018-05-04 07:41:35 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, perealloc2(HT_GET_DATA_ADDR(ht), HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), HT_USED_SIZE(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, zend_bool packed)
|
2014-03-17 21:23:27 +08:00
|
|
|
{
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-04-27 20:54:41 +08:00
|
|
|
zend_hash_real_init_ex(ht, packed);
|
2014-03-17 21:23:27 +08:00
|
|
|
}
|
|
|
|
|
2018-03-23 05:13:45 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht)
|
|
|
|
{
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
HT_ASSERT_RC1(ht);
|
|
|
|
zend_hash_real_init_packed_ex(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht)
|
|
|
|
{
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
HT_ASSERT_RC1(ht);
|
|
|
|
zend_hash_real_init_mixed_ex(ht);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-08-13 22:42:18 +08:00
|
|
|
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *old_buckets = ht->arData;
|
2018-05-04 07:41:35 +08:00
|
|
|
uint32_t nSize = ht->nTableSize;
|
2015-03-13 22:13:19 +08:00
|
|
|
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_PACKED;
|
2018-05-04 07:41:35 +08:00
|
|
|
new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
|
|
|
ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
|
2015-08-13 22:42:18 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, new_data);
|
2015-03-13 22:13:19 +08:00
|
|
|
memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_rehash(ht);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-08-13 23:02:15 +08:00
|
|
|
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *old_buckets = ht->arData;
|
|
|
|
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2017-10-06 07:54:14 +08:00
|
|
|
new_data = pemalloc(HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
|
2015-03-13 22:13:19 +08:00
|
|
|
ht->nTableMask = HT_MIN_MASK;
|
2015-08-13 23:02:15 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, new_data);
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH_RESET_PACKED(ht);
|
|
|
|
memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2000-07-11 22:27:31 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, zend_bool packed)
|
2015-03-04 22:41:01 +08:00
|
|
|
{
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-03-04 22:41:01 +08:00
|
|
|
if (nSize == 0) return;
|
2018-01-22 18:36:15 +08:00
|
|
|
if (UNEXPECTED(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED))) {
|
2015-03-04 22:41:01 +08:00
|
|
|
if (nSize > ht->nTableSize) {
|
|
|
|
ht->nTableSize = zend_hash_check_size(nSize);
|
|
|
|
}
|
2018-03-23 05:13:45 +08:00
|
|
|
zend_hash_real_init(ht, packed);
|
2015-03-04 22:41:01 +08:00
|
|
|
} else {
|
|
|
|
if (packed) {
|
2018-01-22 18:36:15 +08:00
|
|
|
ZEND_ASSERT(HT_FLAGS(ht) & HASH_FLAG_PACKED);
|
2015-03-04 22:41:01 +08:00
|
|
|
if (nSize > ht->nTableSize) {
|
|
|
|
ht->nTableSize = zend_hash_check_size(nSize);
|
2018-05-04 07:41:35 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, perealloc2(HT_GET_DATA_ADDR(ht), HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), HT_USED_SIZE(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
|
2015-03-04 22:41:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-22 18:36:15 +08:00
|
|
|
ZEND_ASSERT(!(HT_FLAGS(ht) & HASH_FLAG_PACKED));
|
2015-03-04 22:41:01 +08:00
|
|
|
if (nSize > ht->nTableSize) {
|
2015-08-13 12:19:35 +08:00
|
|
|
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *old_buckets = ht->arData;
|
2015-08-13 12:19:35 +08:00
|
|
|
nSize = zend_hash_check_size(nSize);
|
|
|
|
ht->nTableSize = nSize;
|
2018-05-04 07:41:35 +08:00
|
|
|
new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
|
|
|
ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
|
2015-08-13 12:19:35 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, new_data);
|
2015-03-13 22:13:19 +08:00
|
|
|
memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2015-03-04 22:41:01 +08:00
|
|
|
zend_hash_rehash(ht);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-07-11 22:27:31 +08:00
|
|
|
|
2015-11-23 22:12:03 +08:00
|
|
|
static uint32_t zend_array_recalc_elements(HashTable *ht)
|
|
|
|
{
|
|
|
|
zval *val;
|
|
|
|
uint32_t num = ht->nNumOfElements;
|
|
|
|
|
|
|
|
ZEND_HASH_FOREACH_VAL(ht, val) {
|
|
|
|
if (Z_TYPE_P(val) == IS_INDIRECT) {
|
|
|
|
if (UNEXPECTED(Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF)) {
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
ZEND_API uint32_t zend_array_count(HashTable *ht)
|
|
|
|
{
|
|
|
|
uint32_t num;
|
2018-01-22 19:58:16 +08:00
|
|
|
if (UNEXPECTED(HT_FLAGS(ht) & HASH_FLAG_HAS_EMPTY_IND)) {
|
2015-11-23 22:12:03 +08:00
|
|
|
num = zend_array_recalc_elements(ht);
|
|
|
|
if (UNEXPECTED(ht->nNumOfElements == num)) {
|
2018-01-22 19:58:16 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_HAS_EMPTY_IND;
|
2015-11-23 22:12:03 +08:00
|
|
|
}
|
|
|
|
} else if (UNEXPECTED(ht == &EG(symbol_table))) {
|
|
|
|
num = zend_array_recalc_elements(ht);
|
|
|
|
} else {
|
|
|
|
num = zend_hash_num_elements(ht);
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2018-03-22 07:41:49 +08:00
|
|
|
static zend_always_inline HashPosition _zend_hash_get_first_pos(const HashTable *ht)
|
|
|
|
{
|
|
|
|
HashPosition pos = 0;
|
|
|
|
|
|
|
|
while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arData[pos].val)) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline HashPosition _zend_hash_get_current_pos(const HashTable *ht)
|
|
|
|
{
|
|
|
|
HashPosition pos = ht->nInternalPointer;
|
|
|
|
|
|
|
|
if (pos == 0) {
|
|
|
|
pos = _zend_hash_get_first_pos(ht);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht)
|
|
|
|
{
|
|
|
|
return _zend_hash_get_current_pos(ht);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
2015-03-31 16:52:30 +08:00
|
|
|
HashTableIterator *iter = EG(ht_iterators);
|
|
|
|
HashTableIterator *end = iter + EG(ht_iterators_count);
|
2015-02-12 18:57:12 +08:00
|
|
|
uint32_t idx;
|
|
|
|
|
2018-01-22 19:14:35 +08:00
|
|
|
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
|
|
|
|
HT_INC_ITERATORS_COUNT(ht);
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
2015-03-31 16:52:30 +08:00
|
|
|
while (iter != end) {
|
2015-02-12 18:57:12 +08:00
|
|
|
if (iter->ht == NULL) {
|
|
|
|
iter->ht = ht;
|
|
|
|
iter->pos = pos;
|
|
|
|
idx = iter - EG(ht_iterators);
|
|
|
|
if (idx + 1 > EG(ht_iterators_used)) {
|
|
|
|
EG(ht_iterators_used) = idx + 1;
|
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
2015-03-31 16:52:30 +08:00
|
|
|
iter++;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
if (EG(ht_iterators) == EG(ht_iterators_slots)) {
|
|
|
|
EG(ht_iterators) = emalloc(sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
|
|
|
|
memcpy(EG(ht_iterators), EG(ht_iterators_slots), sizeof(HashTableIterator) * EG(ht_iterators_count));
|
|
|
|
} else {
|
|
|
|
EG(ht_iterators) = erealloc(EG(ht_iterators), sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
|
|
|
|
}
|
|
|
|
iter = EG(ht_iterators) + EG(ht_iterators_count);
|
|
|
|
EG(ht_iterators_count) += 8;
|
|
|
|
iter->ht = ht;
|
|
|
|
iter->pos = pos;
|
|
|
|
memset(iter + 1, 0, sizeof(HashTableIterator) * 7);
|
|
|
|
idx = iter - EG(ht_iterators);
|
|
|
|
EG(ht_iterators_used) = idx + 1;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
|
|
|
HashTableIterator *iter = EG(ht_iterators) + idx;
|
|
|
|
|
|
|
|
ZEND_ASSERT(idx != (uint32_t)-1);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (UNEXPECTED(iter->ht != ht)) {
|
2015-12-18 05:11:08 +08:00
|
|
|
if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
|
2018-01-22 19:14:35 +08:00
|
|
|
&& EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
|
|
|
|
HT_DEC_ITERATORS_COUNT(iter->ht);
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
2018-01-22 19:14:35 +08:00
|
|
|
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
|
|
|
|
HT_INC_ITERATORS_COUNT(ht);
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
iter->ht = ht;
|
2018-03-22 07:41:49 +08:00
|
|
|
iter->pos = _zend_hash_get_current_pos(ht);
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
return iter->pos;
|
|
|
|
}
|
|
|
|
|
2015-12-21 20:57:53 +08:00
|
|
|
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array)
|
|
|
|
{
|
|
|
|
HashTable *ht = Z_ARRVAL_P(array);
|
|
|
|
HashTableIterator *iter = EG(ht_iterators) + idx;
|
|
|
|
|
|
|
|
ZEND_ASSERT(idx != (uint32_t)-1);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (UNEXPECTED(iter->ht != ht)) {
|
2015-12-21 20:57:53 +08:00
|
|
|
if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
|
2018-01-22 19:14:35 +08:00
|
|
|
&& EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
|
|
|
|
HT_DEC_ITERATORS_COUNT(iter->ht);
|
2015-12-21 20:57:53 +08:00
|
|
|
}
|
|
|
|
SEPARATE_ARRAY(array);
|
|
|
|
ht = Z_ARRVAL_P(array);
|
2018-01-22 19:14:35 +08:00
|
|
|
if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
|
|
|
|
HT_INC_ITERATORS_COUNT(ht);
|
2015-12-21 20:57:53 +08:00
|
|
|
}
|
|
|
|
iter->ht = ht;
|
2018-03-22 07:41:49 +08:00
|
|
|
iter->pos = _zend_hash_get_current_pos(ht);
|
2015-12-21 20:57:53 +08:00
|
|
|
}
|
|
|
|
return iter->pos;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
|
|
|
HashTableIterator *iter = EG(ht_iterators) + idx;
|
|
|
|
|
|
|
|
ZEND_ASSERT(idx != (uint32_t)-1);
|
|
|
|
|
2015-12-18 05:11:08 +08:00
|
|
|
if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
|
2018-01-22 19:14:35 +08:00
|
|
|
&& EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
|
|
|
|
HT_DEC_ITERATORS_COUNT(iter->ht);
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
iter->ht = NULL;
|
|
|
|
|
|
|
|
if (idx == EG(ht_iterators_used) - 1) {
|
|
|
|
while (idx > 0 && EG(ht_iterators)[idx - 1].ht == NULL) {
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
EG(ht_iterators_used) = idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
static zend_never_inline void ZEND_FASTCALL _zend_hash_iterators_remove(HashTable *ht)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
2015-03-31 16:52:30 +08:00
|
|
|
HashTableIterator *iter = EG(ht_iterators);
|
|
|
|
HashTableIterator *end = iter + EG(ht_iterators_used);
|
2015-02-12 18:57:12 +08:00
|
|
|
|
2015-03-31 16:52:30 +08:00
|
|
|
while (iter != end) {
|
2015-02-12 18:57:12 +08:00
|
|
|
if (iter->ht == ht) {
|
2015-12-18 05:11:08 +08:00
|
|
|
iter->ht = HT_POISONED_PTR;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
2015-03-31 16:52:30 +08:00
|
|
|
iter++;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_hash_iterators_remove(HashTable *ht)
|
|
|
|
{
|
2018-01-22 19:14:35 +08:00
|
|
|
if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
|
2015-02-12 18:57:12 +08:00
|
|
|
_zend_hash_iterators_remove(ht);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(HashTable *ht, HashPosition start)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
2015-03-31 16:52:30 +08:00
|
|
|
HashTableIterator *iter = EG(ht_iterators);
|
|
|
|
HashTableIterator *end = iter + EG(ht_iterators_used);
|
2018-03-22 07:41:49 +08:00
|
|
|
HashPosition res = ht->nNumUsed;
|
2015-02-12 18:57:12 +08:00
|
|
|
|
2015-03-31 16:52:30 +08:00
|
|
|
while (iter != end) {
|
2015-02-12 18:57:12 +08:00
|
|
|
if (iter->ht == ht) {
|
|
|
|
if (iter->pos >= start && iter->pos < res) {
|
|
|
|
res = iter->pos;
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 16:52:30 +08:00
|
|
|
iter++;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(HashTable *ht, HashPosition from, HashPosition to)
|
2015-02-12 18:57:12 +08:00
|
|
|
{
|
2015-03-31 16:52:30 +08:00
|
|
|
HashTableIterator *iter = EG(ht_iterators);
|
|
|
|
HashTableIterator *end = iter + EG(ht_iterators_used);
|
2015-02-12 18:57:12 +08:00
|
|
|
|
2015-03-31 16:52:30 +08:00
|
|
|
while (iter != end) {
|
2015-02-12 18:57:12 +08:00
|
|
|
if (iter->ht == ht && iter->pos == from) {
|
|
|
|
iter->pos = to;
|
|
|
|
}
|
2015-03-31 16:52:30 +08:00
|
|
|
iter++;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 07:41:49 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(HashTable *ht, HashPosition step)
|
|
|
|
{
|
|
|
|
HashTableIterator *iter = EG(ht_iterators);
|
|
|
|
HashTableIterator *end = iter + EG(ht_iterators_used);
|
|
|
|
|
|
|
|
while (iter != end) {
|
|
|
|
if (iter->ht == ht) {
|
|
|
|
iter->pos += step;
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 19:01:19 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_find_bucket(const HashTable *ht, zend_string *key, zend_bool known_hash)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *p, *arData;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2017-11-24 19:01:19 +08:00
|
|
|
if (known_hash) {
|
|
|
|
h = ZSTR_H(key);
|
|
|
|
} else {
|
|
|
|
h = zend_string_hash_val(key);
|
|
|
|
}
|
2015-03-13 22:13:19 +08:00
|
|
|
arData = ht->arData;
|
|
|
|
nIndex = h | ht->nTableMask;
|
|
|
|
idx = HT_HASH_EX(arData, nIndex);
|
2018-05-07 20:18:02 +08:00
|
|
|
|
|
|
|
if (UNEXPECTED(idx == HT_INVALID_IDX)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = HT_HASH_TO_BUCKET_EX(arData, idx);
|
|
|
|
if (EXPECTED(p->key == key)) { /* check for the same interned string */
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (p->h == ZSTR_H(key) &&
|
|
|
|
EXPECTED(p->key) &&
|
|
|
|
zend_string_equal_content(p->key, key)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
return p;
|
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
2018-05-07 20:18:02 +08:00
|
|
|
if (idx == HT_INVALID_IDX) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = HT_HASH_TO_BUCKET_EX(arData, idx);
|
|
|
|
if (p->key == key) { /* check for the same interned string */
|
|
|
|
return p;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-09-16 05:01:52 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_str_find_bucket(const HashTable *ht, const char *str, size_t len, zend_ulong h)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *p, *arData;
|
|
|
|
|
|
|
|
arData = ht->arData;
|
|
|
|
nIndex = h | ht->nTableMask;
|
|
|
|
idx = HT_HASH_EX(arData, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
|
|
|
|
p = HT_HASH_TO_BUCKET_EX(arData, idx);
|
2015-01-03 17:22:58 +08:00
|
|
|
if ((p->h == h)
|
2014-02-10 14:04:30 +08:00
|
|
|
&& p->key
|
2015-06-30 18:59:27 +08:00
|
|
|
&& (ZSTR_LEN(p->key) == len)
|
|
|
|
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
return p;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_index_find_bucket(const HashTable *ht, zend_ulong h)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *p, *arData;
|
|
|
|
|
|
|
|
arData = ht->arData;
|
|
|
|
nIndex = h | ht->nTableMask;
|
|
|
|
idx = HT_HASH_EX(arData, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
|
|
|
|
p = HT_HASH_TO_BUCKET_EX(arData, idx);
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p->h == h && !p->key) {
|
|
|
|
return p;
|
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 05:01:52 +08:00
|
|
|
static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_string *key, zval *pData, uint32_t flag ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2018-02-13 05:56:48 +08:00
|
|
|
Bucket *p, *arData;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (UNEXPECTED(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED))) {
|
2018-03-23 05:13:45 +08:00
|
|
|
zend_hash_real_init_mixed(ht);
|
2017-11-24 17:03:59 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(key)) {
|
|
|
|
zend_string_addref(key);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
|
2017-11-24 17:03:59 +08:00
|
|
|
zend_string_hash_val(key);
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
goto add_to_hash;
|
2018-01-22 18:36:15 +08:00
|
|
|
} else if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_packed_to_hash(ht);
|
2017-11-24 17:03:59 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(key)) {
|
|
|
|
zend_string_addref(key);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
|
2017-11-24 17:03:59 +08:00
|
|
|
zend_string_hash_val(key);
|
|
|
|
}
|
2014-11-06 22:45:41 +08:00
|
|
|
} else if ((flag & HASH_ADD_NEW) == 0) {
|
2017-11-24 19:01:19 +08:00
|
|
|
p = zend_hash_find_bucket(ht, key, 0);
|
2014-03-26 22:07:31 +08:00
|
|
|
|
2014-05-26 21:16:16 +08:00
|
|
|
if (p) {
|
|
|
|
zval *data;
|
|
|
|
|
|
|
|
if (flag & HASH_ADD) {
|
2016-03-02 20:09:04 +08:00
|
|
|
if (!(flag & HASH_UPDATE_INDIRECT)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
|
|
|
data = &p->val;
|
|
|
|
if (Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
if (Z_TYPE_P(data) != IS_UNDEF) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
|
|
|
data = &p->val;
|
|
|
|
if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
}
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(data);
|
|
|
|
}
|
|
|
|
ZVAL_COPY_VALUE(data, pData);
|
|
|
|
return data;
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2017-11-24 17:03:59 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(key)) {
|
|
|
|
zend_string_addref(key);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
|
2017-11-24 17:03:59 +08:00
|
|
|
}
|
|
|
|
} else if (!ZSTR_IS_INTERNED(key)) {
|
|
|
|
zend_string_addref(key);
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
|
2017-11-24 17:03:59 +08:00
|
|
|
zend_string_hash_val(key);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-11-06 22:45:41 +08:00
|
|
|
add_to_hash:
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed++;
|
|
|
|
ht->nNumOfElements++;
|
2018-02-13 05:56:48 +08:00
|
|
|
arData = ht->arData;
|
|
|
|
p = arData + idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
p->key = key;
|
2015-06-30 18:59:27 +08:00
|
|
|
p->h = h = ZSTR_H(key);
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
2018-02-13 05:56:48 +08:00
|
|
|
Z_NEXT(p->val) = HT_HASH_EX(arData, nIndex);
|
|
|
|
HT_HASH_EX(arData, nIndex) = HT_IDX_TO_HASH(idx);
|
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
2000-01-20 22:26:31 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return &p->val;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2017-10-31 15:35:28 +08:00
|
|
|
static 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 ZEND_FILE_LINE_DC)
|
|
|
|
{
|
|
|
|
zend_string *key;
|
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
HT_ASSERT_RC1(ht);
|
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (UNEXPECTED(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED))) {
|
2018-03-23 05:13:45 +08:00
|
|
|
zend_hash_real_init_mixed(ht);
|
2017-10-31 15:35:28 +08:00
|
|
|
goto add_to_hash;
|
2018-01-22 18:36:15 +08:00
|
|
|
} else if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2017-10-31 15:35:28 +08:00
|
|
|
zend_hash_packed_to_hash(ht);
|
|
|
|
} else if ((flag & HASH_ADD_NEW) == 0) {
|
|
|
|
p = zend_hash_str_find_bucket(ht, str, len, h);
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
zval *data;
|
|
|
|
|
|
|
|
if (flag & HASH_ADD) {
|
|
|
|
if (!(flag & HASH_UPDATE_INDIRECT)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
|
|
|
data = &p->val;
|
|
|
|
if (Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
if (Z_TYPE_P(data) != IS_UNDEF) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
|
|
|
data = &p->val;
|
|
|
|
if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(data);
|
|
|
|
}
|
|
|
|
ZVAL_COPY_VALUE(data, pData);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
|
|
|
|
|
|
|
|
add_to_hash:
|
|
|
|
idx = ht->nNumUsed++;
|
|
|
|
ht->nNumOfElements++;
|
|
|
|
p = ht->arData + idx;
|
|
|
|
p->key = key = zend_string_init(str, len, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
|
|
|
p->h = ZSTR_H(key) = h;
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
|
2017-10-31 15:35:28 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
|
|
|
nIndex = h | ht->nTableMask;
|
|
|
|
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
|
|
|
|
|
|
|
|
return &p->val;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2018-05-29 06:38:27 +08:00
|
|
|
if (flag == HASH_ADD) {
|
|
|
|
return _zend_hash_add(ht, key, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == HASH_ADD_NEW) {
|
|
|
|
return _zend_hash_add_new(ht, key, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == HASH_UPDATE) {
|
|
|
|
return _zend_hash_update(ht, key, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else {
|
|
|
|
ZEND_ASSERT(flag == (HASH_UPDATE|HASH_UPDATE_INDIRECT));
|
|
|
|
return _zend_hash_update_ind(ht, key, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_add(HashTable *ht, zend_string *key, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
|
|
|
return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_update(HashTable *ht, zend_string *key, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
|
|
|
return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_update_ind(HashTable *ht, zend_string *key, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
|
|
|
return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_add_new(HashTable *ht, zend_string *key, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
|
|
|
return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD_NEW ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, uint32_t flag ZEND_FILE_LINE_DC)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2018-05-29 06:38:27 +08:00
|
|
|
if (flag == HASH_ADD) {
|
|
|
|
return _zend_hash_str_add(ht, str, len, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == HASH_ADD_NEW) {
|
|
|
|
return _zend_hash_str_add_new(ht, str, len, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == HASH_UPDATE) {
|
|
|
|
return _zend_hash_str_update(ht, str, len, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else {
|
|
|
|
ZEND_ASSERT(flag == (HASH_UPDATE|HASH_UPDATE_INDIRECT));
|
|
|
|
return _zend_hash_str_update_ind(ht, str, len, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2017-10-31 15:35:28 +08:00
|
|
|
zend_ulong h = zend_hash_func(str, len);
|
|
|
|
|
|
|
|
return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2017-10-31 15:35:28 +08:00
|
|
|
zend_ulong h = zend_hash_func(str, len);
|
|
|
|
|
|
|
|
return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add(HashTable *ht, const char *str, size_t len, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2017-10-31 15:35:28 +08:00
|
|
|
zend_ulong h = zend_hash_func(str, len);
|
|
|
|
|
|
|
|
return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2017-10-31 15:35:28 +08:00
|
|
|
zend_ulong h = zend_hash_func(str, len);
|
|
|
|
|
|
|
|
return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD_NEW ZEND_FILE_LINE_RELAY_CC);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h)
|
2014-04-24 23:14:29 +08:00
|
|
|
{
|
|
|
|
zval dummy;
|
|
|
|
|
|
|
|
ZVAL_NULL(&dummy);
|
|
|
|
return zend_hash_index_add(ht, h, &dummy);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key)
|
2001-05-17 01:22:01 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zval dummy;
|
2001-05-17 01:22:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_NULL(&dummy);
|
|
|
|
return zend_hash_add(ht, key, &dummy);
|
2001-05-17 01:22:01 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *str, size_t len)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
|
|
|
zval dummy;
|
|
|
|
|
|
|
|
ZVAL_NULL(&dummy);
|
|
|
|
return zend_hash_str_add(ht, str, len, &dummy);
|
|
|
|
}
|
2001-05-17 01:22:01 +08:00
|
|
|
|
2014-09-16 20:28:30 +08:00
|
|
|
static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2018-05-29 05:09:49 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-09-16 20:09:32 +08:00
|
|
|
if (h < ht->nNumUsed) {
|
|
|
|
p = ht->arData + h;
|
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
2018-05-29 05:09:49 +08:00
|
|
|
replace:
|
2014-09-16 20:28:30 +08:00
|
|
|
if (flag & HASH_ADD) {
|
2014-09-16 20:09:32 +08:00
|
|
|
return NULL;
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2014-09-16 20:09:32 +08:00
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(&p->val);
|
2014-04-07 17:31:44 +08:00
|
|
|
}
|
2014-09-16 20:09:32 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
|
|
|
return &p->val;
|
|
|
|
} else { /* we have to keep the order :( */
|
|
|
|
goto convert_to_hash;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-09-16 20:09:32 +08:00
|
|
|
} else if (EXPECTED(h < ht->nTableSize)) {
|
2018-05-29 05:09:49 +08:00
|
|
|
add_to_packed:
|
2014-09-16 20:09:32 +08:00
|
|
|
p = ht->arData + h;
|
2018-05-29 05:09:49 +08:00
|
|
|
/* incremental initialization of empty Buckets */
|
|
|
|
if ((flag & (HASH_ADD_NEW|HASH_ADD_NEXT)) != (HASH_ADD_NEW|HASH_ADD_NEXT)) {
|
|
|
|
if (h > ht->nNumUsed) {
|
|
|
|
Bucket *q = ht->arData + ht->nNumUsed;
|
|
|
|
while (q != p) {
|
|
|
|
ZVAL_UNDEF(&q->val);
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ht->nNextFreeElement = ht->nNumUsed = h + 1;
|
|
|
|
goto add;
|
2014-12-19 18:06:34 +08:00
|
|
|
} else if ((h >> 1) < ht->nTableSize &&
|
|
|
|
(ht->nTableSize >> 1) < ht->nNumOfElements) {
|
2014-09-16 20:09:32 +08:00
|
|
|
zend_hash_packed_grow(ht);
|
2018-05-29 05:09:49 +08:00
|
|
|
goto add_to_packed;
|
2014-09-16 20:09:32 +08:00
|
|
|
} else {
|
2018-05-29 05:09:49 +08:00
|
|
|
if (ht->nNumUsed >= ht->nTableSize) {
|
|
|
|
ht->nTableSize += ht->nTableSize;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2018-05-29 05:09:49 +08:00
|
|
|
convert_to_hash:
|
|
|
|
zend_hash_packed_to_hash(ht);
|
2014-09-16 20:09:32 +08:00
|
|
|
}
|
2018-05-29 05:09:49 +08:00
|
|
|
} else if (!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED)) {
|
|
|
|
if (h < ht->nTableSize) {
|
|
|
|
zend_hash_real_init_packed_ex(ht);
|
|
|
|
goto add_to_packed;
|
2014-09-16 20:09:32 +08:00
|
|
|
}
|
2018-05-29 05:09:49 +08:00
|
|
|
zend_hash_real_init_mixed(ht);
|
|
|
|
} else {
|
|
|
|
if ((flag & HASH_ADD_NEW) == 0) {
|
|
|
|
p = zend_hash_index_find_bucket(ht, h);
|
|
|
|
if (p) {
|
|
|
|
goto replace;
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2018-05-29 05:09:49 +08:00
|
|
|
ZEND_HASH_IF_FULL_DO_RESIZE(ht); /* If the Hash table is full, resize it */
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed++;
|
2018-05-29 05:09:49 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
|
|
|
p = ht->arData + idx;
|
|
|
|
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
|
2014-08-26 01:24:55 +08:00
|
|
|
if ((zend_long)h >= (zend_long)ht->nNextFreeElement) {
|
2014-08-26 02:22:49 +08:00
|
|
|
ht->nNextFreeElement = h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2018-05-29 05:09:49 +08:00
|
|
|
add:
|
|
|
|
ht->nNumOfElements++;
|
2014-02-10 14:04:30 +08:00
|
|
|
p->h = h;
|
|
|
|
p->key = NULL;
|
2014-02-18 20:27:38 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
return &p->val;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2018-05-29 06:38:27 +08:00
|
|
|
if (flag == HASH_ADD) {
|
|
|
|
return _zend_hash_index_add(ht, h, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == (HASH_ADD|HASH_ADD_NEW)) {
|
|
|
|
return _zend_hash_index_add_new(ht, h, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == (HASH_ADD|HASH_ADD_NEXT)) {
|
|
|
|
ZEND_ASSERT(h == ht->nNextFreeElement);
|
|
|
|
return _zend_hash_next_index_insert(ht, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else if (flag == (HASH_ADD|HASH_ADD_NEW|HASH_ADD_NEXT)) {
|
|
|
|
ZEND_ASSERT(h == ht->nNextFreeElement);
|
|
|
|
return _zend_hash_next_index_insert_new(ht, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
} else {
|
|
|
|
ZEND_ASSERT(flag == HASH_UPDATE);
|
|
|
|
return _zend_hash_index_update(ht, h, pData ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
}
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2014-09-16 20:28:30 +08:00
|
|
|
return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2014-09-16 20:28:30 +08:00
|
|
|
return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD | HASH_ADD_NEW ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2014-09-16 20:28:30 +08:00
|
|
|
return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_UPDATE ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_next_index_insert(HashTable *ht, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2014-10-16 23:19:10 +08:00
|
|
|
return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEXT ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_next_index_insert_new(HashTable *ht, zval *pData ZEND_FILE_LINE_DC)
|
2014-05-26 21:16:16 +08:00
|
|
|
{
|
2014-10-16 23:19:10 +08:00
|
|
|
return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEW | HASH_ADD_NEXT ZEND_FILE_LINE_RELAY_CC);
|
2014-05-26 21:16:16 +08:00
|
|
|
}
|
2000-01-18 02:45:46 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2015-03-27 02:05:38 +08:00
|
|
|
if (ht->nNumUsed > ht->nNumOfElements + (ht->nNumOfElements >> 5)) { /* additional term is there to amortize the cost of compaction */
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_rehash(ht);
|
2015-02-13 07:12:42 +08:00
|
|
|
} else if (ht->nTableSize < HT_MAX_SIZE) { /* Let's double the table size */
|
2015-08-13 12:08:57 +08:00
|
|
|
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
|
|
|
|
uint32_t nSize = ht->nTableSize + ht->nTableSize;
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *old_buckets = ht->arData;
|
|
|
|
|
2015-08-13 12:08:57 +08:00
|
|
|
ht->nTableSize = nSize;
|
2018-05-04 07:41:35 +08:00
|
|
|
new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
|
|
|
ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
|
2015-08-13 12:08:57 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, new_data);
|
2015-03-13 22:13:19 +08:00
|
|
|
memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2013-06-18 00:51:59 +08:00
|
|
|
zend_hash_rehash(ht);
|
2015-02-13 07:12:42 +08:00
|
|
|
} else {
|
2016-06-21 21:00:37 +08:00
|
|
|
zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket) + sizeof(uint32_t), sizeof(Bucket));
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_rehash(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
2015-04-24 18:00:56 +08:00
|
|
|
uint32_t nIndex, i;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2010-04-20 18:49:22 +08:00
|
|
|
if (UNEXPECTED(ht->nNumOfElements == 0)) {
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_INITIALIZED) {
|
2015-03-18 04:13:39 +08:00
|
|
|
ht->nNumUsed = 0;
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH_RESET(ht);
|
2014-08-14 17:44:37 +08:00
|
|
|
}
|
2010-04-20 18:49:22 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH_RESET(ht);
|
2015-04-24 18:00:56 +08:00
|
|
|
i = 0;
|
|
|
|
p = ht->arData;
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-24 18:00:56 +08:00
|
|
|
do {
|
|
|
|
nIndex = p->h | ht->nTableMask;
|
|
|
|
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
|
|
|
|
p++;
|
|
|
|
} while (++i < ht->nNumUsed);
|
2015-02-12 18:57:12 +08:00
|
|
|
} else {
|
2015-04-24 18:00:56 +08:00
|
|
|
do {
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) {
|
|
|
|
uint32_t j = i;
|
|
|
|
Bucket *q = p;
|
|
|
|
|
2018-01-22 19:14:35 +08:00
|
|
|
if (EXPECTED(!HT_HAS_ITERATORS(ht))) {
|
2015-04-24 18:00:56 +08:00
|
|
|
while (++i < ht->nNumUsed) {
|
|
|
|
p++;
|
|
|
|
if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &p->val);
|
|
|
|
q->h = p->h;
|
|
|
|
nIndex = q->h | ht->nTableMask;
|
|
|
|
q->key = p->key;
|
|
|
|
Z_NEXT(q->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
|
|
|
|
if (UNEXPECTED(ht->nInternalPointer == i)) {
|
|
|
|
ht->nInternalPointer = j;
|
|
|
|
}
|
|
|
|
q++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, 0);
|
|
|
|
|
|
|
|
while (++i < ht->nNumUsed) {
|
|
|
|
p++;
|
|
|
|
if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &p->val);
|
|
|
|
q->h = p->h;
|
|
|
|
nIndex = q->h | ht->nTableMask;
|
|
|
|
q->key = p->key;
|
|
|
|
Z_NEXT(q->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
|
|
|
|
if (UNEXPECTED(ht->nInternalPointer == i)) {
|
|
|
|
ht->nInternalPointer = j;
|
|
|
|
}
|
|
|
|
if (UNEXPECTED(i == iter_pos)) {
|
|
|
|
zend_hash_iterators_update(ht, i, j);
|
|
|
|
iter_pos = zend_hash_iterators_lower_pos(ht, iter_pos + 1);
|
|
|
|
}
|
|
|
|
q++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
2015-04-24 18:00:56 +08:00
|
|
|
ht->nNumUsed = j;
|
|
|
|
break;
|
2015-02-12 18:57:12 +08:00
|
|
|
}
|
2015-04-24 18:00:56 +08:00
|
|
|
nIndex = p->h | ht->nTableMask;
|
|
|
|
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
|
|
|
|
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
|
|
|
|
p++;
|
|
|
|
} while (++i < ht->nNumUsed);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-09-16 05:01:52 +08:00
|
|
|
static zend_always_inline void _zend_hash_del_el_ex(HashTable *ht, uint32_t idx, Bucket *p, Bucket *prev)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2018-01-22 18:36:15 +08:00
|
|
|
if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (prev) {
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(prev->val) = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 17:39:56 +08:00
|
|
|
idx = HT_HASH_TO_IDX(idx);
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumOfElements--;
|
2018-03-21 17:39:56 +08:00
|
|
|
if (ht->nInternalPointer == idx || UNEXPECTED(HT_HAS_ITERATORS(ht))) {
|
2015-03-18 04:13:39 +08:00
|
|
|
uint32_t new_idx;
|
2015-02-12 18:57:12 +08:00
|
|
|
|
2018-03-21 17:39:56 +08:00
|
|
|
new_idx = idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
while (1) {
|
2015-02-12 18:57:12 +08:00
|
|
|
new_idx++;
|
|
|
|
if (new_idx >= ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
break;
|
2015-02-12 18:57:12 +08:00
|
|
|
} else if (Z_TYPE(ht->arData[new_idx].val) != IS_UNDEF) {
|
2014-02-10 14:04:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 18:57:12 +08:00
|
|
|
if (ht->nInternalPointer == idx) {
|
|
|
|
ht->nInternalPointer = new_idx;
|
|
|
|
}
|
|
|
|
zend_hash_iterators_update(ht, idx, new_idx);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2018-03-22 21:37:34 +08:00
|
|
|
if (ht->nNumUsed - 1 == idx) {
|
|
|
|
do {
|
|
|
|
ht->nNumUsed--;
|
|
|
|
} while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arData[ht->nNumUsed-1].val) == IS_UNDEF)));
|
|
|
|
ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p->key) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_release(p->key);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2014-03-06 20:53:13 +08:00
|
|
|
if (ht->pDestructor) {
|
|
|
|
zval tmp;
|
|
|
|
ZVAL_COPY_VALUE(&tmp, &p->val);
|
|
|
|
ZVAL_UNDEF(&p->val);
|
|
|
|
ht->pDestructor(&tmp);
|
|
|
|
} else {
|
|
|
|
ZVAL_UNDEF(&p->val);
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 05:01:52 +08:00
|
|
|
static zend_always_inline void _zend_hash_del_el(HashTable *ht, uint32_t idx, Bucket *p)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED)) {
|
2015-03-13 22:13:19 +08:00
|
|
|
uint32_t nIndex = p->h | ht->nTableMask;
|
|
|
|
uint32_t i = HT_HASH(ht, nIndex);
|
2014-09-17 04:52:03 +08:00
|
|
|
|
|
|
|
if (i != idx) {
|
2015-03-13 22:13:19 +08:00
|
|
|
prev = HT_HASH_TO_BUCKET(ht, i);
|
2014-09-17 04:52:03 +08:00
|
|
|
while (Z_NEXT(prev->val) != idx) {
|
|
|
|
i = Z_NEXT(prev->val);
|
2015-03-13 22:13:19 +08:00
|
|
|
prev = HT_HASH_TO_BUCKET(ht, i);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:29:06 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p)
|
|
|
|
{
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-04-22 07:29:06 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(p - ht->arData), p);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2000-01-20 03:47:16 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *prev = NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
h = zend_string_hash_val(key);
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
idx = HT_HASH(ht, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
p = HT_HASH_TO_BUCKET(ht, idx);
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((p->key == key) ||
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
2017-12-04 22:17:02 +08:00
|
|
|
zend_string_equal_content(p->key, key))) {
|
2014-02-10 14:04:30 +08:00
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
prev = p;
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key)
|
2014-03-26 22:07:31 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2014-03-26 22:07:31 +08:00
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2014-03-26 22:07:31 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
h = zend_string_hash_val(key);
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
2014-03-26 22:07:31 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
idx = HT_HASH(ht, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
p = HT_HASH_TO_BUCKET(ht, idx);
|
2014-03-26 22:07:31 +08:00
|
|
|
if ((p->key == key) ||
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
2017-12-04 22:17:02 +08:00
|
|
|
zend_string_equal_content(p->key, key))) {
|
2014-03-26 22:07:31 +08:00
|
|
|
if (Z_TYPE(p->val) == IS_INDIRECT) {
|
|
|
|
zval *data = Z_INDIRECT(p->val);
|
|
|
|
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
|
2014-03-26 22:07:31 +08:00
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
if (ht->pDestructor) {
|
2015-11-04 09:53:56 +08:00
|
|
|
zval tmp;
|
|
|
|
ZVAL_COPY_VALUE(&tmp, data);
|
|
|
|
ZVAL_UNDEF(data);
|
|
|
|
ht->pDestructor(&tmp);
|
|
|
|
} else {
|
|
|
|
ZVAL_UNDEF(data);
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
2018-01-22 19:58:16 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
prev = p;
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-18 05:13:39 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *str, size_t len)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2014-03-14 19:35:57 +08:00
|
|
|
h = zend_inline_hash_func(str, len);
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
idx = HT_HASH(ht, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
p = HT_HASH_TO_BUCKET(ht, idx);
|
2015-01-03 17:22:58 +08:00
|
|
|
if ((p->h == h)
|
2014-03-26 22:07:31 +08:00
|
|
|
&& p->key
|
2015-06-30 18:59:27 +08:00
|
|
|
&& (ZSTR_LEN(p->key) == len)
|
|
|
|
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
|
2014-03-26 22:07:31 +08:00
|
|
|
if (Z_TYPE(p->val) == IS_INDIRECT) {
|
|
|
|
zval *data = Z_INDIRECT(p->val);
|
|
|
|
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
|
2014-03-26 22:07:31 +08:00
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(data);
|
|
|
|
}
|
|
|
|
ZVAL_UNDEF(data);
|
2018-01-22 19:58:16 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
prev = p;
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
2014-03-26 22:07:31 +08:00
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-18 05:13:39 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len)
|
2014-03-26 22:07:31 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2014-03-26 22:07:31 +08:00
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2014-03-26 22:07:31 +08:00
|
|
|
|
|
|
|
h = zend_inline_hash_func(str, len);
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
2014-03-26 22:07:31 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
idx = HT_HASH(ht, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
p = HT_HASH_TO_BUCKET(ht, idx);
|
2015-01-03 17:22:58 +08:00
|
|
|
if ((p->h == h)
|
2014-02-10 14:04:30 +08:00
|
|
|
&& p->key
|
2015-06-30 18:59:27 +08:00
|
|
|
&& (ZSTR_LEN(p->key) == len)
|
|
|
|
&& !memcmp(ZSTR_VAL(p->key), str, len)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
prev = p;
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t nIndex;
|
|
|
|
uint32_t idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-09-16 05:04:34 +08:00
|
|
|
if (h < ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + h;
|
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
2015-03-18 04:13:39 +08:00
|
|
|
_zend_hash_del_el_ex(ht, HT_IDX_TO_HASH(h), p, NULL);
|
2014-02-10 14:04:30 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2015-03-13 22:13:19 +08:00
|
|
|
nIndex = h | ht->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2015-03-13 22:13:19 +08:00
|
|
|
idx = HT_HASH(ht, nIndex);
|
|
|
|
while (idx != HT_INVALID_IDX) {
|
|
|
|
p = HT_HASH_TO_BUCKET(ht, idx);
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((p->h == h) && (p->key == NULL)) {
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
prev = p;
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-08-27 04:52:50 +08:00
|
|
|
Bucket *p, *end;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-08-27 04:52:50 +08:00
|
|
|
if (ht->nNumUsed) {
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2000-01-18 02:45:46 +08:00
|
|
|
if (ht->pDestructor) {
|
2014-08-27 04:52:50 +08:00
|
|
|
SET_INCONSISTENT(HT_IS_DESTROYING);
|
|
|
|
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
|
|
|
if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
2014-08-27 04:52:50 +08:00
|
|
|
ht->pDestructor(&p->val);
|
2015-04-25 03:43:50 +08:00
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
2016-10-20 04:10:42 +08:00
|
|
|
} else if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
2014-08-27 04:52:50 +08:00
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-08-27 04:52:50 +08:00
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
|
|
|
} else {
|
2016-10-20 04:10:42 +08:00
|
|
|
if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
2014-08-27 04:52:50 +08:00
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2015-02-12 18:57:12 +08:00
|
|
|
zend_hash_iterators_remove(ht);
|
2018-01-22 18:36:15 +08:00
|
|
|
} else if (EXPECTED(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED))) {
|
2014-08-27 04:52:50 +08:00
|
|
|
return;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
|
2014-11-25 17:58:29 +08:00
|
|
|
{
|
|
|
|
Bucket *p, *end;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
|
2014-11-25 17:58:29 +08:00
|
|
|
|
2015-07-02 18:43:06 +08:00
|
|
|
/* break possible cycles */
|
|
|
|
GC_REMOVE_FROM_BUFFER(ht);
|
2018-02-26 17:49:58 +08:00
|
|
|
GC_TYPE_INFO(ht) = IS_NULL /*???| (GC_WHITE << 16)*/;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2015-07-02 18:43:06 +08:00
|
|
|
if (ht->nNumUsed) {
|
2014-11-25 17:58:29 +08:00
|
|
|
/* In some rare cases destructors of regular arrays may be changed */
|
|
|
|
if (UNEXPECTED(ht->pDestructor != ZVAL_PTR_DTOR)) {
|
|
|
|
zend_hash_destroy(ht);
|
2015-02-24 23:34:37 +08:00
|
|
|
goto free_ht;
|
2014-11-25 17:58:29 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-11-25 17:58:29 +08:00
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
|
|
|
SET_INCONSISTENT(HT_IS_DESTROYING);
|
|
|
|
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
2014-11-25 17:58:29 +08:00
|
|
|
do {
|
2015-02-25 00:21:21 +08:00
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
2014-11-25 17:58:29 +08:00
|
|
|
} while (++p != end);
|
2016-10-20 04:10:42 +08:00
|
|
|
} else if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
|
|
|
if (EXPECTED(p->key)) {
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(p->key, 0);
|
2015-04-25 03:43:50 +08:00
|
|
|
}
|
|
|
|
} while (++p != end);
|
2014-11-25 17:58:29 +08:00
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
2014-11-25 17:58:29 +08:00
|
|
|
if (EXPECTED(p->key)) {
|
2018-05-28 21:27:12 +08:00
|
|
|
zend_string_release_ex(p->key, 0);
|
2014-11-25 17:58:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
2015-02-12 18:57:12 +08:00
|
|
|
zend_hash_iterators_remove(ht);
|
2014-11-25 17:58:29 +08:00
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
2018-01-22 18:36:15 +08:00
|
|
|
} else if (EXPECTED(!(HT_FLAGS(ht) & HASH_FLAG_INITIALIZED))) {
|
2015-02-24 23:34:37 +08:00
|
|
|
goto free_ht;
|
2014-11-25 17:58:29 +08:00
|
|
|
}
|
2015-03-13 22:13:19 +08:00
|
|
|
efree(HT_GET_DATA_ADDR(ht));
|
2015-02-24 23:34:37 +08:00
|
|
|
free_ht:
|
|
|
|
FREE_HASHTABLE(ht);
|
2014-11-25 17:58:29 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-11-25 19:17:21 +08:00
|
|
|
Bucket *p, *end;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-11-25 19:17:21 +08:00
|
|
|
if (ht->nNumUsed) {
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->pDestructor) {
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
|
|
|
if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
2014-11-25 19:17:21 +08:00
|
|
|
ht->pDestructor(&p->val);
|
2015-04-25 03:43:50 +08:00
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
2016-10-20 04:10:42 +08:00
|
|
|
} else if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
2014-11-25 19:17:21 +08:00
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-20 04:10:42 +08:00
|
|
|
if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
|
|
|
if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
2014-11-25 19:17:21 +08:00
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++p != end);
|
|
|
|
}
|
2014-11-25 19:17:21 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2018-01-22 18:36:15 +08:00
|
|
|
if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED)) {
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH_RESET(ht);
|
2014-12-27 16:07:12 +08:00
|
|
|
}
|
2014-11-25 19:17:21 +08:00
|
|
|
}
|
|
|
|
ht->nNumUsed = 0;
|
|
|
|
ht->nNumOfElements = 0;
|
|
|
|
ht->nNextFreeElement = 0;
|
2018-03-22 07:41:49 +08:00
|
|
|
ht->nInternalPointer = 0;
|
2014-11-25 19:17:21 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht)
|
2014-11-25 19:17:21 +08:00
|
|
|
{
|
|
|
|
Bucket *p, *end;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2014-11-25 19:17:21 +08:00
|
|
|
|
|
|
|
if (ht->nNumUsed) {
|
|
|
|
p = ht->arData;
|
|
|
|
end = p + ht->nNumUsed;
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
|
2015-04-24 14:34:50 +08:00
|
|
|
do {
|
2015-04-25 03:43:50 +08:00
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
|
|
|
} while (++p != end);
|
2016-10-20 04:10:42 +08:00
|
|
|
} else if (HT_IS_WITHOUT_HOLES(ht)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
2016-09-28 01:47:48 +08:00
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
2015-04-24 14:34:50 +08:00
|
|
|
} while (++p != end);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
|
|
|
|
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC);
|
2016-09-28 01:47:48 +08:00
|
|
|
if (EXPECTED(p->key)) {
|
|
|
|
zend_string_release(p->key);
|
|
|
|
}
|
2015-04-24 14:34:50 +08:00
|
|
|
}
|
|
|
|
} while (++p != end);
|
2014-12-27 16:07:12 +08:00
|
|
|
}
|
2015-04-24 14:34:50 +08:00
|
|
|
HT_HASH_RESET(ht);
|
2014-11-25 19:17:21 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = 0;
|
2011-04-15 20:43:20 +08:00
|
|
|
ht->nNumOfElements = 0;
|
|
|
|
ht->nNextFreeElement = 0;
|
2018-03-22 07:41:49 +08:00
|
|
|
ht->nInternalPointer = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht)
|
2000-01-17 04:59:03 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2000-01-20 03:44:32 +08:00
|
|
|
Bucket *p;
|
2000-01-17 04:59:03 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-17 04:59:03 +08:00
|
|
|
|
2015-04-24 18:00:56 +08:00
|
|
|
p = ht->arData;
|
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++, p++) {
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
2000-01-17 04:59:03 +08:00
|
|
|
}
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_INITIALIZED) {
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2010-04-20 18:49:22 +08:00
|
|
|
}
|
2000-01-17 04:59:03 +08:00
|
|
|
|
2000-01-18 03:17:58 +08:00
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
2000-01-17 04:59:03 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht)
|
2002-04-20 00:53:36 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2002-04-20 00:53:36 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2002-04-20 00:53:36 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
2015-04-24 18:00:56 +08:00
|
|
|
p = ht->arData + ht->nNumUsed;
|
2014-02-10 14:04:30 +08:00
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
2015-04-24 18:00:56 +08:00
|
|
|
p--;
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
2002-04-20 00:53:36 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_INITIALIZED) {
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2010-04-20 18:49:22 +08:00
|
|
|
}
|
2002-04-20 00:53:36 +08:00
|
|
|
|
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
|
|
|
}
|
|
|
|
|
2015-01-03 17:22:58 +08:00
|
|
|
/* This is used to recurse elements and selectively delete certain entries
|
|
|
|
* from a hashtable. apply_func() receives the data and decides if the entry
|
|
|
|
* should be deleted or recursion should be stopped. The following three
|
2006-11-04 03:02:16 +08:00
|
|
|
* return codes are possible:
|
|
|
|
* ZEND_HASH_APPLY_KEEP - continue
|
|
|
|
* ZEND_HASH_APPLY_STOP - stop iteration
|
|
|
|
* ZEND_HASH_APPLY_REMOVE - delete the element, combineable with the former
|
1999-04-08 02:10:10 +08:00
|
|
|
*/
|
2000-01-17 04:59:03 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2000-01-20 13:27:57 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
int result;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-20 13:27:57 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2015-11-11 12:31:45 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2014-12-14 06:06:14 +08:00
|
|
|
result = apply_func(&p->val);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2006-11-04 03:02:16 +08:00
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
2000-01-20 03:44:32 +08:00
|
|
|
}
|
2006-11-04 03:02:16 +08:00
|
|
|
if (result & ZEND_HASH_APPLY_STOP) {
|
|
|
|
break;
|
|
|
|
}
|
2000-01-20 13:27:57 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2000-01-20 13:27:57 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
int result;
|
2000-01-20 13:27:57 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2015-11-11 12:31:45 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2014-12-14 06:06:14 +08:00
|
|
|
result = apply_func(&p->val, argument);
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2006-11-04 03:02:16 +08:00
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
2000-01-20 13:27:57 +08:00
|
|
|
}
|
2006-11-04 03:02:16 +08:00
|
|
|
if (result & ZEND_HASH_APPLY_STOP) {
|
|
|
|
break;
|
|
|
|
}
|
2000-01-20 13:27:57 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-14 03:19:43 +08:00
|
|
|
ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int num_args, ...)
|
1999-08-26 03:02:13 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2000-01-20 03:44:32 +08:00
|
|
|
Bucket *p;
|
1999-08-26 03:02:13 +08:00
|
|
|
va_list args;
|
|
|
|
zend_hash_key hash_key;
|
2014-02-10 14:04:30 +08:00
|
|
|
int result;
|
1999-08-26 03:02:13 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2015-11-11 12:31:45 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2002-11-15 22:25:44 +08:00
|
|
|
va_start(args, num_args);
|
2000-01-20 03:44:32 +08:00
|
|
|
hash_key.h = p->h;
|
2014-02-10 14:04:30 +08:00
|
|
|
hash_key.key = p->key;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
result = apply_func(&p->val, num_args, args, &hash_key);
|
2006-11-04 03:02:16 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
1999-08-26 03:02:13 +08:00
|
|
|
}
|
2006-11-04 03:02:16 +08:00
|
|
|
if (result & ZEND_HASH_APPLY_STOP) {
|
2011-08-06 06:18:42 +08:00
|
|
|
va_end(args);
|
2006-11-04 03:02:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-11-15 22:25:44 +08:00
|
|
|
va_end(args);
|
1999-08-26 03:02:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func)
|
2001-08-02 15:00:43 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *p;
|
|
|
|
int result;
|
2001-08-02 15:00:43 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
2015-11-11 12:31:45 +08:00
|
|
|
p = ht->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
result = apply_func(&p->val);
|
2001-08-02 15:00:43 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
_zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
|
2001-08-02 15:00:43 +08:00
|
|
|
}
|
|
|
|
if (result & ZEND_HASH_APPLY_STOP) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-26 03:02:13 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2014-03-26 22:07:31 +08:00
|
|
|
zval *new_entry, *data;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(source);
|
|
|
|
IS_CONSISTENT(target);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(target);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2015-01-03 17:22:58 +08:00
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
2014-02-10 14:04:30 +08:00
|
|
|
p = source->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2014-04-15 19:40:40 +08:00
|
|
|
/* INDIRECT element may point to UNDEF-ined slots */
|
2014-03-26 22:07:31 +08:00
|
|
|
data = &p->val;
|
|
|
|
if (Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
|
2014-03-26 22:07:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p->key) {
|
2014-03-26 22:07:31 +08:00
|
|
|
new_entry = zend_hash_update(target, p->key, data);
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
2014-03-26 22:07:31 +08:00
|
|
|
new_entry = zend_hash_index_update(target, p->h, data);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2005-04-25 16:21:15 +08:00
|
|
|
if (pCopyConstructor) {
|
|
|
|
pCopyConstructor(new_entry);
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
static zend_always_inline int zend_array_dup_element(HashTable *source, HashTable *target, uint32_t idx, Bucket *p, Bucket *q, int packed, int static_keys, int with_holes)
|
2014-05-24 00:37:53 +08:00
|
|
|
{
|
2015-04-25 03:43:50 +08:00
|
|
|
zval *data = &p->val;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
if (with_holes) {
|
|
|
|
if (!packed && Z_TYPE_INFO_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
}
|
|
|
|
if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (!packed) {
|
|
|
|
/* INDIRECT element may point to UNDEF-ined slots */
|
|
|
|
if (Z_TYPE_INFO_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
|
|
|
|
return 0;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (Z_OPT_REFCOUNTED_P(data)) {
|
|
|
|
if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1 &&
|
|
|
|
(Z_TYPE_P(Z_REFVAL_P(data)) != IS_ARRAY ||
|
|
|
|
Z_ARRVAL_P(Z_REFVAL_P(data)) != source)) {
|
|
|
|
data = Z_REFVAL_P(data);
|
|
|
|
if (!Z_OPT_REFCOUNTED_P(data)) {
|
|
|
|
break;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
Z_ADDREF_P(data);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
ZVAL_COPY_VALUE(&q->val, data);
|
2015-04-24 14:34:50 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
q->h = p->h;
|
|
|
|
if (packed) {
|
|
|
|
q->key = NULL;
|
|
|
|
} else {
|
|
|
|
uint32_t nIndex;
|
2015-04-24 14:34:50 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
q->key = p->key;
|
|
|
|
if (!static_keys && q->key) {
|
|
|
|
zend_string_addref(q->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
nIndex = q->h | target->nTableMask;
|
|
|
|
Z_NEXT(q->val) = HT_HASH(target, nIndex);
|
|
|
|
HT_HASH(target, nIndex) = HT_IDX_TO_HASH(idx);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_array_dup_packed_elements(HashTable *source, HashTable *target, int with_holes)
|
|
|
|
{
|
|
|
|
Bucket *p = source->arData;
|
|
|
|
Bucket *q = target->arData;
|
|
|
|
Bucket *end = p + source->nNumUsed;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!zend_array_dup_element(source, target, 0, p, q, 1, 1, with_holes)) {
|
|
|
|
if (with_holes) {
|
|
|
|
ZVAL_UNDEF(&q->val);
|
2015-04-24 14:34:50 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
}
|
|
|
|
p++; q++;
|
|
|
|
} while (p != end);
|
|
|
|
}
|
2014-05-24 00:37:53 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
static zend_always_inline uint32_t zend_array_dup_elements(HashTable *source, HashTable *target, int static_keys, int with_holes)
|
|
|
|
{
|
2016-12-21 06:48:43 +08:00
|
|
|
uint32_t idx = 0;
|
2015-04-25 03:43:50 +08:00
|
|
|
Bucket *p = source->arData;
|
|
|
|
Bucket *q = target->arData;
|
|
|
|
Bucket *end = p + source->nNumUsed;
|
2014-05-24 00:37:53 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
do {
|
|
|
|
if (!zend_array_dup_element(source, target, idx, p, q, 0, static_keys, with_holes)) {
|
|
|
|
uint32_t target_idx = idx;
|
|
|
|
|
|
|
|
idx++; p++;
|
|
|
|
while (p != end) {
|
|
|
|
if (zend_array_dup_element(source, target, target_idx, p, q, 0, static_keys, with_holes)) {
|
|
|
|
if (source->nInternalPointer == idx) {
|
|
|
|
target->nInternalPointer = target_idx;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
target_idx++; q++;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
idx++; p++;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
return target_idx;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-04-25 03:43:50 +08:00
|
|
|
idx++; p++; q++;
|
|
|
|
} while (p != end);
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source)
|
|
|
|
{
|
2016-12-21 06:48:43 +08:00
|
|
|
uint32_t idx;
|
2015-04-25 03:43:50 +08:00
|
|
|
HashTable *target;
|
|
|
|
|
|
|
|
IS_CONSISTENT(source);
|
|
|
|
|
|
|
|
ALLOC_HASHTABLE(target);
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_SET_REFCOUNT(target, 1);
|
2016-10-21 22:47:30 +08:00
|
|
|
GC_TYPE_INFO(target) = IS_ARRAY | (GC_COLLECTABLE << GC_FLAGS_SHIFT);
|
2015-04-25 03:43:50 +08:00
|
|
|
|
|
|
|
target->nTableSize = source->nTableSize;
|
2018-03-06 05:56:16 +08:00
|
|
|
target->pDestructor = ZVAL_PTR_DTOR;
|
2015-04-25 03:43:50 +08:00
|
|
|
|
2018-03-21 16:57:54 +08:00
|
|
|
if (source->nNumOfElements == 0) {
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(target) = (HT_FLAGS(source) & ~(HASH_FLAG_INITIALIZED|HASH_FLAG_PACKED)) | HASH_FLAG_STATIC_KEYS;
|
2015-04-25 03:43:50 +08:00
|
|
|
target->nTableMask = HT_MIN_MASK;
|
2014-05-24 00:37:53 +08:00
|
|
|
target->nNumUsed = 0;
|
|
|
|
target->nNumOfElements = 0;
|
|
|
|
target->nNextFreeElement = 0;
|
2018-03-22 07:41:49 +08:00
|
|
|
target->nInternalPointer = 0;
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_SET_DATA_ADDR(target, &uninitialized_bucket);
|
2015-04-25 03:43:50 +08:00
|
|
|
} else if (GC_FLAGS(source) & IS_ARRAY_IMMUTABLE) {
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(target) = HT_FLAGS(source);
|
2015-04-25 03:43:50 +08:00
|
|
|
target->nTableMask = source->nTableMask;
|
|
|
|
target->nNumUsed = source->nNumUsed;
|
|
|
|
target->nNumOfElements = source->nNumOfElements;
|
|
|
|
target->nNextFreeElement = source->nNextFreeElement;
|
|
|
|
HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
|
|
|
|
target->nInternalPointer = source->nInternalPointer;
|
|
|
|
memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_USED_SIZE(source));
|
2018-01-22 18:36:15 +08:00
|
|
|
} else if (HT_FLAGS(source) & HASH_FLAG_PACKED) {
|
|
|
|
HT_FLAGS(target) = HT_FLAGS(source);
|
2018-03-21 16:57:54 +08:00
|
|
|
target->nTableMask = HT_MIN_MASK;
|
2015-04-25 03:43:50 +08:00
|
|
|
target->nNumUsed = source->nNumUsed;
|
|
|
|
target->nNumOfElements = source->nNumOfElements;
|
|
|
|
target->nNextFreeElement = source->nNextFreeElement;
|
2018-05-04 07:41:35 +08:00
|
|
|
HT_SET_DATA_ADDR(target, emalloc(HT_SIZE_EX(target->nTableSize, HT_MIN_MASK)));
|
2018-03-22 07:41:49 +08:00
|
|
|
target->nInternalPointer =
|
|
|
|
(source->nInternalPointer < source->nNumUsed) ?
|
|
|
|
source->nInternalPointer : 0;
|
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
HT_HASH_RESET_PACKED(target);
|
|
|
|
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_IS_WITHOUT_HOLES(target)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
zend_array_dup_packed_elements(source, target, 0);
|
|
|
|
} else {
|
|
|
|
zend_array_dup_packed_elements(source, target, 1);
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(target) = HT_FLAGS(source);
|
2015-04-25 03:43:50 +08:00
|
|
|
target->nTableMask = source->nTableMask;
|
|
|
|
target->nNextFreeElement = source->nNextFreeElement;
|
2018-03-22 07:41:49 +08:00
|
|
|
target->nInternalPointer =
|
|
|
|
(source->nInternalPointer < source->nNumUsed) ?
|
|
|
|
source->nInternalPointer : 0;
|
2016-12-21 06:48:43 +08:00
|
|
|
|
2015-04-25 03:43:50 +08:00
|
|
|
HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
|
|
|
|
HT_HASH_RESET(target);
|
|
|
|
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_HAS_STATIC_KEYS_ONLY(target)) {
|
|
|
|
if (HT_IS_WITHOUT_HOLES(source)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
idx = zend_array_dup_elements(source, target, 1, 0);
|
|
|
|
} else {
|
|
|
|
idx = zend_array_dup_elements(source, target, 1, 1);
|
|
|
|
}
|
|
|
|
} else {
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_IS_WITHOUT_HOLES(source)) {
|
2015-04-25 03:43:50 +08:00
|
|
|
idx = zend_array_dup_elements(source, target, 0, 0);
|
|
|
|
} else {
|
|
|
|
idx = zend_array_dup_elements(source, target, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target->nNumUsed = idx;
|
|
|
|
target->nNumOfElements = idx;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
2015-02-14 03:20:39 +08:00
|
|
|
return target;
|
2014-05-24 00:37:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, zend_bool overwrite ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *t;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(source);
|
|
|
|
IS_CONSISTENT(target);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(target);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2016-03-02 20:09:04 +08:00
|
|
|
if (overwrite) {
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_INDIRECT) &&
|
|
|
|
UNEXPECTED(Z_TYPE_P(Z_INDIRECT(p->val)) == IS_UNDEF)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p->key) {
|
|
|
|
t = _zend_hash_add_or_update_i(target, p->key, &p->val, HASH_UPDATE | HASH_UPDATE_INDIRECT ZEND_FILE_LINE_RELAY_CC);
|
2018-06-01 16:58:57 +08:00
|
|
|
if (pCopyConstructor) {
|
2016-03-01 19:19:07 +08:00
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-02 20:09:04 +08:00
|
|
|
t = zend_hash_index_update(target, p->h, &p->val);
|
2018-06-01 16:58:57 +08:00
|
|
|
if (pCopyConstructor) {
|
2016-03-01 19:19:07 +08:00
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2016-03-02 20:09:04 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_INDIRECT) &&
|
|
|
|
UNEXPECTED(Z_TYPE_P(Z_INDIRECT(p->val)) == IS_UNDEF)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p->key) {
|
|
|
|
t = _zend_hash_add_or_update_i(target, p->key, &p->val, HASH_ADD | HASH_UPDATE_INDIRECT ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
if (t && pCopyConstructor) {
|
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t = zend_hash_index_add(target, p->h, &p->val);
|
|
|
|
if (t && pCopyConstructor) {
|
2014-02-10 14:04:30 +08:00
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
static zend_bool ZEND_FASTCALL zend_hash_replace_checker_wrapper(HashTable *target, zval *source_data, Bucket *p, void *pParam, merge_checker_func_t merge_checker_func)
|
2003-02-04 20:12:34 +08:00
|
|
|
{
|
|
|
|
zend_hash_key hash_key;
|
|
|
|
|
|
|
|
hash_key.h = p->h;
|
2014-02-10 14:04:30 +08:00
|
|
|
hash_key.key = p->key;
|
2003-02-04 20:12:34 +08:00
|
|
|
return merge_checker_func(target, source_data, &hash_key, pParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
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)
|
2000-10-12 00:22:40 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2000-10-12 00:22:40 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *t;
|
2000-10-12 00:22:40 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(source);
|
|
|
|
IS_CONSISTENT(target);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(target);
|
2000-10-12 00:22:40 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2014-02-10 14:04:30 +08:00
|
|
|
if (zend_hash_replace_checker_wrapper(target, &p->val, p, pParam, pMergeSource)) {
|
|
|
|
t = zend_hash_update(target, p->key, &p->val);
|
2018-06-01 16:58:57 +08:00
|
|
|
if (pCopyConstructor) {
|
2003-02-04 20:12:34 +08:00
|
|
|
pCopyConstructor(t);
|
2000-10-12 00:22:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-08 05:26:16 +08:00
|
|
|
/* Returns the hash table data if found and NULL if not. */
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2017-11-24 19:01:19 +08:00
|
|
|
p = zend_hash_find_bucket(ht, key, 0);
|
|
|
|
return p ? &p->val : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_find_known_hash(const HashTable *ht, zend_string *key)
|
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
p = zend_hash_find_bucket(ht, key, 1);
|
2014-02-10 14:04:30 +08:00
|
|
|
return p ? &p->val : NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-03-14 19:35:57 +08:00
|
|
|
h = zend_inline_hash_func(str, len);
|
2014-02-10 14:04:30 +08:00
|
|
|
p = zend_hash_str_find_bucket(ht, str, len, h);
|
|
|
|
return p ? &p->val : NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zend_bool ZEND_FASTCALL zend_hash_exists(const HashTable *ht, zend_string *key)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2017-11-24 19:01:19 +08:00
|
|
|
p = zend_hash_find_bucket(ht, key, 0);
|
2014-02-10 14:04:30 +08:00
|
|
|
return p ? 1 : 0;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zend_bool ZEND_FASTCALL zend_hash_str_exists(const HashTable *ht, const char *str, size_t len)
|
2003-02-05 21:19:59 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_ulong h;
|
2003-02-05 21:19:59 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-03-14 19:35:57 +08:00
|
|
|
h = zend_inline_hash_func(str, len);
|
2014-02-10 14:04:30 +08:00
|
|
|
p = zend_hash_str_find_bucket(ht, str, len, h);
|
|
|
|
return p ? 1 : 0;
|
2003-02-05 21:19:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-09-16 05:04:34 +08:00
|
|
|
if (h < ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + h;
|
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
|
|
|
return &p->val;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
p = zend_hash_index_find_bucket(ht, h);
|
|
|
|
return p ? &p->val : NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 22:33:35 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h)
|
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
p = zend_hash_index_find_bucket(ht, h);
|
|
|
|
return p ? &p->val : NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zend_bool ZEND_FASTCALL zend_hash_index_exists(const HashTable *ht, zend_ulong h)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-09-16 05:04:34 +08:00
|
|
|
if (h < ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (Z_TYPE(ht->arData[h].val) != IS_UNDEF) {
|
|
|
|
return 1;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
p = zend_hash_index_find_bucket(ht, h);
|
|
|
|
return p ? 1 : 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
|
2018-03-22 07:41:49 +08:00
|
|
|
*pos = _zend_hash_get_first_pos(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 17:22:58 +08:00
|
|
|
/* This function will be extremely optimized by remembering
|
1999-04-08 02:10:10 +08:00
|
|
|
* the end of the list
|
|
|
|
*/
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
2014-04-08 03:14:17 +08:00
|
|
|
*pos = idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 07:41:49 +08:00
|
|
|
*pos = ht->nNumUsed;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2000-03-16 00:25:59 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx < ht->nNumUsed) {
|
|
|
|
if (idx == 0) {
|
|
|
|
idx = _zend_hash_get_first_pos(ht);
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
*pos = idx;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
while (1) {
|
|
|
|
idx++;
|
|
|
|
if (idx >= ht->nNumUsed) {
|
2018-03-22 07:41:49 +08:00
|
|
|
*pos = ht->nNumUsed;
|
2014-02-10 14:04:30 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
2014-04-08 03:14:17 +08:00
|
|
|
*pos = idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-22 07:41:49 +08:00
|
|
|
return FAILURE;
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2000-03-16 00:25:59 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx < ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
2014-04-08 03:14:17 +08:00
|
|
|
*pos = idx;
|
2014-02-10 14:04:30 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 07:41:49 +08:00
|
|
|
*pos = ht->nNumUsed;
|
2014-02-10 14:04:30 +08:00
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function should be made binary safe */
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx < ht->nNumUsed) {
|
|
|
|
if (idx == 0) {
|
|
|
|
idx = _zend_hash_get_first_pos(ht);
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
return HASH_KEY_NON_EXISTENT;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
2014-12-24 05:00:18 +08:00
|
|
|
*str_index = p->key;
|
1999-04-08 02:10:10 +08:00
|
|
|
return HASH_KEY_IS_STRING;
|
|
|
|
} else {
|
|
|
|
*num_index = p->h;
|
2014-08-26 01:24:55 +08:00
|
|
|
return HASH_KEY_IS_LONG;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 05:09:06 +08:00
|
|
|
return HASH_KEY_NON_EXISTENT;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2013-02-17 02:13:36 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx >= ht->nNumUsed) {
|
2014-04-03 19:26:23 +08:00
|
|
|
ZVAL_NULL(key);
|
2013-02-17 02:13:36 +08:00
|
|
|
} else {
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx == 0) {
|
|
|
|
idx = _zend_hash_get_first_pos(ht);
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
ZVAL_NULL(key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
2014-09-19 21:32:50 +08:00
|
|
|
ZVAL_STR_COPY(key, p->key);
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
ZVAL_LONG(key, p->h);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2013-02-17 02:13:36 +08:00
|
|
|
}
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos)
|
1999-06-08 06:49:33 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx < ht->nNumUsed) {
|
|
|
|
if (idx == 0) {
|
|
|
|
idx = _zend_hash_get_first_pos(ht);
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
return HASH_KEY_NON_EXISTENT;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
1999-06-08 06:49:33 +08:00
|
|
|
return HASH_KEY_IS_STRING;
|
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
return HASH_KEY_IS_LONG;
|
1999-06-08 06:49:33 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 05:09:06 +08:00
|
|
|
return HASH_KEY_NON_EXISTENT;
|
1999-06-08 06:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx = *pos;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2018-03-22 07:41:49 +08:00
|
|
|
if (idx < ht->nNumUsed) {
|
|
|
|
if (idx == 0) {
|
|
|
|
idx = _zend_hash_get_first_pos(ht);
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
return &p->val;
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q)
|
|
|
|
{
|
2015-01-14 17:22:58 +08:00
|
|
|
zval val;
|
|
|
|
zend_ulong h;
|
|
|
|
zend_string *key;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&val, &p->val);
|
|
|
|
h = p->h;
|
|
|
|
key = p->key;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&p->val, &q->val);
|
|
|
|
p->h = q->h;
|
|
|
|
p->key = q->key;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &val);
|
|
|
|
q->h = h;
|
|
|
|
q->key = key;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q)
|
|
|
|
{
|
2015-01-14 17:22:58 +08:00
|
|
|
zval val;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&val, &p->val);
|
|
|
|
ZVAL_COPY_VALUE(&p->val, &q->val);
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &val);
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q)
|
|
|
|
{
|
2015-01-14 17:22:58 +08:00
|
|
|
zval val;
|
|
|
|
zend_ulong h;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&val, &p->val);
|
|
|
|
h = p->h;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&p->val, &q->val);
|
|
|
|
p->h = q->h;
|
|
|
|
|
|
|
|
ZVAL_COPY_VALUE(&q->val, &val);
|
|
|
|
q->h = h;
|
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, compare_func_t compar, zend_bool renumber)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t i, j;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2016-05-05 20:49:08 +08:00
|
|
|
HT_ASSERT_RC1(ht);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2002-08-18 00:07:26 +08:00
|
|
|
if (!(ht->nNumOfElements>1) && !(renumber && ht->nNumOfElements>0)) { /* Doesn't require sorting */
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-10-20 04:10:42 +08:00
|
|
|
if (HT_IS_WITHOUT_HOLES(ht)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
i = ht->nNumUsed;
|
2005-04-25 16:21:15 +08:00
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
for (j = 0, i = 0; j < ht->nNumUsed; j++) {
|
|
|
|
p = ht->arData + j;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2014-02-10 14:04:30 +08:00
|
|
|
if (i != j) {
|
|
|
|
ht->arData[i] = *p;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2005-04-25 16:21:15 +08:00
|
|
|
}
|
2004-07-30 03:18:46 +08:00
|
|
|
|
2015-01-14 17:22:58 +08:00
|
|
|
sort((void *)ht->arData, i, sizeof(Bucket), compar,
|
|
|
|
(swap_func_t)(renumber? zend_hash_bucket_renum_swap :
|
2018-01-22 18:36:15 +08:00
|
|
|
((HT_FLAGS(ht) & HASH_FLAG_PACKED) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
ht->nNumUsed = i;
|
|
|
|
ht->nInternalPointer = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
if (renumber) {
|
2014-02-10 14:04:30 +08:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
p = ht->arData + j;
|
|
|
|
p->h = j;
|
|
|
|
if (p->key) {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_release(p->key);
|
2014-02-10 14:04:30 +08:00
|
|
|
p->key = NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-11-15 20:12:25 +08:00
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->nNextFreeElement = i;
|
|
|
|
}
|
2018-01-22 18:36:15 +08:00
|
|
|
if (HT_FLAGS(ht) & HASH_FLAG_PACKED) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (!renumber) {
|
|
|
|
zend_hash_packed_to_hash(ht);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (renumber) {
|
2015-08-13 23:02:15 +08:00
|
|
|
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
|
2015-03-13 22:13:19 +08:00
|
|
|
Bucket *old_buckets = ht->arData;
|
|
|
|
|
2017-10-06 07:54:14 +08:00
|
|
|
new_data = pemalloc(HT_SIZE_EX(ht->nTableSize, HT_MIN_MASK), (GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
|
2018-01-22 18:36:15 +08:00
|
|
|
HT_FLAGS(ht) |= HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
|
2015-03-13 22:13:19 +08:00
|
|
|
ht->nTableMask = HT_MIN_MASK;
|
2015-08-13 23:02:15 +08:00
|
|
|
HT_SET_DATA_ADDR(ht, new_data);
|
2015-03-13 22:13:19 +08:00
|
|
|
memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
|
2017-10-06 07:54:14 +08:00
|
|
|
pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
|
2015-03-13 22:13:19 +08:00
|
|
|
HT_HASH_RESET_PACKED(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
|
|
|
zend_hash_rehash(ht);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-06-20 23:35:16 +08:00
|
|
|
static zend_always_inline int zend_hash_compare_impl(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered) {
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx1, idx2;
|
2001-07-16 02:57:43 +08:00
|
|
|
|
2015-06-20 23:23:58 +08:00
|
|
|
if (ht1->nNumOfElements != ht2->nNumOfElements) {
|
|
|
|
return ht1->nNumOfElements > ht2->nNumOfElements ? 1 : -1;
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
2000-06-05 05:59:49 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx1 = 0, idx2 = 0; idx1 < ht1->nNumUsed; idx1++) {
|
2015-06-20 23:35:16 +08:00
|
|
|
Bucket *p1 = ht1->arData + idx1, *p2;
|
|
|
|
zval *pData1, *pData2;
|
|
|
|
int result;
|
2000-06-03 18:34:19 +08:00
|
|
|
|
2015-06-20 23:35:16 +08:00
|
|
|
if (Z_TYPE(p1->val) == IS_UNDEF) continue;
|
2000-06-05 05:59:49 +08:00
|
|
|
if (ordered) {
|
2014-02-10 14:04:30 +08:00
|
|
|
while (1) {
|
2015-06-20 23:35:16 +08:00
|
|
|
ZEND_ASSERT(idx2 != ht2->nNumUsed);
|
2014-02-10 14:04:30 +08:00
|
|
|
p2 = ht2->arData + idx2;
|
2014-04-24 02:33:16 +08:00
|
|
|
if (Z_TYPE(p2->val) != IS_UNDEF) break;
|
2014-02-10 14:04:30 +08:00
|
|
|
idx2++;
|
2015-01-03 17:22:58 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p1->key == NULL && p2->key == NULL) { /* numeric indices */
|
2015-06-20 22:39:23 +08:00
|
|
|
if (p1->h != p2->h) {
|
|
|
|
return p1->h > p2->h ? 1 : -1;
|
2000-06-05 05:59:49 +08:00
|
|
|
}
|
2015-06-20 23:14:48 +08:00
|
|
|
} else if (p1->key != NULL && p2->key != NULL) { /* string indices */
|
2015-06-30 18:59:27 +08:00
|
|
|
if (ZSTR_LEN(p1->key) != ZSTR_LEN(p2->key)) {
|
|
|
|
return ZSTR_LEN(p1->key) > ZSTR_LEN(p2->key) ? 1 : -1;
|
2000-06-05 05:59:49 +08:00
|
|
|
}
|
2015-06-20 23:14:48 +08:00
|
|
|
|
2015-06-30 18:59:27 +08:00
|
|
|
result = memcmp(ZSTR_VAL(p1->key), ZSTR_VAL(p2->key), ZSTR_LEN(p1->key));
|
2014-02-10 14:04:30 +08:00
|
|
|
if (result != 0) {
|
2000-06-05 05:59:49 +08:00
|
|
|
return result;
|
|
|
|
}
|
2015-06-20 23:14:48 +08:00
|
|
|
} else {
|
|
|
|
/* Mixed key types: A string key is considered as larger */
|
|
|
|
return p1->key != NULL ? 1 : -1;
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
pData2 = &p2->val;
|
2015-06-20 23:35:16 +08:00
|
|
|
idx2++;
|
2000-06-05 05:59:49 +08:00
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p1->key == NULL) { /* numeric index */
|
|
|
|
pData2 = zend_hash_index_find(ht2, p1->h);
|
|
|
|
if (pData2 == NULL) {
|
2000-06-05 05:59:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else { /* string index */
|
2014-02-10 14:04:30 +08:00
|
|
|
pData2 = zend_hash_find(ht2, p1->key);
|
|
|
|
if (pData2 == NULL) {
|
2000-06-05 05:59:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-20 23:35:16 +08:00
|
|
|
|
2014-03-26 22:07:31 +08:00
|
|
|
pData1 = &p1->val;
|
|
|
|
if (Z_TYPE_P(pData1) == IS_INDIRECT) {
|
|
|
|
pData1 = Z_INDIRECT_P(pData1);
|
|
|
|
}
|
|
|
|
if (Z_TYPE_P(pData2) == IS_INDIRECT) {
|
|
|
|
pData2 = Z_INDIRECT_P(pData2);
|
|
|
|
}
|
2015-06-20 23:35:16 +08:00
|
|
|
|
2014-03-26 22:07:31 +08:00
|
|
|
if (Z_TYPE_P(pData1) == IS_UNDEF) {
|
|
|
|
if (Z_TYPE_P(pData2) != IS_UNDEF) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (Z_TYPE_P(pData2) == IS_UNDEF) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
result = compar(pData1, pData2);
|
2015-06-20 23:35:16 +08:00
|
|
|
if (result != 0) {
|
|
|
|
return result;
|
|
|
|
}
|
2000-06-05 05:59:49 +08:00
|
|
|
}
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2015-06-20 23:35:16 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
IS_CONSISTENT(ht1);
|
|
|
|
IS_CONSISTENT(ht2);
|
|
|
|
|
2017-10-06 06:34:50 +08:00
|
|
|
if (ht1 == ht2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-06 16:47:11 +08:00
|
|
|
/* It's enough to protect only one of the arrays.
|
|
|
|
* The second one may be referenced from the first and this may cause
|
|
|
|
* false recursion detection.
|
|
|
|
*/
|
|
|
|
if (UNEXPECTED(GC_IS_RECURSIVE(ht1))) {
|
2017-10-06 06:34:50 +08:00
|
|
|
zend_error_noreturn(E_ERROR, "Nesting level too deep - recursive dependency?");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(GC_FLAGS(ht1) & GC_IMMUTABLE)) {
|
|
|
|
GC_PROTECT_RECURSION(ht1);
|
|
|
|
}
|
2015-06-20 23:35:16 +08:00
|
|
|
result = zend_hash_compare_impl(ht1, ht2, compar, ordered);
|
2017-10-06 06:34:50 +08:00
|
|
|
if (!(GC_FLAGS(ht1) & GC_IMMUTABLE)) {
|
|
|
|
GC_UNPROTECT_RECURSION(ht1);
|
|
|
|
}
|
2015-06-20 23:35:16 +08:00
|
|
|
|
|
|
|
return result;
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-09-16 05:01:52 +08:00
|
|
|
uint32_t idx;
|
2001-08-11 23:56:40 +08:00
|
|
|
Bucket *p, *res;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
if (ht->nNumOfElements == 0 ) {
|
2014-02-10 14:04:30 +08:00
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = 0;
|
|
|
|
while (1) {
|
|
|
|
if (idx == ht->nNumUsed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) break;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
res = ht->arData + idx;
|
|
|
|
for (; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
2015-04-24 18:00:56 +08:00
|
|
|
if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
|
2015-01-03 17:22:58 +08:00
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
if (flag) {
|
2014-12-14 06:06:14 +08:00
|
|
|
if (compar(res, p) < 0) { /* max */
|
1999-04-08 02:10:10 +08:00
|
|
|
res = p;
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
if (compar(res, p) > 0) { /* min */
|
1999-04-08 02:10:10 +08:00
|
|
|
res = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return &res->val;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 02:10:09 +08:00
|
|
|
ZEND_API int ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx)
|
2014-09-18 22:50:05 +08:00
|
|
|
{
|
|
|
|
register const char *tmp = key;
|
|
|
|
|
2014-12-24 04:19:04 +08:00
|
|
|
const char *end = key + length;
|
|
|
|
|
|
|
|
if (*tmp == '-') {
|
2014-09-18 22:50:05 +08:00
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
2014-12-24 04:19:04 +08:00
|
|
|
if ((*tmp == '0' && length > 1) /* numbers with leading zeros */
|
2014-09-18 22:50:05 +08:00
|
|
|
|| (end - tmp > MAX_LENGTH_OF_LONG - 1) /* number too long */
|
|
|
|
|| (SIZEOF_ZEND_LONG == 4 &&
|
|
|
|
end - tmp == MAX_LENGTH_OF_LONG - 1 &&
|
|
|
|
*tmp > '2')) { /* overflow */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*idx = (*tmp - '0');
|
|
|
|
while (1) {
|
|
|
|
++tmp;
|
|
|
|
if (tmp == end) {
|
|
|
|
if (*key == '-') {
|
|
|
|
if (*idx-1 > ZEND_LONG_MAX) { /* overflow */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*idx = 0 - *idx;
|
|
|
|
} else if (*idx > ZEND_LONG_MAX) { /* overflow */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (*tmp <= '9' && *tmp >= '0') {
|
|
|
|
*idx = (*idx * 10) + (*tmp - '0');
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 02:20:45 +08:00
|
|
|
/* Takes a "symtable" hashtable (contains integer and non-numeric string keys)
|
|
|
|
* and converts it to a "proptable" (contains only string keys).
|
|
|
|
* If the symtable didn't need duplicating, its refcount is incremented.
|
|
|
|
*/
|
|
|
|
ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht)
|
|
|
|
{
|
|
|
|
zend_ulong num_key;
|
|
|
|
zend_string *str_key;
|
|
|
|
zval *zv;
|
|
|
|
|
|
|
|
if (UNEXPECTED(HT_IS_PACKED(ht))) {
|
|
|
|
goto convert;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
|
|
|
|
if (!str_key) {
|
|
|
|
goto convert;
|
|
|
|
}
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
|
|
|
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_ADDREF(ht);
|
2016-11-15 02:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ht;
|
|
|
|
|
|
|
|
convert:
|
|
|
|
{
|
2017-09-20 07:25:56 +08:00
|
|
|
HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
|
2016-11-15 02:20:45 +08:00
|
|
|
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
|
|
|
|
if (!str_key) {
|
|
|
|
str_key = zend_long_to_str(num_key);
|
|
|
|
zend_string_delref(str_key);
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (Z_OPT_REFCOUNTED_P(zv)) {
|
|
|
|
if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
|
|
|
|
zv = Z_REFVAL_P(zv);
|
|
|
|
if (!Z_OPT_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Z_ADDREF_P(zv);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
zend_hash_update(new_ht, str_key, zv);
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
|
|
|
return new_ht;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Takes a "proptable" hashtable (contains only string keys) and converts it to
|
|
|
|
* a "symtable" (contains integer and non-numeric string keys).
|
|
|
|
* If the proptable didn't need duplicating, its refcount is incremented.
|
|
|
|
*/
|
|
|
|
ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, zend_bool always_duplicate)
|
|
|
|
{
|
|
|
|
zend_ulong num_key;
|
|
|
|
zend_string *str_key;
|
|
|
|
zval *zv;
|
|
|
|
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
|
|
|
|
/* The `str_key &&` here might seem redundant: property tables should
|
|
|
|
* only have string keys. Unfortunately, this isn't true, at the very
|
|
|
|
* least because of ArrayObject, which stores a symtable where the
|
|
|
|
* property table should be.
|
|
|
|
*/
|
|
|
|
if (str_key && ZEND_HANDLE_NUMERIC(str_key, num_key)) {
|
|
|
|
goto convert;
|
|
|
|
}
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
|
|
|
if (always_duplicate) {
|
|
|
|
return zend_array_dup(ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EXPECTED(!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE))) {
|
2017-10-27 06:28:58 +08:00
|
|
|
GC_ADDREF(ht);
|
2016-11-15 02:20:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ht;
|
|
|
|
|
|
|
|
convert:
|
|
|
|
{
|
2017-09-20 07:25:56 +08:00
|
|
|
HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
|
2016-11-15 02:20:45 +08:00
|
|
|
|
|
|
|
ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
|
|
|
|
do {
|
|
|
|
if (Z_OPT_REFCOUNTED_P(zv)) {
|
|
|
|
if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
|
|
|
|
zv = Z_REFVAL_P(zv);
|
|
|
|
if (!Z_OPT_REFCOUNTED_P(zv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Z_ADDREF_P(zv);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
/* Again, thank ArrayObject for `!str_key ||`. */
|
|
|
|
if (!str_key || ZEND_HANDLE_NUMERIC(str_key, num_key)) {
|
|
|
|
zend_hash_index_update(new_ht, num_key, zv);
|
|
|
|
} else {
|
|
|
|
zend_hash_update(new_ht, str_key, zv);
|
|
|
|
}
|
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
|
|
|
|
return new_ht;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2003-02-01 09:49:15 +08:00
|
|
|
* indent-tabs-mode: t
|
1999-04-08 02:10:10 +08:00
|
|
|
* End:
|
2017-07-05 00:12:45 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-08 02:10:10 +08:00
|
|
|
*/
|