LCOV - code coverage report
Current view: top level - tommyds - tommyarray.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 25 25 100.0 %
Date: 2017-11-06 22:14:04 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         523 : void tommy_array_init(tommy_array* array)
      34             : {
      35             :         tommy_uint_t i;
      36             : 
      37             :         /* fixed initial size */
      38         523 :         array->bucket_bit = TOMMY_ARRAY_BIT;
      39         523 :         array->bucket_max = 1 << array->bucket_bit;
      40         523 :         array->bucket[0] = tommy_cast(void**, tommy_calloc(array->bucket_max, sizeof(void*)));
      41        3138 :         for (i = 1; i < TOMMY_ARRAY_BIT; ++i)
      42        2615 :                 array->bucket[i] = array->bucket[0];
      43             : 
      44         523 :         array->count = 0;
      45         523 : }
      46             : 
      47         499 : void tommy_array_done(tommy_array* array)
      48             : {
      49             :         tommy_uint_t i;
      50             : 
      51         499 :         tommy_free(array->bucket[0]);
      52         501 :         for (i = TOMMY_ARRAY_BIT; i < array->bucket_bit; ++i) {
      53           2 :                 void** segment = array->bucket[i];
      54           2 :                 tommy_free(&segment[((tommy_ptrdiff_t)1) << i]);
      55             :         }
      56         499 : }
      57             : 
      58        2215 : void tommy_array_grow(tommy_array* array, tommy_count_t count)
      59             : {
      60        2215 :         if (array->count >= count)
      61           1 :                 return;
      62        2214 :         array->count = count;
      63             : 
      64        4430 :         while (count > array->bucket_max) {
      65             :                 void** segment;
      66             : 
      67             :                 /* allocate one more segment */
      68           2 :                 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           2 :                 array->bucket[array->bucket_bit] = &segment[-(tommy_ptrdiff_t)array->bucket_max];
      73             : 
      74           2 :                 ++array->bucket_bit;
      75           2 :                 array->bucket_max = 1 << array->bucket_bit;
      76             :         }
      77             : }
      78             : 
      79           2 : tommy_size_t tommy_array_memory_usage(tommy_array* array)
      80             : {
      81           2 :         return array->bucket_max * (tommy_size_t)sizeof(void*);
      82             : }
      83             : 

Generated by: LCOV version 1.13