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:
Diffstat (limited to 'source/blender/blenkernel/BKE_utildefines.h')
-rw-r--r--source/blender/blenkernel/BKE_utildefines.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_utildefines.h b/source/blender/blenkernel/BKE_utildefines.h
index 4d43518901e..7d8cb41db82 100644
--- a/source/blender/blenkernel/BKE_utildefines.h
+++ b/source/blender/blenkernel/BKE_utildefines.h
@@ -193,5 +193,54 @@
#define SET_INT_IN_POINTER(i) ((void*)(intptr_t)(i))
#define GET_INT_FROM_POINTER(i) ((int)(intptr_t)(i))
+/*little array macro library. example of usage:
+
+int *arr = NULL;
+V_DECLARE(arr);
+int i;
+
+for (i=0; i<10; i++) {
+ V_GROW(arr);
+ arr[i] = something;
+}
+V_FREE(arr);
+
+arrays are buffered, using double-buffering (so on each reallocation,
+the array size is doubled). supposedly this should give good Big Oh
+behaviour, though it may not be the best in practice.
+*/
+
+#define V_DECLARE(vec) int _##vec##_count=0; void *_##vec##_tmp
+
+/*in the future, I plan on having V_DECLARE allocate stack memory it'll
+ use at first, and switch over to heap when it needs more. that'll mess
+ up cases where you'd want to use this API to build a dynamic list for
+ non-local use, so all such cases should use this macro.*/
+#define V_DYNDECLARE(vec) V_DECLARE(vec)
+
+/*this returns the entire size of the array, including any buffering.*/
+#define V_SIZE(vec) ((signed int)((vec)==NULL ? 0 : MEM_allocN_len(vec) / sizeof(*vec)))
+
+/*this returns the logical size of the array, not including buffering.*/
+#define V_COUNT(vec) _##vec##_count
+
+/*grow the array by one. zeroes the new elements.*/
+#define V_GROW(vec) \
+ V_SIZE(vec) > _##vec##_count ? _##vec##_count++ : \
+ ((_##vec##_tmp = MEM_callocN(sizeof(*vec)*(_##vec##_count*2+2), #vec " " __FILE__ " ")),\
+ (vec && memcpy(_##vec##_tmp, vec, sizeof(*vec) * _##vec##_count)),\
+ (vec && (MEM_freeN(vec),1)),\
+ (vec = _##vec##_tmp),\
+ _##vec##_count++)
+
+#define V_FREE(vec) if (vec) MEM_freeN(vec);
+
+/*resets the logical size of an array to zero, but doesn't
+ free the memory.*/
+#define V_RESET(vec) _##vec##_count=0
+
+/*set the count of the array*/
+#define V_SETCOUNT(vec, count) _##vec##_count = (count)
+
#endif