From a52817eb027f2f90973888f35578d7b9eaf51d10 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Fri, 14 Feb 2014 14:11:52 +0400 Subject: [PATCH] Use better data structures (incomplete) --- Zend/zend_hash.h | 8 +-- sapi/cli/php_cli.c | 73 +++++++++---------------- sapi/cli/php_cli_process_title.c | 2 +- sapi/cli/php_cli_server.c | 91 ++++++++++++++++---------------- 4 files changed, 77 insertions(+), 97 deletions(-) diff --git a/Zend/zend_hash.h b/Zend/zend_hash.h index a3137195e1..0aff20c1e7 100644 --- a/Zend/zend_hash.h +++ b/Zend/zend_hash.h @@ -257,7 +257,7 @@ static inline int zend_symtable_del(HashTable *ht, zend_string *key) } -static inline zval *zend_symtable_find(HashTable *ht, zend_string *key) +static inline zval *zend_symtable_find(const HashTable *ht, zend_string *key) { ZEND_HANDLE_NUMERIC(key->val, key->len+1, zend_hash_index_find(ht, idx)); return zend_hash_find(ht, key); @@ -413,7 +413,7 @@ static inline void *zend_hash_next_index_insert_mem(HashTable *ht, void *pData, return zend_hash_next_index_insert_ptr(ht, p); } -static inline void *zend_hash_find_ptr(HashTable *ht, zend_string *key) +static inline void *zend_hash_find_ptr(const HashTable *ht, zend_string *key) { zval *zv; @@ -421,7 +421,7 @@ static inline void *zend_hash_find_ptr(HashTable *ht, zend_string *key) return zv ? Z_PTR_P(zv) : NULL; } -static inline void *zend_hash_str_find_ptr(HashTable *ht, const char *str, int len) +static inline void *zend_hash_str_find_ptr(const HashTable *ht, const char *str, int len) { zval *zv; @@ -429,7 +429,7 @@ static inline void *zend_hash_str_find_ptr(HashTable *ht, const char *str, int l return zv ? Z_PTR_P(zv) : NULL; } -static inline void *zend_hash_index_find_ptr(HashTable *ht, ulong h) +static inline void *zend_hash_index_find_ptr(const HashTable *ht, ulong h) { zval *zv; diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index fd3881fd06..f5fa168dc5 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -185,18 +185,18 @@ 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(&sorted_registry, (apply_func_t) print_module_info TSRMLS_CC); zend_hash_destroy(&sorted_registry); @@ -425,10 +425,8 @@ static int php_cli_startup(sapi_module_struct *sapi_module) /* {{{ */ /* overwriteable ini defaults must be set in sapi_cli_ini_defaults() */ #define INI_DEFAULT(name,value)\ - Z_SET_REFCOUNT(tmp, 0);\ - Z_UNSET_ISREF(tmp); \ - ZVAL_STRINGL(&tmp, zend_strndup(value, sizeof(value)-1), sizeof(value)-1, 0);\ - zend_hash_update(configuration_hash, name, sizeof(name), &tmp, sizeof(zval), NULL);\ + ZVAL_STR(&tmp, STR_INIT(value, sizeof(value)-1, 1));\ + zend_hash_str_update(configuration_hash, name, sizeof(name)-1, &tmp);\ static void sapi_cli_ini_defaults(HashTable *configuration_hash) { @@ -553,23 +551,16 @@ static php_stream *s_in_process = NULL; static void cli_register_file_handles(TSRMLS_D) /* {{{ */ { - zval *zin, *zout, *zerr; + zval zin, zout, zerr; php_stream *s_in, *s_out, *s_err; php_stream_context *sc_in=NULL, *sc_out=NULL, *sc_err=NULL; zend_constant ic, oc, ec; - MAKE_STD_ZVAL(zin); - MAKE_STD_ZVAL(zout); - MAKE_STD_ZVAL(zerr); - s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); if (s_in==NULL || s_out==NULL || s_err==NULL) { - FREE_ZVAL(zin); - FREE_ZVAL(zout); - FREE_ZVAL(zerr); if (s_in) php_stream_close(s_in); if (s_out) php_stream_close(s_out); if (s_err) php_stream_close(s_err); @@ -584,34 +575,27 @@ static void cli_register_file_handles(TSRMLS_D) /* {{{ */ s_in_process = s_in; - php_stream_to_zval(s_in, zin); - php_stream_to_zval(s_out, zout); - php_stream_to_zval(s_err, zerr); + php_stream_to_zval(s_in, &zin); + php_stream_to_zval(s_out, &zout); + php_stream_to_zval(s_err, &zerr); - ic.value = *zin; + ZVAL_COPY_VALUE(&ic.value, &zin); ic.flags = CONST_CS; - ic.name = zend_strndup(ZEND_STRL("STDIN")); - ic.name_len = sizeof("STDIN"); + ic.name = STR_INIT("STDIN", sizeof("STDIN")-1, 1); ic.module_number = 0; zend_register_constant(&ic TSRMLS_CC); - oc.value = *zout; + ZVAL_COPY_VALUE(&oc.value, &zout); oc.flags = CONST_CS; - oc.name = zend_strndup(ZEND_STRL("STDOUT")); - oc.name_len = sizeof("STDOUT"); + oc.name = STR_INIT("STDOUT", sizeof("STDOUT")-1, 1); oc.module_number = 0; zend_register_constant(&oc TSRMLS_CC); - ec.value = *zerr; + ZVAL_COPY_VALUE(&ec.value, &zerr); ec.flags = CONST_CS; - ec.name = zend_strndup(ZEND_STRL("STDERR")); - ec.name_len = sizeof("STDERR"); + ec.name = STR_INIT("STDERR", sizeof("STDERR")-1, 1); ec.module_number = 0; zend_register_constant(&ec TSRMLS_CC); - - FREE_ZVAL(zin); - FREE_ZVAL(zout); - FREE_ZVAL(zerr); } /* }}} */ @@ -673,11 +657,11 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */ int lineno = 0; const char *param_error=NULL; int hide_argv = 0; + zend_string *key; zend_try { CG(in_compilation) = 0; /* not initialized but needed for several options */ - EG(uninitialized_zval_ptr) = NULL; while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) { switch (c) { @@ -979,7 +963,9 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */ } } - zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC); + key = STR_INIT("_SERVER", sizeof("_SERVER")-1, 0); + zend_is_auto_global(key TSRMLS_CC); + STR_RELEASE(key); PG(during_request_startup) = 0; switch (behavior) { @@ -1040,30 +1026,23 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */ { char *input; size_t len, index = 0; - zval *argn, *argi; + zval argn, argi; cli_register_file_handles(TSRMLS_C); if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1 TSRMLS_CC) == FAILURE) { exit_status=254; } - ALLOC_ZVAL(argi); - Z_TYPE_P(argi) = IS_LONG; - Z_LVAL_P(argi) = index; - INIT_PZVAL(argi); - zend_hash_update(&EG(symbol_table), "argi", sizeof("argi"), &argi, sizeof(zval *), NULL); + ZVAL_LONG(&argi, index); + zend_hash_str_update(&EG(symbol_table).ht, "argi", sizeof("argi")-1, &argi); while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) { len = strlen(input); while (len-- && (input[len]=='\n' || input[len]=='\r')) { input[len] = '\0'; } - ALLOC_ZVAL(argn); - Z_TYPE_P(argn) = IS_STRING; - Z_STRLEN_P(argn) = ++len; - Z_STRVAL_P(argn) = estrndup(input, len); - INIT_PZVAL(argn); - zend_hash_update(&EG(symbol_table), "argn", sizeof("argn"), &argn, sizeof(zval *), NULL); - Z_LVAL_P(argi) = ++index; + ZVAL_STRINGL(&argn, input, len); + zend_hash_str_update(&EG(symbol_table).ht, "argn", sizeof("argn")-1, &argn); + Z_LVAL(argi) = ++index; if (exec_run) { if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1 TSRMLS_CC) == FAILURE) { exit_status=254; diff --git a/sapi/cli/php_cli_process_title.c b/sapi/cli/php_cli_process_title.c index dec5613670..298300f065 100644 --- a/sapi/cli/php_cli_process_title.c +++ b/sapi/cli/php_cli_process_title.c @@ -66,7 +66,7 @@ PHP_FUNCTION(cli_get_process_title) RETURN_NULL(); } - RETURN_STRINGL(title, length, 1); + RETURN_STRINGL(title, length); } /* }}} */ diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 2c68fa17a4..35d06329d7 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -366,9 +366,9 @@ int php_cli_server_get_system_time(char *buf) { } #endif -static void char_ptr_dtor_p(char **p) /* {{{ */ -{ - pefree(*p, 1); +static void char_ptr_dtor_p(zval *zv) /* {{{ */ +{ + pefree(Z_PTR_P(zv), 1); } /* }}} */ static char *get_last_error() /* {{{ */ @@ -449,11 +449,11 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, int persistent) /* {{{ */ { { - char **val; - if (SUCCESS == zend_hash_find(&client->request.headers, "host", sizeof("host"), (void**)&val)) { + char *val; + if (SUCCESS == (val = zend_hash_str_find_ptr(&client->request.headers, "host", sizeof("host")-1))) { smart_str_appendl_ex(buffer, "Host", sizeof("Host") - 1, persistent); smart_str_appendl_ex(buffer, ": ", sizeof(": ") - 1, persistent); - smart_str_appends_ex(buffer, *val, persistent); + smart_str_appends_ex(buffer, val, persistent); smart_str_appendl_ex(buffer, "\r\n", 2, persistent); } } @@ -476,9 +476,8 @@ PHP_FUNCTION(apache_request_headers) /* {{{ */ { php_cli_server_client *client; HashTable *headers; - char *key; - uint key_len; - char **value_pointer; + zend_string *key; + char *value; HashPosition pos; if (zend_parse_parameters_none() == FAILURE) { @@ -491,9 +490,10 @@ PHP_FUNCTION(apache_request_headers) /* {{{ */ array_init_size(return_value, zend_hash_num_elements(headers)); zend_hash_internal_pointer_reset_ex(headers, &pos); - while (zend_hash_get_current_data_ex(headers, (void **)&value_pointer, &pos) == SUCCESS) { - zend_hash_get_current_key_ex(headers, &key, &key_len, NULL, 0, &pos); - add_assoc_string_ex(return_value, key, key_len, *value_pointer, 1); + while ((value = zend_hash_get_current_data_ptr_ex(headers, &pos)) != NULL) { + zend_hash_get_current_key_ex(headers, &key, NULL, 0, &pos); +//??? + add_assoc_string_ex(return_value, key->val, key->len, value, 1); zend_hash_move_forward_ex(headers, &pos); } } @@ -679,11 +679,11 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS static char *sapi_cli_server_read_cookies(TSRMLS_D) /* {{{ */ { php_cli_server_client *client = SG(server_context); - char **val; - if (FAILURE == zend_hash_find(&client->request.headers, "cookie", sizeof("cookie"), (void**)&val)) { + char *val; + if (NULL == (val = zend_hash_str_find_ptr(&client->request.headers, "cookie", sizeof("cookie")-1))) { return NULL; } - return *val; + return val; } /* }}} */ static int sapi_cli_server_read_post(char *buf, uint count_bytes TSRMLS_DC) /* {{{ */ @@ -710,11 +710,11 @@ static void sapi_cli_server_register_variable(zval *track_vars_array, const char static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ { zval *track_vars_array = va_arg(args, zval *); - if (hash_key->nKeyLength) { + if (hash_key->key) { char *real_key, *key; uint i; - key = estrndup(hash_key->arKey, hash_key->nKeyLength); - for(i=0; inKeyLength; i++) { + key = estrndup(hash_key->key->val, hash_key->key->len); + for(i=0; ikey->len; i++) { if (key[i] == '-') { key[i] = '_'; } else { @@ -1411,7 +1411,7 @@ static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */ req->path_info_len = 0; req->query_string = NULL; req->query_string_len = 0; - zend_hash_init(&req->headers, 0, NULL, (void(*)(void*))char_ptr_dtor_p, 1); + zend_hash_init(&req->headers, 0, NULL, char_ptr_dtor_p, 1); zend_hash_init(&req->headers_original_case, 0, NULL, NULL, 1); req->content = NULL; req->content_len = 0; @@ -1682,8 +1682,8 @@ static int php_cli_server_client_read_request_on_header_value(php_http_parser *p } { char *header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len); - zend_hash_add(&client->request.headers, header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL); - zend_hash_add(&client->request.headers_original_case, client->current_header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL); + zend_hash_str_add_ptr(&client->request.headers, header_name, client->current_header_name_len, value); + zend_hash_str_add_ptr(&client->request.headers_original_case, client->current_header_name, client->current_header_name_len, value); efree(header_name); } @@ -1833,7 +1833,7 @@ static size_t php_cli_server_client_send_through(php_cli_server_client *client, static void php_cli_server_client_populate_request_info(const php_cli_server_client *client, sapi_request_info *request_info) /* {{{ */ { - char **val; + char *val; request_info->request_method = php_http_method_str(client->request.request_method); request_info->proto_num = client->request.protocol_version; @@ -1842,8 +1842,8 @@ static void php_cli_server_client_populate_request_info(const php_cli_server_cli request_info->query_string = client->request.query_string; request_info->content_length = client->request.content_len; request_info->auth_user = request_info->auth_password = request_info->auth_digest = NULL; - if (SUCCESS == zend_hash_find(&client->request.headers, "content-type", sizeof("content-type"), (void**)&val)) { - request_info->content_type = *val; + if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "content-type", sizeof("content-type")-1))) { + request_info->content_type = val; } } /* }}} */ @@ -1903,8 +1903,7 @@ static void php_cli_server_close_connection(php_cli_server *server, php_cli_serv static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server_client *client, int status TSRMLS_DC) /* {{{ */ { - char *escaped_request_uri = NULL; - size_t escaped_request_uri_len; + zend_string *escaped_request_uri = NULL; const char *status_string = get_status_string(status); const char *content_template = get_template_string(status); char *errstr = get_last_error(); @@ -1913,7 +1912,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server php_cli_server_content_sender_ctor(&client->content_sender); client->content_sender_initialized = 1; - escaped_request_uri = php_escape_html_entities_ex((unsigned char *)client->request.request_uri, client->request.request_uri_len, &escaped_request_uri_len, 0, ENT_QUOTES, NULL, 0 TSRMLS_CC); + escaped_request_uri = php_escape_html_entities_ex((unsigned char *)client->request.request_uri, client->request.request_uri_len, 0, ENT_QUOTES, NULL, 0 TSRMLS_CC); { static const char prologue_template[] = "%d %s"; @@ -1941,7 +1940,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server php_cli_server_buffer_append(&client->content_sender.buffer, chunk); } { - php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(content_template) + escaped_request_uri_len + 3 + strlen(status_string) + 1); + php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(content_template) + escaped_request_uri->len + 3 + strlen(status_string) + 1); if (!chunk) { goto fail; } @@ -2078,10 +2077,10 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv /* }}} */ static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */ - char **auth; + char *auth; php_cli_server_client_populate_request_info(client, &SG(request_info)); - if (SUCCESS == zend_hash_find(&client->request.headers, "authorization", sizeof("authorization"), (void**)&auth)) { - php_handle_auth_data(*auth TSRMLS_CC); + if (NULL != (auth = zend_hash_str_find_ptr(&client->request.headers, "authorization", sizeof("authorization")-1))) { + php_handle_auth_data(auth TSRMLS_CC); } SG(sapi_headers).http_response_code = 200; if (FAILURE == php_request_startup(TSRMLS_C)) { @@ -2123,10 +2122,10 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server zfd.opened_path = NULL; zend_try { - zval *retval = NULL; + zval retval; if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, &retval, 1, &zfd)) { - if (retval) { - decline = Z_TYPE_P(retval) == IS_BOOL && !Z_LVAL_P(retval); + if (Z_TYPE(retval) != IS_UNDEF) { + decline = Z_TYPE(retval) == IS_BOOL && !Z_LVAL(retval); zval_ptr_dtor(&retval); } } else { @@ -2218,12 +2217,14 @@ static void php_cli_server_dtor(php_cli_server *server TSRMLS_DC) /* {{{ */ } } /* }}} */ -static void php_cli_server_client_dtor_wrapper(php_cli_server_client **p) /* {{{ */ +static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */ { - closesocket((*p)->sock); - php_cli_server_poller_remove(&(*p)->server->poller, POLLIN | POLLOUT, (*p)->sock); - php_cli_server_client_dtor(*p); - pefree(*p, 1); + php_cli_server_client *p = Z_PTR_P(zv); + + closesocket(p->sock); + php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock); + php_cli_server_client_dtor(p); + pefree(p, 1); } /* }}} */ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router TSRMLS_DC) /* {{{ */ @@ -2294,7 +2295,7 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c server->host = host; server->port = port; - zend_hash_init(&server->clients, 0, NULL, (void(*)(void*))php_cli_server_client_dtor_wrapper, 1); + zend_hash_init(&server->clients, 0, NULL, php_cli_server_client_dtor_wrapper, 1); { size_t document_root_len = strlen(document_root); @@ -2438,16 +2439,16 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, int fd, i #ifdef DEBUG php_cli_server_logf("%s Accepted" TSRMLS_CC, client->addr_str); #endif - zend_hash_index_update(&server->clients, client_sock, &client, sizeof(client), NULL); + zend_hash_index_update_ptr(&server->clients, client_sock, client); php_cli_server_recv_event_read_request(server, client TSRMLS_CC); } else { - php_cli_server_client **client; - if (SUCCESS == zend_hash_index_find(&server->clients, fd, (void **)&client)) { + php_cli_server_client *client; + if (NULL != (client = zend_hash_index_find_ptr(&server->clients, fd))) { if (event & POLLIN) { - params->rhandler(server, *client TSRMLS_CC); + params->rhandler(server, client TSRMLS_CC); } if (event & POLLOUT) { - params->whandler(server, *client TSRMLS_CC); + params->whandler(server, client TSRMLS_CC); } } } -- 2.40.0