Skip to content

Commit 8ed8cf8

Browse files
committed
Use zend_string* instead of char*
1 parent 9162116 commit 8ed8cf8

5 files changed

+48
-46
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,25 @@ int zend_accel_invalidate(zend_string *filename, bool force)
13461346
return SUCCESS;
13471347
}
13481348

1349+
static zend_string* accel_new_interned_key(zend_string *key)
1350+
{
1351+
zend_string *new_key;
1352+
1353+
GC_ADDREF(key);
1354+
new_key = accel_new_interned_string(key);
1355+
if (UNEXPECTED(new_key == key)) {
1356+
new_key = zend_shared_alloc(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(ZSTR_LEN(key)), 8));
1357+
if (EXPECTED(new_key)) {
1358+
GC_SET_REFCOUNT(new_key, 2);
1359+
GC_TYPE_INFO(new_key) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT);
1360+
ZSTR_H(new_key) = ZSTR_H(key);
1361+
ZSTR_LEN(new_key) = ZSTR_LEN(key);
1362+
memcpy(ZSTR_VAL(new_key), ZSTR_VAL(key), ZSTR_LEN(new_key) + 1);
1363+
}
1364+
}
1365+
return new_key;
1366+
}
1367+
13491368
/* Adds another key for existing cached script */
13501369
static void zend_accel_add_key(zend_string *key, zend_accel_hash_entry *bucket)
13511370
{
@@ -1355,11 +1374,10 @@ static void zend_accel_add_key(zend_string *key, zend_accel_hash_entry *bucket)
13551374
ZSMMG(memory_exhausted) = 1;
13561375
zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
13571376
} else {
1358-
char *new_key = zend_shared_alloc(ZSTR_LEN(key) + 1);
1377+
zend_string *new_key = accel_new_interned_key(key);
13591378
if (new_key) {
1360-
memcpy(new_key, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
1361-
if (zend_accel_hash_update(&ZCSG(hash), new_key, ZSTR_LEN(key), 1, bucket)) {
1362-
zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", new_key);
1379+
if (zend_accel_hash_update(&ZCSG(hash), new_key, 1, bucket)) {
1380+
zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", ZSTR_VAL(new_key));
13631381
}
13641382
} else {
13651383
zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM);
@@ -1577,19 +1595,18 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
15771595
new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
15781596

15791597
/* store script structure in the hash table */
1580-
bucket = zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(new_persistent_script->script.filename), ZSTR_LEN(new_persistent_script->script.filename), 0, new_persistent_script);
1598+
bucket = zend_accel_hash_update(&ZCSG(hash), new_persistent_script->script.filename, 0, new_persistent_script);
15811599
if (bucket) {
15821600
zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", ZSTR_VAL(new_persistent_script->script.filename));
15831601
if (key &&
15841602
/* key may contain non-persistent PHAR aliases (see issues #115 and #149) */
1585-
memcmp(key, "phar://", sizeof("phar://") - 1) != 0 &&
1603+
memcmp(ZSTR_VAL(key), "phar://", sizeof("phar://") - 1) != 0 &&
15861604
!zend_string_equals(new_persistent_script->script.filename, key)) {
15871605
/* link key to the same persistent script in hash table */
1588-
char *new_key = zend_shared_alloc(ZSTR_LEN(key) + 1);
1606+
zend_string *new_key = accel_new_interned_key(key);
15891607

15901608
if (new_key) {
1591-
memcpy(new_key, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
1592-
if (zend_accel_hash_update(&ZCSG(hash), new_key, ZSTR_LEN(key), 1, bucket)) {
1609+
if (zend_accel_hash_update(&ZCSG(hash), new_key, 1, bucket)) {
15931610
zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", ZSTR_VAL(key));
15941611
} else {
15951612
zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
@@ -3528,11 +3545,11 @@ static void preload_activate(void)
35283545

35293546
static void preload_restart(void)
35303547
{
3531-
zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(ZCSG(preload_script)->script.filename), ZSTR_LEN(ZCSG(preload_script)->script.filename), 0, ZCSG(preload_script));
3548+
zend_accel_hash_update(&ZCSG(hash), ZCSG(preload_script)->script.filename, 0, ZCSG(preload_script));
35323549
if (ZCSG(saved_scripts)) {
35333550
zend_persistent_script **p = ZCSG(saved_scripts);
35343551
while (*p) {
3535-
zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL((*p)->script.filename), ZSTR_LEN((*p)->script.filename), 0, *p);
3552+
zend_accel_hash_update(&ZCSG(hash), (*p)->script.filename, 0, *p);
35363553
p++;
35373554
}
35383555
}
@@ -4472,7 +4489,7 @@ static zend_persistent_script* preload_script_in_shared_memory(zend_persistent_s
44724489
new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
44734490

44744491
/* store script structure in the hash table */
4475-
bucket = zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(new_persistent_script->script.filename), ZSTR_LEN(new_persistent_script->script.filename), 0, new_persistent_script);
4492+
bucket = zend_accel_hash_update(&ZCSG(hash), new_persistent_script->script.filename, 0, new_persistent_script);
44764493
if (bucket) {
44774494
zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", ZSTR_VAL(new_persistent_script->script.filename));
44784495
}

ext/opcache/zend_accelerator_hash.c

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void zend_accel_hash_init(zend_accel_hash *accel_hash, uint32_t hash_size)
7171
* Returns pointer the actual hash entry on success
7272
* key needs to be already allocated as it is not copied
7373
*/
74-
zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const char *key, uint32_t key_length, bool indirect, void *data)
74+
zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, zend_string *key, bool indirect, void *data)
7575
{
7676
zend_ulong hash_value;
7777
zend_ulong index;
@@ -85,7 +85,7 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
8585
}
8686
}
8787

88-
hash_value = zend_inline_hash_func(key, key_length);
88+
hash_value = zend_string_hash_val(key);
8989
#ifndef ZEND_WIN32
9090
hash_value ^= ZCG(root_hash);
9191
#endif
@@ -95,8 +95,7 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
9595
entry = accel_hash->hash_table[index];
9696
while (entry) {
9797
if (entry->hash_value == hash_value
98-
&& entry->key_length == key_length
99-
&& !memcmp(entry->key, key, key_length)) {
98+
&& zend_string_equals(entry->key, key)) {
10099

101100
if (entry->indirect) {
102101
if (indirect_bucket) {
@@ -134,17 +133,18 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
134133
}
135134
entry->hash_value = hash_value;
136135
entry->key = key;
137-
entry->key_length = key_length;
138136
entry->next = accel_hash->hash_table[index];
139137
accel_hash->hash_table[index] = entry;
140138
return entry;
141139
}
142140

143-
static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_hash, const char *key, uint32_t key_length, zend_ulong hash_value, int data)
141+
static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_hash, zend_string *key, int data)
144142
{
145143
zend_ulong index;
146144
zend_accel_hash_entry *entry;
145+
zend_ulong hash_value;
147146

147+
hash_value = zend_string_hash_val(key);
148148
#ifndef ZEND_WIN32
149149
hash_value ^= ZCG(root_hash);
150150
#endif
@@ -153,8 +153,7 @@ static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_h
153153
entry = accel_hash->hash_table[index];
154154
while (entry) {
155155
if (entry->hash_value == hash_value
156-
&& entry->key_length == key_length
157-
&& !memcmp(entry->key, key, key_length)) {
156+
&& zend_string_equals(entry->key, key)) {
158157
if (entry->indirect) {
159158
if (data) {
160159
return ((zend_accel_hash_entry*)entry->data)->data;
@@ -179,34 +178,24 @@ static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_h
179178
*/
180179
void* zend_accel_hash_find(zend_accel_hash *accel_hash, zend_string *key)
181180
{
182-
return zend_accel_hash_find_ex(
183-
accel_hash,
184-
ZSTR_VAL(key),
185-
ZSTR_LEN(key),
186-
zend_string_hash_val(key),
187-
1);
181+
return zend_accel_hash_find_ex(accel_hash, key, 1);
188182
}
189183

190184
/* Returns the hash entry associated with key on success
191185
* Returns NULL if it doesn't exist
192186
*/
193187
zend_accel_hash_entry* zend_accel_hash_find_entry(zend_accel_hash *accel_hash, zend_string *key)
194188
{
195-
return (zend_accel_hash_entry *)zend_accel_hash_find_ex(
196-
accel_hash,
197-
ZSTR_VAL(key),
198-
ZSTR_LEN(key),
199-
zend_string_hash_val(key),
200-
0);
189+
return (zend_accel_hash_entry *)zend_accel_hash_find_ex(accel_hash, key, 0);
201190
}
202191

203-
int zend_accel_hash_unlink(zend_accel_hash *accel_hash, const char *key, uint32_t key_length)
192+
int zend_accel_hash_unlink(zend_accel_hash *accel_hash, zend_string *key)
204193
{
205194
zend_ulong hash_value;
206195
zend_ulong index;
207196
zend_accel_hash_entry *entry, *last_entry=NULL;
208197

209-
hash_value = zend_inline_hash_func(key, key_length);
198+
hash_value = zend_string_hash_val(key);
210199
#ifndef ZEND_WIN32
211200
hash_value ^= ZCG(root_hash);
212201
#endif
@@ -215,8 +204,7 @@ int zend_accel_hash_unlink(zend_accel_hash *accel_hash, const char *key, uint32_
215204
entry = accel_hash->hash_table[index];
216205
while (entry) {
217206
if (entry->hash_value == hash_value
218-
&& entry->key_length == key_length
219-
&& !memcmp(entry->key, key, key_length)) {
207+
&& zend_string_equals(entry->key, key)) {
220208
if (!entry->indirect) {
221209
accel_hash->num_direct_entries--;
222210
}

ext/opcache/zend_accelerator_hash.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ typedef struct _zend_accel_hash_entry zend_accel_hash_entry;
4646

4747
struct _zend_accel_hash_entry {
4848
zend_ulong hash_value;
49-
const char *key;
49+
zend_string *key;
5050
zend_accel_hash_entry *next;
5151
void *data;
52-
uint32_t key_length;
53-
bool indirect;
52+
bool indirect;
5453
};
5554

5655
typedef struct _zend_accel_hash {
@@ -66,9 +65,8 @@ void zend_accel_hash_clean(zend_accel_hash *accel_hash);
6665

6766
zend_accel_hash_entry* zend_accel_hash_update(
6867
zend_accel_hash *accel_hash,
69-
const char *key,
70-
uint32_t key_length,
71-
bool indirect,
68+
zend_string *key,
69+
bool indirect,
7270
void *data);
7371

7472
void* zend_accel_hash_find(
@@ -81,8 +79,7 @@ zend_accel_hash_entry* zend_accel_hash_find_entry(
8179

8280
int zend_accel_hash_unlink(
8381
zend_accel_hash *accel_hash,
84-
const char *key,
85-
uint32_t key_length);
82+
zend_string *key);
8683

8784
static inline bool zend_accel_hash_is_full(zend_accel_hash *accel_hash)
8885
{

ext/opcache/zend_accelerator_module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static int accelerator_get_scripts(zval *return_value)
562562
timerclear(&exec_time);
563563
timerclear(&fetch_time);
564564

565-
zend_hash_str_update(Z_ARRVAL_P(return_value), cache_entry->key, cache_entry->key_length, &persistent_script_report);
565+
zend_hash_update(Z_ARRVAL_P(return_value), cache_entry->key, &persistent_script_report);
566566
}
567567
}
568568
accelerator_shm_read_unlock();

ext/opcache/zend_file_cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
18821882
script->dynamic_members.checksum = zend_accel_script_checksum(script);
18831883
script->dynamic_members.last_used = ZCG(request_time);
18841884

1885-
zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(script->script.filename), ZSTR_LEN(script->script.filename), 0, script);
1885+
zend_accel_hash_update(&ZCSG(hash), script->script.filename, 0, script);
18861886

18871887
zend_shared_alloc_unlock();
18881888
zend_accel_error(ACCEL_LOG_INFO, "File cached script loaded into memory '%s'", ZSTR_VAL(script->script.filename));

0 commit comments

Comments
 (0)