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