CMagic  0.5.0
Portable C library of utilities and data structures
vector.h
Go to the documentation of this file.
1 
8 #ifndef CMAGIC_VECTOR_H
9 #define CMAGIC_VECTOR_H
10 
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include "cmagic/utils.h"
15 #include "cmagic/memory.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 void **
22 cmagic_vector_new(size_t member_size, const cmagic_memory_alloc_packet_t *alloc_packet);
23 
24 void
25 cmagic_vector_free(void **vector_ptr);
26 
27 bool
28 cmagic_vector_allocate_back(void **vector_ptr);
29 
30 bool
31 cmagic_vector_push_back(void **vector_ptr, const void *new_element_ptr);
32 
33 void
34 cmagic_vector_pop_back(void **vector_ptr);
35 
36 size_t
37 cmagic_vector_size(void **vector_ptr);
38 
40 cmagic_vector_get_alloc_packet(void **vector_ptr);
41 
46 #define CMAGIC_VECTOR(type) type**
47 
53 #define CMAGIC_VECTOR_DATA(cmagic_vector) (*(cmagic_vector))
54 
61 #define CMAGIC_VECTOR_BACK(cmagic_vector) (assert(CMAGIC_VECTOR_SIZE(cmagic_vector) > 0), \
62  CMAGIC_VECTOR_DATA(cmagic_vector) + CMAGIC_VECTOR_SIZE(cmagic_vector) - 1)
63 
71 #define CMAGIC_VECTOR_NEW(type, alloc_packet) \
72  ((CMAGIC_VECTOR(type))cmagic_vector_new(sizeof(type), (alloc_packet)))
73 
79 #define CMAGIC_VECTOR_FREE(cmagic_vector) cmagic_vector_free((void**)(cmagic_vector))
80 
88 #define CMAGIC_VECTOR_ALLOCATE_BACK(cmagic_vector) \
89  cmagic_vector_allocate_back((void**)(cmagic_vector))
90 
99 #define CMAGIC_VECTOR_PUSH_BACK(cmagic_vector, new_element_ptr) \
100  (CMAGIC_UTILS_ASSERT_SAME_TYPE(**(cmagic_vector), *(new_element_ptr)), \
101  cmagic_vector_push_back((void**)(cmagic_vector), (new_element_ptr)))
102 
108 #define CMAGIC_VECTOR_POP_BACK(cmagic_vector) cmagic_vector_pop_back((void**)(cmagic_vector))
109 
115 #define CMAGIC_VECTOR_SIZE(cmagic_vector) cmagic_vector_size((void**)(cmagic_vector))
116 
123 #define CMAGIC_VECTOR_GET_ALLOC_PACKET(cmagic_vector) \
124  cmagic_vector_get_alloc_packet((void**)(cmagic_vector))
125 
126 #ifdef __cplusplus
127 } // extern "C"
128 #endif
129 
130 #endif /* CMAGIC_VECTOR_H */
Portable substitutes of standard malloc() and free() functions.
Set of allocation functions. Used in some CMagic structures to specify a desired memory pool...
Definition: memory.h:187
General purpose utilities.