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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:09 +0000 (7 days ago)

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