Use better data structures (incomplete)

This commit is contained in:
Dmitry Stogov 2014-02-14 15:11:21 +04:00
parent 686a078258
commit e85790a4d4
3 changed files with 29 additions and 41 deletions

View File

@ -226,17 +226,17 @@ static int module_name_cmp(const void *a, const void *b TSRMLS_DC)
Bucket *f = (Bucket *) a;
Bucket *s = (Bucket *) b;
return strcasecmp( ((zend_module_entry *)f->xData)->name,
((zend_module_entry *)s->xData)->name);
return strcasecmp( ((zend_module_entry *)Z_PTR(f->val))->name,
((zend_module_entry *)Z_PTR(s->val))->name);
}
static void print_modules(TSRMLS_D)
{
HashTable sorted_registry;
zend_module_entry tmp;
zend_hash_init(&sorted_registry, 50, NULL, NULL, 1);
zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
//??? zend_hash_copy(&sorted_registry, &module_registry, NULL, &tmp, sizeof(zend_module_entry));
zend_hash_copy(&sorted_registry, &module_registry, NULL);
zend_hash_sort(&sorted_registry, zend_qsort, module_name_cmp, 0 TSRMLS_CC);
zend_hash_apply_with_argument(&sorted_registry, (apply_func_arg_t) print_module_info, NULL TSRMLS_CC);
zend_hash_destroy(&sorted_registry);
@ -619,7 +619,7 @@ static char *sapi_fcgi_read_cookies(TSRMLS_D)
static void cgi_php_load_env_var(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg TSRMLS_DC)
{
zval *array_ptr = (zval*)arg;
int filter_arg = (array_ptr == PG(http_globals)[TRACK_VARS_ENV])?PARSE_ENV:PARSE_SERVER;
int filter_arg = (Z_ARR_P(array_ptr) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV]))?PARSE_ENV:PARSE_SERVER;
unsigned int new_val_len;
if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len TSRMLS_CC)) {
@ -629,25 +629,19 @@ static void cgi_php_load_env_var(char *var, unsigned int var_len, char *val, uns
static void cgi_php_import_environment_variables(zval *array_ptr TSRMLS_DC)
{
if (PG(http_globals)[TRACK_VARS_ENV] &&
array_ptr != PG(http_globals)[TRACK_VARS_ENV] &&
Z_TYPE_P(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_ENV])) > 0
if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV]) &&
zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_ENV])) > 0
) {
zval_dtor(array_ptr);
*array_ptr = *PG(http_globals)[TRACK_VARS_ENV];
INIT_PZVAL(array_ptr);
zval_copy_ctor(array_ptr);
ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_ENV]);
return;
} else if (PG(http_globals)[TRACK_VARS_SERVER] &&
array_ptr != PG(http_globals)[TRACK_VARS_SERVER] &&
Z_TYPE_P(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
zend_hash_num_elements(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER])) > 0
} else if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]) &&
zend_hash_num_elements(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER])) > 0
) {
zval_dtor(array_ptr);
*array_ptr = *PG(http_globals)[TRACK_VARS_SERVER];
INIT_PZVAL(array_ptr);
zval_copy_ctor(array_ptr);
ZVAL_DUP(array_ptr, &PG(http_globals)[TRACK_VARS_SERVER]);
return;
}
@ -758,13 +752,12 @@ static void php_cgi_ini_activate_user_config(char *path, int path_len, const cha
time_t request_time = sapi_get_request_time(TSRMLS_C);
/* Find cached config entry: If not found, create one */
if (zend_hash_find(&CGIG(user_config_cache), path, path_len + 1, (void **) &entry) == FAILURE) {
if ((entry = zend_hash_str_find_ptr(&CGIG(user_config_cache), path, path_len)) == NULL) {
new_entry = pemalloc(sizeof(user_config_cache_entry), 1);
new_entry->expires = 0;
new_entry->user_config = (HashTable *) pemalloc(sizeof(HashTable), 1);
zend_hash_init(new_entry->user_config, 0, NULL, (dtor_func_t) config_zval_dtor, 1);
zend_hash_update(&CGIG(user_config_cache), path, path_len + 1, new_entry, sizeof(user_config_cache_entry), (void **) &entry);
free(new_entry);
entry = zend_hash_str_update_ptr(&CGIG(user_config_cache), path, path_len, new_entry);
}
/* Check whether cache entry has expired and rescan if it is */
@ -2250,7 +2243,7 @@ consult the installation file that came with this distribution, or visit \n\
if (script_file) {
/* override path_translated if -f on command line */
STR_FREE(SG(request_info).path_translated);
if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
SG(request_info).path_translated = script_file;
/* before registering argv to module exchange the *new* argv[0] */
/* we can achieve this without allocating more memory */
@ -2259,7 +2252,7 @@ consult the installation file that came with this distribution, or visit \n\
SG(request_info).argv[0] = script_file;
} else if (argc > php_optind) {
/* file is on command line, but not in -f opt */
STR_FREE(SG(request_info).path_translated);
if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
SG(request_info).path_translated = estrdup(argv[php_optind]);
/* arguments after the file are considered script args */
SG(request_info).argc = argc - php_optind;
@ -2362,7 +2355,7 @@ consult the installation file that came with this distribution, or visit \n\
goto fastcgi_request_done;
}
STR_FREE(SG(request_info).path_translated);
if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
if (free_query_string && SG(request_info).query_string) {
free(SG(request_info).query_string);
@ -2503,7 +2496,7 @@ consult the installation file that came with this distribution, or visit \n\
fastcgi_request_done:
{
STR_FREE(SG(request_info).path_translated);
if (SG(request_info).path_translated) efree(SG(request_info).path_translated);
php_request_shutdown((void *) 0);

View File

@ -973,7 +973,7 @@ static int fcgi_read_request(fcgi_request *req)
}
} else if (hdr.type == FCGI_GET_VALUES) {
unsigned char *p = buf + sizeof(fcgi_header);
zval ** value;
zval *value;
unsigned int zlen;
fcgi_hash_bucket *q;
@ -989,10 +989,10 @@ static int fcgi_read_request(fcgi_request *req)
q = req->env.list;
while (q != NULL) {
if (zend_hash_find(&fcgi_mgmt_vars, q->var, q->var_len, (void**) &value) != SUCCESS) {
if ((value = zend_hash_str_find(&fcgi_mgmt_vars, q->var, q->var_len)) == NULL) {
continue;
}
zlen = Z_STRLEN_PP(value);
zlen = Z_STRLEN_P(value);
if ((p + 4 + 4 + q->var_len + zlen) >= (buf + sizeof(buf))) {
break;
}
@ -1014,7 +1014,7 @@ static int fcgi_read_request(fcgi_request *req)
}
memcpy(p, q->var, q->var_len);
p += q->var_len;
memcpy(p, Z_STRVAL_PP(value), zlen);
memcpy(p, Z_STRVAL_P(value), zlen);
p += zlen;
}
len = p - buf - sizeof(fcgi_header);
@ -1510,19 +1510,14 @@ void fcgi_impersonate(void)
void fcgi_set_mgmt_var(const char * name, size_t name_len, const char * value, size_t value_len)
{
zval * zvalue;
zvalue = pemalloc(sizeof(*zvalue), 1);
Z_TYPE_P(zvalue) = IS_STRING;
Z_STRVAL_P(zvalue) = pestrndup(value, value_len, 1);
Z_STRLEN_P(zvalue) = value_len;
zend_hash_add(&fcgi_mgmt_vars, name, name_len, &zvalue, sizeof(zvalue), NULL);
zval zvalue;
ZVAL_STR(&zvalue, STR_INIT(value, value_len, 1));
zend_hash_str_add(&fcgi_mgmt_vars, name, name_len, &zvalue);
}
void fcgi_free_mgmt_var_cb(void * ptr)
void fcgi_free_mgmt_var_cb(zval *zv)
{
zval ** var = (zval **)ptr;
pefree(Z_STRVAL_PP(var), 1);
pefree(*var, 1);
pefree(Z_STR_P(zv), 1);
}
/*

View File

@ -139,7 +139,7 @@ void fcgi_impersonate(void);
#endif
void fcgi_set_mgmt_var(const char * name, size_t name_len, const char * value, size_t value_len);
void fcgi_free_mgmt_var_cb(void * ptr);
void fcgi_free_mgmt_var_cb(zval *zv);
/*
* Local variables: