2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-12-30 19:28:10 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2012-12-30 19:28:10 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2019-05-23 08:32:58 +02:00
|
|
|
typedef struct BLI_Buffer {
|
2012-12-30 19:28:10 +01:00
|
|
|
void *data;
|
2023-07-17 10:46:26 +02:00
|
|
|
size_t elem_size;
|
2015-10-04 04:58:40 +02:00
|
|
|
size_t count, alloc_count;
|
2013-01-02 06:00:02 +01:00
|
|
|
int flag;
|
2012-12-30 19:28:10 +01:00
|
|
|
} BLI_Buffer;
|
|
|
|
|
2013-01-02 06:00:02 +01:00
|
|
|
enum {
|
|
|
|
BLI_BUFFER_NOP = 0,
|
|
|
|
BLI_BUFFER_USE_STATIC = (1 << 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BLI_buffer_declare_static(type_, name_, flag_, static_count_) \
|
2013-03-16 01:12:14 +01:00
|
|
|
char name_##user; /* warn for free only */ \
|
2013-07-26 17:26:51 +02:00
|
|
|
type_ name_##_static_[static_count_]; \
|
2013-01-02 06:00:02 +01:00
|
|
|
BLI_Buffer name_ = { \
|
2015-12-23 15:52:54 +01:00
|
|
|
(name_##_static_), sizeof(type_), 0, static_count_, BLI_BUFFER_USE_STATIC | (flag_)}
|
2013-01-02 06:00:02 +01:00
|
|
|
|
2021-06-26 13:35:18 +02:00
|
|
|
/* Never use static. */
|
2013-01-02 06:00:02 +01:00
|
|
|
#define BLI_buffer_declare(type_, name_, flag_) \
|
2013-03-16 01:12:14 +01:00
|
|
|
bool name_##user; /* warn for free only */ \
|
2015-12-23 15:52:54 +01:00
|
|
|
BLI_Buffer name_ = {NULL, sizeof(type_), 0, 0, (flag_)}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-02 06:19:55 +01:00
|
|
|
#define BLI_buffer_at(buffer_, type_, index_) \
|
2013-03-16 01:12:14 +01:00
|
|
|
((((type_ *)(buffer_) \
|
2023-08-17 15:13:41 +02:00
|
|
|
->data)[((BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
|
|
|
|
(BLI_assert((int)(index_) >= 0 && (size_t)(index_) < (buffer_)->count)), \
|
|
|
|
index_)]))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-16 01:12:14 +01:00
|
|
|
#define BLI_buffer_array(buffer_, type_) (&(BLI_buffer_at(buffer_, type_, 0)))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-16 01:12:14 +01:00
|
|
|
#define BLI_buffer_resize_data(buffer_, type_, new_count_) \
|
|
|
|
((BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-10-04 05:14:28 +02:00
|
|
|
#define BLI_buffer_reinit_data(buffer_, type_, new_count_) \
|
|
|
|
((BLI_buffer_reinit(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-02 06:19:55 +01:00
|
|
|
#define BLI_buffer_append(buffer_, type_, val_) \
|
2023-08-17 15:13:41 +02:00
|
|
|
((BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
|
|
|
|
(BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_)))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-02-01 02:06:57 +01:00
|
|
|
#define BLI_buffer_clear(buffer_) \
|
|
|
|
{ \
|
2014-10-10 10:10:13 +02:00
|
|
|
(buffer_)->count = 0; \
|
|
|
|
} \
|
|
|
|
(void)0
|
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* \note Never decreases the amount of memory allocated.
|
|
|
|
*/
|
2022-01-12 12:49:36 +01:00
|
|
|
void BLI_buffer_resize(BLI_Buffer *buffer, size_t new_count);
|
2012-12-30 19:28:10 +01:00
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* Ensure size, throwing away old data, respecting #BLI_BUFFER_USE_CALLOC.
|
|
|
|
*
|
|
|
|
* Similar to #BLI_buffer_resize, but use when the existing data can be:
|
|
|
|
* - Ignored (malloc'd).
|
|
|
|
* - Cleared (when #BLI_BUFFER_USE_CALLOC is set).
|
|
|
|
*/
|
2022-01-12 12:49:36 +01:00
|
|
|
void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count);
|
2015-10-04 05:14:28 +02:00
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* Append an array of elements.
|
|
|
|
*
|
|
|
|
* Callers use #BLI_buffer_append_array.
|
|
|
|
*/
|
2020-11-23 16:26:54 +01:00
|
|
|
void _bli_buffer_append_array(BLI_Buffer *buffer, void *data, size_t count);
|
|
|
|
#define BLI_buffer_append_array(buffer_, type_, data_, count_) \
|
|
|
|
{ \
|
|
|
|
type_ *__tmp = (data_); \
|
|
|
|
BLI_assert(sizeof(type_) == (buffer_)->elem_size); \
|
|
|
|
_bli_buffer_append_array(buffer_, __tmp, count_); \
|
|
|
|
} \
|
|
|
|
(void)0
|
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* Does not free the buffer structure itself.
|
|
|
|
*
|
|
|
|
* Callers use #BLI_buffer_free.
|
|
|
|
*/
|
2013-03-16 01:12:14 +01:00
|
|
|
void _bli_buffer_free(BLI_Buffer *buffer);
|
|
|
|
#define BLI_buffer_free(name_) \
|
|
|
|
{ \
|
|
|
|
_bli_buffer_free(name_); \
|
|
|
|
(void)name_##user; /* ensure we free */ \
|
|
|
|
} \
|
|
|
|
(void)0
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* A buffer embedded in a struct. Using #memcpy is allowed until first resize.
|
|
|
|
*/
|
2019-10-26 10:06:19 +02:00
|
|
|
#define BLI_buffer_field_init(name_, type_) \
|
|
|
|
{ \
|
2023-07-17 10:46:26 +02:00
|
|
|
memset((void *)name_, 0, sizeof(*name_)); \
|
2019-10-26 10:06:19 +02:00
|
|
|
*(size_t *)&((name_)->elem_size) = sizeof(type_); \
|
|
|
|
} \
|
|
|
|
(void)0
|
|
|
|
|
|
|
|
#define BLI_buffer_field_free(name_) _bli_buffer_free(name_)
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|