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

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2011, 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 "tommyarray.h"
      29             : 
      30             : /******************************************************************************/
      31             : /* array */
      32             : 
      33           3 : void tommy_array_init(tommy_array* array)
      34             : {
      35             :         tommy_uint_t i;
      36             : 
      37             :         /* fixed initial size */
      38           3 :         array->bucket_bit = TOMMY_ARRAY_BIT;
      39           3 :         array->bucket_max = 1 << array->bucket_bit;
      40           3 :         array->bucket[0] = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
      41          18 :         for (i = 1; i < TOMMY_ARRAY_BIT; ++i)
      42          15 :                 array->bucket[i] = array->bucket[0];
      43             : 
      44           3 :         array->count = 0;
      45           3 : }
      46             : 
      47           3 : void tommy_array_done(tommy_array* array)
      48             : {
      49             :         tommy_uint_t i;
      50             : 
      51           3 :         tommy_free(array->bucket[0]);
      52          39 :         for (i = TOMMY_ARRAY_BIT; i < array->bucket_bit; ++i) {
      53          36 :                 void** segment = array->bucket[i];
      54          36 :                 tommy_free(&segment[((tommy_ptrdiff_t)1) << i]);
      55             :         }
      56           3 : }
      57             : 
      58    50024417 : void tommy_array_grow(tommy_array* array, tommy_size_t count)
      59             : {
      60    50024417 :         if (array->count >= count)
      61           1 :                 return;
      62    50024416 :         array->count = count;
      63             : 
      64   100048868 :         while (count > array->bucket_max) {
      65             :                 void** segment;
      66             : 
      67             :                 /* allocate one more segment */
      68          36 :                 segment = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
      69             : 
      70             :                 /* store it adjusting the offset */
      71             :                 /* cast to ptrdiff_t to ensure to get a negative value */
      72          36 :                 array->bucket[array->bucket_bit] = &segment[-(tommy_ptrdiff_t)array->bucket_max];
      73             : 
      74          36 :                 ++array->bucket_bit;
      75          36 :                 array->bucket_max = 1 << array->bucket_bit;
      76             :         }
      77             : }
      78             : 
      79           3 : tommy_size_t tommy_array_memory_usage(tommy_array* array)
      80             : {
      81           3 :         return array->bucket_max * (tommy_size_t)sizeof(void*);
      82             : }
      83             : 

Generated by: LCOV version 1.13