xref: /PHP-7.2/Zend/zend_string.c (revision 902d39a3)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@zend.com>                             |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* $Id: $ */
20 
21 #include "zend.h"
22 #include "zend_globals.h"
23 
24 ZEND_API zend_string *(*zend_new_interned_string)(zend_string *str);
25 
26 static zend_string *zend_new_interned_string_permanent(zend_string *str);
27 static zend_string *zend_new_interned_string_request(zend_string *str);
28 
29 /* Any strings interned in the startup phase. Common to all the threads,
30    won't be free'd until process exit. If we want an ability to
31    add permanent strings even after startup, it would be still
32    possible on costs of locking in the thread safe builds. */
33 static HashTable interned_strings_permanent;
34 
35 static zend_new_interned_string_func_t interned_string_request_handler = zend_new_interned_string_request;
36 static zend_string_copy_storage_func_t interned_string_copy_storage = NULL;
37 
38 ZEND_API zend_string  *zend_empty_string = NULL;
39 ZEND_API zend_string  *zend_one_char_string[256];
40 ZEND_API zend_string **zend_known_strings = NULL;
41 
zend_hash_func(const char * str,size_t len)42 ZEND_API zend_ulong zend_hash_func(const char *str, size_t len)
43 {
44 	return zend_inline_hash_func(str, len);
45 }
46 
_str_dtor(zval * zv)47 static void _str_dtor(zval *zv)
48 {
49 	zend_string *str = Z_STR_P(zv);
50 	pefree(str, GC_FLAGS(str) & IS_STR_PERSISTENT);
51 }
52 
53 static const char *known_strings[] = {
54 #define _ZEND_STR_DSC(id, str) str,
55 ZEND_KNOWN_STRINGS(_ZEND_STR_DSC)
56 #undef _ZEND_STR_DSC
57 	NULL
58 };
59 
zend_init_interned_strings_ht(HashTable * interned_strings,int permanent)60 static void zend_init_interned_strings_ht(HashTable *interned_strings, int permanent)
61 {
62 	zend_hash_init(interned_strings, 1024, NULL, _str_dtor, permanent);
63 	zend_hash_real_init(interned_strings, 0);
64 }
65 
zend_interned_strings_init(void)66 ZEND_API void zend_interned_strings_init(void)
67 {
68 	char s[2];
69 	int i;
70 	zend_string *str;
71 
72 	interned_string_request_handler = zend_new_interned_string_request;
73 	interned_string_copy_storage = NULL;
74 
75 	zend_empty_string = NULL;
76 	zend_known_strings = NULL;
77 
78 	zend_init_interned_strings_ht(&interned_strings_permanent, 1);
79 
80 	zend_new_interned_string = zend_new_interned_string_permanent;
81 
82 	/* interned empty string */
83 	str = zend_string_alloc(sizeof("")-1, 1);
84 	ZSTR_VAL(str)[0] = '\000';
85 	zend_empty_string = zend_new_interned_string_permanent(str);
86 
87 	s[1] = 0;
88 	for (i = 0; i < 256; i++) {
89 		s[0] = i;
90 		zend_one_char_string[i] = zend_new_interned_string_permanent(zend_string_init(s, 1, 1));
91 	}
92 
93 	/* known strings */
94 	zend_known_strings = pemalloc(sizeof(zend_string*) * ((sizeof(known_strings) / sizeof(known_strings[0]) - 1)), 1);
95 	for (i = 0; i < (sizeof(known_strings) / sizeof(known_strings[0])) - 1; i++) {
96 		str = zend_string_init(known_strings[i], strlen(known_strings[i]), 1);
97 		zend_known_strings[i] = zend_new_interned_string_permanent(str);
98 	}
99 }
100 
zend_interned_strings_dtor(void)101 ZEND_API void zend_interned_strings_dtor(void)
102 {
103 	zend_hash_destroy(&interned_strings_permanent);
104 
105 	free(zend_known_strings);
106 	zend_known_strings = NULL;
107 }
108 
zend_interned_string_ht_lookup(zend_string * str,HashTable * interned_strings)109 static zend_always_inline zend_string *zend_interned_string_ht_lookup(zend_string *str, HashTable *interned_strings)
110 {
111 	zend_ulong h;
112 	uint32_t nIndex;
113 	uint32_t idx;
114 	Bucket *p;
115 
116 	h = zend_string_hash_val(str);
117 	nIndex = h | interned_strings->nTableMask;
118 	idx = HT_HASH(interned_strings, nIndex);
119 	while (idx != HT_INVALID_IDX) {
120 		p = HT_HASH_TO_BUCKET(interned_strings, idx);
121 		if ((p->h == h) && (ZSTR_LEN(p->key) == ZSTR_LEN(str))) {
122 			if (!memcmp(ZSTR_VAL(p->key), ZSTR_VAL(str), ZSTR_LEN(str))) {
123 				return p->key;
124 			}
125 		}
126 		idx = Z_NEXT(p->val);
127 	}
128 
129 	return NULL;
130 }
131 
132 /* This function might be not thread safe at least because it would update the
133    hash val in the passed string. Be sure it is called in the appropriate context. */
zend_add_interned_string(zend_string * str,HashTable * interned_strings,uint32_t flags)134 static zend_always_inline zend_string *zend_add_interned_string(zend_string *str, HashTable *interned_strings, uint32_t flags)
135 {
136 	zval val;
137 
138 	GC_REFCOUNT(str) = 1;
139 	GC_FLAGS(str) |= IS_STR_INTERNED | flags;
140 
141 	ZVAL_INTERNED_STR(&val, str);
142 
143 	zend_hash_add_new(interned_strings, str, &val);
144 
145 	return str;
146 }
147 
zend_interned_string_find_permanent(zend_string * str)148 ZEND_API zend_string *zend_interned_string_find_permanent(zend_string *str)
149 {
150 	return zend_interned_string_ht_lookup(str, &interned_strings_permanent);
151 }
152 
153 
zend_new_interned_string_permanent(zend_string * str)154 static zend_string *zend_new_interned_string_permanent(zend_string *str)
155 {
156 	zend_string *ret;
157 
158 	if (ZSTR_IS_INTERNED(str)) {
159 		return str;
160 	}
161 
162 	ret = zend_interned_string_ht_lookup(str, &interned_strings_permanent);
163 	if (ret) {
164 		zend_string_release(str);
165 		return ret;
166 	}
167 
168 	return zend_add_interned_string(str, &interned_strings_permanent, IS_STR_PERMANENT);
169 }
170 
zend_new_interned_string_request(zend_string * str)171 static zend_string *zend_new_interned_string_request(zend_string *str)
172 {
173 	zend_string *ret;
174 
175 	if (ZSTR_IS_INTERNED(str)) {
176 		return str;
177 	}
178 
179 	/* Check for permanent strings, the table is readonly at this point. */
180 	ret = zend_interned_string_ht_lookup(str, &interned_strings_permanent);
181 	if (ret) {
182 		zend_string_release(str);
183 		return ret;
184 	}
185 
186 	ret = zend_interned_string_ht_lookup(str, &CG(interned_strings));
187 	if (ret) {
188 		zend_string_release(str);
189 		return ret;
190 	}
191 
192 	/* Create a short living interned, freed after the request. */
193 	ret = zend_add_interned_string(str, &CG(interned_strings), 0);
194 
195 	return ret;
196 }
197 
zend_interned_strings_activate(void)198 ZEND_API void zend_interned_strings_activate(void)
199 {
200 	zend_init_interned_strings_ht(&CG(interned_strings), 0);
201 }
202 
zend_interned_strings_deactivate(void)203 ZEND_API void zend_interned_strings_deactivate(void)
204 {
205 	zend_hash_destroy(&CG(interned_strings));
206 }
207 
zend_interned_strings_set_request_storage_handler(zend_new_interned_string_func_t handler)208 ZEND_API void zend_interned_strings_set_request_storage_handler(zend_new_interned_string_func_t handler)
209 {
210 	interned_string_request_handler = handler;
211 }
212 
zend_interned_strings_set_permanent_storage_copy_handler(zend_string_copy_storage_func_t handler)213 ZEND_API void zend_interned_strings_set_permanent_storage_copy_handler(zend_string_copy_storage_func_t handler)
214 {
215 	interned_string_copy_storage = handler;
216 }
217 
zend_interned_strings_switch_storage(void)218 ZEND_API void zend_interned_strings_switch_storage(void)
219 {
220 	if (interned_string_copy_storage) {
221 		interned_string_copy_storage();
222 	}
223 	zend_new_interned_string = interned_string_request_handler;
224 }
225 
226 /*
227  * Local variables:
228  * tab-width: 4
229  * c-basic-offset: 4
230  * indent-tabs-mode: t
231  * End:
232  * vim600: sw=4 ts=4 fdm=marker
233  * vim<600: sw=4 ts=4
234  */
235