tommyarrayblkof.h
Go to the documentation of this file.
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 
41 #ifndef __TOMMYARRAYBLKOF_H
42 #define __TOMMYARRAYBLKOF_H
43 
44 #include "tommytypes.h"
45 #include "tommyarray.h"
46 
47 #include <assert.h> /* for assert */
48 
49 /******************************************************************************/
50 /* array */
51 
55 #define TOMMY_ARRAYBLKOF_SIZE (4 * 1024)
56 
61 typedef struct tommy_arrayblkof_struct {
66 
71 void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size);
72 
77 
83 
89 tommy_inline void* tommy_arrayblkof_ref(tommy_arrayblkof* array, tommy_count_t pos)
90 {
91  unsigned char* base;
92 
93  assert(pos < array->count);
94 
95  base = tommy_cast(unsigned char*, tommy_array_get(&array->block, pos / TOMMY_ARRAYBLKOF_SIZE));
96 
97  return base + (pos % TOMMY_ARRAYBLKOF_SIZE) * array->element_size;
98 }
99 
104 {
105  return array->count;
106 }
107 
112 
113 #endif
114 
#define TOMMY_ARRAYBLKOF_SIZE
Elements for each block.
Definition: tommyarrayblkof.h:55
tommy_uint32_t tommy_count_t
Generic unsigned integer for counting objects.
Definition: tommytypes.h:67
struct tommy_arrayblkof_struct tommy_arrayblkof
Array container type.
void tommy_arrayblkof_grow(tommy_arrayblkof *array, tommy_count_t size)
Grows the size up to the specified value.
tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof *array)
Gets the size of allocated memory.
Array container type.
Definition: tommyarrayblkof.h:61
void * tommy_arrayblkof_ref(tommy_arrayblkof *array, tommy_count_t pos)
Gets a reference of the element at the specified position.
Definition: tommyarrayblkof.h:89
Dynamic array based on segments of exponential growing size.
void tommy_arrayblkof_init(tommy_arrayblkof *array, tommy_size_t element_size)
Initializes the array.
tommy_count_t tommy_arrayblkof_size(tommy_arrayblkof *array)
Gets the initialized size of the array.
Definition: tommyarrayblkof.h:103
tommy_size_t element_size
Size of the stored element in bytes.
Definition: tommyarrayblkof.h:63
tommy_array block
Array of blocks.
Definition: tommyarrayblkof.h:62
void tommy_arrayblkof_done(tommy_arrayblkof *array)
Deinitializes the array.
void * tommy_array_get(tommy_array *array, tommy_count_t pos)
Gets the element at the specified position.
Definition: tommyarray.h:120
size_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:50
Array container type.
Definition: tommyarray.h:65
Generic types.
tommy_count_t count
Number of initialized elements in the array.
Definition: tommyarrayblkof.h:64