How to cache zval * in HashTable?
[email protected] (xiangchao liu)
| Newsgroups | php.pecl.dev |
|---|---|
| Message-ID | <[email protected]> |
Hi, everyone,
I now want to cache zval * in global variable HashTable, so i can retrieve
it through requests, codes like below:
php_rockphp.h
================================================
ZEND_BEGIN_MODULE_GLOBALS(rockphp)
HashTable global_vars;
ZEND_END_MODULE_GLOBALS(rockphp)
rockphp.c
===============================================
...
static void php_rockphp_init_globals(zend_rockphp_globals *rockphp_globals)
{
zend_hash_init(&rockphp_globals->global_vars, 0, NULL, NULL, 1);
}
static void php_rockphp_destroy_globals(zend_rockphp_globals
*rockphp_globals) {
zend_hash_destroy(&rockphp_globals->global_vars);
}
...
PHP_FUNCTION(rock_set_var) {
char *param;
int paramLen;
zval *value;
zval *tmpValue;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", ¶m,
¶mLen, &value) == FAILURE) {
RETURN_NULL();
}
tmpValue = rock_copy_zval(value);
//php_printf("before:ref_count:%d type:%d is_ref:%d\n",
(tmpValue)->refcount, (tmpValue)->type, (tmpValue)->is_ref);
zend_hash_update(&ROCKPHP_G(global_vars), param, paramLen + 1, &tmpValue,
sizeof(zval *), NULL);
}
PHP_FUNCTION(rock_get_var) {
zval **value;
zval *tmpValue;
char *param;
int paramLen;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", ¶m, ¶mLen)
== FAILURE) {
RETURN_NULL();
}
if (!zend_hash_exists(&ROCKPHP_G(global_vars), param, paramLen + 1)) {
RETURN_NULL();
}
if (zend_hash_find(&ROCKPHP_G(global_vars), param, paramLen + 1, (void
**)&value) == SUCCESS) {
//php_printf("after:ref_count:%d type:%d is_ref:%d\n", (*value)->refcount,
(*value)->type, (*value)->is_ref);
if ((*value)->refcount < 1) {
zend_hash_del(&ROCKPHP_G(global_vars), param, paramLen + 1);
RETURN_NULL();
}
tmpValue = rock_copy_zval(*value);
RETURN_ZVAL(tmpValue, 1, ZVAL_PTR_DTOR);
}
RETURN_NULL();
}
static zval *rock_copy_zval(zval *z) {
zval *copy;
MAKE_STD_ZVAL(copy);
*copy = *z;
zval_copy_ctor(copy);
INIT_PZVAL(copy);
return copy;
}
So after compiling it to dll, i can use rock_set_var($name, $value) to
cache $value, and rock_get_var($name) to retrieve it, this is done in one
request everything goes well:
rock_set_var("hello", "Hello,World");
echo rock_get_var("hello");//I got Hello,World
but when i only call rock_get_var($name), sometimes i can get right value,
sometimes can not:
//rock_set_var("hello", "Hello,World");
echo rock_get_var("hello");//I got Hello,World in first some times, after
that, got nothing
I think zval * reference in HashTable is removed after per request.
So, can i hold zval * in HashTable, so that i can cache variables in global
HashTable?
--
Architect, Bokan Technologies
BokanTech.com