LCOV - code coverage report
Current view: top level - tommyds - tommyarray.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 16 16 100.0 %
Date: 2017-11-06 22:14:04 Functions: 5 5 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             : /** \file
      29             :  * Dynamic array based on segments of exponential growing size.
      30             :  *
      31             :  * This array is able to grow dynamically upon request, without any reallocation.
      32             :  *
      33             :  * The grow operation involves an allocation of a new array segment, without reallocating
      34             :  * the already used memory, and then not increasing the heap fragmentation.
      35             :  * This also implies that the address of the stored elements never change.
      36             :  *
      37             :  * Allocated segments grow in size exponentially.
      38             :  */
      39             : 
      40             : #ifndef __TOMMYARRAY_H
      41             : #define __TOMMYARRAY_H
      42             : 
      43             : #include "tommytypes.h"
      44             : 
      45             : #include <assert.h> /* for assert */
      46             : 
      47             : /******************************************************************************/
      48             : /* array */
      49             : 
      50             : /**
      51             :  * Initial and minimal size of the array expressed as a power of 2.
      52             :  * The initial size is 2^TOMMY_ARRAY_BIT.
      53             :  */
      54             : #define TOMMY_ARRAY_BIT 6
      55             : 
      56             : /** \internal
      57             :  * Max number of elements as a power of 2.
      58             :  */
      59             : #define TOMMY_ARRAY_BIT_MAX 32
      60             : 
      61             : /**
      62             :  * Array container type.
      63             :  * \note Don't use internal fields directly, but access the container only using functions.
      64             :  */
      65             : typedef struct tommy_array_struct {
      66             :         void** bucket[TOMMY_ARRAY_BIT_MAX]; /**< Dynamic array of buckets. */
      67             :         tommy_uint_t bucket_bit; /**< Bits used in the bit mask. */
      68             :         tommy_count_t bucket_max; /**< Number of buckets. */
      69             :         tommy_count_t count; /**< Number of initialized elements in the array. */
      70             : } tommy_array;
      71             : 
      72             : /**
      73             :  * Initializes the array.
      74             :  */
      75             : void tommy_array_init(tommy_array* array);
      76             : 
      77             : /**
      78             :  * Deinitializes the array.
      79             :  */
      80             : void tommy_array_done(tommy_array* array);
      81             : 
      82             : /**
      83             :  * Grows the size up to the specified value.
      84             :  * All the new elements in the array are initialized with the 0 value.
      85             :  */
      86             : void tommy_array_grow(tommy_array* array, tommy_count_t size);
      87             : 
      88             : /**
      89             :  * Gets a reference of the element at the specified position.
      90             :  * You must be sure that space for this position is already
      91             :  * allocated calling tommy_array_grow().
      92             :  */
      93    10268423 : tommy_inline void** tommy_array_ref(tommy_array* array, tommy_count_t pos)
      94             : {
      95             :         tommy_uint_t bsr;
      96             : 
      97    10268423 :         assert(pos < array->count);
      98             : 
      99             :         /* get the highest bit set, in case of all 0, return 0 */
     100    10268423 :         bsr = tommy_ilog2_u32(pos | 1);
     101             : 
     102    10268423 :         return &array->bucket[bsr][pos];
     103             : }
     104             : 
     105             : /**
     106             :  * Sets the element at the specified position.
     107             :  * You must be sure that space for this position is already
     108             :  * allocated calling tommy_array_grow().
     109             :  */
     110        2214 : tommy_inline void tommy_array_set(tommy_array* array, tommy_count_t pos, void* element)
     111             : {
     112        2214 :         *tommy_array_ref(array, pos) = element;
     113        2214 : }
     114             : 
     115             : /**
     116             :  * Gets the element at the specified position.
     117             :  * You must be sure that space for this position is already
     118             :  * allocated calling tommy_array_grow().
     119             :  */
     120    10266209 : tommy_inline void* tommy_array_get(tommy_array* array, tommy_count_t pos)
     121             : {
     122    10266209 :         return *tommy_array_ref(array, pos);
     123             : }
     124             : 
     125             : /**
     126             :  * Grows and inserts a new element at the end of the array.
     127             :  */
     128         256 : tommy_inline void tommy_array_insert(tommy_array* array, void* element)
     129             : {
     130         256 :         tommy_count_t pos = array->count;
     131             : 
     132         256 :         tommy_array_grow(array, pos + 1);
     133             : 
     134         256 :         tommy_array_set(array, pos, element);
     135         256 : }
     136             : 
     137             : /**
     138             :  * Gets the initialized size of the array.
     139             :  */
     140     1575778 : tommy_inline tommy_count_t tommy_array_size(tommy_array* array)
     141             : {
     142     1575778 :         return array->count;
     143             : }
     144             : 
     145             : /**
     146             :  * Gets the size of allocated memory.
     147             :  */
     148             : tommy_size_t tommy_array_memory_usage(tommy_array* array);
     149             : 
     150             : #endif
     151             : 

Generated by: LCOV version 1.13