2010-04-20 19:05:54 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2015-01-15 23:27:30 +08:00
|
|
|
| Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
|
2010-04-20 19:05:54 +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 |
|
2010-04-20 19:05:54 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Dmitry Stogov <dmitry@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id: $ */
|
|
|
|
|
|
|
|
#ifndef ZEND_STRING_H
|
|
|
|
#define ZEND_STRING_H
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
|
2012-07-27 05:37:02 +08:00
|
|
|
BEGIN_EXTERN_C()
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ZEND_API extern zend_string *(*zend_new_interned_string)(zend_string *str);
|
|
|
|
ZEND_API extern void (*zend_interned_strings_snapshot)(void);
|
|
|
|
ZEND_API extern void (*zend_interned_strings_restore)(void);
|
2010-05-25 17:00:20 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
ZEND_API zend_ulong zend_hash_func(const char *str, size_t len);
|
2014-12-14 06:06:14 +08:00
|
|
|
void zend_interned_strings_init(void);
|
|
|
|
void zend_interned_strings_dtor(void);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2012-07-27 05:37:02 +08:00
|
|
|
END_EXTERN_C()
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
/* Shortcuts */
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
#define ZSTR_VAL(zstr) zend_string_get_val(zstr)
|
|
|
|
#define ZSTR_LEN(zstr) (zstr)->len
|
|
|
|
#define ZSTR_HASH(zstr) zend_string_hash_val(zstr)
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
/* Compatibility macros */
|
2014-04-24 01:05:16 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
#define IS_INTERNED(s) ZSTR_IS_INTERNED(s)
|
|
|
|
#define STR_EMPTY_ALLOC() ZSTR_EMPTY_ALLOC()
|
|
|
|
#define _STR_HEADER_SIZE _ZSTR_HEADER_SIZE
|
|
|
|
#define STR_ALLOCA_ALLOC(str, _len, use_heap) ZSTR_ALLOCA_ALLOC(str, _len, use_heap)
|
|
|
|
#define STR_ALLOCA_INIT(str, s, len, use_heap) ZSTR_ALLOCA_INIT(str, s, len, use_heap)
|
|
|
|
#define STR_ALLOCA_FREE(str, use_heap) ZSTR_ALLOCA_FREE(str, use_heap)
|
|
|
|
|
|
|
|
/*---*/
|
|
|
|
|
|
|
|
#define ZSTR_IS_INTERNED(s) (GC_FLAGS(s) & IS_STR_INTERNED)
|
|
|
|
|
|
|
|
#define ZSTR_EMPTY_ALLOC() CG(empty_string)
|
|
|
|
|
|
|
|
#define _ZSTR_HEADER_SIZE XtOffsetOf(zend_string, val)
|
|
|
|
|
|
|
|
#define _ZSTR_STRUCT_SIZE(len) (_ZSTR_HEADER_SIZE + len + 1)
|
|
|
|
|
|
|
|
#define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \
|
|
|
|
(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(_len)), (use_heap)); \
|
2014-05-02 23:01:36 +08:00
|
|
|
GC_REFCOUNT(str) = 1; \
|
2015-04-22 04:26:34 +08:00
|
|
|
GC_TYPE_INFO(str) = IS_STRING; \
|
2015-06-29 21:44:54 +08:00
|
|
|
zend_string_forget_hash_val(str); \
|
|
|
|
zend_string_set_len(str, _len); \
|
2014-05-02 23:01:36 +08:00
|
|
|
} while (0)
|
2015-06-29 21:44:54 +08:00
|
|
|
|
|
|
|
#define ZSTR_ALLOCA_INIT(str, s, len, use_heap) do { \
|
|
|
|
ZSTR_ALLOCA_ALLOC(str, len, use_heap); \
|
|
|
|
memcpy(ZSTR_VAL(str), (s), (len)); \
|
|
|
|
ZSTR_VAL(str)[(len)] = '\0'; \
|
2014-05-02 23:01:36 +08:00
|
|
|
} while (0)
|
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
#define ZSTR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)
|
|
|
|
|
|
|
|
/*---*/
|
|
|
|
|
|
|
|
static zend_always_inline char * zend_string_get_val(zend_string *s)
|
|
|
|
{
|
|
|
|
return s->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_string_set_len(zend_string *s, size_t len)
|
|
|
|
{
|
|
|
|
s->len = len;
|
|
|
|
}
|
2014-05-02 23:01:36 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_ulong zend_string_hash_val(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
|
|
|
if (!s->h) {
|
2015-06-29 21:44:54 +08:00
|
|
|
s->h = zend_hash_func(ZSTR_VAL(s), ZSTR_LEN(s));
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return s->h;
|
|
|
|
}
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline void zend_string_forget_hash_val(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
|
|
|
s->h = 0;
|
|
|
|
}
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
static zend_always_inline uint32_t zend_string_refcount(const zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-04-02 18:34:44 +08:00
|
|
|
return GC_REFCOUNT(s);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
static zend_always_inline uint32_t zend_string_addref(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-04-02 18:34:44 +08:00
|
|
|
return ++GC_REFCOUNT(s);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:28:33 +08:00
|
|
|
static zend_always_inline uint32_t zend_string_delref(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-04-02 18:34:44 +08:00
|
|
|
return --GC_REFCOUNT(s);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_alloc(size_t len, int persistent)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_REFCOUNT(ret) = 1;
|
2014-04-03 19:26:23 +08:00
|
|
|
#if 1
|
|
|
|
/* optimized single assignment */
|
|
|
|
GC_TYPE_INFO(ret) = IS_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << 8);
|
|
|
|
#else
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_TYPE(ret) = IS_STRING;
|
|
|
|
GC_FLAGS(ret) = (persistent ? IS_STR_PERSISTENT : 0);
|
|
|
|
GC_INFO(ret) = 0;
|
2014-04-03 19:26:23 +08:00
|
|
|
#endif
|
2015-06-29 21:44:54 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
zend_string_set_len(ret, len);
|
2014-02-10 14:04:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, int persistent)
|
2014-04-16 01:56:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
|
2014-04-16 01:56:30 +08:00
|
|
|
|
|
|
|
GC_REFCOUNT(ret) = 1;
|
|
|
|
#if 1
|
|
|
|
/* optimized single assignment */
|
|
|
|
GC_TYPE_INFO(ret) = IS_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << 8);
|
|
|
|
#else
|
|
|
|
GC_TYPE(ret) = IS_STRING;
|
|
|
|
GC_FLAGS(ret) = (persistent ? IS_STR_PERSISTENT : 0);
|
|
|
|
GC_INFO(ret) = 0;
|
|
|
|
#endif
|
2015-06-29 21:44:54 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
zend_string_set_len(ret, (n * m) + l);
|
2014-04-16 01:56:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_init(const char *str, size_t len, int persistent)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string *ret = zend_string_alloc(len, persistent);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
memcpy(ZSTR_VAL(ret), str, len);
|
|
|
|
ZSTR_VAL(ret)[len] = '\0';
|
2014-02-10 14:04:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_copy(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-09-19 19:41:01 +08:00
|
|
|
GC_REFCOUNT(s)++;
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_dup(zend_string *s, int persistent)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (ZSTR_IS_INTERNED(s)) {
|
2014-04-01 20:31:03 +08:00
|
|
|
return s;
|
|
|
|
} else {
|
2015-06-29 21:44:54 +08:00
|
|
|
return zend_string_init(ZSTR_VAL(s), ZSTR_LEN(s), persistent);
|
2014-04-01 20:31:03 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2010-04-20 19:05:54 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_realloc(zend_string *s, size_t len, int persistent)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
|
|
|
zend_string *ret;
|
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2015-03-20 07:02:42 +08:00
|
|
|
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
|
2015-06-29 21:44:54 +08:00
|
|
|
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
|
|
|
|
zend_string_set_len(ret, len);
|
2015-03-20 07:02:42 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
GC_REFCOUNT(s)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = zend_string_alloc(len, persistent);
|
2015-06-29 21:44:54 +08:00
|
|
|
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN(len, ZSTR_LEN(s)) + 1);
|
2015-03-20 07:02:42 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t len, int persistent)
|
|
|
|
{
|
|
|
|
zend_string *ret;
|
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
ZEND_ASSERT(len >= ZSTR_LEN(s));
|
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2015-03-20 07:02:42 +08:00
|
|
|
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
|
2015-06-29 21:44:54 +08:00
|
|
|
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
|
|
|
|
zend_string_set_len(ret, len);
|
2015-03-20 07:02:42 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
GC_REFCOUNT(s)--;
|
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2015-03-20 07:02:42 +08:00
|
|
|
ret = zend_string_alloc(len, persistent);
|
2015-06-29 21:44:54 +08:00
|
|
|
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), ZSTR_LEN(s) + 1);
|
2015-03-20 07:02:42 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size_t len, int persistent)
|
|
|
|
{
|
|
|
|
zend_string *ret;
|
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
ZEND_ASSERT(len <= ZSTR_LEN(s));
|
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2015-03-20 07:02:42 +08:00
|
|
|
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
|
2015-06-29 21:44:54 +08:00
|
|
|
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
|
|
|
|
zend_string_set_len(ret, len);
|
2015-03-20 07:02:42 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
GC_REFCOUNT(s)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = zend_string_alloc(len, persistent);
|
2015-06-29 21:44:54 +08:00
|
|
|
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), len + 1);
|
2014-02-10 14:04:30 +08:00
|
|
|
return ret;
|
2013-09-14 00:45:02 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, int persistent)
|
2014-04-16 01:56:30 +08:00
|
|
|
{
|
|
|
|
zend_string *ret;
|
|
|
|
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2015-03-20 07:02:42 +08:00
|
|
|
if (GC_REFCOUNT(s) == 1) {
|
2015-06-29 21:44:54 +08:00
|
|
|
ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
|
|
|
|
zend_string_set_len(ret, (n * m) + l);
|
2015-03-20 07:02:42 +08:00
|
|
|
zend_string_forget_hash_val(ret);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
GC_REFCOUNT(s)--;
|
|
|
|
}
|
2014-04-16 01:56:30 +08:00
|
|
|
}
|
2015-03-20 07:02:42 +08:00
|
|
|
ret = zend_string_safe_alloc(n, m, l, persistent);
|
2015-06-29 21:44:54 +08:00
|
|
|
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN((n * m) + l, ZSTR_LEN(s)) + 1);
|
2014-04-16 01:56:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline void zend_string_free(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-09-19 19:41:01 +08:00
|
|
|
ZEND_ASSERT(GC_REFCOUNT(s) <= 1);
|
2014-04-02 18:34:44 +08:00
|
|
|
pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 00:45:02 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static zend_always_inline void zend_string_release(zend_string *s)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
if (!ZSTR_IS_INTERNED(s)) {
|
2014-09-19 19:41:01 +08:00
|
|
|
if (--GC_REFCOUNT(s) == 0) {
|
2014-04-02 18:34:44 +08:00
|
|
|
pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 00:45:02 +08:00
|
|
|
|
2015-02-11 16:03:48 +08:00
|
|
|
|
2014-08-26 04:40:58 +08:00
|
|
|
static zend_always_inline zend_bool zend_string_equals(zend_string *s1, zend_string *s2)
|
|
|
|
{
|
2015-06-29 21:44:54 +08:00
|
|
|
return s1 == s2 || (ZSTR_LEN(s1) == ZSTR_LEN(s2) && !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)));
|
2014-08-26 04:40:58 +08:00
|
|
|
}
|
|
|
|
|
2015-02-11 16:06:47 +08:00
|
|
|
#define zend_string_equals_ci(s1, s2) \
|
2015-06-29 21:44:54 +08:00
|
|
|
(ZSTR_LEN(s1) == ZSTR_LEN(s2) && !zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)))
|
2015-02-11 16:03:48 +08:00
|
|
|
|
2014-08-29 13:19:14 +08:00
|
|
|
#define zend_string_equals_literal_ci(str, c) \
|
2015-06-29 21:44:54 +08:00
|
|
|
(ZSTR_LEN(str) == sizeof(c) - 1 && !zend_binary_strcasecmp(ZSTR_VAL(str), ZSTR_LEN(str), (c), sizeof(c) - 1))
|
2014-08-29 13:19:14 +08:00
|
|
|
|
2014-08-26 04:40:58 +08:00
|
|
|
#define zend_string_equals_literal(str, literal) \
|
2015-06-29 21:44:54 +08:00
|
|
|
(ZSTR_LEN(str) == sizeof(literal)-1 && !memcmp(ZSTR_VAL(str), literal, sizeof(literal) - 1))
|
2014-08-26 04:40:58 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
/*
|
|
|
|
* DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
|
|
|
|
*
|
|
|
|
* This is Daniel J. Bernstein's popular `times 33' hash function as
|
|
|
|
* posted by him years ago on comp.lang.c. It basically uses a function
|
|
|
|
* like ``hash(i) = hash(i-1) * 33 + str[i]''. This is one of the best
|
|
|
|
* known hash functions for strings. Because it is both computed very
|
|
|
|
* fast and distributes very well.
|
|
|
|
*
|
|
|
|
* The magic of number 33, i.e. why it works better than many other
|
|
|
|
* constants, prime or not, has never been adequately explained by
|
|
|
|
* anyone. So I try an explanation: if one experimentally tests all
|
|
|
|
* multipliers between 1 and 256 (as RSE did now) one detects that even
|
|
|
|
* numbers are not useable at all. The remaining 128 odd numbers
|
|
|
|
* (except for the number 1) work more or less all equally well. They
|
|
|
|
* all distribute in an acceptable way and this way fill a hash table
|
2015-01-03 17:22:58 +08:00
|
|
|
* with an average percent of approx. 86%.
|
2014-02-10 14:04:30 +08:00
|
|
|
*
|
|
|
|
* If one compares the Chi^2 values of the variants, the number 33 not
|
|
|
|
* even has the best value. But the number 33 and a few other equally
|
|
|
|
* good numbers like 17, 31, 63, 127 and 129 have nevertheless a great
|
|
|
|
* advantage to the remaining numbers in the large set of possible
|
|
|
|
* multipliers: their multiply operation can be replaced by a faster
|
|
|
|
* operation based on just one shift plus either a single addition
|
|
|
|
* or subtraction operation. And because a hash function has to both
|
|
|
|
* distribute good _and_ has to be very fast to compute, those few
|
|
|
|
* numbers should be preferred and seems to be the reason why Daniel J.
|
|
|
|
* Bernstein also preferred it.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* -- Ralf S. Engelschall <rse@engelschall.com>
|
|
|
|
*/
|
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ulong zend_inline_hash_func(const char *str, size_t len)
|
2014-02-10 14:04:30 +08:00
|
|
|
{
|
2014-08-26 02:57:25 +08:00
|
|
|
register zend_ulong hash = Z_UL(5381);
|
2014-02-10 14:04:30 +08:00
|
|
|
|
|
|
|
/* variant with the hash unrolled eight times */
|
|
|
|
for (; len >= 8; len -= 8) {
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
hash = ((hash << 5) + hash) + *str++;
|
|
|
|
}
|
|
|
|
switch (len) {
|
|
|
|
case 7: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 6: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 5: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 4: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 3: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 2: hash = ((hash << 5) + hash) + *str++; /* fallthrough... */
|
|
|
|
case 1: hash = ((hash << 5) + hash) + *str++; break;
|
|
|
|
case 0: break;
|
|
|
|
EMPTY_SWITCH_DEFAULT_CASE()
|
|
|
|
}
|
2014-12-13 11:42:53 +08:00
|
|
|
|
2015-03-24 21:28:26 +08:00
|
|
|
/* Hash value can't be zero, so we always set the high bit */
|
|
|
|
#if SIZEOF_ZEND_LONG == 8
|
|
|
|
return hash | Z_UL(0x8000000000000000);
|
|
|
|
#elif SIZEOF_ZEND_LONG == 4
|
|
|
|
return hash | Z_UL(0x80000000);
|
|
|
|
#else
|
|
|
|
# error "Unknown SIZEOF_ZEND_LONG"
|
|
|
|
#endif
|
2014-02-10 14:04:30 +08:00
|
|
|
}
|
2013-09-14 00:45:02 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_always_inline void zend_interned_empty_string_init(zend_string **s)
|
2014-10-06 21:44:43 +08:00
|
|
|
{
|
|
|
|
zend_string *str;
|
|
|
|
|
|
|
|
str = zend_string_alloc(sizeof("")-1, 1);
|
2015-06-29 21:44:54 +08:00
|
|
|
ZSTR_VAL(str)[0] = '\000';
|
2014-10-06 21:44:43 +08:00
|
|
|
|
|
|
|
#ifndef ZTS
|
2014-12-14 06:06:14 +08:00
|
|
|
*s = zend_new_interned_string(str);
|
2014-10-06 21:44:43 +08:00
|
|
|
#else
|
|
|
|
zend_string_hash_val(str);
|
2015-06-29 21:44:54 +08:00
|
|
|
GC_FLAGS(str) |= IS_STR_INTERNED;
|
2014-10-06 21:44:43 +08:00
|
|
|
*s = str;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_always_inline void zend_interned_empty_string_free(zend_string **s)
|
2014-10-06 21:44:43 +08:00
|
|
|
{
|
|
|
|
if (NULL != *s) {
|
|
|
|
free(*s);
|
|
|
|
*s = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-20 19:05:54 +08:00
|
|
|
#endif /* ZEND_STRING_H */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|