[Bf-blender-cvs] [a53b2b45648] functions: allow tuple stack allocation in C

Jacques Lucke noreply at git.blender.org
Sun Mar 10 13:14:30 CET 2019


Commit: a53b2b45648ddccf670867ea186bbc70bd616136
Author: Jacques Lucke
Date:   Sun Mar 10 12:06:51 2019 +0100
Branches: functions
https://developer.blender.org/rBa53b2b45648ddccf670867ea186bbc70bd616136

allow tuple stack allocation in C

===================================================================

M	source/blender/functions/FN-C.h
M	source/blender/functions/backends/tuple_call/tuple.hpp
M	source/blender/functions/c_wrapper.cpp
M	source/blender/modifiers/intern/MOD_functiondeform.c

===================================================================

diff --git a/source/blender/functions/FN-C.h b/source/blender/functions/FN-C.h
index 8e33109b5d4..4d6c33175fd 100644
--- a/source/blender/functions/FN-C.h
+++ b/source/blender/functions/FN-C.h
@@ -2,6 +2,7 @@
 #define __FUNCTIONS_H__
 
 #include "BLI_utildefines.h"
+#include "BLI_alloca.h"
 #include "DNA_node_types.h"
 
 #ifdef __cplusplus
@@ -58,6 +59,24 @@ float FN_tuple_get_float(FnTuple tuple, uint index);
 int32_t FN_tuple_get_int32(FnTuple tuple, uint index);
 void FN_tuple_get_float_vector_3(FnTuple tuple, uint index, float dst[3]);
 
+uint fn_tuple_stack_prepare_size(FnTupleCallBody body);
+void fn_tuple_prepare_stack(
+	FnTupleCallBody body,
+	void *buffer,
+	FnTuple *fn_in,
+	FnTuple *fn_out);
+
+void fn_tuple_destruct(FnTuple tuple);
+
+#define FN_TUPLE_CALL_PREPARE_STACK(body, fn_in, fn_out) \
+	FnTuple fn_in, fn_out; \
+	void *fn_in##_##fn_out##_buffer = alloca(fn_tuple_stack_prepare_size(body)); \
+	fn_tuple_prepare_stack(body, fn_in##_##fn_out##_buffer, &fn_in, &fn_out);
+
+#define FN_TUPLE_CALL_DESTRUCT_STACK(fn_in, fn_out) \
+	fn_tuple_destruct(fn_in); \
+	fn_tuple_destruct(fn_out);
+
 
 /*************** Dependencies ****************/
 
diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index 59ee1ad75ff..d8563d9f7b4 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -45,6 +45,8 @@ namespace FN {
 			return m_total_size;
 		}
 
+		inline uint total_stack_size() const;
+
 		uint element_amount() const
 		{
 			return m_types.size();
@@ -297,6 +299,11 @@ namespace FN {
 		SharedTupleMeta m_meta;
 	};
 
+	inline uint TupleMeta::total_stack_size() const
+	{
+		return sizeof(Tuple) + this->total_data_size() + this->element_amount();
+	}
+
 } /* namespace FN */
 
 #define FN_TUPLE_STACK_ALLOC(name, meta_expr) \
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index 687d7867993..251d49955fc 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -138,6 +138,42 @@ void FN_tuple_free(FnTuple tuple)
 	delete unwrap(tuple);
 }
 
+uint fn_tuple_stack_prepare_size(FnTupleCallBody body_)
+{
+	TupleCallBody *body = unwrap(body_);
+	return body->meta_in()->total_stack_size() + body->meta_out()->total_stack_size();
+}
+
+static Tuple *init_tuple(SharedTupleMeta &meta, void *buffer)
+{
+	char *buf = (char *)buffer;
+	char *tuple_buf = buf + 0;
+	char *data_buf = buf + sizeof(Tuple);
+	char *init_buf = data_buf + meta->total_data_size();
+	return new(tuple_buf) Tuple(meta, data_buf, (bool *)init_buf, false);
+}
+
+void fn_tuple_prepare_stack(
+	FnTupleCallBody body_,
+	void *buffer,
+	FnTuple *fn_in_,
+	FnTuple *fn_out_)
+{
+	TupleCallBody *body = unwrap(body_);
+	char *buf = (char *)buffer;
+	char *buf_in = buf + 0;
+	char *buf_out = buf + body->meta_in()->total_stack_size();
+	Tuple *fn_in = init_tuple(body->meta_in(), buf_in);
+	Tuple *fn_out = init_tuple(body->meta_out(), buf_out);
+	*fn_in_ = wrap(fn_in);
+	*fn_out_ = wrap(fn_out);
+}
+
+void fn_tuple_destruct(FnTuple tuple)
+{
+	unwrap(tuple)->~Tuple();
+}
+
 void FN_tuple_set_float(FnTuple tuple, uint index, float value)
 {
 	unwrap(tuple)->set<float>(index, value);
diff --git a/source/blender/modifiers/intern/MOD_functiondeform.c b/source/blender/modifiers/intern/MOD_functiondeform.c
index 832e2e74689..2a7092b9ed3 100644
--- a/source/blender/modifiers/intern/MOD_functiondeform.c
+++ b/source/blender/modifiers/intern/MOD_functiondeform.c
@@ -80,8 +80,7 @@ static void do_deformation(
 	FnTupleCallBody body = FN_tuple_call_get(fn);
 	BLI_assert(body);
 
-	FnTuple fn_in = FN_tuple_for_input(body);
-	FnTuple fn_out = FN_tuple_for_output(body);
+	FN_TUPLE_CALL_PREPARE_STACK(body, fn_in, fn_out);
 
 	clock_t start = clock();
 
@@ -98,8 +97,7 @@ static void do_deformation(
 	clock_t end = clock();
 	printf("Time taken: %f s\n", (float)(end - start) / (float)CLOCKS_PER_SEC);
 
-	FN_tuple_free(fn_in);
-	FN_tuple_free(fn_out);
+	FN_TUPLE_CALL_DESTRUCT_STACK(fn_in, fn_out);
 	FN_function_free(fn);
 }



More information about the Bf-blender-cvs mailing list