BLI_math_rotation: properly name the quaternion power function.
[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 MEM_mallocN(buffer->elem_size * len, "BLI_Buffer.data");
60 }
61
62 static void *buffer_realloc(BLI_Buffer *buffer, const size_t len)
63 {
64         return MEM_reallocN_id(buffer->data, buffer->elem_size * len, "BLI_Buffer.data");
65 }
66
67 void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
68 {
69         if (UNLIKELY(new_count > buffer->alloc_count)) {
70                 if (buffer->flag & BLI_BUFFER_USE_STATIC) {
71                         void *orig = buffer->data;
72
73                         buffer->data = buffer_alloc(buffer, new_count);
74                         memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
75                         buffer->alloc_count = new_count;
76                         buffer->flag &= ~BLI_BUFFER_USE_STATIC;
77                 }
78                 else {
79                         if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
80                                 buffer->alloc_count *= 2;
81                         }
82                         else {
83                                 buffer->alloc_count = new_count;
84                         }
85
86                         buffer->data = buffer_realloc(buffer, buffer->alloc_count);
87                 }
88         }
89
90         buffer->count = new_count;
91 }
92
93 /**
94  * Similar to #BLI_buffer_resize, but use when the existing data can be:
95  * - Ignored (malloc'd)
96  * - Cleared (when BLI_BUFFER_USE_CALLOC is set)
97  */
98 void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
99 {
100         if (UNLIKELY(new_count > buffer->alloc_count)) {
101                 if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
102                         if (buffer->data) {
103                                 MEM_freeN(buffer->data);
104                         }
105                 }
106
107                 if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
108                         buffer->alloc_count *= 2;
109                 }
110                 else {
111                         buffer->alloc_count = new_count;
112                 }
113
114                 buffer->flag &= ~BLI_BUFFER_USE_STATIC;
115                 buffer->data = buffer_alloc(buffer, buffer->alloc_count);
116         }
117
118         buffer->count = new_count;
119 }
120
121 /* callers use BLI_buffer_free */
122 void _bli_buffer_free(BLI_Buffer *buffer)
123 {
124         if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
125                 if (buffer->data) {
126                         MEM_freeN(buffer->data);
127                 }
128         }
129         memset(buffer, 0, sizeof(*buffer));
130 }