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: 2016-02-05 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 05 Feb 2016 08:39:18 +0000 (35 hours ago)

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