1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 /** \file
20  * \ingroup bli
21  */
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef struct BLI_Buffer {
28   void *data;
29   const size_t elem_size;
30   size_t count, alloc_count;
31   int flag;
32 } BLI_Buffer;
33 
34 enum {
35   BLI_BUFFER_NOP = 0,
36   BLI_BUFFER_USE_STATIC = (1 << 0),
37 };
38 
39 #define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
40   char name_##user; /* warn for free only */ \
41   type_ name_##_static_[static_count_]; \
42   BLI_Buffer name_ = { \
43       (name_##_static_), sizeof(type_), 0, static_count_, BLI_BUFFER_USE_STATIC | (flag_)}
44 
45 /* never use static*/
46 #define BLI_buffer_declare(type_, name_, flag_) \
47   bool name_##user; /* warn for free only */ \
48   BLI_Buffer name_ = {NULL, sizeof(type_), 0, 0, (flag_)}
49 
50 #define BLI_buffer_at(buffer_, type_, index_) \
51   ((((type_ *)(buffer_) \
52          ->data)[(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
53                  (BLI_assert((int)(index_) >= 0 && (size_t)(index_) < (buffer_)->count)), \
54                  index_]))
55 
56 #define BLI_buffer_array(buffer_, type_) (&(BLI_buffer_at(buffer_, type_, 0)))
57 
58 #define BLI_buffer_resize_data(buffer_, type_, new_count_) \
59   ((BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
60 
61 #define BLI_buffer_reinit_data(buffer_, type_, new_count_) \
62   ((BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
63 
64 #define BLI_buffer_append(buffer_, type_, val_) \
65   (BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
66    (BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_))
67 
68 #define BLI_buffer_clear(buffer_) \
69   { \
70     (buffer_)->count = 0; \
71   } \
72   (void)0
73 
74 /* Never decreases the amount of memory allocated */
75 void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count);
76 
77 /* Ensure size, throwing away old data, respecting BLI_BUFFER_USE_CALLOC */
78 void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count);
79 
80 /* Does not free the buffer structure itself */
81 void _bli_buffer_free(BLI_Buffer *buffer);
82 #define BLI_buffer_free(name_) \
83   { \
84     _bli_buffer_free(name_); \
85     (void)name_##user; /* ensure we free */ \
86   } \
87   (void)0
88 
89 /* A buffer embedded in a struct. Using memcpy is allowed until first resize. */
90 #define BLI_buffer_field_init(name_, type_) \
91   { \
92     memset(name_, 0, sizeof(*name_)); \
93     *(size_t *)&((name_)->elem_size) = sizeof(type_); \
94   } \
95   (void)0
96 
97 #define BLI_buffer_field_free(name_) _bli_buffer_free(name_)
98 
99 #ifdef __cplusplus
100 }
101 #endif
102