Skip to content

Commit fae9bcf

Browse files
committed
Get rid of global notices hash
The idea came up in c7a86a3
1 parent 09e569f commit fae9bcf

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

ext/pgsql/pgsql.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ static void pgsql_link_free(pgsql_link_handle *link)
174174

175175
link->conn = NULL;
176176
zend_string_release(link->hash);
177+
if (link->notices) {
178+
zend_hash_destroy(link->notices);
179+
}
177180
}
178181

179182
static void pgsql_link_free_obj(zend_object *obj)
@@ -310,26 +313,28 @@ static void _close_pgsql_plink(zend_resource *rsrc)
310313
PGG(num_links)--;
311314
}
312315

313-
static void _php_pgsql_notice_handler(void *resource_id, const char *message)
316+
static void _php_pgsql_notice_handler(void *link, const char *message)
314317
{
315-
zval *notices;
318+
HashTable *notices, tmp_notices;
316319
zval tmp;
317-
char *trimed_message;
318-
size_t trimed_message_len;
320+
char *trimmed_message;
321+
size_t trimmed_message_len;
319322

320323
if (! PGG(ignore_notices)) {
321-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong)resource_id);
324+
notices = ((pgsql_link_handle *) link)->notices;
322325
if (!notices) {
323-
array_init(&tmp);
324-
notices = &tmp;
325-
zend_hash_index_update(&PGG(notices), (zend_ulong)resource_id, notices);
326+
zend_hash_init(&tmp_notices, 1, NULL, ZVAL_PTR_DTOR, 0);
327+
notices = &tmp_notices;
326328
}
327-
trimed_message = _php_pgsql_trim_message(message, &trimed_message_len);
329+
trimmed_message = _php_pgsql_trim_message(message, &trimmed_message_len);
328330
if (PGG(log_notices)) {
329-
php_error_docref(NULL, E_NOTICE, "%s", trimed_message);
331+
php_error_docref(NULL, E_NOTICE, "%s", trimmed_message);
330332
}
331-
add_next_index_stringl(notices, trimed_message, trimed_message_len);
332-
efree(trimed_message);
333+
334+
ZVAL_STRINGL(&tmp, trimmed_message, trimmed_message_len);
335+
zend_hash_next_index_insert(notices, &tmp);
336+
efree(trimmed_message);
337+
zval_ptr_dtor(&tmp);
333338
}
334339
}
335340

@@ -409,7 +414,6 @@ static PHP_GINIT_FUNCTION(pgsql)
409414
#endif
410415
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
411416
/* Initialize notice message hash at MINIT only */
412-
zend_hash_init(&pgsql_globals->notices, 0, NULL, ZVAL_PTR_DTOR, 1);
413417
zend_hash_init(&pgsql_globals->regular_list, 0, NULL, ZVAL_PTR_DTOR, 1);
414418
}
415419

@@ -581,7 +585,6 @@ PHP_MINIT_FUNCTION(pgsql)
581585
PHP_MSHUTDOWN_FUNCTION(pgsql)
582586
{
583587
UNREGISTER_INI_ENTRIES();
584-
zend_hash_destroy(&PGG(notices));
585588
zend_hash_destroy(&PGG(regular_list));
586589

587590
return SUCCESS;
@@ -597,7 +600,6 @@ PHP_RINIT_FUNCTION(pgsql)
597600
PHP_RSHUTDOWN_FUNCTION(pgsql)
598601
{
599602
/* clean up notice messages */
600-
zend_hash_clean(&PGG(notices));
601603
zend_hash_clean(&PGG(regular_list));
602604
/* clean up persistent connection */
603605
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
@@ -716,6 +718,8 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
716718
object_init_ex(return_value, pgsql_link_ce);
717719
link = Z_PGSQL_LINK_P(return_value);
718720
link->conn = pgsql;
721+
link->hash = zend_string_copy(str.s);
722+
link->notices = NULL;
719723
} else { /* Non persistent connection */
720724
zval *index_ptr, new_index_ptr;
721725

@@ -762,6 +766,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
762766
link = Z_PGSQL_LINK_P(return_value);
763767
link->conn = pgsql;
764768
link->hash = zend_string_copy(str.s);
769+
link->notices = NULL;
765770

766771
/* add it to the hash */
767772
ZVAL_COPY(&new_index_ptr, return_value);
@@ -776,9 +781,9 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
776781
}
777782
/* set notice processor */
778783
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
779-
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)(zend_uintptr_t)Z_OBJ_P(return_value)->handle);
784+
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
780785
}
781-
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(return_value)));
786+
php_pgsql_set_default_link(link);
782787

783788
cleanup:
784789
smart_str_free(&str);
@@ -1497,7 +1502,8 @@ PHP_FUNCTION(pg_affected_rows)
14971502
PHP_FUNCTION(pg_last_notice)
14981503
{
14991504
zval *pgsql_link = NULL;
1500-
zval *notice, *notices;
1505+
zval *notice;
1506+
HashTable *notices;
15011507
pgsql_link_handle *link;
15021508
zend_long option = PGSQL_NOTICE_LAST;
15031509

@@ -1508,12 +1514,12 @@ PHP_FUNCTION(pg_last_notice)
15081514
link = Z_PGSQL_LINK_P(pgsql_link);
15091515
CHECK_PGSQL_LINK(link);
15101516

1511-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong) Z_OBJ_P(pgsql_link)->handle);
1517+
notices = link->notices;
15121518
switch (option) {
15131519
case PGSQL_NOTICE_LAST:
15141520
if (notices) {
1515-
zend_hash_internal_pointer_end(Z_ARRVAL_P(notices));
1516-
if ((notice = zend_hash_get_current_data(Z_ARRVAL_P(notices))) == NULL) {
1521+
zend_hash_internal_pointer_end(notices);
1522+
if ((notice = zend_hash_get_current_data(notices)) == NULL) {
15171523
RETURN_EMPTY_STRING();
15181524
}
15191525
RETURN_COPY(notice);
@@ -1523,15 +1529,15 @@ PHP_FUNCTION(pg_last_notice)
15231529
break;
15241530
case PGSQL_NOTICE_ALL:
15251531
if (notices) {
1526-
RETURN_COPY(notices);
1532+
RETURN_ARR(zend_array_dup(notices));
15271533
} else {
15281534
array_init(return_value);
15291535
return;
15301536
}
15311537
break;
15321538
case PGSQL_NOTICE_CLEAR:
15331539
if (notices) {
1534-
zend_hash_clean(&PGG(notices));
1540+
zend_hash_clean(link->notices);
15351541
}
15361542
RETURN_TRUE;
15371543
break;

ext/pgsql/php_pgsql.h

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

@@ -186,7 +187,6 @@ ZEND_BEGIN_MODULE_GLOBALS(pgsql)
186187
zend_long allow_persistent;
187188
zend_long auto_reset_persistent;
188189
int ignore_notices,log_notices;
189-
HashTable notices; /* notice message for each connection */
190190
pgsql_link_handle *default_link; /* default link when connection is omitted */
191191
HashTable regular_list; /* connection list */
192192
ZEND_END_MODULE_GLOBALS(pgsql)

0 commit comments

Comments
 (0)