Skip to content

Commit 47ea219

Browse files
committed
Get rid of global notices hash
The idea came up in c7a86a3
1 parent 005ff3e commit 47ea219

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

ext/pgsql/pgsql.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ static void pgsql_link_free(pgsql_link_handle *link)
171171

172172
link->conn = NULL;
173173
zend_string_release(link->hash);
174+
if (link->notices) {
175+
zend_hash_destroy(link->notices);
176+
}
174177
}
175178

176179
static void pgsql_link_free_obj(zend_object *obj)
@@ -298,14 +301,15 @@ static void _close_pgsql_plink(zend_resource *rsrc)
298301
PGG(num_links)--;
299302
}
300303

301-
static void _php_pgsql_notice_handler(void *resource_id, const char *message)
304+
static void _php_pgsql_notice_handler(void *link, const char *message)
302305
{
303306
if (PGG(ignore_notices)) {
304307
return;
305308
}
306309

310+
HashTable *notices, tmp_notices;
307311
zval tmp;
308-
zval *notices = zend_hash_index_find(&PGG(notices), (zend_ulong)resource_id);
312+
zval *notices = notices = ((pgsql_link_handle *) link)->notices;
309313
if (!notices) {
310314
array_init(&tmp);
311315
notices = &tmp;
@@ -400,7 +404,6 @@ static PHP_GINIT_FUNCTION(pgsql)
400404
#endif
401405
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
402406
/* Initialize notice message hash at MINIT only */
403-
zend_hash_init(&pgsql_globals->notices, 0, NULL, ZVAL_PTR_DTOR, 1);
404407
zend_hash_init(&pgsql_globals->regular_list, 0, NULL, ZVAL_PTR_DTOR, 1);
405408
}
406409

@@ -571,7 +574,6 @@ PHP_MINIT_FUNCTION(pgsql)
571574
PHP_MSHUTDOWN_FUNCTION(pgsql)
572575
{
573576
UNREGISTER_INI_ENTRIES();
574-
zend_hash_destroy(&PGG(notices));
575577
zend_hash_destroy(&PGG(regular_list));
576578

577579
return SUCCESS;
@@ -589,7 +591,6 @@ PHP_RINIT_FUNCTION(pgsql)
589591
PHP_RSHUTDOWN_FUNCTION(pgsql)
590592
{
591593
/* clean up notice messages */
592-
zend_hash_clean(&PGG(notices));
593594
zend_hash_clean(&PGG(hashes));
594595
zend_hash_destroy(&PGG(field_oids));
595596
zend_hash_destroy(&PGG(table_oids));
@@ -711,6 +712,8 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
711712
object_init_ex(return_value, pgsql_link_ce);
712713
link = Z_PGSQL_LINK_P(return_value);
713714
link->conn = pgsql;
715+
link->hash = zend_string_copy(str.s);
716+
link->notices = NULL;
714717
} else { /* Non persistent connection */
715718
zval *index_ptr, new_index_ptr;
716719

@@ -757,6 +760,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
757760
link = Z_PGSQL_LINK_P(return_value);
758761
link->conn = pgsql;
759762
link->hash = zend_string_copy(str.s);
763+
link->notices = NULL;
760764

761765
/* add it to the hash */
762766
ZVAL_COPY(&new_index_ptr, return_value);
@@ -771,9 +775,9 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
771775
}
772776
/* set notice processor */
773777
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
774-
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)(zend_uintptr_t)Z_OBJ_P(return_value)->handle);
778+
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
775779
}
776-
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(return_value)));
780+
php_pgsql_set_default_link(link);
777781

778782
cleanup:
779783
smart_str_free(&str);
@@ -1484,7 +1488,8 @@ PHP_FUNCTION(pg_affected_rows)
14841488
PHP_FUNCTION(pg_last_notice)
14851489
{
14861490
zval *pgsql_link = NULL;
1487-
zval *notice, *notices;
1491+
zval *notice;
1492+
HashTable *notices;
14881493
pgsql_link_handle *link;
14891494
zend_long option = PGSQL_NOTICE_LAST;
14901495

@@ -1495,12 +1500,12 @@ PHP_FUNCTION(pg_last_notice)
14951500
link = Z_PGSQL_LINK_P(pgsql_link);
14961501
CHECK_PGSQL_LINK(link);
14971502

1498-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong) Z_OBJ_P(pgsql_link)->handle);
1503+
notices = link->notices;
14991504
switch (option) {
15001505
case PGSQL_NOTICE_LAST:
15011506
if (notices) {
1502-
zend_hash_internal_pointer_end(Z_ARRVAL_P(notices));
1503-
if ((notice = zend_hash_get_current_data(Z_ARRVAL_P(notices))) == NULL) {
1507+
zend_hash_internal_pointer_end(notices);
1508+
if ((notice = zend_hash_get_current_data(notices)) == NULL) {
15041509
RETURN_EMPTY_STRING();
15051510
}
15061511
RETURN_COPY(notice);
@@ -1510,15 +1515,15 @@ PHP_FUNCTION(pg_last_notice)
15101515
break;
15111516
case PGSQL_NOTICE_ALL:
15121517
if (notices) {
1513-
RETURN_COPY(notices);
1518+
RETURN_ARR(zend_array_dup(notices));
15141519
} else {
15151520
array_init(return_value);
15161521
return;
15171522
}
15181523
break;
15191524
case PGSQL_NOTICE_CLEAR:
15201525
if (notices) {
1521-
zend_hash_clean(&PGG(notices));
1526+
zend_hash_clean(link->notices);
15221527
}
15231528
RETURN_TRUE;
15241529
break;

ext/pgsql/php_pgsql.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ typedef enum _php_pgsql_data_type {
146146
typedef struct pgsql_link_handle {
147147
PGconn *conn;
148148
zend_string *hash;
149+
HashTable *notices;
149150
zend_object std;
150151
} pgsql_link_handle;
151152

@@ -185,7 +186,6 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
185186
zend_long allow_persistent;
186187
zend_long auto_reset_persistent;
187188
int ignore_notices,log_notices;
188-
HashTable notices; /* notice message for each connection */
189189
pgsql_link_handle *default_link; /* default link when connection is omitted */
190190
HashTable hashes; /* hashes for each connection */
191191
HashTable field_oids;

0 commit comments

Comments
 (0)