Blender V4.3
buffer.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
32#include <string.h>
33
34#include "MEM_guardedalloc.h"
35
36#include "BLI_buffer.h"
37#include "BLI_utildefines.h"
38
39#include "BLI_strict_flags.h" /* Keep last. */
40
41static void *buffer_alloc(const BLI_Buffer *buffer, const size_t len)
42{
43 return MEM_mallocN(buffer->elem_size * len, "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;
60 buffer->flag &= ~BLI_BUFFER_USE_STATIC;
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
93 buffer->flag &= ~BLI_BUFFER_USE_STATIC;
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:24
#define UNLIKELY(x)
Read Guarded memory(de)allocation.
void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count)
Definition buffer.c:51
static void * buffer_alloc(const BLI_Buffer *buffer, const size_t len)
Definition buffer.c:41
static void * buffer_realloc(BLI_Buffer *buffer, const size_t len)
Definition buffer.c:46
void BLI_buffer_reinit(BLI_Buffer *buffer, const size_t new_count)
Definition buffer.c:77
void _bli_buffer_append_array(BLI_Buffer *buffer, void *new_data, size_t count)
Definition buffer.c:100
void _bli_buffer_free(BLI_Buffer *buffer)
Definition buffer.c:109
int len
int count
void *(* MEM_reallocN_id)(void *vmemh, size_t len, const char *str)
Definition mallocn.cc:40
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
unsigned char uint8_t
Definition stdint.h:78
size_t elem_size
Definition BLI_buffer.h:17
void * data
Definition BLI_buffer.h:16
size_t alloc_count
Definition BLI_buffer.h:18
size_t count
Definition BLI_buffer.h:18