-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_string.c
265 lines (215 loc) · 8.57 KB
/
apc_string.c
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
/*
+----------------------------------------------------------------------+
| APC |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Dmitry Stogov <[email protected]> |
+----------------------------------------------------------------------+
This software was contributed to PHP by Community Connect Inc. in 2002
and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
Future revisions and derivatives of this source code must acknowledge
Community Connect Inc. as the original contributor of this module by
leaving this note intact in the source code.
All other licensing and usage conditions are those of the PHP Group.
*/
/* $Id: apc_string.c 327244 2012-08-23 09:08:12Z ab $ */
#include "apc.h"
#include "apc_globals.h"
#include "apc_php.h"
#include "apc_lock.h"
#ifdef ZEND_ENGINE_2_4
#ifndef ZTS
typedef struct _apc_interned_strings_data_t {
char *interned_strings_start;
char *interned_strings_end;
char *interned_strings_top;
apc_lck_t lock;
HashTable interned_strings;
} apc_interned_strings_data_t;
apc_interned_strings_data_t *apc_interned_strings_data = NULL;
#define APCSG(v) (apc_interned_strings_data->v)
static char *old_interned_strings_start;
static char *old_interned_strings_end;
static const char *(*old_new_interned_string)(const char *str, int len, int free_src TSRMLS_DC);
static void (*old_interned_strings_snapshot)(TSRMLS_D);
static void (*old_interned_strings_restore)(TSRMLS_D);
static const char *apc_dummy_new_interned_string_for_php(const char *str, int len, int free_src TSRMLS_DC)
{
return str;
}
static void apc_dummy_interned_strings_snapshot_for_php(TSRMLS_D)
{
}
static void apc_dummy_interned_strings_restore_for_php(TSRMLS_D)
{
}
#endif
const char *apc_new_interned_string(const char *arKey, int nKeyLength TSRMLS_DC)
{
#ifndef ZTS
ulong h;
uint nIndex;
Bucket *p;
if (arKey >= APCSG(interned_strings_start) && arKey < APCSG(interned_strings_end)) {
return arKey;
}
h = zend_inline_hash_func(arKey, nKeyLength);
nIndex = h & APCSG(interned_strings).nTableMask;
p = APCSG(interned_strings).arBuckets[nIndex];
while (p != NULL) {
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
if (!memcmp(p->arKey, arKey, nKeyLength)) {
return p->arKey;
}
}
p = p->pNext;
}
if (APCSG(interned_strings_top) + ZEND_MM_ALIGNED_SIZE(sizeof(Bucket) + nKeyLength + 1) >=
APCSG(interned_strings_end)) {
/* no memory */
return NULL;
}
p = (Bucket *) APCSG(interned_strings_top);
APCSG(interned_strings_top) += ZEND_MM_ALIGNED_SIZE(sizeof(Bucket) + nKeyLength + 1);
p->arKey = (char*)(p+1);
memcpy(p->arKey, arKey, nKeyLength);
((char *)p->arKey)[nKeyLength] = '\0';
p->nKeyLength = nKeyLength;
p->h = h;
p->pData = &p->pDataPtr;
p->pDataPtr = p;
p->pNext = APCSG(interned_strings).arBuckets[nIndex];
p->pLast = NULL;
if (p->pNext) {
p->pNext->pLast = p;
}
APCSG(interned_strings).arBuckets[nIndex] = p;
p->pListLast = APCSG(interned_strings).pListTail;
APCSG(interned_strings).pListTail = p;
p->pListNext = NULL;
if (p->pListLast != NULL) {
p->pListLast->pListNext = p;
}
if (!APCSG(interned_strings).pListHead) {
APCSG(interned_strings).pListHead = p;
}
APCSG(interned_strings).nNumOfElements++;
return p->arKey;
#else
return zend_new_interned_string(arKey, nKeyLength, 0 TSRMLS_CC);
#endif
}
#ifndef ZTS
static void apc_copy_internal_strings(TSRMLS_D)
{
Bucket *p, *q;
p = CG(function_table)->pListHead;
while (p) {
if (p->nKeyLength) {
p->arKey = apc_new_interned_string(p->arKey, p->nKeyLength TSRMLS_CC);
}
p = p->pListNext;
}
p = CG(class_table)->pListHead;
while (p) {
zend_class_entry *ce = (zend_class_entry*)(p->pDataPtr);
if (p->nKeyLength) {
p->arKey = apc_new_interned_string(p->arKey, p->nKeyLength TSRMLS_CC);
}
if (ce->name) {
ce->name = apc_new_interned_string(ce->name, ce->name_length+1 TSRMLS_CC);
}
q = ce->properties_info.pListHead;
while (q) {
zend_property_info *info = (zend_property_info*)(q->pData);
if (q->nKeyLength) {
q->arKey = apc_new_interned_string(q->arKey, q->nKeyLength TSRMLS_CC);
}
if (info->name) {
info->name = apc_new_interned_string(info->name, info->name_length+1 TSRMLS_CC);
}
q = q->pListNext;
}
q = ce->function_table.pListHead;
while (q) {
if (q->nKeyLength) {
q->arKey = apc_new_interned_string(q->arKey, q->nKeyLength TSRMLS_CC);
}
q = q->pListNext;
}
q = ce->constants_table.pListHead;
while (q) {
if (q->nKeyLength) {
q->arKey = apc_new_interned_string(q->arKey, q->nKeyLength TSRMLS_CC);
}
q = q->pListNext;
}
p = p->pListNext;
}
p = EG(zend_constants)->pListHead;
while (p) {
if (p->nKeyLength) {
p->arKey = apc_new_interned_string(p->arKey, p->nKeyLength TSRMLS_CC);
}
p = p->pListNext;
}
}
void apc_interned_strings_init(TSRMLS_D)
{
int count = APCG(shm_strings_buffer) / (sizeof(Bucket) + sizeof(Bucket*) * 2);
apc_interned_strings_data = (apc_interned_strings_data_t*) apc_sma_malloc(APCG(shm_strings_buffer) TSRMLS_CC);
if (apc_interned_strings_data) {
memset((void *)apc_interned_strings_data, 0, APCG(shm_strings_buffer));
CREATE_LOCK(APCSG(lock));
zend_hash_init(&APCSG(interned_strings), count, NULL, NULL, 1);
APCSG(interned_strings).nTableMask = APCSG(interned_strings).nTableSize - 1;
APCSG(interned_strings).arBuckets = (Bucket**)((char*)apc_interned_strings_data + sizeof(apc_interned_strings_data_t));
APCSG(interned_strings_start) = (char*)APCSG(interned_strings).arBuckets + APCSG(interned_strings).nTableSize * sizeof(Bucket *);
APCSG(interned_strings_end) = (char*)apc_interned_strings_data + APCG(shm_strings_buffer);
APCSG(interned_strings_top) = APCSG(interned_strings_start);
old_interned_strings_start = CG(interned_strings_start);
old_interned_strings_end = CG(interned_strings_end);
old_new_interned_string = zend_new_interned_string;
old_interned_strings_snapshot = zend_interned_strings_snapshot;
old_interned_strings_restore = zend_interned_strings_restore;
CG(interned_strings_start) = APCSG(interned_strings_start);
CG(interned_strings_end) = APCSG(interned_strings_end);
zend_new_interned_string = apc_dummy_new_interned_string_for_php;
zend_interned_strings_snapshot = apc_dummy_interned_strings_snapshot_for_php;
zend_interned_strings_restore = apc_dummy_interned_strings_restore_for_php;
apc_copy_internal_strings(TSRMLS_C);
}
}
void apc_interned_strings_shutdown(TSRMLS_D)
{
if (apc_interned_strings_data) {
zend_hash_clean(CG(function_table));
zend_hash_clean(CG(class_table));
zend_hash_clean(EG(zend_constants));
CG(interned_strings_start) = old_interned_strings_start;
CG(interned_strings_end) = old_interned_strings_end;
zend_new_interned_string = old_new_interned_string;
zend_interned_strings_snapshot = old_interned_strings_snapshot;
zend_interned_strings_restore = old_interned_strings_restore;
DESTROY_LOCK(APCSG(lock));
}
}
#endif
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim>600: expandtab sw=4 ts=4 sts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4 sts=4
*/