changes to BLI_buffer
[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 #include "MEM_guardedalloc.h"
22
23 #include "BLI_buffer.h"
24 #include "BLI_utildefines.h"
25
26 #include <string.h>
27
28 static void *buffer_alloc(BLI_Buffer *buffer, int len)
29 {
30         return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
31                 MEM_callocN : MEM_mallocN)
32                 (buffer->elem_size * len, "BLI_Buffer.data");
33 }
34
35 static void *buffer_realloc(BLI_Buffer *buffer, int len)
36 {
37         return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
38                 MEM_recallocN : MEM_reallocN)
39                 (buffer->data, (buffer->elem_size * len));
40 }
41
42 void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
43 {
44         if (new_count > buffer->alloc_count) {
45                 if (buffer->flag & BLI_BUFFER_USE_STATIC) {
46                         void *orig = buffer->data;
47
48                         buffer->data = buffer_alloc(buffer, new_count);
49                         memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
50                         buffer->alloc_count = new_count;
51                         buffer->flag &= ~BLI_BUFFER_USE_STATIC;
52                 }
53                 else {
54                         if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
55                                 buffer->alloc_count *= 2;
56                         }
57                         else {
58                                 buffer->alloc_count = new_count;
59                         }
60
61                         if (buffer->data) {
62                                 buffer->data = buffer_realloc(buffer, buffer->alloc_count);
63                         }
64                         else {
65                                 buffer->data = buffer_alloc(buffer, buffer->alloc_count);
66                         }
67                 }
68         }
69
70         buffer->count = new_count;
71 }
72
73 /* callers use BLI_buffer_free */
74 void _bli_buffer_free(BLI_Buffer *buffer)
75 {
76         if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
77                 if (buffer->data) {
78                         MEM_freeN(buffer->data);
79                 }
80         }
81         memset(buffer, 0, sizeof(*buffer));
82 }