PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqlnd - mysqlnd_block_alloc.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 43 77 55.8 %
Date: 2014-07-27 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 5                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2006-2013 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Georg Richter <georg@mysql.com>                             |
      16             :   |          Andrey Hristov <andrey@mysql.com>                           |
      17             :   |          Ulf Wendel <uwendel@mysql.com>                              |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "php.h"
      24             : #include "mysqlnd.h"
      25             : #include "mysqlnd_block_alloc.h"
      26             : #include "mysqlnd_debug.h"
      27             : #include "mysqlnd_priv.h"
      28             : 
      29             : 
      30             : /* {{{ mysqlnd_mempool_free_chunk */
      31             : static void
      32       81330 : mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk TSRMLS_DC)
      33             : {
      34       81330 :         MYSQLND_MEMORY_POOL * pool = chunk->pool;
      35       81330 :         DBG_ENTER("mysqlnd_mempool_free_chunk");
      36       81330 :         if (chunk->from_pool) {
      37             :                 /* Try to back-off and guess if this is the last block allocated */
      38       29729 :                 if (chunk->ptr == (pool->arena + (pool->arena_size - pool->free_size - chunk->size))) {
      39             :                         /*
      40             :                                 This was the last allocation. Lucky us, we can free
      41             :                                 a bit of memory from the pool. Next time we will return from the same ptr.
      42             :                         */
      43       27790 :                         pool->free_size += chunk->size;
      44             :                 }
      45       29729 :                 pool->refcount--;
      46             :         } else {
      47       51601 :                 mnd_free(chunk->ptr);
      48             :         }
      49       81330 :         mnd_free(chunk);
      50       81330 :         DBG_VOID_RETURN;
      51             : }
      52             : /* }}} */
      53             : 
      54             : 
      55             : /* {{{ mysqlnd_mempool_resize_chunk */
      56             : static enum_func_status
      57           0 : mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL_CHUNK * chunk, unsigned int size TSRMLS_DC)
      58             : {
      59           0 :         DBG_ENTER("mysqlnd_mempool_resize_chunk");
      60           0 :         if (chunk->from_pool) {
      61           0 :                 MYSQLND_MEMORY_POOL * pool = chunk->pool;
      62             :                 /* Try to back-off and guess if this is the last block allocated */
      63           0 :                 if (chunk->ptr == (pool->arena + (pool->arena_size - pool->free_size - chunk->size))) {
      64             :                         /*
      65             :                                 This was the last allocation. Lucky us, we can free
      66             :                                 a bit of memory from the pool. Next time we will return from the same ptr.
      67             :                         */
      68           0 :                         if ((chunk->size + pool->free_size) < size) {
      69             :                                 zend_uchar *new_ptr;
      70           0 :                                 new_ptr = mnd_malloc(size);
      71           0 :                                 if (!new_ptr) {
      72           0 :                                         DBG_RETURN(FAIL);
      73             :                                 }
      74           0 :                                 memcpy(new_ptr, chunk->ptr, chunk->size);
      75           0 :                                 chunk->ptr = new_ptr;
      76           0 :                                 pool->free_size += chunk->size;
      77           0 :                                 chunk->size = size;
      78           0 :                                 chunk->pool = NULL; /* now we have no pool memory */
      79           0 :                                 pool->refcount--;
      80             :                         } else {
      81             :                                 /* If the chunk is > than asked size then free_memory increases, otherwise decreases*/
      82           0 :                                 pool->free_size += (chunk->size - size);
      83             :                         }
      84             :                 } else {
      85             :                         /* Not last chunk, if the user asks for less, give it to him */
      86           0 :                         if (chunk->size >= size) {
      87             :                                 ; /* nop */
      88             :                         } else {
      89             :                                 zend_uchar *new_ptr;
      90           0 :                                 new_ptr = mnd_malloc(size);
      91           0 :                                 if (!new_ptr) {
      92           0 :                                         DBG_RETURN(FAIL);
      93             :                                 }
      94           0 :                                 memcpy(new_ptr, chunk->ptr, chunk->size);
      95           0 :                                 chunk->ptr = new_ptr;
      96           0 :                                 chunk->size = size;
      97           0 :                                 chunk->pool = NULL; /* now we have non-pool memory */
      98           0 :                                 pool->refcount--;
      99             :                         }
     100             :                 }
     101             :         } else {
     102           0 :                 zend_uchar *new_ptr = mnd_realloc(chunk->ptr, size);
     103           0 :                 if (!new_ptr) {
     104           0 :                         DBG_RETURN(FAIL);
     105             :                 }
     106           0 :                 chunk->ptr = new_ptr;
     107             :         }
     108           0 :         DBG_RETURN(PASS);
     109             : }
     110             : /* }}} */
     111             : 
     112             : 
     113             : /* {{{ mysqlnd_mempool_get_chunk */
     114             : static
     115       81335 : MYSQLND_MEMORY_POOL_CHUNK * mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, unsigned int size TSRMLS_DC)
     116             : {
     117       81335 :         MYSQLND_MEMORY_POOL_CHUNK *chunk = NULL;
     118       81335 :         DBG_ENTER("mysqlnd_mempool_get_chunk");
     119             : 
     120       81335 :         chunk = mnd_malloc(sizeof(MYSQLND_MEMORY_POOL_CHUNK));
     121       81335 :         if (chunk) {
     122       81335 :                 chunk->free_chunk = mysqlnd_mempool_free_chunk;
     123       81335 :                 chunk->resize_chunk = mysqlnd_mempool_resize_chunk;
     124       81335 :                 chunk->size = size;
     125             :                 /*
     126             :                   Should not go over MYSQLND_MAX_PACKET_SIZE, since we
     127             :                   expect non-arena memory in mysqlnd_wireprotocol.c . We
     128             :                   realloc the non-arena memory.
     129             :                 */
     130       81335 :                 chunk->pool = pool;
     131       81335 :                 if (size > pool->free_size) {
     132       51601 :                         chunk->from_pool = FALSE;
     133       51601 :                         chunk->ptr = mnd_malloc(size);
     134       51601 :                         if (!chunk->ptr) {
     135           0 :                                 chunk->free_chunk(chunk TSRMLS_CC);
     136           0 :                                 chunk = NULL;
     137             :                         }
     138             :                 } else {
     139       29734 :                         chunk->from_pool = TRUE;
     140       29734 :                         ++pool->refcount;
     141       29734 :                         chunk->ptr = pool->arena + (pool->arena_size - pool->free_size);
     142             :                         /* Last step, update free_size */
     143       29734 :                         pool->free_size -= size;
     144             :                 }
     145             :         }
     146       81335 :         DBG_RETURN(chunk);
     147             : }
     148             : /* }}} */
     149             : 
     150             : 
     151             : /* {{{ mysqlnd_mempool_create */
     152             : PHPAPI MYSQLND_MEMORY_POOL *
     153        4597 : mysqlnd_mempool_create(size_t arena_size TSRMLS_DC)
     154             : {
     155             :         /* We calloc, because we free(). We don't mnd_calloc()  for a reason. */
     156        4597 :         MYSQLND_MEMORY_POOL * ret = mnd_calloc(1, sizeof(MYSQLND_MEMORY_POOL));
     157        4597 :         DBG_ENTER("mysqlnd_mempool_create");
     158        4597 :         if (ret) {
     159        4597 :                 ret->get_chunk = mysqlnd_mempool_get_chunk;
     160        4597 :                 ret->free_size = ret->arena_size = arena_size ? arena_size : 0;
     161        4597 :                 ret->refcount = 0;
     162             :                 /* OOM ? */
     163        4597 :                 ret->arena = mnd_malloc(ret->arena_size);
     164        4597 :                 if (!ret->arena) {
     165           0 :                         mysqlnd_mempool_destroy(ret TSRMLS_CC);
     166           0 :                         ret = NULL;
     167             :                 }
     168             :         }
     169        4597 :         DBG_RETURN(ret);
     170             : }
     171             : /* }}} */
     172             : 
     173             : 
     174             : /* {{{ mysqlnd_mempool_destroy */
     175             : PHPAPI void
     176        4598 : mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool TSRMLS_DC)
     177             : {
     178        4598 :         DBG_ENTER("mysqlnd_mempool_destroy");
     179             :         /* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
     180        4598 :         mnd_free(pool->arena);
     181        4598 :         mnd_free(pool);
     182        4598 :         DBG_VOID_RETURN;
     183             : }
     184             : /* }}} */
     185             : 
     186             : 
     187             : /*
     188             :  * Local variables:
     189             :  * tab-width: 4
     190             :  * c-basic-offset: 4
     191             :  * End:
     192             :  * vim600: noet sw=4 ts=4 fdm=marker
     193             :  * vim<600: noet sw=4 ts=4
     194             :  */

Generated by: LCOV version 1.10

Generated at Sun, 27 Jul 2014 12:58:31 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.