Skip to content

Commit

Permalink
Get rid of the hashes global hash
Browse files Browse the repository at this point in the history
The idea came up in c7a86a3
  • Loading branch information
kocsismate committed Apr 2, 2021
1 parent 0555a58 commit 09e569f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
19 changes: 4 additions & 15 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,17 @@ static zend_function *pgsql_link_get_constructor(zend_object *object) {
static void pgsql_link_free(pgsql_link_handle *link)
{
PGresult *res;
zval *hash;

while ((res = PQgetResult(link->conn))) {
PQclear(res);
}
PQfinish(link->conn);
PGG(num_links)--;

/* Remove connection hash for this link */
hash = zend_hash_index_find(&PGG(hashes), (uintptr_t) link->conn);
if (hash) {
zend_hash_index_del(&PGG(hashes), (uintptr_t) link->conn);
zend_hash_del(&PGG(regular_list), Z_STR_P(hash));
}
zend_hash_del(&PGG(regular_list), link->hash);

link->conn = NULL;
zend_string_release(link->hash);
}

static void pgsql_link_free_obj(zend_object *obj)
Expand Down Expand Up @@ -415,7 +410,6 @@ static PHP_GINIT_FUNCTION(pgsql)
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
/* Initialize notice message hash at MINIT only */
zend_hash_init(&pgsql_globals->notices, 0, NULL, ZVAL_PTR_DTOR, 1);
zend_hash_init(&pgsql_globals->hashes, 0, NULL, ZVAL_PTR_DTOR, 1);
zend_hash_init(&pgsql_globals->regular_list, 0, NULL, ZVAL_PTR_DTOR, 1);
}

Expand Down Expand Up @@ -588,7 +582,6 @@ PHP_MSHUTDOWN_FUNCTION(pgsql)
{
UNREGISTER_INI_ENTRIES();
zend_hash_destroy(&PGG(notices));
zend_hash_destroy(&PGG(hashes));
zend_hash_destroy(&PGG(regular_list));

return SUCCESS;
Expand All @@ -605,7 +598,6 @@ PHP_RSHUTDOWN_FUNCTION(pgsql)
{
/* clean up notice messages */
zend_hash_clean(&PGG(notices));
zend_hash_clean(&PGG(hashes));
zend_hash_clean(&PGG(regular_list));
/* clean up persistent connection */
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
Expand Down Expand Up @@ -769,6 +761,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
object_init_ex(return_value, pgsql_link_ce);
link = Z_PGSQL_LINK_P(return_value);
link->conn = pgsql;
link->hash = zend_string_copy(str.s);

/* add it to the hash */
ZVAL_COPY(&new_index_ptr, return_value);
Expand All @@ -778,11 +771,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
* when the connection is closed. This uses the address of the connection rather than the
* zend_resource, because the resource destructor is passed a stack copy of the resource
* structure. */
{
zval tmp;
ZVAL_STR_COPY(&tmp, str.s);
zend_hash_index_update(&PGG(hashes), (uintptr_t) pgsql, &tmp);
}

PGG(num_links)++;
}
/* set notice processor */
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/php_pgsql.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ typedef enum _php_pgsql_data_type {

typedef struct pgsql_link_handle {
PGconn *conn;
zend_string *hash;
zend_object std;
} pgsql_link_handle;

Expand Down Expand Up @@ -187,7 +188,6 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
int ignore_notices,log_notices;
HashTable notices; /* notice message for each connection */
pgsql_link_handle *default_link; /* default link when connection is omitted */
HashTable hashes; /* hashes for each connection */
HashTable regular_list; /* connection list */
ZEND_END_MODULE_GLOBALS(pgsql)

Expand Down

0 comments on commit 09e569f

Please sign in to comment.