Blender V4.3
BLI_buffer.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15typedef struct BLI_Buffer {
16 void *data;
17 size_t elem_size;
19 int flag;
21
22enum {
25};
26
27#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
28 char name_##user; /* warn for free only */ \
29 type_ name_##_static_[static_count_]; \
30 BLI_Buffer name_ = { \
31 (name_##_static_), sizeof(type_), 0, static_count_, BLI_BUFFER_USE_STATIC | (flag_)}
32
33/* Never use static. */
34#define BLI_buffer_declare(type_, name_, flag_) \
35 bool name_##user; /* warn for free only */ \
36 BLI_Buffer name_ = {NULL, sizeof(type_), 0, 0, (flag_)}
37
38#define BLI_buffer_at(buffer_, type_, index_) \
39 ((((type_ *)(buffer_) \
40 ->data)[((BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
41 (BLI_assert((int)(index_) >= 0 && (size_t)(index_) < (buffer_)->count)), \
42 index_)]))
43
44#define BLI_buffer_array(buffer_, type_) (&(BLI_buffer_at(buffer_, type_, 0)))
45
46#define BLI_buffer_resize_data(buffer_, type_, new_count_) \
47 ((BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
48
49#define BLI_buffer_reinit_data(buffer_, type_, new_count_) \
50 ((BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
51
52#define BLI_buffer_append(buffer_, type_, val_) \
53 ((BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
54 (BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_)))
55
56#define BLI_buffer_clear(buffer_) \
57 { \
58 (buffer_)->count = 0; \
59 } \
60 (void)0
61
65void BLI_buffer_resize(BLI_Buffer *buffer, size_t new_count);
66
74void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count);
75
81void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count);
82#define BLI_buffer_append_array(buffer_, type_, data_, count_) \
83 { \
84 type_ *__tmp = (data_); \
85 BLI_assert(sizeof(type_) == (buffer_)->elem_size); \
86 _bli_buffer_append_array(buffer_, __tmp, count_); \
87 } \
88 (void)0
89
95void _bli_buffer_free(BLI_Buffer *buffer);
96#define BLI_buffer_free(name_) \
97 { \
98 _bli_buffer_free(name_); \
99 (void)name_##user; /* ensure we free */ \
100 } \
101 (void)0
102
106#define BLI_buffer_field_init(name_, type_) \
107 { \
108 memset((void *)name_, 0, sizeof(*name_)); \
109 *(size_t *)&((name_)->elem_size) = sizeof(type_); \
110 } \
111 (void)0
112
113#define BLI_buffer_field_free(name_) _bli_buffer_free(name_)
114
115#ifdef __cplusplus
116}
117#endif
void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count)
Definition buffer.c:77
@ BLI_BUFFER_NOP
Definition BLI_buffer.h:23
@ BLI_BUFFER_USE_STATIC
Definition BLI_buffer.h:24
void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
Definition buffer.c:100
struct BLI_Buffer BLI_Buffer
void _bli_buffer_free(BLI_Buffer *buffer)
Definition buffer.c:109
void BLI_buffer_resize(BLI_Buffer *buffer, size_t new_count)
Definition buffer.c:51
int count
size_t elem_size
Definition BLI_buffer.h:17
void * data
Definition BLI_buffer.h:16
size_t alloc_count
Definition BLI_buffer.h:18
size_t count
Definition BLI_buffer.h:18