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