diff options
Diffstat (limited to 'ext/mysqlnd/mysqlnd_block_alloc.c')
-rw-r--r-- | ext/mysqlnd/mysqlnd_block_alloc.c | 121 |
1 files changed, 57 insertions, 64 deletions
diff --git a/ext/mysqlnd/mysqlnd_block_alloc.c b/ext/mysqlnd/mysqlnd_block_alloc.c index 8c7f7d951..1288e7690 100644 --- a/ext/mysqlnd/mysqlnd_block_alloc.c +++ b/ext/mysqlnd/mysqlnd_block_alloc.c @@ -1,8 +1,8 @@ /* +----------------------------------------------------------------------+ - | PHP Version 6 | + | PHP Version 5 | +----------------------------------------------------------------------+ - | Copyright (c) 2006-2009 The PHP Group | + | Copyright (c) 2006-2010 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: mysqlnd_block_alloc.c 293779 2010-01-20 17:09:28Z johannes $ */ +/* $Id: mysqlnd_block_alloc.c 298917 2010-05-03 17:07:18Z andrey $ */ #include "php.h" #include "mysqlnd.h" @@ -29,23 +29,7 @@ /* {{{ mysqlnd_mempool_free_chunk */ static void -mysqlnd_mempool_free_contents(MYSQLND_MEMORY_POOL * pool TSRMLS_DC) -{ - unsigned int i; - DBG_ENTER("mysqlnd_mempool_dtor"); - for (i = 0; i < pool->free_chunk_list_elements; i++) { - MYSQLND_MEMORY_POOL_CHUNK * chunk = pool->free_chunk_list[i]; - chunk->free_chunk(chunk, FALSE TSRMLS_CC); - } - - DBG_VOID_RETURN; -} -/* }}} */ - - -/* {{{ mysqlnd_mempool_free_chunk */ -static void -mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, zend_bool cache_it TSRMLS_DC) +mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk TSRMLS_DC) { MYSQLND_MEMORY_POOL * pool = chunk->pool; DBG_ENTER("mysqlnd_mempool_free_chunk"); @@ -62,20 +46,14 @@ mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, zend_bool cache_it } else { mnd_free(chunk->ptr); } - if (cache_it && pool->free_chunk_list_elements < MYSQLND_MEMORY_POOL_CHUNK_LIST_SIZE) { - chunk->ptr = NULL; - pool->free_chunk_list[pool->free_chunk_list_elements++] = chunk; - } else { - /* We did not cache it -> free it */ - mnd_free(chunk); - } + mnd_free(chunk); DBG_VOID_RETURN; } /* }}} */ /* {{{ mysqlnd_mempool_resize_chunk */ -static void +static enum_func_status mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, unsigned int size TSRMLS_DC) { DBG_ENTER("mysqlnd_mempool_resize_chunk"); @@ -90,6 +68,9 @@ mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, unsigned int siz if ((chunk->size + pool->free_size) < size) { zend_uchar *new_ptr; new_ptr = mnd_malloc(size); + if (!new_ptr) { + DBG_RETURN(FAIL); + } memcpy(new_ptr, chunk->ptr, chunk->size); chunk->ptr = new_ptr; pool->free_size += chunk->size; @@ -107,17 +88,25 @@ mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, unsigned int siz } else { zend_uchar *new_ptr; new_ptr = mnd_malloc(size); + if (!new_ptr) { + DBG_RETURN(FAIL); + } memcpy(new_ptr, chunk->ptr, chunk->size); chunk->ptr = new_ptr; chunk->size = size; - chunk->pool = NULL; /* now we have no pool memory */ - pool->refcount--; + chunk->pool = NULL; /* now we have non-pool memory */ + pool->refcount--; } } } else { - chunk->ptr = mnd_realloc(chunk->ptr, size); + zend_uchar *new_ptr = mnd_realloc(chunk->ptr, size); + if (!new_ptr) { + DBG_RETURN(FAIL); + } + chunk->ptr = new_ptr; + } - DBG_VOID_RETURN; + DBG_RETURN(PASS); } /* }}} */ @@ -129,30 +118,31 @@ MYSQLND_MEMORY_POOL_CHUNK * mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool MYSQLND_MEMORY_POOL_CHUNK *chunk = NULL; DBG_ENTER("mysqlnd_mempool_get_chunk"); - if (pool->free_chunk_list_elements) { - chunk = pool->free_chunk_list[--pool->free_chunk_list_elements]; - } else { - chunk = mnd_malloc(sizeof(MYSQLND_MEMORY_POOL_CHUNK)); - } - - chunk->free_chunk = mysqlnd_mempool_free_chunk; - chunk->resize_chunk = mysqlnd_mempool_resize_chunk; - chunk->size = size; - /* - Should not go over MYSQLND_MAX_PACKET_SIZE, since we - expect non-arena memory in mysqlnd_wireprotocol.c . We - realloc the non-arena memory. - */ - chunk->pool = pool; - if (size > pool->free_size) { - chunk->ptr = mnd_malloc(size); - chunk->from_pool = FALSE; - } else { - chunk->from_pool = TRUE; - ++pool->refcount; - chunk->ptr = pool->arena + (pool->arena_size - pool->free_size); - /* Last step, update free_size */ - pool->free_size -= size; + chunk = mnd_malloc(sizeof(MYSQLND_MEMORY_POOL_CHUNK)); + if (chunk) { + chunk->free_chunk = mysqlnd_mempool_free_chunk; + chunk->resize_chunk = mysqlnd_mempool_resize_chunk; + chunk->size = size; + /* + Should not go over MYSQLND_MAX_PACKET_SIZE, since we + expect non-arena memory in mysqlnd_wireprotocol.c . We + realloc the non-arena memory. + */ + chunk->pool = pool; + if (size > pool->free_size) { + chunk->from_pool = FALSE; + chunk->ptr = mnd_malloc(size); + if (!chunk->ptr) { + chunk->free_chunk(chunk TSRMLS_CC); + chunk = NULL; + } + } else { + chunk->from_pool = TRUE; + ++pool->refcount; + chunk->ptr = pool->arena + (pool->arena_size - pool->free_size); + /* Last step, update free_size */ + pool->free_size -= size; + } } DBG_RETURN(chunk); } @@ -166,13 +156,17 @@ mysqlnd_mempool_create(size_t arena_size TSRMLS_DC) /* We calloc, because we free(). We don't mnd_calloc() for a reason. */ MYSQLND_MEMORY_POOL * ret = mnd_calloc(1, sizeof(MYSQLND_MEMORY_POOL)); DBG_ENTER("mysqlnd_mempool_create"); - - ret->free_size = ret->arena_size = arena_size; - ret->refcount = 0; - /* OOM ? */ - ret->arena = mnd_malloc(ret->arena_size); - ret->get_chunk = mysqlnd_mempool_get_chunk; - + if (ret) { + ret->get_chunk = mysqlnd_mempool_get_chunk; + ret->free_size = ret->arena_size = arena_size ? arena_size : 0; + ret->refcount = 0; + /* OOM ? */ + ret->arena = mnd_malloc(ret->arena_size); + if (!ret->arena) { + mysqlnd_mempool_destroy(ret TSRMLS_CC); + ret = NULL; + } + } DBG_RETURN(ret); } /* }}} */ @@ -184,7 +178,6 @@ mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool TSRMLS_DC) { DBG_ENTER("mysqlnd_mempool_destroy"); /* mnd_free will reference LOCK_access and might crash, depending on the caller...*/ - mysqlnd_mempool_free_contents(pool TSRMLS_CC); mnd_free(pool->arena); mnd_free(pool); DBG_VOID_RETURN; |