Skip to content

Commit b1e9967

Browse files
committed
Get rid of global notices hash
The idea came up in c7a86a3
1 parent 8da9069 commit b1e9967

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
@@ -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)
@@ -307,26 +310,28 @@ static void _close_pgsql_plink(zend_resource *rsrc)
307310
PGG(num_links)--;
308311
}
309312

310-
static void _php_pgsql_notice_handler(void *resource_id, const char *message)
313+
static void _php_pgsql_notice_handler(void *link, const char *message)
311314
{
312-
zval *notices;
315+
HashTable *notices, tmp_notices;
313316
zval tmp;
314-
char *trimed_message;
315-
size_t trimed_message_len;
317+
char *trimmed_message;
318+
size_t trimmed_message_len;
316319

317320
if (! PGG(ignore_notices)) {
318-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong)resource_id);
321+
notices = ((pgsql_link_handle *) link)->notices;
319322
if (!notices) {
320-
array_init(&tmp);
321-
notices = &tmp;
322-
zend_hash_index_update(&PGG(notices), (zend_ulong)resource_id, notices);
323+
zend_hash_init(&tmp_notices, 1, NULL, ZVAL_PTR_DTOR, 0);
324+
notices = &tmp_notices;
323325
}
324-
trimed_message = _php_pgsql_trim_message(message, &trimed_message_len);
326+
trimmed_message = _php_pgsql_trim_message(message, &trimmed_message_len);
325327
if (PGG(log_notices)) {
326-
php_error_docref(NULL, E_NOTICE, "%s", trimed_message);
328+
php_error_docref(NULL, E_NOTICE, "%s", trimmed_message);
327329
}
328-
add_next_index_stringl(notices, trimed_message, trimed_message_len);
329-
efree(trimed_message);
330+
331+
ZVAL_STRINGL(&tmp, trimmed_message, trimmed_message_len);
332+
zend_hash_next_index_insert(notices, &tmp);
333+
efree(trimmed_message);
334+
zval_ptr_dtor(&tmp);
330335
}
331336
}
332337

@@ -406,7 +411,6 @@ static PHP_GINIT_FUNCTION(pgsql)
406411
#endif
407412
memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
408413
/* Initialize notice message hash at MINIT only */
409-
zend_hash_init(&pgsql_globals->notices, 0, NULL, ZVAL_PTR_DTOR, 1);
410414
zend_hash_init(&pgsql_globals->regular_list, 0, NULL, ZVAL_PTR_DTOR, 1);
411415
}
412416

@@ -578,7 +582,6 @@ PHP_MINIT_FUNCTION(pgsql)
578582
PHP_MSHUTDOWN_FUNCTION(pgsql)
579583
{
580584
UNREGISTER_INI_ENTRIES();
581-
zend_hash_destroy(&PGG(notices));
582585
zend_hash_destroy(&PGG(regular_list));
583586

584587
return SUCCESS;
@@ -594,7 +597,6 @@ PHP_RINIT_FUNCTION(pgsql)
594597
PHP_RSHUTDOWN_FUNCTION(pgsql)
595598
{
596599
/* clean up notice messages */
597-
zend_hash_clean(&PGG(notices));
598600
zend_hash_clean(&PGG(regular_list));
599601
/* clean up persistent connection */
600602
zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions);
@@ -713,6 +715,8 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
713715
object_init_ex(return_value, pgsql_link_ce);
714716
link = Z_PGSQL_LINK_P(return_value);
715717
link->conn = pgsql;
718+
link->hash = zend_string_copy(str.s);
719+
link->notices = NULL;
716720
} else { /* Non persistent connection */
717721
zval *index_ptr, new_index_ptr;
718722

@@ -759,6 +763,7 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
759763
link = Z_PGSQL_LINK_P(return_value);
760764
link->conn = pgsql;
761765
link->hash = zend_string_copy(str.s);
766+
link->notices = NULL;
762767

763768
/* add it to the hash */
764769
ZVAL_COPY(&new_index_ptr, return_value);
@@ -773,9 +778,9 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
773778
}
774779
/* set notice processor */
775780
if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_OBJECT) {
776-
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)(zend_uintptr_t)Z_OBJ_P(return_value)->handle);
781+
PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, link);
777782
}
778-
php_pgsql_set_default_link(pgsql_link_from_obj(Z_OBJ_P(return_value)));
783+
php_pgsql_set_default_link(link);
779784

780785
cleanup:
781786
smart_str_free(&str);
@@ -1490,7 +1495,8 @@ PHP_FUNCTION(pg_affected_rows)
14901495
PHP_FUNCTION(pg_last_notice)
14911496
{
14921497
zval *pgsql_link = NULL;
1493-
zval *notice, *notices;
1498+
zval *notice;
1499+
HashTable *notices;
14941500
pgsql_link_handle *link;
14951501
zend_long option = PGSQL_NOTICE_LAST;
14961502

@@ -1501,12 +1507,12 @@ PHP_FUNCTION(pg_last_notice)
15011507
link = Z_PGSQL_LINK_P(pgsql_link);
15021508
CHECK_PGSQL_LINK(link);
15031509

1504-
notices = zend_hash_index_find(&PGG(notices), (zend_ulong) Z_OBJ_P(pgsql_link)->handle);
1510+
notices = link->notices;
15051511
switch (option) {
15061512
case PGSQL_NOTICE_LAST:
15071513
if (notices) {
1508-
zend_hash_internal_pointer_end(Z_ARRVAL_P(notices));
1509-
if ((notice = zend_hash_get_current_data(Z_ARRVAL_P(notices))) == NULL) {
1514+
zend_hash_internal_pointer_end(notices);
1515+
if ((notice = zend_hash_get_current_data(notices)) == NULL) {
15101516
RETURN_EMPTY_STRING();
15111517
}
15121518
RETURN_COPY(notice);
@@ -1516,15 +1522,15 @@ PHP_FUNCTION(pg_last_notice)
15161522
break;
15171523
case PGSQL_NOTICE_ALL:
15181524
if (notices) {
1519-
RETURN_COPY(notices);
1525+
RETURN_ARR(zend_array_dup(notices));
15201526
} else {
15211527
array_init(return_value);
15221528
return;
15231529
}
15241530
break;
15251531
case PGSQL_NOTICE_CLEAR:
15261532
if (notices) {
1527-
zend_hash_clean(&PGG(notices));
1533+
zend_hash_clean(link->notices);
15281534
}
15291535
RETURN_TRUE;
15301536
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)