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