2012-12-30 19:28:10 +01:00
|
|
|
/*
|
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __BLI_BUFFER_H__
|
|
|
|
#define __BLI_BUFFER_H__
|
|
|
|
|
|
|
|
/* Note: this more or less fills same purpose as BLI_array, but makes
|
|
|
|
* it much easier to resize the array outside of the function it was
|
|
|
|
* declared in since */
|
|
|
|
|
|
|
|
/* Usage examples:
|
|
|
|
*
|
|
|
|
* {
|
2013-01-02 06:00:02 +01:00
|
|
|
* BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 32);
|
2012-12-30 19:28:10 +01:00
|
|
|
*
|
|
|
|
* BLI_buffer_append(my_int_array, int, 42);
|
|
|
|
* assert(my_int_array.count == 1);
|
|
|
|
* assert(BLI_buffer_at(my_int_array, int, 0) == 42);
|
|
|
|
*
|
|
|
|
* BLI_buffer_free(&my_int_array);
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *data;
|
|
|
|
const int elem_size;
|
|
|
|
int 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),
|
|
|
|
BLI_BUFFER_USE_CALLOC = (1 << 1), /* ensure the array is always calloc'd */
|
|
|
|
};
|
|
|
|
|
|
|
|
#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_ = { \
|
|
|
|
/* clear the static memory if this is a calloc'd array */ \
|
|
|
|
((void)((flag_ & BLI_BUFFER_USE_CALLOC) ? \
|
2013-08-07 05:55:21 +02:00
|
|
|
memset(name_ ## _static_, 0, sizeof(name_ ## _static_)) : NULL \
|
2013-01-02 06:00:02 +01:00
|
|
|
), /* memset-end */ \
|
|
|
|
name_ ## _static_), \
|
|
|
|
sizeof(type_), \
|
|
|
|
0, \
|
|
|
|
static_count_, \
|
|
|
|
BLI_BUFFER_USE_STATIC | flag_}
|
|
|
|
|
|
|
|
/* never use static*/
|
|
|
|
#define BLI_buffer_declare(type_, name_, flag_) \
|
2013-03-16 01:12:14 +01:00
|
|
|
bool name_ ## user; /* warn for free only */ \
|
2013-01-02 06:00:02 +01:00
|
|
|
BLI_Buffer name_ = {NULL, \
|
|
|
|
sizeof(type_), \
|
|
|
|
0, \
|
|
|
|
0, \
|
|
|
|
flag_}
|
2012-12-30 19:28:10 +01: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_)->data)[ \
|
|
|
|
(BLI_assert(sizeof(type_) == (buffer_)->elem_size)), \
|
|
|
|
(BLI_assert(index_ >= 0 && index_ < (buffer_)->count)), \
|
|
|
|
index_]))
|
|
|
|
|
|
|
|
#define BLI_buffer_array(buffer_, type_) ( \
|
|
|
|
&(BLI_buffer_at(buffer_, type_, 0)))
|
|
|
|
|
|
|
|
#define BLI_buffer_resize_data(buffer_, type_, new_count_) ( \
|
|
|
|
(BLI_buffer_resize(buffer_, new_count_), new_count_ ? BLI_buffer_array(buffer_, type_) : NULL))
|
|
|
|
|
|
|
|
|
2013-01-02 06:19:55 +01:00
|
|
|
|
|
|
|
#define BLI_buffer_append(buffer_, type_, val_) ( \
|
|
|
|
BLI_buffer_resize(buffer_, (buffer_)->count + 1), \
|
|
|
|
(BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_) \
|
|
|
|
)
|
2012-12-30 19:28:10 +01:00
|
|
|
|
|
|
|
/* Never decreases the amount of memory allocated */
|
|
|
|
void BLI_buffer_resize(BLI_Buffer *buffer, int new_count);
|
|
|
|
|
|
|
|
/* Does not free the buffer structure itself */
|
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
|
2012-12-30 19:28:10 +01:00
|
|
|
|
2013-01-02 06:00:02 +01:00
|
|
|
#endif /* __BLI_BUFFER_H__ */
|