e2172148d1edec69ac84e080fdc4c76703faaf2d
[blender.git] / source / blender / blenlib / intern / buffer.c
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 /** \file blender/blenlib/intern/buffer.c
22  *  \ingroup bli
23  *
24  * Primitive generic buffer library.
25  *
26  * - Automatically grow as needed.
27  *   (currently never shrinks).
28  * - Can be passed between functions.
29  * - Supports using stack memory by default,
30  *   falling back to heap as needed.
31  *
32  * Usage examples:
33  * \code{.c}
34  * BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
35  *
36  * BLI_buffer_append(my_int_array, int, 42);
37  * assert(my_int_array.count == 1);
38  * assert(BLI_buffer_at(my_int_array, int, 0) == 42);
39  *
40  * BLI_buffer_free(&my_int_array);
41  * \endcode
42  *
43  * \note this more or less fills same purpose as #BLI_array,
44  * but supports resizing the array outside of the function
45  * it was declared in.
46  */
47
48 #include <string.h>
49
50 #include "MEM_guardedalloc.h"
51
52 #include "BLI_buffer.h"
53 #include "BLI_utildefines.h"
54
55 #include "BLI_strict_flags.h"
56
57 static void *buffer_alloc(BLI_Buffer *buffer, const size_t len)
58 {
59         return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
60                 MEM_callocN : MEM_mallocN)
61                 (buffer->elem_size * len, "BLI_Buffer.data");
62 }
63
64 static void *buffer_realloc(BLI_Buffer *buffer, const size_t len)
65 {
66         return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
67                 MEM_recallocN_id : MEM_reallocN_id)
68                 (buffer->data, buffer->elem_size * len, "BLI_Buffer.data");
69 }
70
71 void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
72 {
73         if (UNLIKELY(new_count > buffer->alloc_count)) {
74                 if (buffer->flag & BLI_BUFFER_USE_STATIC) {
75                         void *orig = buffer->data;
76
77                         buffer->data = buffer_alloc(buffer, new_count);
78                         memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
79                         buffer->alloc_count = new_count;
80                         buffer->flag &= ~BLI_BUFFER_USE_STATIC;
81                 }
82                 else {
83                         if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
84                                 buffer->alloc_count *= 2;
85                         }
86                         else {
87                                 buffer->alloc_count = new_count;
88                         }
89
90                         buffer->data = buffer_realloc(buffer, buffer->alloc_count);
91                 }
92         }
93         else {
94                 if (buffer->flag & BLI_BUFFER_USE_CALLOC) {
95                         if (new_count > buffer->count) {
96                                 memset(POINTER_OFFSET(buffer->data, buffer->elem_size * buffer->count), 0,
97                                        buffer->elem_size * (new_count - buffer->count));
98                         }
99                 }
100         }
101
102         buffer->count = new_count;
103 }
104
105 /**
106  * Similar to #BLI_buffer_resize, but use when the existing data can be:
107  * - Ignored (malloc'd)
108  * - Cleared (when BLI_BUFFER_USE_CALLOC is set)
109  */
110 void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
111 {
112         if (UNLIKELY(new_count > buffer->alloc_count)) {
113                 if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
114                         if (buffer->data) {
115                                 MEM_freeN(buffer->data);
116                         }
117                 }
118
119                 if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
120                         buffer->alloc_count *= 2;
121                 }
122                 else {
123                         buffer->alloc_count = new_count;
124                 }
125
126                 buffer->data = buffer_alloc(buffer, buffer->alloc_count);
127         }
128         else {
129                 if (buffer->flag & BLI_BUFFER_USE_CALLOC) {
130                         memset(buffer->data, 0,
131                                buffer->elem_size * new_count);
132                 }
133         }
134
135         buffer->count = new_count;
136 }
137
138 /* callers use BLI_buffer_free */
139 void _bli_buffer_free(BLI_Buffer *buffer)
140 {
141         if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
142                 if (buffer->data) {
143                         MEM_freeN(buffer->data);
144                 }
145         }
146         memset(buffer, 0, sizeof(*buffer));
147 }