2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* ***** END GPL LICENSE BLOCK *****
21
#include "MEM_guardedalloc.h"
23
#include "BLI_buffer.h"
24
#include "BLI_utildefines.h"
28
static void *buffer_alloc(BLI_Buffer *buffer, int len)
30
return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
31
MEM_callocN : MEM_mallocN)
32
(buffer->elem_size * len, "BLI_Buffer.data");
35
static void *buffer_realloc(BLI_Buffer *buffer, int len)
37
return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ?
38
MEM_recallocN : MEM_reallocN)
39
(buffer->data, (buffer->elem_size * len));
42
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count)
44
if (new_count > buffer->alloc_count) {
45
if (buffer->flag & BLI_BUFFER_USE_STATIC) {
46
void *orig = buffer->data;
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;
54
if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
55
buffer->alloc_count *= 2;
58
buffer->alloc_count = new_count;
62
buffer->data = buffer_realloc(buffer, buffer->alloc_count);
65
buffer->data = buffer_alloc(buffer, buffer->alloc_count);
70
buffer->count = new_count;
73
void BLI_buffer_free(BLI_Buffer *buffer)
75
if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
77
MEM_freeN(buffer->data);
80
memset(buffer, 0, sizeof(*buffer));