]> granicus.if.org Git - php/commitdiff
Don't allocate 2 chunks of memory when one can fit. Reduces memory
authorAndrey Hristov <andrey@php.net>
Wed, 10 May 2006 11:53:13 +0000 (11:53 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 10 May 2006 11:53:13 +0000 (11:53 +0000)
fragmentation. There is one more place that fragments memory but it will
complicate the ongoing Unicode upgrade of mysqli so leaving it away for now.

ext/mysqli/mysqli.c
ext/mysqli/mysqli_api.c

index 684cddf305a87736935c36d148ed4720d8ee9ae9..8f9b5119d4d65ddb1e4d8e5c2d835442ac82543d 100644 (file)
@@ -69,10 +69,6 @@ void php_free_stmt_bind_buffer(BIND_BUFFER bbuf, int type)
                return;
        }
 
-       if (bbuf.is_null) {
-               efree(bbuf.is_null);
-       }
-
        for (i=0; i < bbuf.var_cnt; i++) {
 
                /* free temporary bind buffer */
@@ -89,9 +85,18 @@ void php_free_stmt_bind_buffer(BIND_BUFFER bbuf, int type)
                efree(bbuf.vars);
        }
 
+       /*
+         Don't free bbuf.is_null for FETCH_RESULT since we have allocated
+         is_null and buf in one block so we free only buf, which is the beginning
+         of the block. When FETCH_SIMPLE then buf wasn't allocated together with
+         buf and we have to free it.
+       */
        if (type == FETCH_RESULT) {
                efree(bbuf.buf);
+       } else if (type == FETCH_SIMPLE){
+               efree(bbuf.is_null);
        }
+
        bbuf.var_cnt = 0;
        return;
 }
index 43eb19347c838ac048f0bba6d57922bd72b31527..1dcaec2a5dda8b354256bfb32f98b47baf23d581 100644 (file)
@@ -250,12 +250,16 @@ PHP_FUNCTION(mysqli_stmt_bind_result)
        }
 
        bind = (MYSQL_BIND *)ecalloc(var_cnt, sizeof(MYSQL_BIND));
-       stmt->result.buf = (VAR_BUFFER *)ecalloc(var_cnt,sizeof(VAR_BUFFER));
-       stmt->result.is_null = (char *)ecalloc(var_cnt, sizeof(char));
+       {
+               int size;
+               char *p= emalloc(size= var_cnt * (sizeof(char) + sizeof(VAR_BUFFER)));
+               stmt->result.buf = (VAR_BUFFER *) p;
+               stmt->result.is_null = p + var_cnt * sizeof(VAR_BUFFER);
+               memset(p, 0, size);
+       }
 
        for (i=start; i < var_cnt + start ; i++) {
                ofs = i - start;
-               stmt->result.is_null[ofs] = 0;
                col_type = (stmt->stmt->fields) ? stmt->stmt->fields[ofs].type : MYSQL_TYPE_STRING;
 
                switch (col_type) {
@@ -373,8 +377,8 @@ PHP_FUNCTION(mysqli_stmt_bind_result)
                                efree(stmt->result.buf[i].val);
                        }
                }
+               /* Don't free stmt->result.is_null because is_null & buf are one block of memory  */
                efree(stmt->result.buf);
-               efree(stmt->result.is_null);
                RETVAL_FALSE;
        } else {
                stmt->result.var_cnt = var_cnt;