diff options
-rw-r--r-- | source/blender/blenlib/BLI_buffer.h | 69 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/buffer.c | 58 |
3 files changed, 129 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_buffer.h b/source/blender/blenlib/BLI_buffer.h new file mode 100644 index 00000000000..7c14736ec40 --- /dev/null +++ b/source/blender/blenlib/BLI_buffer.h @@ -0,0 +1,69 @@ +/* + * ***** 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: + * + * { + * BLI_buffer_declare(int, my_int_array, 32); + * + * 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; + int using_static; +} BLI_Buffer; + +#define BLI_buffer_declare(type_, name_, static_count_) \ + type_ *name_ ## _static_[static_count_]; \ + BLI_Buffer name_ = {name_ ## _static_, \ + sizeof(type_), \ + 0, \ + static_count_, \ + TRUE} + +#define BLI_buffer_at(buffer_, type_, index_) \ + (((type_*)(buffer_)->data)[index_]) + +#define BLI_buffer_append(buffer_, type_, val_) \ + BLI_buffer_resize(buffer_, (buffer_)->count + 1); \ + BLI_buffer_at(buffer_, type_, (buffer_)->count - 1) = val_ + +/* Never decreases the amount of memory allocated */ +void BLI_buffer_resize(BLI_Buffer *buffer, int new_count); + +/* Does not free the buffer structure itself */ +void BLI_buffer_free(BLI_Buffer *buffer); + +#endif diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 6644c58611f..49e3db0e62c 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -48,6 +48,7 @@ set(SRC intern/BLI_mempool.c intern/DLRB_tree.c intern/boxpack2d.c + intern/buffer.c intern/callbacks.c intern/cpu.c intern/dynlib.c @@ -96,6 +97,7 @@ set(SRC BLI_array.h BLI_bitmap.h BLI_blenlib.h + BLI_buffer.h BLI_boxpack2d.h BLI_callbacks.h BLI_cpu.h diff --git a/source/blender/blenlib/intern/buffer.c b/source/blender/blenlib/intern/buffer.c new file mode 100644 index 00000000000..98151c8eebf --- /dev/null +++ b/source/blender/blenlib/intern/buffer.c @@ -0,0 +1,58 @@ +/* + * ***** 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 ***** + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_buffer.h" +#include "BLI_utildefines.h" + +#include <string.h> + +void BLI_buffer_resize(BLI_Buffer *buffer, int new_count) +{ + if (new_count > buffer->alloc_count) { + if (buffer->using_static) { + void *orig = buffer->data; + buffer->data = MEM_callocN(buffer->elem_size * new_count, + "BLI_Buffer.data"); + memcpy(buffer->data, orig, buffer->elem_size * buffer->count); + buffer->alloc_count = new_count; + buffer->using_static = FALSE; + } + else { + if (new_count < buffer->alloc_count * 2) + buffer->alloc_count *= 2; + else + buffer->alloc_count = new_count; + buffer->data = MEM_reallocN(buffer->data, + (buffer->elem_size * + buffer->alloc_count)); + } + } + + buffer->count = new_count; +} + +void BLI_buffer_free(BLI_Buffer *buffer) +{ + if (!buffer->using_static) + MEM_freeN(buffer->data); + memset(buffer, 0, sizeof(*buffer)); +} |