[Bf-blender-cvs] [05e3ee5ae6a] functions: automatically destruct stack allocated tuples in c++

Jacques Lucke noreply at git.blender.org
Sat Apr 6 18:47:23 CEST 2019


Commit: 05e3ee5ae6ae9dac2e53afb6b712f433ac7d864c
Author: Jacques Lucke
Date:   Sat Apr 6 18:05:34 2019 +0200
Branches: functions
https://developer.blender.org/rB05e3ee5ae6ae9dac2e53afb6b712f433ac7d864c

automatically destruct stack allocated tuples in c++

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

M	source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
M	source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
M	source/blender/functions/backends/tuple_call/tuple.hpp
M	source/blender/functions/backends/tuple_call/tuple_call-c.cpp
M	source/blender/functions/functions/auto_vectorization.cpp

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

diff --git a/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp b/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
index 5bfc9f844a9..fe268108b18 100644
--- a/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
+++ b/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
@@ -150,9 +150,9 @@ namespace FN {
 			/* Allocate temporary stack buffer for tuple input and output. */
 			auto &meta_in = m_tuple_call->meta_in();
 			auto &meta_out = m_tuple_call->meta_out();
-			llvm::Value *tuple_in_data_ptr = builder.CreateAllocaBytes_BytePtr(meta_in->total_data_size());
+			llvm::Value *tuple_in_data_ptr = builder.CreateAllocaBytes_BytePtr(meta_in->size_of_data());
 			tuple_in_data_ptr->setName("tuple_in_data");
-			llvm::Value *tuple_out_data_ptr = builder.CreateAllocaBytes_BytePtr(meta_out->total_data_size());
+			llvm::Value *tuple_out_data_ptr = builder.CreateAllocaBytes_BytePtr(meta_out->size_of_data());
 			tuple_out_data_ptr->setName("tuple_out_data");
 
 			/* Write input values into buffer. */
diff --git a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
index 6241207dd94..46d77c5208d 100644
--- a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
+++ b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
@@ -124,9 +124,9 @@ namespace FN {
 				BLI_assert(body != nullptr);
 
 				m_tuple_offsets.append(m_combined_tuples_size);
-				m_combined_tuples_size += body->meta_in()->total_size();
+				m_combined_tuples_size += body->meta_in()->size_of_full_tuple();
 				m_tuple_offsets.append(m_combined_tuples_size);
-				m_combined_tuples_size += body->meta_out()->total_size();
+				m_combined_tuples_size += body->meta_out()->size_of_full_tuple();
 
 				NodeTupleIndices indices;
 				indices.in = tuple_indices.size() * 2 + 0;
@@ -278,7 +278,7 @@ namespace FN {
 					{
 						auto meta = SharedTupleMeta::FromPointer(task.data.init_tuple.meta);
 						void *ptr = (char *)buffer + m_tuple_offsets[task.data.init_tuple.index];
-						Tuple::NewInBuffer(meta, ptr);
+						Tuple::ConstructInBuffer(meta, ptr);
 						meta.extract_ptr();
 						break;
 					}
@@ -410,9 +410,6 @@ namespace FN {
 				ctx.stack().pop();
 
 				Tuple::copy_element(tmp_out, socket.index(), out, out_index);
-
-				FN_TUPLE_STACK_FREE(tmp_in);
-				FN_TUPLE_STACK_FREE(tmp_out);
 			}
 		}
 	};
@@ -539,9 +536,6 @@ namespace FN {
 							fn_out, output_index,
 							temp_storage, socket_indices.lookup(output_socket));
 					}
-
-					FN_TUPLE_STACK_FREE(fn_in);
-					FN_TUPLE_STACK_FREE(fn_out);
 				}
 				else if (fn->has_body<TupleCallBody>()) {
 					auto *body = node->function()->body<TupleCallBody>();
@@ -570,9 +564,6 @@ namespace FN {
 								temp_storage, temp_index);
 						}
 					}
-
-					FN_TUPLE_STACK_FREE(fn_in);
-					FN_TUPLE_STACK_FREE(fn_out);
 				}
 				else {
 					BLI_assert(false);
diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index 92b83bcd170..46934ef9339 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -9,20 +9,23 @@ namespace FN {
 		SmallTypeVector m_types;
 		SmallVector<CPPTypeInfo *> m_type_info;
 		SmallVector<uint> m_offsets;
-		uint m_total_size;
+		uint m_size__data;
+		uint m_size__data_and_init;
 
 	public:
 		TupleMeta(const SmallTypeVector &types = {})
 			: m_types(types)
 		{
-			m_total_size = 0;
+			m_size__data = 0;
 			for (const SharedType &type : types) {
 				CPPTypeInfo *info = type->extension<CPPTypeInfo>();
-				m_offsets.append(m_total_size);
+				m_offsets.append(m_size__data);
 				m_type_info.append(info);
-				m_total_size += info->size_of_type();
+				m_size__data += info->size_of_type();
 			}
-			m_offsets.append(m_total_size);
+			m_offsets.append(m_size__data);
+
+			m_size__data_and_init = m_size__data + this->element_amount();
 		}
 
 		const SmallTypeVector &types() const
@@ -40,12 +43,22 @@ namespace FN {
 			return m_offsets;
 		}
 
-		uint total_data_size() const
+		uint size_of_data() const
+		{
+			return m_size__data;
+		}
+
+		uint size_of_init() const
 		{
-			return m_total_size;
+			return m_size__data_and_init - m_size__data;
 		}
 
-		inline uint total_size() const;
+		uint size_of_data_and_init() const
+		{
+			return m_size__data_and_init;
+		}
+
+		inline uint size_of_full_tuple() const;
 
 		uint element_amount() const
 		{
@@ -67,7 +80,7 @@ namespace FN {
 		{
 			m_initialized = (bool *)MEM_calloc_arrayN(
 				m_meta->element_amount(), sizeof(bool), __func__);
-			m_data = MEM_mallocN(m_meta->total_data_size(), __func__);
+			m_data = MEM_mallocN(m_meta->size_of_data(), __func__);
 			m_owns_mem = true;
 		}
 
@@ -89,14 +102,21 @@ namespace FN {
 			}
 		}
 
-		static Tuple &NewInBuffer(
+		Tuple(
+			SharedTupleMeta &meta,
+			void *buffer)
+			: Tuple(
+				meta,
+				buffer,
+				(bool *)buffer + meta->size_of_data(),
+				false,
+				false) {}
+
+		static Tuple &ConstructInBuffer(
 			SharedTupleMeta &meta,
 			void *buffer)
 		{
-			void *tuple_ptr = buffer;
-			void *data_ptr = (char *)buffer + sizeof(Tuple);
-			void *init_ptr = (char *)data_ptr + meta->total_data_size();
-			Tuple *tuple = new(tuple_ptr) Tuple(meta, data_ptr, (bool *)init_ptr, false);
+			Tuple *tuple = new(buffer) Tuple(meta, (char *)buffer + sizeof(Tuple));
 			return *tuple;
 		}
 
@@ -379,17 +399,14 @@ namespace FN {
 		SharedTupleMeta m_meta;
 	};
 
-	inline uint TupleMeta::total_size() const
+	inline uint TupleMeta::size_of_full_tuple() const
 	{
-		return sizeof(Tuple) + this->total_data_size() + this->element_amount();
+		return sizeof(Tuple) + this->size_of_data_and_init();
 	}
 
 } /* namespace FN */
 
 #define FN_TUPLE_STACK_ALLOC(name, meta_expr) \
 	SharedTupleMeta &name##_meta = (meta_expr); \
-	void *name##_buffer = alloca(name##_meta->total_size()); \
-	Tuple &name = Tuple::NewInBuffer(name##_meta, name##_buffer);
-
-#define FN_TUPLE_STACK_FREE(name) \
-	name.~Tuple();
+	void *name##_buffer = alloca(name##_meta->size_of_data_and_init()); \
+	Tuple name(name##_meta, name##_buffer);
diff --git a/source/blender/functions/backends/tuple_call/tuple_call-c.cpp b/source/blender/functions/backends/tuple_call/tuple_call-c.cpp
index f6058755b01..a31e036df26 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call-c.cpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call-c.cpp
@@ -50,7 +50,7 @@ void FN_tuple_free(FnTuple tuple)
 uint fn_tuple_stack_prepare_size(FnTupleCallBody body_)
 {
 	TupleCallBody *body = unwrap(body_);
-	return body->meta_in()->total_size() + body->meta_out()->total_size();
+	return body->meta_in()->size_of_full_tuple() + body->meta_out()->size_of_full_tuple();
 }
 
 void fn_tuple_prepare_stack(
@@ -62,9 +62,9 @@ void fn_tuple_prepare_stack(
 	TupleCallBody *body = unwrap(body_);
 	char *buf = (char *)buffer;
 	char *buf_in = buf + 0;
-	char *buf_out = buf + body->meta_in()->total_size();
-	Tuple::NewInBuffer(body->meta_in(), buf_in);
-	Tuple::NewInBuffer(body->meta_out(), buf_out);
+	char *buf_out = buf + body->meta_in()->size_of_full_tuple();
+	Tuple::ConstructInBuffer(body->meta_in(), buf_in);
+	Tuple::ConstructInBuffer(body->meta_out(), buf_out);
 	*fn_in_ = wrap((Tuple *)buf_in);
 	*fn_out_ = wrap((Tuple *)buf_out);
 }
diff --git a/source/blender/functions/functions/auto_vectorization.cpp b/source/blender/functions/functions/auto_vectorization.cpp
index 31cb2501d8c..9c3c426dd36 100644
--- a/source/blender/functions/functions/auto_vectorization.cpp
+++ b/source/blender/functions/functions/auto_vectorization.cpp
@@ -43,8 +43,8 @@ namespace FN { namespace Functions {
 				m_max_len_in_size = 0;
 				m_max_len_out_size = 0;
 				for (TupleCallBody *body : m_get_length_bodies) {
-					m_max_len_in_size = std::max(m_max_len_in_size, body->meta_in()->total_size());
-					m_max_len_out_size = std::max(m_max_len_out_size, body->meta_out()->total_size());
+					m_max_len_in_size = std::max(m_max_len_in_size, body->meta_in()->size_of_full_tuple());
+					m_max_len_out_size = std::max(m_max_len_out_size, body->meta_out()->size_of_full_tuple());
 				}
 
 				for (auto output : main->signature().outputs()) {
@@ -89,9 +89,6 @@ namespace FN { namespace Functions {
 					this->append_to_output(main_out, fn_out, i, ctx);
 				}
 			}
-
-			FN_TUPLE_STACK_FREE(main_in);
-			FN_TUPLE_STACK_FREE(main_out);
 		}
 
 	private:
@@ -104,8 +101,8 @@ namespace FN { namespace Functions {
 				uint index = m_list_inputs[i];
 				TupleCallBody *body = m_get_length_bodies[i];
 
-				Tuple &len_in = Tuple::NewInBuffer(body->meta_in(), buf_in);
-				Tuple &len_out = Tuple::NewInBuffer(body->meta_out(), buf_out);
+				Tuple &len_in = Tuple::ConstructInBuffer(body->meta_in(), buf_in);
+				Tuple &len_out = Tuple::ConstructInBuffer(body->meta_out(), buf_out);
 
 				Tuple::copy_element(fn_in, index, len_in, 0);
 
@@ -139,9 +136,6 @@ namespace FN { namespace Functions {
 			body->call__setup_stack(get_element_in, get_element_out, ctx);
 
 			Tuple::relocate_element(get_element_out, 0, main_in, index);
-
-			FN_TUPLE_STACK_FREE(get_element_in);
-			FN_TUPLE_STACK_FREE(get_element_out);
 		}
 
 		void initialize_empty_lists(Tuple &fn_out, ExecutionContext &ctx) const
@@ -161,9 +155,6 @@ namespace FN { namespace Functions {
 			body->call__setup_stack(create_list_in, create_list_out, ctx);
 
 			Tuple::relocate_element(create_list_out, 0, fn_out, index);
-
-			FN_TUPLE_STACK_FREE(create_list_in);
-			FN_TUPLE_STACK_FREE(create_list_out);
 		}
 
 		void append_to_output(Tuple &main_out, Tuple &fn_out, uint index, ExecutionContext &ctx) const
@@ -179,9 +170,6 @@ namespace FN { namespace Functions {
 			body->call__setup_stack(append_in, append_out, ctx);
 
 			Tuple::relocate_element(append_out, index, fn_out, index);
-
-			FN_TUPLE_STACK_FREE(append_in);
-			FN_TUPLE_STACK_FREE(append_out);
 		}
 	};



More information about the Bf-blender-cvs mailing list