C Container Collection (CCC)
Loading...
Searching...
No Matches
impl_buffer.h
1
16#ifndef CCC_IMPL_BUFFER_H
17#define CCC_IMPL_BUFFER_H
18
20#include <assert.h>
21#include <stddef.h>
24#include "../types.h"
25
26/* NOLINTBEGIN(readability-identifier-naming) */
27
32struct ccc_buffer
33{
35 void *mem;
37 size_t count;
39 size_t capacity;
41 size_t sizeof_type;
43 ccc_any_alloc_fn *alloc;
45 void *aux;
46};
47
49#define IMPL_BUF_NON_IMPL_BUF_DEFAULT_SIZE(...) __VA_ARGS__
51#define IMPL_BUF_DEFAULT_SIZE(...) 0
53#define IMPL_BUF_OPTIONAL_SIZE(...) \
54 __VA_OPT__(IMPL_BUF_NON_)##IMPL_BUF_DEFAULT_SIZE(__VA_ARGS__)
55
60#define ccc_impl_buf_init(impl_mem, impl_alloc_fn, impl_aux_data, \
61 impl_capacity, ...) \
62 { \
63 .mem = (impl_mem), \
64 .sizeof_type = sizeof(*(impl_mem)), \
65 .count = IMPL_BUF_OPTIONAL_SIZE(__VA_ARGS__), \
66 .capacity = (impl_capacity), \
67 .alloc = (impl_alloc_fn), \
68 .aux = (impl_aux_data), \
69 }
70
72#define ccc_impl_buf_emplace(buf_ptr, index, type_initializer...) \
73 (__extension__({ \
74 typeof(type_initializer) *impl_buf_res = NULL; \
75 __auto_type impl_i = (index); \
76 __auto_type impl_emplace_buff_ptr = (buf_ptr); \
77 impl_buf_res = ccc_buf_at(impl_emplace_buff_ptr, index); \
78 if (impl_buf_res) \
79 { \
80 *impl_buf_res = type_initializer; \
81 } \
82 impl_buf_res; \
83 }))
84
86#define ccc_impl_buf_emplace_back(buf_ptr, type_initializer...) \
87 (__extension__({ \
88 typeof(type_initializer) *impl_buf_res = NULL; \
89 __auto_type impl_emplace_back_buf_ptr = (buf_ptr); \
90 assert(sizeof(typeof(*impl_buf_res)) \
91 == ccc_buf_sizeof_type(impl_emplace_back_buf_ptr)); \
92 impl_buf_res = ccc_buf_alloc_back((impl_emplace_back_buf_ptr)); \
93 if (impl_buf_res) \
94 { \
95 *impl_buf_res = type_initializer; \
96 } \
97 impl_buf_res; \
98 }))
99
100/* NOLINTEND(readability-identifier-naming) */
101
102#endif /* CCC_IMPL_BUF_H */
struct ccc_buffer ccc_buffer
A contiguous block of storage for elements of the same type.
Definition: buffer.h:65
void * ccc_any_alloc_fn(void *ptr, size_t size, void *aux)
An allocation function at the core of all containers.
Definition: types.h:312