Blender V4.5
buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
31
32#include <cstring>
33
34#include "MEM_guardedalloc.h"
35
36#include "BLI_buffer.h"
37#include "BLI_utildefines.h"
38
39#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
40
41static void *buffer_alloc(const BLI_Buffer *buffer, const size_t len)
42{
43 return MEM_malloc_arrayN(len, buffer->elem_size, "BLI_Buffer.data");
44}
45
46static void *buffer_realloc(BLI_Buffer *buffer, const size_t len)
47{
48 return MEM_reallocN_id(buffer->data, buffer->elem_size * len, "BLI_Buffer.data");
49}
50
51void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
52{
53 if (UNLIKELY(new_count > buffer->alloc_count)) {
54 if (buffer->flag & BLI_BUFFER_USE_STATIC) {
55 const void *orig = buffer->data;
56
57 buffer->data = buffer_alloc(buffer, new_count);
58 memcpy(buffer->data, orig, buffer->elem_size * buffer->count);
59 buffer->alloc_count = new_count;
61 }
62 else {
63 if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
64 buffer->alloc_count *= 2;
65 }
66 else {
67 buffer->alloc_count = new_count;
68 }
69
70 buffer->data = buffer_realloc(buffer, buffer->alloc_count);
71 }
72 }
73
74 buffer->count = new_count;
75}
76
77void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
78{
79 if (UNLIKELY(new_count > buffer->alloc_count)) {
80 if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
81 if (buffer->data) {
82 MEM_freeN(buffer->data);
83 }
84 }
85
86 if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) {
87 buffer->alloc_count *= 2;
88 }
89 else {
90 buffer->alloc_count = new_count;
91 }
92
94 buffer->data = buffer_alloc(buffer, buffer->alloc_count);
95 }
96
97 buffer->count = new_count;
98}
99
100void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
101{
102 size_t size = buffer->count;
103 BLI_buffer_resize(buffer, size + count);
104
105 uint8_t *bytes = (uint8_t *)buffer->data;
106 memcpy(bytes + size * buffer->elem_size, new_data, count * buffer->elem_size);
107}
108
110{
111 if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) {
112 if (buffer->data) {
113 MEM_freeN(buffer->data);
114 }
115 }
116 memset(buffer, 0, sizeof(*buffer));
117}
@ BLI_BUFFER_USE_STATIC
Definition BLI_buffer.h:22
#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.cc:51
static void * buffer_alloc(const BLI_Buffer *buffer, const size_t len)
Definition buffer.cc:41
static void * buffer_realloc(BLI_Buffer *buffer, const size_t len)
Definition buffer.cc:46
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
Definition buffer.cc:77
void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
Definition buffer.cc:100
void _bli_buffer_free(BLI_Buffer *buffer)
Definition buffer.cc:109
int count
void *(* MEM_reallocN_id)(void *vmemh, size_t len, const char *str)
Definition mallocn.cc:40
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
size_t elem_size
Definition BLI_buffer.h:15
void * data
Definition BLI_buffer.h:14
size_t alloc_count
Definition BLI_buffer.h:16
size_t count
Definition BLI_buffer.h:16
uint len