summaryrefslogtreecommitdiff
path: root/Zend/zend_string.h
blob: f30ae2994182abb68f2d624a28ee37b7e655d661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        | 
   | 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"

BEGIN_EXTERN_C()

ZEND_API extern zend_string *(*zend_new_interned_string)(zend_string *str TSRMLS_DC);
ZEND_API extern void (*zend_interned_strings_snapshot)(TSRMLS_D);
ZEND_API extern void (*zend_interned_strings_restore)(TSRMLS_D);

ZEND_API zend_uint_t zend_hash_func(const char *str, zend_size_t len);
void zend_interned_strings_init(TSRMLS_D);
void zend_interned_strings_dtor(TSRMLS_D);

END_EXTERN_C()

#define IS_INTERNED(s)					(GC_FLAGS(s) & IS_STR_INTERNED)

#define STR_HASH_VAL(s)					zend_str_hash_val(s)
#define STR_FORGET_HASH_VAL(s)			zend_str_forget_hash_val(s)

#define STR_REFCOUNT(s)					zend_str_refcount(s)
#define STR_ADDREF(s)					zend_str_addref(s)
#define STR_DELREF(s)					zend_str_delref(s)
#define STR_ALLOC(len, persistent)		zend_str_alloc(len, persistent)
#define STR_SAFE_ALLOC(n, m, l, p)		zend_str_safe_alloc(n, m, l, p)
#define STR_INIT(str, len, persistent)	zend_str_init(str, len, persistent)
#define STR_COPY(s)						zend_str_copy(s)
#define STR_DUP(s, persistent)			zend_str_dup(s, persistent)
#define STR_REALLOC(s, len, persistent)	zend_str_realloc(s, len, persistent)
#define STR_SAFE_REALLOC(s, n, m, l, p)	zend_str_safe_realloc(s, n, m, l, p)
#define STR_FREE(s)						zend_str_free(s)
#define STR_RELEASE(s)					zend_str_release(s)
#define STR_EMPTY_ALLOC()				CG(empty_string)

#define _STR_HEADER_SIZE XtOffsetOf(zend_string, val)

#define STR_ALLOCA_ALLOC(str, _len, use_heap) do { \
	(str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + (_len) + 1), (use_heap)); \
	GC_REFCOUNT(str) = 1; \
	(str)->h = 0; \
	(str)->len = (_len); \
} while (0)
#define STR_ALLOCA_INIT(str, s, len, use_heap) do { \
	STR_ALLOCA_ALLOC(str, len, use_heap); \
	memcpy((str)->val, (s), (len)); \
	(str)->val[(len)] = '\0'; \
} while (0)

#define STR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)

static zend_always_inline zend_uint_t zend_str_hash_val(zend_string *s)
{
	if (!s->h) {
		s->h = zend_hash_func(s->val, s->len);
	}
	return s->h;
}

static zend_always_inline void zend_str_forget_hash_val(zend_string *s)
{
	s->h = 0;
}

static zend_always_inline zend_uint zend_str_refcount(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		return GC_REFCOUNT(s);
	}
	return 1;
}

static zend_always_inline zend_uint zend_str_addref(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		return ++GC_REFCOUNT(s);
	}
	return 1;
}

static zend_always_inline zend_uint zend_str_delref(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		return --GC_REFCOUNT(s);
	}
	return 1;
}

static zend_always_inline zend_string *zend_str_alloc(zend_size_t len, int persistent)
{
	zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);

	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
	ret->h = 0;
	ret->len = len;
	return ret;
}

static zend_always_inline zend_string *zend_str_safe_alloc(size_t n, size_t m, size_t l, int persistent)
{
	zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + l + 1), persistent);

	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
	ret->h = 0;
	ret->len = (n * m) + l;
	return ret;
}

static zend_always_inline zend_string *zend_str_init(const char *str, zend_size_t len, int persistent)
{
	zend_string *ret = STR_ALLOC(len, persistent);

	memcpy(ret->val, str, len);
	ret->val[len] = '\0';
	return ret;
}

static zend_always_inline zend_string *zend_str_copy(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		STR_ADDREF(s);
	}
	return s;
}

static zend_always_inline zend_string *zend_str_dup(zend_string *s, int persistent)
{
	if (IS_INTERNED(s)) {
		return s;
	} else {
		return STR_INIT(s->val, s->len, persistent);
	}
}

static zend_always_inline zend_string *zend_str_realloc(zend_string *s, zend_size_t len, int persistent)
{
	zend_string *ret;

	if (IS_INTERNED(s)) {
		ret = STR_ALLOC(len, persistent);
		memcpy(ret->val, s->val, (len > s->len ? s->len : len) + 1);
	} else if (EXPECTED(STR_REFCOUNT(s) == 1)) {
		ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + len + 1), persistent);
		ret->len = len;
		STR_FORGET_HASH_VAL(ret);
	} else {
		ret = STR_ALLOC(len, persistent);
		memcpy(ret->val, s->val, (len > s->len ? s->len : len) + 1);
		STR_DELREF(s);
	}
	return ret;
}

static zend_always_inline zend_string *zend_str_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, int persistent)
{
	zend_string *ret;

	if (IS_INTERNED(s)) {
		ret = STR_SAFE_ALLOC(n, m, l, persistent);
		memcpy(ret->val, s->val, ((n * m) + l > (size_t)s->len ? (size_t)s->len : ((n * m) + l)) + 1);
	} else if (STR_REFCOUNT(s) == 1) {
		ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_STR_HEADER_SIZE + l + 1), persistent);
		ret->len = (n * m) + l;
		STR_FORGET_HASH_VAL(ret);
	} else {
		ret = STR_SAFE_ALLOC(n, m, l, persistent);
		memcpy(ret->val, s->val, ((n * m) + l > (size_t)s->len ? (size_t)s->len : ((n * m) + l)) + 1);
		STR_DELREF(s);
	}
	return ret;
}

static zend_always_inline void zend_str_free(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		ZEND_ASSERT(STR_REFCOUNT(s) <= 1);
		pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
	}
}

static zend_always_inline void zend_str_release(zend_string *s)
{
	if (!IS_INTERNED(s)) {
		if (STR_DELREF(s) == 0) {
			pefree(s, GC_FLAGS(s) & IS_STR_PERSISTENT);
		}
	}
}

/*
 * 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
 * with an average percent of approx. 86%. 
 *
 * 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>
 */

static inline zend_uint_t zend_inline_hash_func(const char *str, zend_size_t len)
{
	register zend_uint_t hash = Z_UI(5381);

	/* 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()
	}
	return hash;
}

#endif /* ZEND_STRING_H */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * indent-tabs-mode: t
 * End:
 */