LCOV - code coverage report
Current view: top level - tommyds/tommyds - tommyarrayblkof.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 24 24 100.0 %
Date: 2018-04-02 17:50:51 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013, Andrea Mazzoleni. All rights reserved.
       3             :  *
       4             :  * Redistribution and use in source and binary forms, with or without
       5             :  * modification, are permitted provided that the following conditions
       6             :  * are met:
       7             :  *
       8             :  * 1. Redistributions of source code must retain the above copyright
       9             :  *    notice, this list of conditions and the following disclaimer.
      10             :  *
      11             :  * 2. Redistributions in binary form must reproduce the above copyright
      12             :  *    notice, this list of conditions and the following disclaimer in the
      13             :  *    documentation and/or other materials provided with the distribution.
      14             :  *
      15             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      16             :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      17             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      18             :  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
      19             :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      20             :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      21             :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      22             :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      23             :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      24             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      25             :  * POSSIBILITY OF SUCH DAMAGE.
      26             :  */
      27             : 
      28             : #include "tommyarrayblkof.h"
      29             : 
      30             : /******************************************************************************/
      31             : /* array */
      32             : 
      33           1 : void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size)
      34             : {
      35           1 :         tommy_array_init(&array->block);
      36             : 
      37           1 :         array->element_size = element_size;
      38           1 :         array->count = 0;
      39           1 : }
      40             : 
      41           1 : void tommy_arrayblkof_done(tommy_arrayblkof* array)
      42             : {
      43             :         tommy_size_t i;
      44             : 
      45       12209 :         for (i = 0; i < tommy_array_size(&array->block); ++i)
      46       12208 :                 tommy_free(tommy_array_get(&array->block, i));
      47             : 
      48           1 :         tommy_array_done(&array->block);
      49           1 : }
      50             : 
      51    50000001 : void tommy_arrayblkof_grow(tommy_arrayblkof* array, tommy_size_t count)
      52             : {
      53             :         tommy_size_t block_max;
      54             :         tommy_size_t block_mac;
      55             : 
      56    50000001 :         if (array->count >= count)
      57           1 :                 return;
      58    50000000 :         array->count = count;
      59             : 
      60    50000000 :         block_max = (count + TOMMY_ARRAYBLKOF_SIZE - 1) / TOMMY_ARRAYBLKOF_SIZE;
      61    50000000 :         block_mac = tommy_array_size(&array->block);
      62             : 
      63    50000000 :         if (block_mac < block_max) {
      64             :                 /* grow the block array */
      65       12208 :                 tommy_array_grow(&array->block, block_max);
      66             : 
      67             :                 /* allocate new blocks */
      68       36624 :                 while (block_mac < block_max) {
      69       12208 :                         void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size));
      70             : 
      71             :                         /* set the new block */
      72       12208 :                         tommy_array_set(&array->block, block_mac, ptr);
      73             : 
      74       12208 :                         ++block_mac;
      75             :                 }
      76             :         }
      77             : }
      78             : 
      79           1 : tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array)
      80             : {
      81           1 :         return tommy_array_memory_usage(&array->block) + tommy_array_size(&array->block) * TOMMY_ARRAYBLKOF_SIZE * array->element_size;
      82             : }
      83             : 

Generated by: LCOV version 1.13