Data Structures | Macros | Typedefs | Functions
tommyarrayblkof.h File Reference

Dynamic array based on fixed-size blocks. More...

Go to the source code of this file.

Data Structures

struct  tommy_arrayblkof_struct
 Array container type. More...
 

Macros

#define TOMMY_ARRAYBLKOF_BIT   6
 Initial and minimal capacity of the block directory expressed as a power of 2. More...
 
#define TOMMY_ARRAYBLKOF_SIZE   (4 * 1024)
 Elements for each block. More...
 

Typedefs

typedef struct tommy_arrayblkof_struct tommy_arrayblkof
 Array container type. More...
 

Functions

TOMMY_API void tommy_arrayblkof_init (tommy_arrayblkof *array, tommy_size_t element_size)
 Initializes the array. More...
 
TOMMY_API void tommy_arrayblkof_done (tommy_arrayblkof *array)
 Deinitializes the array. More...
 
TOMMY_API void tommy_arrayblkof_reserve (tommy_arrayblkof *array, tommy_size_t size)
 Allocates space for at least the specified number of elements. More...
 
void tommy_arrayblkof_grow (tommy_arrayblkof *array, tommy_size_t size)
 Grows the size up to the specified value. More...
 
TOMMY_API void tommy_arrayblkof_resize (tommy_arrayblkof *array, tommy_size_t size)
 Changes the initialized size, preserving the common prefix. More...
 
void tommy_arrayblkof_clear (tommy_arrayblkof *array)
 Removes all elements, preserving the allocated capacity. More...
 
TOMMY_API void tommy_arrayblkof_shrink (tommy_arrayblkof *array)
 Releases unused allocated memory. More...
 
void * tommy_arrayblkof_ref (tommy_arrayblkof *array, tommy_size_t pos)
 Gets a reference to the element at the specified position. More...
 
void * tommy_arrayblkof_tail (tommy_arrayblkof *array)
 Gets a reference to the last element. More...
 
void * tommy_arrayblkof_insert_tail (tommy_arrayblkof *array)
 Adds a zero-initialized element and returns its reference. More...
 
void tommy_arrayblkof_remove_tail (tommy_arrayblkof *array)
 Removes the last element without copying it. More...
 
tommy_bool_t tommy_arrayblkof_empty (const tommy_arrayblkof *array)
 Checks whether the array is empty. More...
 
tommy_size_t tommy_arrayblkof_size (const tommy_arrayblkof *array)
 Gets the initialized size of the array. More...
 
tommy_size_t tommy_arrayblkof_capacity (const tommy_arrayblkof *array)
 Gets the number of elements that fit without further allocation. More...
 
void tommy_arrayblkof_swap (tommy_arrayblkof *first, tommy_arrayblkof *second)
 Exchanges two initialized arrays without copying their elements. More...
 
TOMMY_API void tommy_arrayblkof_foreach (tommy_arrayblkof *array, tommy_foreach_func *func)
 Calls the specified function for each element in the array. More...
 
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. More...
 
TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage (const tommy_arrayblkof *array)
 Gets the size of allocated memory. More...
 

Detailed Description

Dynamic array based on fixed-size blocks.

This array is able to grow dynamically without reallocating stored elements. The directory of block pointers may be reallocated, but element addresses never change.

This is very similar to tommy_arrayblk, but it allows storing elements of any size and not just pointers.

The container allocates space for elements but never copies user data into or out of it. tommy_arrayblkof_ref(), tommy_arrayblkof_tail(), and tommy_arrayblkof_insert_tail() return an element's address so the caller can read or write it directly.

Note
Only types with fundamental alignment requirements are supported. Storage is allocated using calloc(), which does not guarantee the alignment required by over-aligned types (e.g. _Alignas(32) or _Alignas(64)). When storing typed elements, use sizeof(type) as the element size to preserve alignment for every element.

Macro Definition Documentation

◆ TOMMY_ARRAYBLKOF_BIT

#define TOMMY_ARRAYBLKOF_BIT   6

Initial and minimal capacity of the block directory expressed as a power of 2.

◆ TOMMY_ARRAYBLKOF_SIZE

#define TOMMY_ARRAYBLKOF_SIZE   (4 * 1024)

Elements for each block.

Typedef Documentation

◆ tommy_arrayblkof

Array container type.

Note
Don't use internal fields directly, but access the container only using functions.

Function Documentation

◆ tommy_arrayblkof_init()

TOMMY_API void tommy_arrayblkof_init ( tommy_arrayblkof *  array,
tommy_size_t  element_size 
)

Initializes the array.

Parameters
arrayThe array.
element_sizeSize in bytes of the element to store in the array.

◆ tommy_arrayblkof_done()

TOMMY_API void tommy_arrayblkof_done ( tommy_arrayblkof *  array)

Deinitializes the array.

Parameters
arrayThe array.

◆ tommy_arrayblkof_reserve()

TOMMY_API void tommy_arrayblkof_reserve ( tommy_arrayblkof *  array,
tommy_size_t  size 
)

Allocates space for at least the specified number of elements.

The initialized size and existing elements are unchanged. Existing element references remain valid.

Parameters
arrayThe array.
sizeNumber of elements to reserve space for.

◆ tommy_arrayblkof_grow()

void tommy_arrayblkof_grow ( tommy_arrayblkof *  array,
tommy_size_t  size 
)

Grows the size up to the specified value.

All the new elements in the array are initialized with the 0 value.

Parameters
arrayThe array.
sizeNew size of the array.

◆ tommy_arrayblkof_resize()

TOMMY_API void tommy_arrayblkof_resize ( tommy_arrayblkof *  array,
tommy_size_t  size 
)

Changes the initialized size, preserving the common prefix.

New elements are initialized to zero. Reducing the size does not release allocated capacity.

Parameters
arrayThe array.
sizeNew size of the array.

◆ tommy_arrayblkof_clear()

void tommy_arrayblkof_clear ( tommy_arrayblkof *  array)

Removes all elements, preserving the allocated capacity.

Call tommy_arrayblkof_shrink() after clearing to restore the initial capacity. The array remains initialized and can be reused immediately.

Parameters
arrayThe array.

◆ tommy_arrayblkof_shrink()

TOMMY_API void tommy_arrayblkof_shrink ( tommy_arrayblkof *  array)

Releases unused allocated memory.

Preserves the size, values, and addresses of existing elements.

Parameters
arrayThe array.

◆ tommy_arrayblkof_ref()

void * tommy_arrayblkof_ref ( tommy_arrayblkof *  array,
tommy_size_t  pos 
)

Gets a reference to the element at the specified position.

You must be sure that space for this position is already allocated by calling tommy_arrayblkof_grow().

Parameters
arrayThe array.
posPosition of the element.
Returns
Pointer to the slot at the specified position.

◆ tommy_arrayblkof_tail()

void * tommy_arrayblkof_tail ( tommy_arrayblkof *  array)

Gets a reference to the last element.

The array must not be empty.

Parameters
arrayThe array.
Returns
Pointer to the last element.

◆ tommy_arrayblkof_insert_tail()

void * tommy_arrayblkof_insert_tail ( tommy_arrayblkof *  array)

Adds a zero-initialized element and returns its reference.

Parameters
arrayThe array.
Returns
Pointer to the new element.

◆ tommy_arrayblkof_remove_tail()

void tommy_arrayblkof_remove_tail ( tommy_arrayblkof *  array)

Removes the last element without copying it.

The array must not be empty. The removed slot is cleared.

Parameters
arrayThe array.

◆ tommy_arrayblkof_empty()

tommy_bool_t tommy_arrayblkof_empty ( const tommy_arrayblkof *  array)

Checks whether the array is empty.

Parameters
arrayThe array.
Returns
Nonzero if the array is empty, 0 otherwise.

◆ tommy_arrayblkof_size()

tommy_size_t tommy_arrayblkof_size ( const tommy_arrayblkof *  array)

Gets the initialized size of the array.

Parameters
arrayThe array.
Returns
The initialized size of the array.

◆ tommy_arrayblkof_capacity()

tommy_size_t tommy_arrayblkof_capacity ( const tommy_arrayblkof *  array)

Gets the number of elements that fit without further allocation.

Parameters
arrayThe array.
Returns
The capacity of the array.

◆ tommy_arrayblkof_swap()

void tommy_arrayblkof_swap ( tommy_arrayblkof *  first,
tommy_arrayblkof *  second 
)

Exchanges two initialized arrays without copying their elements.

Element references remain valid and belong to the other array. Passing the same array twice has no effect.

Parameters
firstThe first array.
secondThe second array.
Note
This operation is O(1).

◆ tommy_arrayblkof_foreach()

TOMMY_API void tommy_arrayblkof_foreach ( tommy_arrayblkof *  array,
tommy_foreach_func *  func 
)

Calls the specified function for each element in the array.

You cannot add or remove elements, nor change the size of the array, from inside the callback.

Elements are stored inside the array: passing free() as the callback is invalid. A callback may release resources owned by each element, but must not free the element itself. Call tommy_arrayblkof_clear() before reusing it, or tommy_arrayblkof_done() to release its storage.

Parameters
arrayThe array.
funcFunction to call with each element.

◆ tommy_arrayblkof_foreach_arg()

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.

Parameters
arrayThe array.
funcFunction to call with each element.
argArgument to pass to the function.

◆ tommy_arrayblkof_memory_usage()

TOMMY_API tommy_size_t tommy_arrayblkof_memory_usage ( const tommy_arrayblkof *  array)

Gets the size of allocated memory.

Parameters
arrayThe array.
Returns
Total memory allocated by the array, in bytes.