andrey          Fri Nov  9 12:13:15 2007 UTC

  Modified files:              
    /php-src/ext/mysqli mysqli.c mysqli_api.c mysqli_nonapi.c 
                        php_mysqli_structs.h 
  Log:
  Fix crashes with pconn (merge from 5_3)
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/mysqli/mysqli.c?r1=1.112&r2=1.113&diff_format=u
Index: php-src/ext/mysqli/mysqli.c
diff -u php-src/ext/mysqli/mysqli.c:1.112 php-src/ext/mysqli/mysqli.c:1.113
--- php-src/ext/mysqli/mysqli.c:1.112   Wed Oct 17 08:17:34 2007
+++ php-src/ext/mysqli/mysqli.c Fri Nov  9 12:13:15 2007
@@ -17,7 +17,7 @@
   |          Ulf Wendel <[EMAIL PROTECTED]>                                    
 |
   +----------------------------------------------------------------------+
 
-  $Id: mysqli.c,v 1.112 2007/10/17 08:17:34 tony2001 Exp $ 
+  $Id: mysqli.c,v 1.113 2007/11/09 12:13:15 andrey Exp $ 
 */
 
 #ifdef HAVE_CONFIG_H
@@ -73,42 +73,16 @@
 static int le_pmysqli;
 
 
-static int php_mysqli_persistent_on_rshut(zend_rsrc_list_entry *le TSRMLS_DC)
-{
-       if (le->type == le_pmysqli) {
-               mysqli_plist_entry *plist = (mysqli_plist_entry *) le->ptr;
-               HashPosition pos;
-               MYSQL **mysql;
-               ulong idx;
-               dtor_func_t pDestructor = plist->used_links.pDestructor;
-               plist->used_links.pDestructor = NULL; /* Don't call pDestructor 
now */
-
-               zend_hash_internal_pointer_reset_ex(&plist->used_links, &pos);
-               while (SUCCESS == 
zend_hash_get_current_data_ex(&plist->used_links, (void **)&mysql, &pos)) {
-                       zend_hash_get_current_key_ex(&plist->used_links, NULL, 
NULL, &idx, FALSE, &pos);
-                       /* Make it free */
-                       zend_hash_next_index_insert(&plist->free_links, mysql, 
sizeof(MYSQL *), NULL);
-                       /* First move forward */                        
-                       zend_hash_move_forward_ex(&plist->used_links, &pos);
-                       /* The delete, because del will free memory, but we 
need it's ->nextItem */
-                       zend_hash_index_del(&plist->used_links, idx);
-               }
-
-               /* restore pDestructor, which should be 
php_mysqli_dtor_p_elements() */
-               plist->used_links.pDestructor = pDestructor;
-       }
-       return ZEND_HASH_APPLY_KEEP;
-}
 
 /* Destructor for mysqli entries in free_links/used_links */
 void php_mysqli_dtor_p_elements(void *data)
 {
-       MYSQL **mysql = (MYSQL **) data;
+       MYSQL *mysql = (MYSQL *) data;
        TSRMLS_FETCH();
 #if defined(HAVE_MYSQLND)
-       mysqlnd_end_psession(*mysql);
+       mysqlnd_end_psession(mysql);
 #endif
-       mysqli_close(*mysql, MYSQLI_CLOSE_IMPLICIT);
+       mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT);
 }
 
 
@@ -116,8 +90,8 @@
 {
        if (rsrc->ptr) {
                mysqli_plist_entry *plist = (mysqli_plist_entry *) rsrc->ptr;
-               zend_hash_destroy(&plist->free_links);
-               zend_hash_destroy(&plist->used_links);
+               zend_ptr_stack_clean(&plist->free_links, 
php_mysqli_dtor_p_elements, 0);
+               zend_ptr_stack_destroy(&plist->free_links);
                free(plist);
        }
 }
@@ -225,6 +199,8 @@
 }
 /* }}} */
 
+/* mysqli_link_free_storage partly doubles the work of 
PHP_FUNCTION(mysqli_close) */
+
 /* {{{ mysqli_link_free_storage
  */
 static void mysqli_link_free_storage(void *object TSRMLS_DC)
@@ -238,6 +214,19 @@
                if (mysql->mysql) {
                        if (!mysql->persistent) {
                                mysqli_close(mysql->mysql, 
MYSQLI_CLOSE_IMPLICIT);
+                       } else {
+                               zend_rsrc_list_entry *le;
+                               if (zend_hash_find(&EG(persistent_list), 
mysql->hash_key, strlen(mysql->hash_key) + 1, (void **)&le) == SUCCESS) {
+                                       if (Z_TYPE_P(le) == php_le_pmysqli()) {
+                                               mysqli_plist_entry *plist = 
(mysqli_plist_entry *) le->ptr;
+                                       
+                                               
zend_ptr_stack_push(&plist->free_links, mysql->mysql);
+
+                                               MyG(num_links)--;
+                                               MyG(num_active_persistent)--;
+                                               MyG(num_inactive_persistent)++;
+                                       }
+                               }
                        }
                }
                php_clear_mysql(mysql);
@@ -851,9 +840,6 @@
  */
 PHP_RSHUTDOWN_FUNCTION(mysqli)
 {
-       /* check persistent connections, move used to free */
-       zend_hash_apply(&EG(persistent_list), (apply_func_t) 
php_mysqli_persistent_on_rshut TSRMLS_CC);
-
 #if !defined(HAVE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
        mysql_thread_end();
 #endif
http://cvs.php.net/viewvc.cgi/php-src/ext/mysqli/mysqli_api.c?r1=1.154&r2=1.155&diff_format=u
Index: php-src/ext/mysqli/mysqli_api.c
diff -u php-src/ext/mysqli/mysqli_api.c:1.154 
php-src/ext/mysqli/mysqli_api.c:1.155
--- php-src/ext/mysqli/mysqli_api.c:1.154       Mon Oct 29 09:50:49 2007
+++ php-src/ext/mysqli/mysqli_api.c     Fri Nov  9 12:13:15 2007
@@ -17,7 +17,7 @@
   |          Ulf Wendel <[EMAIL PROTECTED]>                                    
 |
   +----------------------------------------------------------------------+
 
-  $Id: mysqli_api.c,v 1.154 2007/10/29 09:50:49 jani Exp $ 
+  $Id: mysqli_api.c,v 1.155 2007/11/09 12:13:15 andrey Exp $ 
 */
 
 #ifdef HAVE_CONFIG_H
@@ -571,13 +571,8 @@
                if (zend_hash_find(&EG(persistent_list), mysql->hash_key, 
strlen(mysql->hash_key) + 1, (void **)&le) == SUCCESS) {
                        if (Z_TYPE_P(le) == php_le_pmysqli()) {
                                mysqli_plist_entry *plist = (mysqli_plist_entry 
*) le->ptr;
-                               dtor_func_t pDestructor = 
plist->used_links.pDestructor;
+                               zend_ptr_stack_push(&plist->free_links, 
mysql->mysql);
 
-                               plist->used_links.pDestructor = NULL; /* Don't 
call pDestructor now */
-                               zend_hash_index_del(&plist->used_links, 
mysql->hash_index);
-                               plist->used_links.pDestructor = pDestructor; /* 
Restore the destructor */
-
-                               zend_hash_next_index_insert(&plist->free_links, 
&mysql->mysql, sizeof(MYSQL *), NULL);
                                MyG(num_links)--;
                                MyG(num_active_persistent)--;
                                MyG(num_inactive_persistent)++;
http://cvs.php.net/viewvc.cgi/php-src/ext/mysqli/mysqli_nonapi.c?r1=1.73&r2=1.74&diff_format=u
Index: php-src/ext/mysqli/mysqli_nonapi.c
diff -u php-src/ext/mysqli/mysqli_nonapi.c:1.73 
php-src/ext/mysqli/mysqli_nonapi.c:1.74
--- php-src/ext/mysqli/mysqli_nonapi.c:1.73     Tue Oct  2 10:43:09 2007
+++ php-src/ext/mysqli/mysqli_nonapi.c  Fri Nov  9 12:13:15 2007
@@ -17,7 +17,7 @@
   |          Ulf Wendel <[EMAIL PROTECTED]>                                    
 |
   +----------------------------------------------------------------------+
 
-  $Id: mysqli_nonapi.c,v 1.73 2007/10/02 10:43:09 andrey Exp $ 
+  $Id: mysqli_nonapi.c,v 1.74 2007/11/09 12:13:15 andrey Exp $ 
 */
 
 #ifdef HAVE_CONFIG_H
@@ -111,37 +111,18 @@
                                                                port, 
SAFE_STR(username), SAFE_STR(dbname), 
                                                                
SAFE_STR(passwd));
 
+                       mysql->hash_key = hash_key;
+
                        /* check if we can reuse exisiting connection ... */
                        if (zend_hash_find(&EG(persistent_list), hash_key, 
hash_len + 1, (void **)&le) == SUCCESS) {
                                if (Z_TYPE_P(le) == php_le_pmysqli()) {
                                        plist = (mysqli_plist_entry *) le->ptr;
 
                                        do {
-                                               if 
(zend_hash_num_elements(&plist->free_links)) {
-                                                       HashPosition pos;
-                                                       MYSQL **free_mysql;
-                                                       ulong idx;
-                                                       dtor_func_t pDestructor 
= plist->free_links.pDestructor;
-
-                                                       
zend_hash_internal_pointer_reset_ex(&plist->free_links, &pos);
-                                                       if (SUCCESS != 
zend_hash_get_current_data_ex(&plist->free_links,
-                                                                               
                                                                (void 
**)&free_mysql, &pos)) {
-                                                               break;
-                                                       }
-                                                       if (HASH_KEY_IS_LONG != 
zend_hash_get_current_key_ex(&plist->free_links, NULL,
-                                                                               
                                                                                
 NULL, &idx, FALSE, &pos)) {
-                                                               break;
-                                                       }
-                                                       mysql->mysql = 
*free_mysql;
-                                                       
plist->free_links.pDestructor = NULL; /* Don't call pDestructor now */
-                                                       if (SUCCESS != 
zend_hash_index_del(&plist->free_links, idx)) {
-                                                               
plist->used_links.pDestructor = pDestructor;  /* Restore the destructor */
-                                                               break;
-                                                       }
-                                                       
plist->used_links.pDestructor = pDestructor;  /* Restore the destructor */
-                                                       
MyG(num_inactive_persistent)--;
-                                                       
MyG(num_active_persistent)++;
+                                               if 
(zend_ptr_stack_num_elements(&plist->free_links)) {
+                                                       mysql->mysql = 
zend_ptr_stack_pop(&plist->free_links);
 
+                                                       
MyG(num_inactive_persistent)--;
                                                        /* reset variables */
                                                        /* todo: option for 
ping or change_user */
 #if G0
@@ -152,15 +133,14 @@
 #ifdef HAVE_MYSQLND
                                                                
mysqlnd_restart_psession(mysql->mysql);
 #endif
-                                                               idx = 
zend_hash_next_free_element(&plist->used_links);
-                                                               if (SUCCESS != 
zend_hash_next_index_insert(&plist->used_links, &free_mysql,
-                                                                               
                                                                   sizeof(MYSQL 
*), NULL)) {
-                                                                       
php_mysqli_dtor_p_elements(free_mysql);
-                                                                       break;
-                                                               }
-                                                               
mysql->hash_index = idx;
-                                                               mysql->hash_key 
= hash_key;
-                                                               goto end; 
+                                                               
MyG(num_active_persistent)++;
+                                                               goto end;
+                                                       } else {
+#if defined(HAVE_MYSQLND)
+                                                               
mysqlnd_end_psession(mysql->mysql);
+#endif 
+                                                               
mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
+                                                               mysql->mysql = 
NULL;
                                                        }
                                                }
                                        } while (0);
@@ -170,8 +150,7 @@
                                le.type = php_le_pmysqli();
                                le.ptr = plist = calloc(1, 
sizeof(mysqli_plist_entry));
 
-                               zend_hash_init(&plist->free_links, MAX(10, 
MyG(max_persistent)), NULL, php_mysqli_dtor_p_elements, 1);
-                               zend_hash_init(&plist->used_links, MAX(10, 
MyG(max_persistent)), NULL, php_mysqli_dtor_p_elements, 1);
+                               zend_ptr_stack_init_ex(&plist->free_links, 1);
                                zend_hash_update(&EG(persistent_list), 
hash_key, hash_len + 1, (void *)&le, sizeof(le), NULL);
                        }
                }
@@ -254,14 +233,6 @@
 
        /* store persistent connection */
        if (persistent && new_connection) {
-               ulong hash_index = 
zend_hash_next_free_element(&plist->used_links);
-               /* save persistent connection */
-               if (SUCCESS != zend_hash_next_index_insert(&plist->used_links, 
&mysql->mysql,
-                                                                               
                   sizeof(MYSQL *), NULL)) {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can't 
store persistent connection");
-               } else {
-                       mysql->hash_index = hash_index;
-               }
                MyG(num_active_persistent)++;
        }
 
@@ -284,7 +255,7 @@
 
 err:
        efree(mysql);
-       if (persistent) {
+       if (hash_key) {
                efree(hash_key);
        }
        RETVAL_FALSE;
http://cvs.php.net/viewvc.cgi/php-src/ext/mysqli/php_mysqli_structs.h?r1=1.5&r2=1.6&diff_format=u
Index: php-src/ext/mysqli/php_mysqli_structs.h
diff -u php-src/ext/mysqli/php_mysqli_structs.h:1.5 
php-src/ext/mysqli/php_mysqli_structs.h:1.6
--- php-src/ext/mysqli/php_mysqli_structs.h:1.5 Mon Oct  8 15:40:26 2007
+++ php-src/ext/mysqli/php_mysqli_structs.h     Fri Nov  9 12:13:15 2007
@@ -15,7 +15,7 @@
   | Author: Georg Richter <[EMAIL PROTECTED]>                                |
   +----------------------------------------------------------------------+
 
-  $Id: php_mysqli_structs.h,v 1.5 2007/10/08 15:40:26 andrey Exp $ 
+  $Id: php_mysqli_structs.h,v 1.6 2007/11/09 12:13:15 andrey Exp $ 
 */
 
 #ifndef PHP_MYSQLI_STRUCTS_H
@@ -102,7 +102,6 @@
        zval                    *li_read;
        php_stream              *li_stream;
        zend_bool               persistent;
-       unsigned long   hash_index; /* Used when persistent, hold the index in 
plist->used_links */
        unsigned int    multi_query;
        UConverter              *conv;
 } MY_MYSQL;
@@ -148,8 +147,7 @@
 #endif
 
 typedef struct {
-       HashTable free_links;
-       HashTable used_links;
+       zend_ptr_stack free_links;
 } mysqli_plist_entry;
 
 #ifdef PHP_WIN32

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to