diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-10-04 06:14:28 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-10-04 06:29:27 +0300 |
commit | 74a2fa309555da9b76abdc205cec43909872fc20 (patch) | |
tree | 2bb8a658c2282b7af1b8738278530822152047c3 /source/blender/blenlib/intern/buffer.c | |
parent | 9f046e95c0a5c4b842d7405111ebab90e1ba63b6 (diff) |
BLI_Buffer: add BLI_buffer_reinit
Useful for re-using a buffer when the existing data can be thrown away.
Diffstat (limited to 'source/blender/blenlib/intern/buffer.c')
-rw-r--r-- | source/blender/blenlib/intern/buffer.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/buffer.c b/source/blender/blenlib/intern/buffer.c index 7588a6ec14d..6ab57221a63 100644 --- a/source/blender/blenlib/intern/buffer.c +++ b/source/blender/blenlib/intern/buffer.c @@ -102,6 +102,37 @@ void BLI_buffer_resize(BLI_Buffer *buffer, const size_t new_count) buffer->count = new_count; } +/** + * Similar to #BLI_buffer_resize, but use when the existing data can be: + * - Ignored (malloc'd) + * - Cleared (when BLI_BUFFER_USE_CALLOC is set) + */ +void BLI_buffer_reinit(BLI_Buffer *buffer, size_t new_count) +{ + if (UNLIKELY(new_count > buffer->alloc_count)) { + if ((buffer->flag & BLI_BUFFER_USE_STATIC) == 0) { + MEM_freeN(buffer->data); + } + + if (buffer->alloc_count && (new_count < buffer->alloc_count * 2)) { + buffer->alloc_count *= 2; + } + else { + buffer->alloc_count = new_count; + } + + buffer->data = buffer_alloc(buffer, new_count); + } + else { + if (buffer->flag & BLI_BUFFER_USE_CALLOC) { + memset(buffer->data, 0, + buffer->elem_size * new_count); + } + } + + buffer->count = new_count; +} + /* callers use BLI_buffer_free */ void _bli_buffer_free(BLI_Buffer *buffer) { |