From 9f046e95c0a5c4b842d7405111ebab90e1ba63b6 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 4 Oct 2015 13:58:40 +1100 Subject: BLI_Buffer: support over 2gb allocations Also move comment to C-source and enumerate useful features. --- source/blender/blenlib/intern/buffer.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'source/blender/blenlib/intern/buffer.c') diff --git a/source/blender/blenlib/intern/buffer.c b/source/blender/blenlib/intern/buffer.c index ed47efd93eb..7588a6ec14d 100644 --- a/source/blender/blenlib/intern/buffer.c +++ b/source/blender/blenlib/intern/buffer.c @@ -20,30 +20,55 @@ /** \file blender/blenlib/intern/buffer.c * \ingroup bli + * + * Primitive generic buffer library. + * + * - Automatically grow as needed. + * (currently never shrinks). + * - Can be passed between functions. + * - Supports using stack memory by default, + * falling back to heap as needed. + * + * Usage examples: + * \code{.c} + * BLI_buffer_declare_static(int, my_int_array, BLI_BUFFER_NOP, 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); + * \endcode + * + * \note this more or less fills same purpose as #BLI_array, + * but supports resizing the array outside of the function + * it was declared in. */ +#include + #include "MEM_guardedalloc.h" #include "BLI_buffer.h" #include "BLI_utildefines.h" -#include +#include "BLI_strict_flags.h" -static void *buffer_alloc(BLI_Buffer *buffer, int len) +static void *buffer_alloc(BLI_Buffer *buffer, const size_t len) { return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ? MEM_callocN : MEM_mallocN) (buffer->elem_size * len, "BLI_Buffer.data"); } -static void *buffer_realloc(BLI_Buffer *buffer, int len) +static void *buffer_realloc(BLI_Buffer *buffer, const size_t len) { return ((buffer->flag & BLI_BUFFER_USE_CALLOC) ? MEM_recallocN_id : MEM_reallocN_id) (buffer->data, buffer->elem_size * len, "BLI_Buffer.data"); } -void BLI_buffer_resize(BLI_Buffer *buffer, int new_count) +void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count) { if (UNLIKELY(new_count > buffer->alloc_count)) { if (buffer->flag & BLI_BUFFER_USE_STATIC) { -- cgit v1.2.3