naev 0.12.5
array.h
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
36#pragma once
37
39#include <assert.h>
40#include <stdalign.h>
41#include <stddef.h>
43
44#include "attributes.h"
45
46#define ARRAY_SENTINEL 0x15bada55
47
51typedef struct {
52#if DEBUG_ARRAYS
53 int _sentinel;
54#endif /* DEBUG_ARRAYS */
55 size_t _reserved;
56 size_t _size;
57 char alignas( max_align_t ) _array[];
59
60void *_array_create_helper( size_t e_size, size_t initial_size );
61void *_array_grow_helper( void **a, size_t e_size );
62void _array_resize_helper( void **a, size_t e_size, size_t new_size );
63void _array_erase_helper( void **a, size_t e_size, void *first, void *last );
64void _array_shrink_helper( void **a, size_t e_size );
65void _array_free_helper( void *a );
66void *_array_copy_helper( size_t e_size, void *a );
67
75{
76 assert( "NULL array!" && ( a != NULL ) );
77
79
80#if DEBUG_ARRAYS
81 assert( "Sentinel not found. Use array_create() to create the array." &&
82 ( c->_sentinel == ARRAY_SENTINEL ) );
83#endif /* DEBUG_ARRAYS */
84
85 return c;
86}
87
93#define array_create( basic_type ) \
94 ( (basic_type *)( _array_create_helper( sizeof( basic_type ), 1 ) ) )
95
102#define array_create_size( basic_type, capacity ) \
103 ( (basic_type *)( _array_create_helper( sizeof( basic_type ), capacity ) ) )
104// NOLINTBEGIN(bugprone-sizeof-expression)
113#define array_resize( ptr_array, new_size ) \
114 ( _array_resize_helper( (void **)( ptr_array ), \
115 sizeof( ( ptr_array )[0][0] ), new_size ) )
116// NOLINTEND(bugprone-sizeof-expression)
122#define array_grow( ptr_array ) \
123 ( *(__typeof__( ( ptr_array )[0] ))_array_grow_helper( \
124 (void **)( ptr_array ), \
125 sizeof( ( ptr_array )[0][0] ) ) ) // NOLINT (bugprone-sizeof-expression)
126
134#define array_push_back( ptr_array, element ) \
135 do \
136 array_grow( ptr_array ) = element; \
137 while ( 0 )
138// NOLINTBEGIN(bugprone-sizeof-expression)
148#define array_erase( ptr_array, first, last ) \
149 ( _array_erase_helper( (void **)( ptr_array ), \
150 sizeof( ( ptr_array )[0][0] ), (void *)( first ), \
151 (void *)( last ) ) )
152// NOLINTEND(bugprone-sizeof-expression)
160#define array_shrink( ptr_array ) \
161 ( _array_shrink_helper( (void **)( ptr_array ), \
162 sizeof( ( ptr_array )[0][0] ) ) ) // NOLINT
163
170#define array_free( ptr_array ) _array_free_helper( (void *)( ptr_array ) )
171
179ALWAYS_INLINE static inline int array_size( const void *array )
180{
181 const _private_container *c1 = array;
182
183 if ( c1 == NULL )
184 return 0;
185
186#if DEBUG_ARRAYS
187 assert( "Sentinel not found. Use array_create() to create the array." &&
188 ( c1[-1]._sentinel == ARRAY_SENTINEL ) );
189#endif /* DEBUG_ARRAYS */
190
191 return c1[-1]._size;
192}
193
199#define array_reserved( array ) ( _array_private_container( array )->_reserved )
206#define array_begin( array ) ( array )
214#define array_end( array ) ( ( array ) + array_size( array ) )
221#define array_front( ptr_array ) ( *array_begin( ptr_array ) )
228#define array_back( ptr_array ) ( *( array_end( ptr_array ) - 1 ) )
230#define array_copy( basic_type, ptr_array ) \
231 ( (basic_type *)( _array_copy_helper( sizeof( basic_type ), \
232 (void *)( ptr_array ) ) ) )
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
static _private_container * _array_private_container(void *a)
Gets the container of an array.
Definition array.h:74
#define ARRAY_SENTINEL
Definition array.h:46
static const double c[]
Definition rng.c:256
Private container type for the arrays.
Definition array.h:51
char _array[]
Definition array.h:57
size_t _reserved
Definition array.h:55