tommyarrayblkof.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2013 Andrea Mazzoleni
3
26#ifndef TOMMY_ARRAYBLKOF_H
27#define TOMMY_ARRAYBLKOF_H
28
29#include "tommytypes.h"
30
31#include <assert.h> /* for assert */
32#include <string.h> /* for memset */
33
34/******************************************************************************/
35/* array */
36
40#define TOMMY_ARRAYBLKOF_BIT 6
41
45#define TOMMY_ARRAYBLKOF_SIZE (4 * 1024)
46
52 unsigned char** block;
58
64TOMMY_API void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size);
65
71
80
88{
89 if (size > array->count) {
90 array->count = size;
91
92 if (size > array->block_count * TOMMY_ARRAYBLKOF_SIZE)
93 tommy_arrayblkof_reserve(array, size);
94 }
95}
96
105
113{
114 tommy_arrayblkof_resize(array, 0);
115}
116
123
133{
134 assert(pos < array->count);
135
136 return array->block[pos / TOMMY_ARRAYBLKOF_SIZE] + (pos % TOMMY_ARRAYBLKOF_SIZE) * array->element_size;
137}
138
145tommy_inline void* tommy_arrayblkof_tail(tommy_arrayblkof* array)
146{
147 assert(array->count != 0);
148 return tommy_arrayblkof_ref(array, array->count - 1);
149}
150
157{
158 tommy_size_t pos = array->count;
159
160 ++array->count;
161 if (array->count > array->block_count * TOMMY_ARRAYBLKOF_SIZE)
162 tommy_arrayblkof_reserve(array, array->count);
163
164 return tommy_arrayblkof_ref(array, pos);
165}
166
173{
174 assert(array->count != 0);
175
176 tommy_size_t pos = array->count - 1;
177 void* ptr = tommy_arrayblkof_ref(array, pos);
178
179 /* keep unused slots zero so growing within capacity needs no initialization */
180 memset(ptr, 0, array->element_size);
181 array->count = pos;
182}
183
190{
191 return array->count == 0;
192}
193
200{
201 return array->count;
202}
203
210{
211 return array->block_count * TOMMY_ARRAYBLKOF_SIZE;
212}
213
223{
224 /* keep record size with the storage it describes */
225 tommy_arrayblkof tmp = *first;
226 *first = *second;
227 *second = tmp;
228}
229
244
253
260
261#endif
262
Array container type.
Definition: tommyarrayblkof.h:51
unsigned char ** block
Directory of blocks.
Definition: tommyarrayblkof.h:52
tommy_size_t element_size
Size of the stored element in bytes.
Definition: tommyarrayblkof.h:55
tommy_size_t count
Number of initialized elements in the array.
Definition: tommyarrayblkof.h:56
tommy_size_t block_count
Number of allocated blocks.
Definition: tommyarrayblkof.h:53
tommy_size_t block_capacity
Number of directory entries allocated.
Definition: tommyarrayblkof.h:54
TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage(const tommy_arrayblkof *array)
Gets the size of allocated memory.
#define TOMMY_ARRAYBLKOF_SIZE
Elements for each block.
Definition: tommyarrayblkof.h:45
TOMMY_API void tommy_arrayblkof_shrink(tommy_arrayblkof *array)
Releases unused allocated memory.
TOMMY_API void tommy_arrayblkof_foreach_arg(tommy_arrayblkof *array, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the array.
void tommy_arrayblkof_remove_tail(tommy_arrayblkof *array)
Removes the last element without copying it.
Definition: tommyarrayblkof.h:172
void * tommy_arrayblkof_ref(tommy_arrayblkof *array, tommy_size_t pos)
Gets a reference to the element at the specified position.
Definition: tommyarrayblkof.h:132
TOMMY_API void tommy_arrayblkof_foreach(tommy_arrayblkof *array, tommy_foreach_func *func)
Calls the specified function for each element in the array.
tommy_size_t tommy_arrayblkof_capacity(const tommy_arrayblkof *array)
Gets the number of elements that fit without further allocation.
Definition: tommyarrayblkof.h:209
TOMMY_API void tommy_arrayblkof_done(tommy_arrayblkof *array)
Deinitializes the array.
void * tommy_arrayblkof_tail(tommy_arrayblkof *array)
Gets a reference to the last element.
Definition: tommyarrayblkof.h:145
tommy_bool_t tommy_arrayblkof_empty(const tommy_arrayblkof *array)
Checks whether the array is empty.
Definition: tommyarrayblkof.h:189
void * tommy_arrayblkof_insert_tail(tommy_arrayblkof *array)
Adds a zero-initialized element and returns its reference.
Definition: tommyarrayblkof.h:156
void tommy_arrayblkof_clear(tommy_arrayblkof *array)
Removes all elements, preserving the allocated capacity.
Definition: tommyarrayblkof.h:112
TOMMY_API void tommy_arrayblkof_reserve(tommy_arrayblkof *array, tommy_size_t size)
Allocates space for at least the specified number of elements.
void tommy_arrayblkof_swap(tommy_arrayblkof *first, tommy_arrayblkof *second)
Exchanges two initialized arrays without copying their elements.
Definition: tommyarrayblkof.h:222
void tommy_arrayblkof_grow(tommy_arrayblkof *array, tommy_size_t size)
Grows the size up to the specified value.
Definition: tommyarrayblkof.h:87
tommy_size_t tommy_arrayblkof_size(const tommy_arrayblkof *array)
Gets the initialized size of the array.
Definition: tommyarrayblkof.h:199
TOMMY_API void tommy_arrayblkof_init(tommy_arrayblkof *array, tommy_size_t element_size)
Initializes the array.
struct tommy_arrayblkof_struct tommy_arrayblkof
Array container type.
TOMMY_API void tommy_arrayblkof_resize(tommy_arrayblkof *array, tommy_size_t size)
Changes the initialized size, preserving the common prefix.
Generic types.
void tommy_foreach_func(void *elem)
Foreach function.
Definition: tommytypes.h:346
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:40
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53