1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1998-2014 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, |
|
1999-07-16 22:58:16 +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> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
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"
|
1999-09-07 00:14:08 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
#if ZEND_DEBUG
|
2014-02-10 14:04:30 +08:00
|
|
|
/*
|
|
|
|
#define HASH_MASK_CONSISTENCY 0x60
|
|
|
|
*/
|
|
|
|
#define HT_OK 0x00
|
|
|
|
#define HT_IS_DESTROYING 0x20
|
|
|
|
#define HT_DESTROYED 0x40
|
|
|
|
#define HT_CLEANING 0x60
|
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
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((ht->flags & HASH_MASK_CONSISTENCY) == HT_OK) {
|
2000-06-03 09:49:49 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
switch ((ht->flags & HASH_MASK_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 { \
|
|
|
|
(ht)->flags |= n; \
|
|
|
|
} 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
|
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
#define HASH_PROTECT_RECURSION(ht) \
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((ht)->flags & HASH_FLAG_APPLY_PROTECTION) { \
|
2001-07-16 02:57:43 +08:00
|
|
|
if ((ht)->nApplyCount++ >= 3) { \
|
|
|
|
zend_error(E_ERROR, "Nesting level too deep - recursive dependency?"); \
|
2000-07-11 22:27:31 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2002-04-20 00:43:27 +08:00
|
|
|
#define HASH_UNPROTECT_RECURSION(ht) \
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((ht)->flags & HASH_FLAG_APPLY_PROTECTION) { \
|
2002-04-20 00:43:27 +08:00
|
|
|
(ht)->nApplyCount--; \
|
|
|
|
}
|
2000-06-17 22:11:57 +08:00
|
|
|
|
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); \
|
|
|
|
}
|
|
|
|
|
2013-06-18 00:51:59 +08:00
|
|
|
static void zend_hash_do_resize(HashTable *ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
#define CHECK_INIT(ht, packed) do { \
|
|
|
|
if (UNEXPECTED((ht)->nTableMask == 0)) { \
|
|
|
|
if (packed) { \
|
2014-04-07 22:22:03 +08:00
|
|
|
(ht)->arData = (Bucket *) safe_pemalloc((ht)->nTableSize, sizeof(Bucket), 0, (ht)->flags & HASH_FLAG_PERSISTENT); \
|
2014-02-10 14:04:30 +08:00
|
|
|
(ht)->flags |= HASH_FLAG_PACKED; \
|
|
|
|
} else { \
|
2014-04-07 22:22:03 +08:00
|
|
|
(ht)->arData = (Bucket *) safe_pemalloc((ht)->nTableSize, sizeof(Bucket) + sizeof(zend_uint), 0, (ht)->flags & HASH_FLAG_PERSISTENT); \
|
|
|
|
(ht)->arHash = (zend_uint*)((ht)->arData + (ht)->nTableSize); \
|
2014-02-10 14:04:30 +08:00
|
|
|
memset((ht)->arHash, INVALID_IDX, (ht)->nTableSize * sizeof(zend_uint)); \
|
|
|
|
} \
|
2011-04-15 17:59:42 +08:00
|
|
|
(ht)->nTableMask = (ht)->nTableSize - 1; \
|
2010-04-20 18:49:22 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static const zend_uint uninitialized_bucket = {INVALID_IDX};
|
2003-07-23 00:06:07 +08:00
|
|
|
|
2013-11-28 22:40:46 +08:00
|
|
|
ZEND_API int _zend_hash_init(HashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2001-07-11 04:31:42 +08:00
|
|
|
uint i = 3;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-03-04 18:17:17 +08:00
|
|
|
ht->flags = 0;
|
2000-01-18 03:17:58 +08:00
|
|
|
SET_INCONSISTENT(HT_OK);
|
2001-07-11 04:31:42 +08:00
|
|
|
|
2007-01-21 07:10:02 +08:00
|
|
|
if (nSize >= 0x80000000) {
|
|
|
|
/* prevent overflow */
|
|
|
|
ht->nTableSize = 0x80000000;
|
|
|
|
} else {
|
|
|
|
while ((1U << i) < nSize) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
ht->nTableSize = 1 << i;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2001-07-11 04:31:42 +08:00
|
|
|
|
2010-04-20 18:49:22 +08:00
|
|
|
ht->nTableMask = 0; /* 0 means that ht->arBuckets is uninitialized */
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->pDestructor = pDestructor;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arData = NULL;
|
|
|
|
ht->arHash = (zend_uint*)&uninitialized_bucket;
|
|
|
|
ht->nNumUsed = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->nNumOfElements = 0;
|
|
|
|
ht->nNextFreeElement = 0;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
2014-03-04 18:17:17 +08:00
|
|
|
ht->flags |= HASH_FLAG_APPLY_PROTECTION;
|
2014-02-10 14:04:30 +08:00
|
|
|
if (persistent) {
|
|
|
|
ht->flags |= HASH_FLAG_PERSISTENT;
|
|
|
|
}
|
2000-06-17 22:11:57 +08:00
|
|
|
ht->nApplyCount = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static void zend_hash_packed_grow(HashTable *ht)
|
|
|
|
{
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-04-07 22:22:03 +08:00
|
|
|
ht->arData = (Bucket *) safe_perealloc(ht->arData, (ht->nTableSize << 1), sizeof(Bucket), 0, ht->flags & HASH_FLAG_PERSISTENT);
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nTableSize = (ht->nTableSize << 1);
|
|
|
|
ht->nTableMask = ht->nTableSize - 1;
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:23:27 +08:00
|
|
|
ZEND_API void zend_hash_real_init(HashTable *ht, int packed)
|
|
|
|
{
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
CHECK_INIT(ht, packed);
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void zend_hash_packed_to_hash(HashTable *ht)
|
|
|
|
{
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
ht->flags &= ~HASH_FLAG_PACKED;
|
2014-04-07 22:22:03 +08:00
|
|
|
ht->arData = (Bucket *) safe_perealloc(ht->arData, ht->nTableSize, sizeof(Bucket) + sizeof(zend_uint), 0, ht->flags & HASH_FLAG_PERSISTENT);
|
|
|
|
ht->arHash = (zend_uint*)(ht->arData + ht->nTableSize);
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_rehash(ht);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API void zend_hash_to_packed(HashTable *ht)
|
|
|
|
{
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
ht->flags |= HASH_FLAG_PACKED;
|
2014-04-07 22:22:03 +08:00
|
|
|
//??? pefree(ht->arHash, ht->flags & HASH_FLAG_PERSISTENT);
|
|
|
|
ht->arData = erealloc(ht->arData, ht->nTableSize * sizeof(Bucket));
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash = (zend_uint*)&uninitialized_bucket;
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
2000-07-11 22:27:31 +08:00
|
|
|
|
2013-11-28 22:40:46 +08:00
|
|
|
ZEND_API int _zend_hash_init_ex(HashTable *ht, uint nSize, dtor_func_t pDestructor, zend_bool persistent, zend_bool bApplyProtection ZEND_FILE_LINE_DC)
|
2000-07-11 22:27:31 +08:00
|
|
|
{
|
2013-11-28 22:40:46 +08:00
|
|
|
int retval = _zend_hash_init(ht, nSize, pDestructor, persistent ZEND_FILE_LINE_CC);
|
2000-07-11 22:27:31 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (!bApplyProtection) {
|
|
|
|
ht->flags &= ~HASH_FLAG_APPLY_PROTECTION;
|
|
|
|
}
|
2000-07-11 22:27:31 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEND_API void zend_hash_set_apply_protection(HashTable *ht, zend_bool bApplyProtection)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
if (bApplyProtection) {
|
|
|
|
ht->flags |= HASH_FLAG_APPLY_PROTECTION;
|
|
|
|
} else {
|
|
|
|
ht->flags &= ~HASH_FLAG_APPLY_PROTECTION;
|
|
|
|
}
|
2000-07-11 22:27:31 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_find_bucket(const HashTable *ht, zend_string *key)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
ulong h;
|
|
|
|
uint nIndex;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
h = STR_HASH_VAL(key);
|
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->key == key) || /* check for the the same interned string */
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
|
|
|
p->key->len == key->len &&
|
|
|
|
memcmp(p->key->val, key->val, key->len) == 0)) {
|
|
|
|
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-02-10 14:04:30 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_str_find_bucket(const HashTable *ht, const char *str, int len, ulong h)
|
|
|
|
{
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
2010-04-20 18:49:22 +08:00
|
|
|
|
2001-07-11 04:31:42 +08:00
|
|
|
nIndex = h & ht->nTableMask;
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->h == h)
|
|
|
|
&& p->key
|
|
|
|
&& (p->key->len == len)
|
|
|
|
&& !memcmp(p->key->val, str, len)) {
|
|
|
|
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-02-10 14:04:30 +08:00
|
|
|
static zend_always_inline Bucket *zend_hash_index_find_bucket(const HashTable *ht, ulong h)
|
|
|
|
{
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
2000-01-20 22:26:31 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
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-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *_zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, int flag ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
ulong h;
|
1999-04-08 02:10:10 +08:00
|
|
|
uint nIndex;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2011-09-06 14:34:36 +08:00
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 05:16:50 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
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
|
|
|
CHECK_INIT(ht, 0);
|
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
zend_hash_packed_to_hash(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
h = STR_HASH_VAL(key);
|
|
|
|
p = zend_hash_find_bucket(ht, key);
|
2000-01-20 22:26:31 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p) {
|
2014-03-26 22:07:31 +08:00
|
|
|
zval *data;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (flag & HASH_ADD) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
2014-03-26 22:07:31 +08:00
|
|
|
data = &p->val;
|
|
|
|
if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
if (ht->pDestructor) {
|
2014-03-26 22:07:31 +08:00
|
|
|
ht->pDestructor(data);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2014-03-26 22:07:31 +08:00
|
|
|
ZVAL_COPY_VALUE(data, pData);
|
2014-02-10 14:04:30 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2014-03-26 22:07:31 +08:00
|
|
|
return data;
|
1999-04-08 02:10:10 +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
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed++;
|
|
|
|
ht->nNumOfElements++;
|
|
|
|
if (ht->nInternalPointer == INVALID_IDX) {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
}
|
|
|
|
p = ht->arData + idx;
|
|
|
|
p->h = h;
|
|
|
|
p->key = key;
|
|
|
|
STR_ADDREF(key);
|
2014-02-18 20:27:38 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
2014-02-10 14:04:30 +08:00
|
|
|
nIndex = h & ht->nTableMask;
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(p->val) = ht->arHash[nIndex];
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash[nIndex] = idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *_zend_hash_str_add_or_update(HashTable *ht, const char *str, int len, zval *pData, int flag ZEND_FILE_LINE_DC)
|
|
|
|
{
|
|
|
|
zend_string *key = STR_INIT(str, len, ht->flags & HASH_FLAG_PERSISTENT);
|
|
|
|
zval *ret = _zend_hash_add_or_update(ht, key, pData, flag ZEND_FILE_LINE_CC);
|
|
|
|
STR_RELEASE(key);
|
|
|
|
return ret;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *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
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_hash_str_add_empty_element(HashTable *ht, const char *str, int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
zval dummy;
|
|
|
|
|
|
|
|
ZVAL_NULL(&dummy);
|
|
|
|
return zend_hash_str_add(ht, str, len, &dummy);
|
|
|
|
}
|
2001-05-17 01:22:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *_zend_hash_index_update_or_next_insert(HashTable *ht, ulong h, zval *pData, int flag ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
uint nIndex;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2011-09-06 14:34:36 +08:00
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 06:54:14 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
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 (flag & HASH_NEXT_INSERT) {
|
|
|
|
h = ht->nNextFreeElement;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
CHECK_INIT(ht, h >= 0 && h < ht->nTableSize);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
2014-04-07 17:31:44 +08:00
|
|
|
if (EXPECTED(h >= 0)) {
|
|
|
|
if (h < ht->nNumUsed) {
|
|
|
|
p = ht->arData + h;
|
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
|
|
|
if (flag & (HASH_NEXT_INSERT | HASH_ADD)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
}
|
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
|
|
|
if ((long)h >= (long)ht->nNextFreeElement) {
|
|
|
|
ht->nNextFreeElement = h < LONG_MAX ? h + 1 : LONG_MAX;
|
|
|
|
}
|
|
|
|
return &p->val;
|
|
|
|
} else { /* we have to keep the order :( */
|
|
|
|
goto convert_to_hash;
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2014-04-07 17:31:44 +08:00
|
|
|
} else if (EXPECTED(h < ht->nTableSize)) {
|
|
|
|
p = ht->arData + h;
|
|
|
|
} else if (h < ht->nTableSize * 2 &&
|
|
|
|
ht->nTableSize - ht->nNumOfElements < ht->nTableSize / 2) {
|
|
|
|
zend_hash_packed_grow(ht);
|
|
|
|
p = ht->arData + h;
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
|
|
|
goto convert_to_hash;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-04-07 17:31:44 +08:00
|
|
|
/* incremental initialization of empty Buckets */
|
2014-02-10 14:04:30 +08:00
|
|
|
if (h >= ht->nNumUsed) {
|
2014-04-07 17:31:44 +08:00
|
|
|
Bucket *q = ht->arData + ht->nNumUsed;
|
|
|
|
while (q != p) {
|
|
|
|
ZVAL_UNDEF(&q->val);
|
|
|
|
q++;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = h + 1;
|
|
|
|
}
|
|
|
|
ht->nNumOfElements++;
|
|
|
|
if (ht->nInternalPointer == INVALID_IDX) {
|
|
|
|
ht->nInternalPointer = h;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2003-08-12 14:21:02 +08:00
|
|
|
if ((long)h >= (long)ht->nNextFreeElement) {
|
2009-06-08 03:28:15 +08:00
|
|
|
ht->nNextFreeElement = h < LONG_MAX ? h + 1 : LONG_MAX;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
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-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(p->val) = INVALID_IDX;
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
|
|
|
|
return &p->val;
|
|
|
|
} else {
|
|
|
|
convert_to_hash:
|
|
|
|
zend_hash_packed_to_hash(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
p = zend_hash_index_find_bucket(ht, h);
|
|
|
|
if (p) {
|
2014-04-07 17:31:44 +08:00
|
|
|
if (flag & (HASH_NEXT_INSERT | HASH_ADD)) {
|
2014-02-10 14:04:30 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ZEND_ASSERT(&p->val != pData);
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
}
|
2014-02-18 20:27:38 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
2014-02-10 14:04:30 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
if ((long)h >= (long)ht->nNextFreeElement) {
|
|
|
|
ht->nNextFreeElement = h < LONG_MAX ? h + 1 : LONG_MAX;
|
|
|
|
}
|
|
|
|
return &p->val;
|
1999-04-08 02:10:10 +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 */
|
2000-01-20 22:26:31 +08:00
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed++;
|
|
|
|
ht->nNumOfElements++;
|
|
|
|
if (ht->nInternalPointer == INVALID_IDX) {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
}
|
2003-08-12 14:21:02 +08:00
|
|
|
if ((long)h >= (long)ht->nNextFreeElement) {
|
2009-06-08 03:28:15 +08:00
|
|
|
ht->nNextFreeElement = h < LONG_MAX ? h + 1 : LONG_MAX;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
p->h = h;
|
|
|
|
p->key = NULL;
|
|
|
|
nIndex = h & ht->nTableMask;
|
2014-02-18 20:27:38 +08:00
|
|
|
ZVAL_COPY_VALUE(&p->val, pData);
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(p->val) = ht->arHash[nIndex];
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash[nIndex] = idx;
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
|
|
|
|
return &p->val;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2000-01-18 02:45:46 +08:00
|
|
|
|
2013-06-18 00:51:59 +08:00
|
|
|
static void zend_hash_do_resize(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2011-09-06 14:34:36 +08:00
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 05:16:50 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
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
|
|
|
if (ht->nNumUsed < ht->nNumOfElements) {
|
2013-06-18 00:51:59 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_rehash(ht);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
} else if ((ht->nTableSize << 1) > 0) { /* Let's double the table size */
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-04-07 22:22:03 +08:00
|
|
|
ht->arData = (Bucket *) safe_perealloc(ht->arData, (ht->nTableSize << 1), sizeof(Bucket) + sizeof(zend_uint), 0, ht->flags & HASH_FLAG_PERSISTENT);
|
|
|
|
ht->arHash = (zend_uint*)(ht->arData + (ht->nTableSize << 1));
|
2013-06-18 00:51:59 +08:00
|
|
|
ht->nTableSize = (ht->nTableSize << 1);
|
|
|
|
ht->nTableMask = ht->nTableSize - 1;
|
|
|
|
zend_hash_rehash(ht);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-05 05:02:35 +08:00
|
|
|
ZEND_API int zend_hash_rehash(HashTable *ht)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
uint nIndex, i, j;
|
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
|
|
|
memset(ht->arHash, INVALID_IDX, ht->nTableSize * sizeof(zend_uint));
|
|
|
|
|
2010-04-20 18:49:22 +08:00
|
|
|
if (UNEXPECTED(ht->nNumOfElements == 0)) {
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (i = 0, j = 0; i < ht->nNumUsed; i++) {
|
|
|
|
p = ht->arData + i;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (i != j) {
|
|
|
|
ht->arData[j] = ht->arData[i];
|
|
|
|
if (ht->nInternalPointer == i) {
|
|
|
|
ht->nInternalPointer = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nIndex = ht->arData[j].h & ht->nTableMask;
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(ht->arData[j].val) = ht->arHash[nIndex];
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash[nIndex] = j;
|
|
|
|
j++;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nNumUsed = j;
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static zend_always_inline void _zend_hash_del_el_ex(HashTable *ht, uint idx, Bucket *p, Bucket *prev)
|
|
|
|
{
|
|
|
|
if (!(ht->flags & HASH_FLAG_PACKED)) {
|
|
|
|
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 {
|
2014-04-02 18:34:44 +08:00
|
|
|
ht->arHash[p->h & ht->nTableMask] = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ht->nNumUsed - 1 == idx) {
|
|
|
|
do {
|
|
|
|
ht->nNumUsed--;
|
|
|
|
} while (ht->nNumUsed > 0 && (Z_TYPE(ht->arData[ht->nNumUsed-1].val) == IS_UNDEF));
|
|
|
|
}
|
|
|
|
ht->nNumOfElements--;
|
|
|
|
if (ht->nInternalPointer == idx) {
|
|
|
|
while (1) {
|
|
|
|
idx++;
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
|
|
|
break;
|
|
|
|
} else if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->key) {
|
|
|
|
STR_RELEASE(p->key);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void _zend_hash_del_el(HashTable *ht, uint idx, Bucket *p)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
uint nIndex;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *prev = NULL;
|
|
|
|
|
|
|
|
if (!(ht->flags & HASH_FLAG_PACKED)) {
|
|
|
|
nIndex = p->h & ht->nTableMask;
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
if (p != ht->arData + idx) {
|
|
|
|
prev = ht->arData + idx;
|
2014-04-02 18:34:44 +08:00
|
|
|
while (ht->arData + Z_NEXT(prev->val) != p) {
|
|
|
|
idx = Z_NEXT(prev->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
prev = ht->arData + idx;
|
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(prev->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API int zend_hash_del(HashTable *ht, zend_string *key)
|
|
|
|
{
|
|
|
|
ulong h;
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
2000-01-20 03:47:16 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
Bucket *prev = NULL;
|
2011-09-06 14:34:36 +08:00
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 05:16:50 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-19 19:26:43 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
h = STR_HASH_VAL(key);
|
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->key == key) ||
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
|
|
|
p->key->len == key->len &&
|
|
|
|
memcmp(p->key->val, key->val, key->len) == 0)) {
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-26 22:07:31 +08:00
|
|
|
ZEND_API int zend_hash_del_ind(HashTable *ht, zend_string *key)
|
|
|
|
{
|
|
|
|
ulong h;
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
#ifdef ZEND_SIGNALS
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = STR_HASH_VAL(key);
|
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->key == key) ||
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
|
|
|
p->key->len == key->len &&
|
|
|
|
memcmp(p->key->val, key->val, key->len) == 0)) {
|
|
|
|
if (Z_TYPE(p->val) == IS_INDIRECT) {
|
|
|
|
zval *data = Z_INDIRECT(p->val);
|
|
|
|
|
|
|
|
if (Z_TYPE_P(data) == IS_UNDEF) {
|
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(data);
|
|
|
|
}
|
|
|
|
ZVAL_UNDEF(data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_hash_str_del(HashTable *ht, const char *str, int len)
|
|
|
|
{
|
|
|
|
ulong h;
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
#ifdef ZEND_SIGNALS
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-03-14 19:35:57 +08:00
|
|
|
h = zend_inline_hash_func(str, len);
|
2001-07-11 04:31:42 +08:00
|
|
|
nIndex = h & ht->nTableMask;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-03-26 22:07:31 +08:00
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->h == h)
|
|
|
|
&& p->key
|
|
|
|
&& (p->key->len == len)
|
|
|
|
&& !memcmp(p->key->val, str, len)) {
|
|
|
|
if (Z_TYPE(p->val) == IS_INDIRECT) {
|
|
|
|
zval *data = Z_INDIRECT(p->val);
|
|
|
|
|
|
|
|
if (Z_TYPE_P(data) == IS_UNDEF) {
|
|
|
|
return FAILURE;
|
|
|
|
} else {
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(data);
|
|
|
|
}
|
|
|
|
ZVAL_UNDEF(data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API int zend_hash_str_del_ind(HashTable *ht, const char *str, int len)
|
|
|
|
{
|
|
|
|
ulong h;
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
#ifdef ZEND_SIGNALS
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
h = zend_inline_hash_func(str, len);
|
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
2006-02-01 09:01:05 +08:00
|
|
|
if ((p->h == h)
|
2014-02-10 14:04:30 +08:00
|
|
|
&& p->key
|
|
|
|
&& (p->key->len == len)
|
|
|
|
&& !memcmp(p->key->val, str, len)) {
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
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
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_hash_index_del(HashTable *ht, ulong h)
|
|
|
|
{
|
|
|
|
uint nIndex;
|
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
|
|
|
Bucket *prev = NULL;
|
|
|
|
#ifdef ZEND_SIGNALS
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
2014-04-07 17:31:44 +08:00
|
|
|
if (h >=0 && h < ht->nNumUsed) {
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + h;
|
|
|
|
if (Z_TYPE(p->val) != IS_UNDEF) {
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
_zend_hash_del_el_ex(ht, h, p, NULL);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
nIndex = h & ht->nTableMask;
|
|
|
|
|
|
|
|
idx = ht->arHash[nIndex];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if ((p->h == h) && (p->key == NULL)) {
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
_zend_hash_del_el_ex(ht, idx, p, prev);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
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
|
|
|
|
|
|
|
ZEND_API void zend_hash_destroy(HashTable *ht)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2000-01-18 03:17:58 +08:00
|
|
|
SET_INCONSISTENT(HT_IS_DESTROYING);
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0 ; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
2000-01-18 02:45:46 +08:00
|
|
|
if (ht->pDestructor) {
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->pDestructor(&p->val);
|
2000-01-18 02:45:46 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p->key) {
|
|
|
|
STR_RELEASE(p->key);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-20 18:49:22 +08:00
|
|
|
if (ht->nTableMask) {
|
2014-02-10 14:04:30 +08:00
|
|
|
pefree(ht->arData, ht->flags & HASH_FLAG_PERSISTENT);
|
2010-04-20 18:49:22 +08:00
|
|
|
}
|
2000-01-15 21:40:17 +08:00
|
|
|
|
2000-01-18 03:17:58 +08:00
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEND_API void zend_hash_clean(HashTable *ht)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
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
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (ht->pDestructor) {
|
|
|
|
ht->pDestructor(&p->val);
|
|
|
|
}
|
|
|
|
if (p->key) {
|
|
|
|
STR_RELEASE(p->key);
|
|
|
|
}
|
2011-04-15 20:43:20 +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;
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
|
|
|
if (ht->nTableMask) {
|
|
|
|
if (!(ht->flags & HASH_FLAG_PACKED)) {
|
|
|
|
memset(ht->arHash, INVALID_IDX, ht->nTableSize * sizeof(zend_uint));
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-17 04:59:03 +08:00
|
|
|
/* This function is used by the various apply() functions.
|
|
|
|
* It deletes the passed bucket, and returns the address of the
|
|
|
|
* next bucket. The hash *may* be altered during that time, the
|
|
|
|
* returned value will still be valid.
|
|
|
|
*/
|
2014-02-10 14:04:30 +08:00
|
|
|
static void zend_hash_apply_deleter(HashTable *ht, uint idx, Bucket *p)
|
2000-01-17 04:59:03 +08:00
|
|
|
{
|
2011-09-06 14:34:36 +08:00
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 05:16:50 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
2000-01-17 04:59:03 +08:00
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2014-02-10 14:04:30 +08:00
|
|
|
_zend_hash_del_el(ht, idx, p);
|
2006-08-24 17:42:35 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2000-01-17 04:59:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEND_API void zend_hash_graceful_destroy(HashTable *ht)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2000-01-20 03:44:32 +08:00
|
|
|
Bucket *p;
|
2000-01-17 04:59:03 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
zend_hash_apply_deleter(ht, idx, p);
|
2000-01-17 04:59:03 +08:00
|
|
|
}
|
2010-04-20 18:49:22 +08:00
|
|
|
if (ht->nTableMask) {
|
2014-02-10 14:04:30 +08:00
|
|
|
pefree(ht->arData, ht->flags & HASH_FLAG_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
|
|
|
}
|
|
|
|
|
2002-04-20 00:53:36 +08:00
|
|
|
ZEND_API void zend_hash_graceful_reverse_destroy(HashTable *ht)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2002-04-20 00:53:36 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
zend_hash_apply_deleter(ht, idx, p);
|
2002-04-20 00:53:36 +08:00
|
|
|
}
|
|
|
|
|
2010-04-20 18:49:22 +08:00
|
|
|
if (ht->nTableMask) {
|
2014-02-10 14:04:30 +08:00
|
|
|
pefree(ht->arData, ht->flags & HASH_FLAG_PERSISTENT);
|
2010-04-20 18:49:22 +08:00
|
|
|
}
|
2002-04-20 00:53:36 +08:00
|
|
|
|
|
|
|
SET_INCONSISTENT(HT_DESTROYED);
|
|
|
|
}
|
|
|
|
|
2006-11-04 03:02:16 +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
|
|
|
|
* 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
|
|
|
|
2001-07-31 12:53:54 +08:00
|
|
|
ZEND_API void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint 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
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_PROTECT_RECURSION(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
|
|
|
|
result = apply_func(&p->val TSRMLS_CC);
|
2006-11-04 03:02:16 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_apply_deleter(ht, 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
|
|
|
}
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 12:53:54 +08:00
|
|
|
ZEND_API void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint 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);
|
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_PROTECT_RECURSION(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
|
|
|
|
result = apply_func(&p->val, argument TSRMLS_CC);
|
2006-11-04 03:02:16 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_apply_deleter(ht, 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
|
|
|
}
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-25 03:52:24 +08:00
|
|
|
ZEND_API void zend_hash_apply_with_arguments(HashTable *ht TSRMLS_DC, apply_func_args_t apply_func, int num_args, ...)
|
1999-08-26 03:02:13 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint 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);
|
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_PROTECT_RECURSION(ht);
|
1999-08-26 03:02:13 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (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;
|
|
|
|
|
|
|
|
result = apply_func(&p->val TSRMLS_CC, num_args, args, &hash_key);
|
2006-11-04 03:02:16 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_apply_deleter(ht, 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
|
|
|
}
|
2000-06-17 22:11:57 +08:00
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht);
|
1999-08-26 03:02:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-02 15:00:43 +08:00
|
|
|
ZEND_API void zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
Bucket *p;
|
|
|
|
int result;
|
2001-08-02 15:00:43 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
|
|
|
HASH_PROTECT_RECURSION(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
|
|
|
|
result = apply_func(&p->val TSRMLS_CC);
|
2001-08-02 15:00:43 +08:00
|
|
|
|
|
|
|
if (result & ZEND_HASH_APPLY_REMOVE) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_apply_deleter(ht, idx, p);
|
2001-08-02 15:00:43 +08:00
|
|
|
}
|
|
|
|
if (result & ZEND_HASH_APPLY_STOP) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HASH_UNPROTECT_RECURSION(ht);
|
|
|
|
}
|
|
|
|
|
1999-08-26 03:02:13 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void zend_hash_copy(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2014-03-26 22:07:31 +08:00
|
|
|
zval *new_entry, *data;
|
2007-07-25 02:28:39 +08:00
|
|
|
zend_bool setTargetPointer;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(source);
|
|
|
|
IS_CONSISTENT(target);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
setTargetPointer = (target->nInternalPointer == INVALID_IDX);
|
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
|
|
|
|
if (setTargetPointer && source->nInternalPointer == idx) {
|
|
|
|
target->nInternalPointer = INVALID_IDX;
|
2007-07-25 02:28:39 +08:00
|
|
|
}
|
2014-03-26 22:07:31 +08:00
|
|
|
//???
|
|
|
|
data = &p->val;
|
|
|
|
if (Z_TYPE_P(data) == IS_INDIRECT) {
|
|
|
|
data = Z_INDIRECT_P(data);
|
|
|
|
if (Z_TYPE_P(data) == IS_UNDEF) {
|
|
|
|
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
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (target->nInternalPointer == INVALID_IDX && target->nNumOfElements > 0) {
|
|
|
|
idx = 0;
|
|
|
|
while (Z_TYPE(target->arData[idx].val) == IS_UNDEF) {
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
target->nInternalPointer = idx;
|
2007-07-25 02:28:39 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void _zend_hash_merge(HashTable *target, HashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite ZEND_FILE_LINE_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *t;
|
1999-05-30 02:59:58 +08:00
|
|
|
int mode = (overwrite?HASH_UPDATE:HASH_ADD);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(source);
|
|
|
|
IS_CONSISTENT(target);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (p->key) {
|
|
|
|
t = _zend_hash_add_or_update(target, p->key, &p->val, mode ZEND_FILE_LINE_RELAY_CC);
|
|
|
|
if (t && pCopyConstructor) {
|
1999-04-08 02:10:10 +08:00
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((mode==HASH_UPDATE || !zend_hash_index_exists(target, p->h))) {
|
|
|
|
t = zend_hash_index_update(target, p->h, &p->val);
|
|
|
|
if (t && pCopyConstructor) {
|
|
|
|
pCopyConstructor(t);
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (target->nNumOfElements > 0) {
|
|
|
|
idx = 0;
|
|
|
|
while (Z_TYPE(target->arData[idx].val) == IS_UNDEF) {
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
target->nInternalPointer = idx;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
static zend_bool 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void 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-02-10 14:04:30 +08:00
|
|
|
uint 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);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < source->nNumUsed; idx++) {
|
|
|
|
p = source->arData + idx;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (zend_hash_replace_checker_wrapper(target, &p->val, p, pParam, pMergeSource)) {
|
|
|
|
t = zend_hash_update(target, p->key, &p->val);
|
|
|
|
if (t && pCopyConstructor) {
|
2003-02-04 20:12:34 +08:00
|
|
|
pCopyConstructor(t);
|
2000-10-12 00:22:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (target->nNumOfElements > 0) {
|
|
|
|
idx = 0;
|
|
|
|
while (Z_TYPE(target->arData[idx].val) == IS_UNDEF) {
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
target->nInternalPointer = idx;
|
|
|
|
}
|
2000-10-12 00:22:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
/* Returns SUCCESS if found and FAILURE if not. The pointer to the
|
|
|
|
* data is returned in pData. The reason is that there's no reason
|
|
|
|
* someone using the hash table might not want to have NULL data
|
|
|
|
*/
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *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);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
p = zend_hash_find_bucket(ht, key);
|
|
|
|
return p ? &p->val : NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_hash_str_find(const HashTable *ht, const char *str, int len)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
ulong h;
|
1999-04-08 02:10:10 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int 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);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
p = zend_hash_find_bucket(ht, key);
|
|
|
|
return p ? 1 : 0;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_hash_str_exists(const HashTable *ht, const char *str, int len)
|
2003-02-05 21:19:59 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
ulong h;
|
2003-02-05 21:19:59 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
return 0;
|
2003-02-05 21:19:59 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_hash_index_find(const HashTable *ht, ulong h)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
2014-04-07 17:31:44 +08:00
|
|
|
if (h >= 0 && 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
ZEND_API int zend_hash_index_exists(const HashTable *ht, ulong h)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
2014-04-07 17:31:44 +08:00
|
|
|
if (h >= 0 && 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
ZEND_API int zend_hash_num_elements(const HashTable *ht)
|
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
|
|
|
return ht->nNumOfElements;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
ZEND_API int zend_hash_get_pointer(const HashTable *ht, HashPointer *ptr)
|
2007-01-10 23:58:08 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
ptr->pos = ht->nInternalPointer;
|
|
|
|
ptr->ht = (HashTable*)ht;
|
|
|
|
if (ht->nInternalPointer != INVALID_IDX) {
|
|
|
|
ptr->h = ht->arData[ht->nInternalPointer].h;
|
2007-01-10 23:58:08 +08:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
ptr->h = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API int zend_hash_set_pointer(HashTable *ht, const HashPointer *ptr)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2007-01-10 23:58:08 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ptr->pos == INVALID_IDX) {
|
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
|
|
|
} else if (ptr->ht != ht) {
|
2007-01-10 23:58:08 +08:00
|
|
|
IS_CONSISTENT(ht);
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
idx = INVALID_IDX;
|
|
|
|
return 0;
|
|
|
|
} else if (ht->nInternalPointer != ptr->pos) {
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
if (Z_TYPE(ht->arData[ptr->h].val) != IS_UNDEF) {
|
|
|
|
ht->nInternalPointer = ptr->h;
|
2007-01-10 23:58:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
|
|
|
idx = ht->arHash[ptr->h & ht->nTableMask];
|
|
|
|
while (idx != INVALID_IDX) {
|
|
|
|
if (idx == ptr->pos) {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx = Z_NEXT(ht->arData[idx].val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2007-01-10 23:58:08 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-03-13 23:25:18 +08:00
|
|
|
ZEND_API void zend_hash_internal_pointer_reset_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
for (idx = 0; idx < ht->nNumUsed; idx++) {
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
if (pos) {
|
|
|
|
*pos = idx;
|
|
|
|
} else {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pos) {
|
|
|
|
*pos = INVALID_IDX;
|
|
|
|
} else {
|
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function will be extremely optimized by remembering
|
|
|
|
* the end of the list
|
|
|
|
*/
|
2000-03-13 23:25:18 +08:00
|
|
|
ZEND_API void zend_hash_internal_pointer_end_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = ht->nNumUsed;
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
if (pos) {
|
|
|
|
*pos = idx;
|
|
|
|
} else {
|
|
|
|
ht->nInternalPointer = idx;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pos) {
|
|
|
|
*pos = INVALID_IDX;
|
|
|
|
} else {
|
|
|
|
ht->nInternalPointer = INVALID_IDX;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-16 00:25:59 +08:00
|
|
|
ZEND_API int zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
HashPosition *current = pos ? pos : &ht->nInternalPointer;
|
|
|
|
uint idx = *current;
|
2000-03-16 00:25:59 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx != INVALID_IDX) {
|
|
|
|
while (1) {
|
|
|
|
idx++;
|
|
|
|
if (idx >= ht->nNumUsed) {
|
|
|
|
*current = INVALID_IDX;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
*current = idx;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2000-03-16 00:25:59 +08:00
|
|
|
ZEND_API int zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
HashPosition *current = pos ? pos : &ht->nInternalPointer;
|
|
|
|
uint idx = *current;
|
2000-03-16 00:25:59 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx != INVALID_IDX) {
|
|
|
|
while (idx > 0) {
|
|
|
|
idx--;
|
|
|
|
if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
|
|
|
|
*current = idx;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*current = INVALID_IDX;
|
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This function should be made binary safe */
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, ulong *num_index, zend_bool duplicate, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = pos ? (*pos) : ht->nInternalPointer;
|
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
|
|
|
if (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
2000-12-22 20:49:51 +08:00
|
|
|
if (duplicate) {
|
2014-04-01 21:10:15 +08:00
|
|
|
*str_index = STR_COPY(p->key);
|
2000-12-22 20:49:51 +08:00
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
*str_index = p->key;
|
2000-05-03 01:52:31 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
return HASH_KEY_IS_STRING;
|
|
|
|
} else {
|
|
|
|
*num_index = p->h;
|
|
|
|
return HASH_KEY_IS_LONG;
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 05:09:06 +08:00
|
|
|
return HASH_KEY_NON_EXISTENT;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API void zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, HashPosition *pos)
|
|
|
|
{
|
|
|
|
uint idx;
|
2013-02-17 02:13:36 +08:00
|
|
|
Bucket *p;
|
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = pos ? (*pos) : ht->nInternalPointer;
|
2013-02-17 02:13:36 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx == INVALID_IDX) {
|
2014-04-03 19:26:23 +08:00
|
|
|
ZVAL_NULL(key);
|
2013-02-17 02:13:36 +08:00
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
|
|
|
ZVAL_STR(key, p->key);
|
|
|
|
STR_ADDREF(p->key);
|
|
|
|
} else {
|
|
|
|
ZVAL_LONG(key, p->h);
|
|
|
|
}
|
2013-02-17 02:13:36 +08:00
|
|
|
}
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-03-13 23:25:18 +08:00
|
|
|
ZEND_API int zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos)
|
1999-06-08 06:49:33 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = pos ? (*pos) : ht->nInternalPointer;
|
1999-06-08 06:49:33 +08:00
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx;
|
|
|
|
if (p->key) {
|
1999-06-08 06:49:33 +08:00
|
|
|
return HASH_KEY_IS_STRING;
|
|
|
|
} else {
|
|
|
|
return HASH_KEY_IS_LONG;
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 05:09:06 +08:00
|
|
|
return HASH_KEY_NON_EXISTENT;
|
1999-06-08 06:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_hash_get_current_data_ex(HashTable *ht, HashPosition *pos)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx;
|
2000-03-13 23:25:18 +08:00
|
|
|
Bucket *p;
|
2005-04-25 16:21:15 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx = pos ? (*pos) : ht->nInternalPointer;
|
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
|
|
|
if (idx != INVALID_IDX) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 19:30:56 +08:00
|
|
|
/* This function changes key of current element without changing elements'
|
2005-07-07 23:16:57 +08:00
|
|
|
* order. If element with target key already exists, it will be deleted first.
|
|
|
|
*/
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API int zend_hash_update_current_key_ex(HashTable *ht, int key_type, zend_string *str_index, ulong num_index, int mode, HashPosition *pos)
|
2005-07-07 23:16:57 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx1, idx2;
|
2011-08-01 19:21:23 +08:00
|
|
|
Bucket *p, *q;
|
|
|
|
ulong h;
|
|
|
|
#ifdef ZEND_SIGNALS
|
2011-06-03 05:16:50 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
#endif
|
2005-07-07 23:16:57 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx1 = pos ? (*pos) : ht->nInternalPointer;
|
2005-07-07 23:16:57 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx1 != INVALID_IDX) {
|
|
|
|
p = ht->arData + idx1;
|
2005-07-07 23:16:57 +08:00
|
|
|
if (key_type == HASH_KEY_IS_LONG) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (p->h == num_index && p->key == NULL) {
|
2005-07-07 23:16:57 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2008-08-01 22:22:03 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx2 = ht->arHash[num_index & ht->nTableMask];
|
|
|
|
while (idx2 != INVALID_IDX) {
|
|
|
|
q = ht->arData + idx2;
|
|
|
|
if (q->h == num_index && q->key == NULL) {
|
2011-08-01 19:21:23 +08:00
|
|
|
break;
|
2008-08-01 22:22:03 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx2 = Z_NEXT(q->val);
|
2008-08-01 22:22:03 +08:00
|
|
|
}
|
2005-07-07 23:16:57 +08:00
|
|
|
} else if (key_type == HASH_KEY_IS_STRING) {
|
2014-02-10 14:04:30 +08:00
|
|
|
h = STR_HASH_VAL(str_index);
|
|
|
|
if (p->key == str_index ||
|
|
|
|
(p->h == h &&
|
|
|
|
p->key &&
|
|
|
|
p->key->len == str_index->len &&
|
|
|
|
memcmp(p->key->val, str_index->val, str_index->len) == 0)) {
|
2005-07-07 23:16:57 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2008-08-01 22:22:03 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
idx2 = ht->arHash[h & ht->nTableMask];
|
|
|
|
while (idx2 != INVALID_IDX) {
|
|
|
|
q = ht->arData + idx2;
|
|
|
|
if (q->key == str_index ||
|
|
|
|
(q->h == h && q->key && q->key->len == str_index->len &&
|
|
|
|
memcmp(q->key->val, str_index->val, str_index->len) == 0)) {
|
2011-08-01 19:21:23 +08:00
|
|
|
break;
|
2008-08-01 22:22:03 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
idx2 = Z_NEXT(q->val);
|
2008-08-01 22:22:03 +08:00
|
|
|
}
|
2005-07-07 23:16:57 +08:00
|
|
|
} else {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (idx2 != INVALID_IDX) {
|
|
|
|
/* we have another bucket with the key equal to new one */
|
2011-08-01 19:21:23 +08:00
|
|
|
if (mode != HASH_UPDATE_KEY_ANYWAY) {
|
2014-02-10 14:04:30 +08:00
|
|
|
int found = (idx1 < idx2) ? HASH_UPDATE_KEY_IF_BEFORE : HASH_UPDATE_KEY_IF_AFTER;
|
|
|
|
|
2011-08-01 19:21:23 +08:00
|
|
|
if (mode & found) {
|
|
|
|
/* delete current bucket */
|
2014-02-10 14:04:30 +08:00
|
|
|
_zend_hash_del_el(ht, idx1, p);
|
2011-08-01 19:21:23 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* delete another bucket with the same key */
|
2014-02-10 14:04:30 +08:00
|
|
|
_zend_hash_del_el(ht, idx2, q);
|
2005-07-07 23:16:57 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
/* remove old key from hash */
|
|
|
|
if (ht->arHash[p->h & ht->nTableMask] == idx1) {
|
2014-04-02 18:34:44 +08:00
|
|
|
ht->arHash[p->h & ht->nTableMask] = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
} else {
|
|
|
|
uint idx3 = ht->arHash[p->h & ht->nTableMask];
|
2014-04-02 18:34:44 +08:00
|
|
|
while (Z_NEXT(ht->arData[idx3].val) != idx1) {
|
|
|
|
idx3 = Z_NEXT(ht->arData[idx3].val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(ht->arData[idx3].val) = Z_NEXT(p->val);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update key */
|
|
|
|
if (p->key) {
|
|
|
|
STR_RELEASE(p->key);
|
|
|
|
}
|
2005-07-07 23:16:57 +08:00
|
|
|
if (key_type == HASH_KEY_IS_LONG) {
|
|
|
|
p->h = num_index;
|
2014-02-10 14:04:30 +08:00
|
|
|
p->key = NULL;
|
2005-07-07 23:16:57 +08:00
|
|
|
} else {
|
2011-08-01 19:21:23 +08:00
|
|
|
p->h = h;
|
2014-02-10 14:04:30 +08:00
|
|
|
p->key = str_index;
|
|
|
|
STR_ADDREF(str_index);
|
2005-07-07 23:16:57 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
/* insert new key into hash */
|
2014-04-02 18:34:44 +08:00
|
|
|
Z_NEXT(p->val) = ht->arHash[p->h & ht->nTableMask];
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash[p->h & ht->nTableMask] = idx1;
|
2005-07-07 23:16:57 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
} else {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
|
1999-10-16 04:37:53 +08:00
|
|
|
ZEND_API int zend_hash_sort(HashTable *ht, sort_func_t sort_func,
|
2001-09-19 18:25:04 +08:00
|
|
|
compare_func_t compar, int renumber TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
Bucket *p;
|
|
|
|
int i, j;
|
|
|
|
|
2000-01-15 21:40:17 +08:00
|
|
|
IS_CONSISTENT(ht);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->nNumUsed == ht->nNumOfElements) {
|
|
|
|
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;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
if (i != j) {
|
|
|
|
ht->arData[i] = *p;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2005-04-25 16:21:15 +08:00
|
|
|
}
|
2004-07-30 03:18:46 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
(*sort_func)((void *) ht->arData, i, sizeof(Bucket), compar TSRMLS_CC);
|
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
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) {
|
|
|
|
STR_RELEASE(p->key);
|
|
|
|
p->key = NULL;
|
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
if (renumber) {
|
1999-04-08 02:10:10 +08:00
|
|
|
ht->nNextFreeElement = i;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (ht->flags & HASH_FLAG_PACKED) {
|
|
|
|
if (!renumber) {
|
|
|
|
zend_hash_packed_to_hash(ht);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (renumber) {
|
|
|
|
ht->flags |= HASH_FLAG_PACKED;
|
2014-04-07 22:22:03 +08:00
|
|
|
//??? pefree(ht->arHash, ht->flags & HASH_FLAG_PERSISTENT);
|
|
|
|
ht->arData = erealloc(ht->arData, ht->nTableSize * sizeof(Bucket));
|
2014-02-10 14:04:30 +08:00
|
|
|
ht->arHash = (zend_uint*)&uninitialized_bucket;
|
|
|
|
} else {
|
|
|
|
zend_hash_rehash(ht);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-30 12:54:16 +08:00
|
|
|
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC)
|
2000-06-03 18:34:19 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint idx1, idx2;
|
2006-02-07 04:37:11 +08:00
|
|
|
Bucket *p1, *p2 = NULL;
|
2000-06-03 18:34:19 +08:00
|
|
|
int result;
|
2014-03-26 22:07:31 +08:00
|
|
|
zval *pData1, *pData2;
|
2000-06-03 18:34:19 +08:00
|
|
|
|
|
|
|
IS_CONSISTENT(ht1);
|
|
|
|
IS_CONSISTENT(ht2);
|
|
|
|
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_PROTECT_RECURSION(ht1);
|
|
|
|
HASH_PROTECT_RECURSION(ht2);
|
|
|
|
|
2000-06-03 18:34:19 +08:00
|
|
|
result = ht1->nNumOfElements - ht2->nNumOfElements;
|
|
|
|
if (result!=0) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-03 18:34:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
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++) {
|
|
|
|
p1 = ht1->arData + idx1;
|
|
|
|
if (Z_TYPE(p1->val) == IS_UNDEF) continue;
|
2000-06-03 18:34:19 +08:00
|
|
|
|
2000-06-05 05:59:49 +08:00
|
|
|
if (ordered) {
|
2014-02-10 14:04:30 +08:00
|
|
|
while (1) {
|
|
|
|
p2 = ht2->arData + idx2;
|
|
|
|
if (idx2 == ht2->nNumUsed) {
|
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
|
|
|
return 1; /* That's not supposed to happen */
|
|
|
|
}
|
|
|
|
if (p2) break;
|
|
|
|
idx2++;
|
|
|
|
}
|
|
|
|
if (p1->key == NULL && p2->key == NULL) { /* numeric indices */
|
2000-06-05 05:59:49 +08:00
|
|
|
result = p1->h - p2->h;
|
2014-02-10 14:04:30 +08:00
|
|
|
if (result != 0) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-05 05:59:49 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else { /* string indices */
|
2014-02-10 14:04:30 +08:00
|
|
|
result = (p1->key ? p1->key->len : 0) - (p2->key ? p2->key->len : 0);
|
|
|
|
if (result != 0) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-05 05:59:49 +08:00
|
|
|
return result;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
result = memcmp(p1->key->val, p2->key->val, p1->key->len);
|
|
|
|
if (result != 0) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-05 05:59:49 +08:00
|
|
|
return result;
|
|
|
|
}
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
pData2 = &p2->val;
|
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) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
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) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-05 05:59:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2000-06-03 18:34:19 +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);
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
result = compar(pData1, pData2 TSRMLS_CC);
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
if (result != 0) {
|
2001-07-16 02:57:43 +08:00
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-03 18:34:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
2000-06-05 05:59:49 +08:00
|
|
|
if (ordered) {
|
2014-02-10 14:04:30 +08:00
|
|
|
idx2++;
|
2000-06-05 05:59:49 +08:00
|
|
|
}
|
2000-06-03 18:34:19 +08:00
|
|
|
}
|
2001-07-16 02:57:43 +08:00
|
|
|
|
|
|
|
HASH_UNPROTECT_RECURSION(ht1);
|
|
|
|
HASH_UNPROTECT_RECURSION(ht2);
|
2000-06-03 18:34:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZEND_API zval *zend_hash_minmax(const HashTable *ht, compare_func_t compar, int flag TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
uint 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;
|
|
|
|
if (Z_TYPE(p->val) == IS_UNDEF) continue;
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
if (flag) {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (compar(res, p TSRMLS_CC) < 0) { /* max */
|
1999-04-08 02:10:10 +08:00
|
|
|
res = p;
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 14:04:30 +08:00
|
|
|
if (compar(res, p TSRMLS_CC) > 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
|
|
|
}
|
|
|
|
|
2008-08-13 01:20:25 +08:00
|
|
|
ZEND_API ulong zend_hash_next_free_element(const HashTable *ht)
|
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
|
|
|
return ht->nNextFreeElement;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
*/
|