[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53440] trunk/blender/source/blender/ blenlib: Add BLI_buffer, an alternative to BLI_array

Nicholas Bishop nicholasbishop at gmail.com
Sun Dec 30 19:28:11 CET 2012


Revision: 53440
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53440
Author:   nicholasbishop
Date:     2012-12-30 18:28:10 +0000 (Sun, 30 Dec 2012)
Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/CMakeLists.txt

Added Paths:
-----------
    trunk/blender/source/blender/blenlib/BLI_buffer.h
    trunk/blender/source/blender/blenlib/intern/buffer.c

Added: trunk/blender/source/blender/blenlib/BLI_buffer.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_buffer.h	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_buffer.h	2012-12-30 18:28:10 UTC (rev 53440)
@@ -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

Modified: trunk/blender/source/blender/blenlib/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/blenlib/CMakeLists.txt	2012-12-30 18:27:33 UTC (rev 53439)
+++ trunk/blender/source/blender/blenlib/CMakeLists.txt	2012-12-30 18:28:10 UTC (rev 53440)
@@ -48,6 +48,7 @@
 	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 @@
 	BLI_array.h
 	BLI_bitmap.h
 	BLI_blenlib.h
+	BLI_buffer.h
 	BLI_boxpack2d.h
 	BLI_callbacks.h
 	BLI_cpu.h

Added: trunk/blender/source/blender/blenlib/intern/buffer.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/buffer.c	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/buffer.c	2012-12-30 18:28:10 UTC (rev 53440)
@@ -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));
+}




More information about the Bf-blender-cvs mailing list