C Container Collection (CCC)
Loading...
Searching...
No Matches
impl_buffer.h
1#ifndef CCC_IMPL_BUFFER_H
2#define CCC_IMPL_BUFFER_H
3
5#include <assert.h>
6#include <stddef.h>
9#include "../types.h"
10
11/* NOLINTBEGIN(readability-identifier-naming) */
12
14struct ccc_buf_
15{
16 void *mem_;
17 size_t elem_sz_;
18 size_t sz_;
19 size_t capacity_;
20 ccc_alloc_fn *alloc_;
21 void *aux_;
22};
23
25#define IMPL_BUF_NON_IMPL_BUF_DEFAULT_SIZE(...) __VA_ARGS__
27#define IMPL_BUF_DEFAULT_SIZE(...) 0
29#define IMPL_BUF_OPTIONAL_SIZE(...) \
30 __VA_OPT__(IMPL_BUF_NON_)##IMPL_BUF_DEFAULT_SIZE(__VA_ARGS__)
31
33#define ccc_impl_buf_init(mem, alloc_fn, aux_data, capacity, ...) \
34 { \
35 .mem_ = (mem), \
36 .elem_sz_ = sizeof(*(mem)), \
37 .sz_ = IMPL_BUF_OPTIONAL_SIZE(__VA_ARGS__), \
38 .capacity_ = (capacity), \
39 .alloc_ = (alloc_fn), \
40 .aux_ = (aux_data), \
41 }
42
44#define ccc_impl_buf_emplace(ccc_buf_ptr, index, type_initializer...) \
45 (__extension__({ \
46 typeof(type_initializer) *buf_res_ = NULL; \
47 __auto_type i_ = (index); \
48 __auto_type emplace_buff_ptr_ = (ccc_buf_ptr); \
49 assert(sizeof(typeof(*buf_res_)) \
50 == ccc_buf_elem_size(emplace_buff_ptr_)); \
51 buf_res_ = ccc_buf_at(emplace_buff_ptr_, index); \
52 if (buf_res_) \
53 { \
54 *buf_res_ = type_initializer; \
55 } \
56 buf_res_; \
57 }))
58
60#define ccc_impl_buf_emplace_back(ccc_buf_ptr, type_initializer...) \
61 (__extension__({ \
62 typeof(type_initializer) *buf_res_ = NULL; \
63 __auto_type emplace_back_buf_ptr_ = (ccc_buf_ptr); \
64 assert(sizeof(typeof(*buf_res_)) \
65 == ccc_buf_elem_size(emplace_back_buf_ptr_)); \
66 buf_res_ = ccc_buf_alloc_back((emplace_back_buf_ptr_)); \
67 if (buf_res_) \
68 { \
69 *buf_res_ = type_initializer; \
70 } \
71 buf_res_; \
72 }))
73
74/* NOLINTEND(readability-identifier-naming) */
75
76#endif /* CCC_IMPL_BUF_H */
void * ccc_alloc_fn(void *ptr, size_t size, void *aux)
An allocation function at the core of all containers.
Definition: types.h:283