Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Bishop <nicholasbishop@gmail.com>2012-12-30 22:28:10 +0400
committerNicholas Bishop <nicholasbishop@gmail.com>2012-12-30 22:28:10 +0400
commit2c9d22fe31dc20df9fecc6322bdb7cbe56783164 (patch)
treeb08b5c39090f1b9384dd1272e9c128bd35b324d2 /source/blender/blenlib/BLI_buffer.h
parentfc442dbd51a5ef71757a1a179222f358b6efdc8c (diff)
Add BLI_buffer, an alternative to BLI_array
BLI_buffer is a dynamic homogeneous array similar to BLI_array, but it allocates a structure that can be passed around making it possible to resize the array outside the function it was declared in.
Diffstat (limited to 'source/blender/blenlib/BLI_buffer.h')
-rw-r--r--source/blender/blenlib/BLI_buffer.h69
1 files changed, 69 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