summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2021-03-22 14:56:16 +0300
committerDmitry Stogov <dmitry@zend.com>2021-03-22 14:56:16 +0300
commit8ed8cf86a98d1317c633189857ce4d64f105e6f7 (patch)
tree67bdbddb0e43c8437aad862b79dc76231426a52e
parent9162116a854fe37ce2c07a75b5ef7d797acba0ee (diff)
downloadphp-git-8ed8cf86a98d1317c633189857ce4d64f105e6f7.tar.gz
Use zend_string* instead of char*
-rw-r--r--ext/opcache/ZendAccelerator.c41
-rw-r--r--ext/opcache/zend_accelerator_hash.c36
-rw-r--r--ext/opcache/zend_accelerator_hash.h13
-rw-r--r--ext/opcache/zend_accelerator_module.c2
-rw-r--r--ext/opcache/zend_file_cache.c2
5 files changed, 48 insertions, 46 deletions
diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c
index 5ef84b2a2a..fc24cff389 100644
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@ -1346,6 +1346,25 @@ int zend_accel_invalidate(zend_string *filename, bool force)
return SUCCESS;
}
+static zend_string* accel_new_interned_key(zend_string *key)
+{
+ zend_string *new_key;
+
+ GC_ADDREF(key);
+ new_key = accel_new_interned_string(key);
+ if (UNEXPECTED(new_key == key)) {
+ new_key = zend_shared_alloc(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(ZSTR_LEN(key)), 8));
+ if (EXPECTED(new_key)) {
+ GC_SET_REFCOUNT(new_key, 2);
+ GC_TYPE_INFO(new_key) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT);
+ ZSTR_H(new_key) = ZSTR_H(key);
+ ZSTR_LEN(new_key) = ZSTR_LEN(key);
+ memcpy(ZSTR_VAL(new_key), ZSTR_VAL(key), ZSTR_LEN(new_key) + 1);
+ }
+ }
+ return new_key;
+}
+
/* Adds another key for existing cached script */
static void zend_accel_add_key(zend_string *key, zend_accel_hash_entry *bucket)
{
@@ -1355,11 +1374,10 @@ static void zend_accel_add_key(zend_string *key, zend_accel_hash_entry *bucket)
ZSMMG(memory_exhausted) = 1;
zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_HASH);
} else {
- char *new_key = zend_shared_alloc(ZSTR_LEN(key) + 1);
+ zend_string *new_key = accel_new_interned_key(key);
if (new_key) {
- memcpy(new_key, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
- if (zend_accel_hash_update(&ZCSG(hash), new_key, ZSTR_LEN(key), 1, bucket)) {
- zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", new_key);
+ if (zend_accel_hash_update(&ZCSG(hash), new_key, 1, bucket)) {
+ zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", ZSTR_VAL(new_key));
}
} else {
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
new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
/* store script structure in the hash table */
- 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);
+ bucket = zend_accel_hash_update(&ZCSG(hash), new_persistent_script->script.filename, 0, new_persistent_script);
if (bucket) {
zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", ZSTR_VAL(new_persistent_script->script.filename));
if (key &&
/* key may contain non-persistent PHAR aliases (see issues #115 and #149) */
- memcmp(key, "phar://", sizeof("phar://") - 1) != 0 &&
+ memcmp(ZSTR_VAL(key), "phar://", sizeof("phar://") - 1) != 0 &&
!zend_string_equals(new_persistent_script->script.filename, key)) {
/* link key to the same persistent script in hash table */
- char *new_key = zend_shared_alloc(ZSTR_LEN(key) + 1);
+ zend_string *new_key = accel_new_interned_key(key);
if (new_key) {
- memcpy(new_key, ZSTR_VAL(key), ZSTR_LEN(key) + 1);
- if (zend_accel_hash_update(&ZCSG(hash), new_key, ZSTR_LEN(key), 1, bucket)) {
+ if (zend_accel_hash_update(&ZCSG(hash), new_key, 1, bucket)) {
zend_accel_error(ACCEL_LOG_INFO, "Added key '%s'", ZSTR_VAL(key));
} else {
zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
@@ -3528,11 +3545,11 @@ static void preload_activate(void)
static void preload_restart(void)
{
- zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(ZCSG(preload_script)->script.filename), ZSTR_LEN(ZCSG(preload_script)->script.filename), 0, ZCSG(preload_script));
+ zend_accel_hash_update(&ZCSG(hash), ZCSG(preload_script)->script.filename, 0, ZCSG(preload_script));
if (ZCSG(saved_scripts)) {
zend_persistent_script **p = ZCSG(saved_scripts);
while (*p) {
- zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL((*p)->script.filename), ZSTR_LEN((*p)->script.filename), 0, *p);
+ zend_accel_hash_update(&ZCSG(hash), (*p)->script.filename, 0, *p);
p++;
}
}
@@ -4472,7 +4489,7 @@ static zend_persistent_script* preload_script_in_shared_memory(zend_persistent_s
new_persistent_script->dynamic_members.checksum = zend_accel_script_checksum(new_persistent_script);
/* store script structure in the hash table */
- 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);
+ bucket = zend_accel_hash_update(&ZCSG(hash), new_persistent_script->script.filename, 0, new_persistent_script);
if (bucket) {
zend_accel_error(ACCEL_LOG_INFO, "Cached script '%s'", ZSTR_VAL(new_persistent_script->script.filename));
}
diff --git a/ext/opcache/zend_accelerator_hash.c b/ext/opcache/zend_accelerator_hash.c
index e18960fb7d..a39b6a3ecf 100644
--- a/ext/opcache/zend_accelerator_hash.c
+++ b/ext/opcache/zend_accelerator_hash.c
@@ -71,7 +71,7 @@ void zend_accel_hash_init(zend_accel_hash *accel_hash, uint32_t hash_size)
* Returns pointer the actual hash entry on success
* key needs to be already allocated as it is not copied
*/
-zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const char *key, uint32_t key_length, bool indirect, void *data)
+zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, zend_string *key, bool indirect, void *data)
{
zend_ulong hash_value;
zend_ulong index;
@@ -85,7 +85,7 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
}
}
- hash_value = zend_inline_hash_func(key, key_length);
+ hash_value = zend_string_hash_val(key);
#ifndef ZEND_WIN32
hash_value ^= ZCG(root_hash);
#endif
@@ -95,8 +95,7 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
entry = accel_hash->hash_table[index];
while (entry) {
if (entry->hash_value == hash_value
- && entry->key_length == key_length
- && !memcmp(entry->key, key, key_length)) {
+ && zend_string_equals(entry->key, key)) {
if (entry->indirect) {
if (indirect_bucket) {
@@ -134,17 +133,18 @@ zend_accel_hash_entry* zend_accel_hash_update(zend_accel_hash *accel_hash, const
}
entry->hash_value = hash_value;
entry->key = key;
- entry->key_length = key_length;
entry->next = accel_hash->hash_table[index];
accel_hash->hash_table[index] = entry;
return entry;
}
-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)
+static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_hash, zend_string *key, int data)
{
zend_ulong index;
zend_accel_hash_entry *entry;
+ zend_ulong hash_value;
+ hash_value = zend_string_hash_val(key);
#ifndef ZEND_WIN32
hash_value ^= ZCG(root_hash);
#endif
@@ -153,8 +153,7 @@ static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_h
entry = accel_hash->hash_table[index];
while (entry) {
if (entry->hash_value == hash_value
- && entry->key_length == key_length
- && !memcmp(entry->key, key, key_length)) {
+ && zend_string_equals(entry->key, key)) {
if (entry->indirect) {
if (data) {
return ((zend_accel_hash_entry*)entry->data)->data;
@@ -179,12 +178,7 @@ static zend_always_inline void* zend_accel_hash_find_ex(zend_accel_hash *accel_h
*/
void* zend_accel_hash_find(zend_accel_hash *accel_hash, zend_string *key)
{
- return zend_accel_hash_find_ex(
- accel_hash,
- ZSTR_VAL(key),
- ZSTR_LEN(key),
- zend_string_hash_val(key),
- 1);
+ return zend_accel_hash_find_ex(accel_hash, key, 1);
}
/* Returns the hash entry associated with key on success
@@ -192,21 +186,16 @@ void* zend_accel_hash_find(zend_accel_hash *accel_hash, zend_string *key)
*/
zend_accel_hash_entry* zend_accel_hash_find_entry(zend_accel_hash *accel_hash, zend_string *key)
{
- return (zend_accel_hash_entry *)zend_accel_hash_find_ex(
- accel_hash,
- ZSTR_VAL(key),
- ZSTR_LEN(key),
- zend_string_hash_val(key),
- 0);
+ return (zend_accel_hash_entry *)zend_accel_hash_find_ex(accel_hash, key, 0);
}
-int zend_accel_hash_unlink(zend_accel_hash *accel_hash, const char *key, uint32_t key_length)
+int zend_accel_hash_unlink(zend_accel_hash *accel_hash, zend_string *key)
{
zend_ulong hash_value;
zend_ulong index;
zend_accel_hash_entry *entry, *last_entry=NULL;
- hash_value = zend_inline_hash_func(key, key_length);
+ hash_value = zend_string_hash_val(key);
#ifndef ZEND_WIN32
hash_value ^= ZCG(root_hash);
#endif
@@ -215,8 +204,7 @@ int zend_accel_hash_unlink(zend_accel_hash *accel_hash, const char *key, uint32_
entry = accel_hash->hash_table[index];
while (entry) {
if (entry->hash_value == hash_value
- && entry->key_length == key_length
- && !memcmp(entry->key, key, key_length)) {
+ && zend_string_equals(entry->key, key)) {
if (!entry->indirect) {
accel_hash->num_direct_entries--;
}
diff --git a/ext/opcache/zend_accelerator_hash.h b/ext/opcache/zend_accelerator_hash.h
index b2feba5ed2..f3df0c0885 100644
--- a/ext/opcache/zend_accelerator_hash.h
+++ b/ext/opcache/zend_accelerator_hash.h
@@ -46,11 +46,10 @@ typedef struct _zend_accel_hash_entry zend_accel_hash_entry;
struct _zend_accel_hash_entry {
zend_ulong hash_value;
- const char *key;
+ zend_string *key;
zend_accel_hash_entry *next;
void *data;
- uint32_t key_length;
- bool indirect;
+ bool indirect;
};
typedef struct _zend_accel_hash {
@@ -66,9 +65,8 @@ void zend_accel_hash_clean(zend_accel_hash *accel_hash);
zend_accel_hash_entry* zend_accel_hash_update(
zend_accel_hash *accel_hash,
- const char *key,
- uint32_t key_length,
- bool indirect,
+ zend_string *key,
+ bool indirect,
void *data);
void* zend_accel_hash_find(
@@ -81,8 +79,7 @@ zend_accel_hash_entry* zend_accel_hash_find_entry(
int zend_accel_hash_unlink(
zend_accel_hash *accel_hash,
- const char *key,
- uint32_t key_length);
+ zend_string *key);
static inline bool zend_accel_hash_is_full(zend_accel_hash *accel_hash)
{
diff --git a/ext/opcache/zend_accelerator_module.c b/ext/opcache/zend_accelerator_module.c
index d2d847678e..e8b4fb6e8e 100644
--- a/ext/opcache/zend_accelerator_module.c
+++ b/ext/opcache/zend_accelerator_module.c
@@ -562,7 +562,7 @@ static int accelerator_get_scripts(zval *return_value)
timerclear(&exec_time);
timerclear(&fetch_time);
- zend_hash_str_update(Z_ARRVAL_P(return_value), cache_entry->key, cache_entry->key_length, &persistent_script_report);
+ zend_hash_update(Z_ARRVAL_P(return_value), cache_entry->key, &persistent_script_report);
}
}
accelerator_shm_read_unlock();
diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c
index 884ee796e0..835ec5ac15 100644
--- a/ext/opcache/zend_file_cache.c
+++ b/ext/opcache/zend_file_cache.c
@@ -1882,7 +1882,7 @@ use_process_mem:
script->dynamic_members.checksum = zend_accel_script_checksum(script);
script->dynamic_members.last_used = ZCG(request_time);
- zend_accel_hash_update(&ZCSG(hash), ZSTR_VAL(script->script.filename), ZSTR_LEN(script->script.filename), 0, script);
+ zend_accel_hash_update(&ZCSG(hash), script->script.filename, 0, script);
zend_shared_alloc_unlock();
zend_accel_error(ACCEL_LOG_INFO, "File cached script loaded into memory '%s'", ZSTR_VAL(script->script.filename));