Blender  V2.93
buffer.c
Go to the documentation of this file.
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 
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 
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_append_array. */
118 void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
119 {
120  size_t size = buffer->count;
122 
123  uint8_t *bytes = (uint8_t *)buffer->data;
124  memcpy(bytes + size * buffer->elem_size, new_data, count * buffer->elem_size);
125 }
126 
127 /* callers use BLI_buffer_free */
129 {
130  if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
131  if (buffer->data) {
132  MEM_freeN(buffer->data);
133  }
134  }
135  memset(buffer, 0, sizeof(*buffer));
136 }
@ BLI_BUFFER_USE_STATIC
Definition: BLI_buffer.h:36
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define UNLIKELY(x)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
Definition: buffer.c:63
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
Definition: buffer.c:94
void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
Definition: buffer.c:118
void _bli_buffer_free(BLI_Buffer *buffer)
Definition: buffer.c:128
static void * buffer_alloc(BLI_Buffer *buffer, const size_t len)
Definition: buffer.c:53
static void * buffer_realloc(BLI_Buffer *buffer, const size_t len)
Definition: buffer.c:58
int count
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_reallocN_id)(void *vmemh, size_t len, const char *str)
Definition: mallocn.c:43
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
unsigned char uint8_t
Definition: stdint.h:81
uint len