[Bf-blender-cvs] [2a87cfd809a] functions: allocate function bodies within function
Jacques Lucke
noreply at git.blender.org
Thu May 2 11:45:01 CEST 2019
Commit: 2a87cfd809ad8901eb85a95a7546d04ce142707e
Author: Jacques Lucke
Date: Thu May 2 11:39:42 2019 +0200
Branches: functions
https://developer.blender.org/rB2a87cfd809ad8901eb85a95a7546d04ce142707e
allocate function bodies within function
===================================================================
M source/blender/functions/backends/dependencies/fgraph_dependencies.cpp
M source/blender/functions/backends/llvm/fgraph_ir_generation.cpp
M source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
M source/blender/functions/backends/llvm/ir_to_tuple_call.cpp
M source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
M source/blender/functions/backends/tuple_call/lazy_to_normal.cpp
M source/blender/functions/core/function.hpp
M source/blender/functions/core/type.hpp
M source/blender/functions/frontends/data_flow_nodes/inserters.cpp
M source/blender/functions/functions/auto_vectorization.cpp
M source/blender/functions/functions/lists.cpp
M source/blender/functions/functions/object_input.cpp
M source/blender/functions/functions/random.cpp
M source/blender/functions/functions/ranges.cpp
M source/blender/functions/functions/scalar_math.cpp
M source/blender/functions/functions/simple_conversions.cpp
M source/blender/functions/functions/switch.cpp
M source/blender/functions/functions/vectors.cpp
===================================================================
diff --git a/source/blender/functions/backends/dependencies/fgraph_dependencies.cpp b/source/blender/functions/backends/dependencies/fgraph_dependencies.cpp
index 6ec86b21aab..fd0cdff2883 100644
--- a/source/blender/functions/backends/dependencies/fgraph_dependencies.cpp
+++ b/source/blender/functions/backends/dependencies/fgraph_dependencies.cpp
@@ -24,7 +24,7 @@ class FGraphDependencies : public DependenciesBody {
void fgraph_add_DependenciesBody(SharedFunction &fn, FunctionGraph &fgraph)
{
- fn->add_body(new FGraphDependencies(fgraph));
+ fn->add_body<FGraphDependencies>(fgraph);
}
} // namespace FN
diff --git a/source/blender/functions/backends/llvm/fgraph_ir_generation.cpp b/source/blender/functions/backends/llvm/fgraph_ir_generation.cpp
index 9e6576c0123..b732cb5f2b7 100644
--- a/source/blender/functions/backends/llvm/fgraph_ir_generation.cpp
+++ b/source/blender/functions/backends/llvm/fgraph_ir_generation.cpp
@@ -214,7 +214,7 @@ class BuildGraphIR : public LLVMBuildIRBody {
void fgraph_add_LLVMBuildIRBody(SharedFunction &fn, FunctionGraph &fgraph)
{
- fn->add_body(new BuildGraphIR(fgraph));
+ fn->add_body<BuildGraphIR>(fgraph);
}
} /* namespace FN */
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 0b36d6d4763..02f65897dfa 100644
--- a/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
+++ b/source/blender/functions/backends/llvm/ir_for_tuple_call.cpp
@@ -178,7 +178,7 @@ void derive_LLVMBuildIRBody_from_TupleCallBody(SharedFunction &fn)
BLI_assert(fn->has_body<TupleCallBody>());
BLI_assert(!fn->has_body<LLVMBuildIRBody>());
- fn->add_body(new TupleCallLLVM(fn->body<TupleCallBody>()));
+ fn->add_body<TupleCallLLVM>(fn->body<TupleCallBody>());
}
} /* namespace FN */
diff --git a/source/blender/functions/backends/llvm/ir_to_tuple_call.cpp b/source/blender/functions/backends/llvm/ir_to_tuple_call.cpp
index bfc6058c712..4360681189a 100644
--- a/source/blender/functions/backends/llvm/ir_to_tuple_call.cpp
+++ b/source/blender/functions/backends/llvm/ir_to_tuple_call.cpp
@@ -120,13 +120,14 @@ class LLVMTupleCall : public TupleCallBody {
}
};
-static TupleCallBody *compile_ir_to_tuple_call(SharedFunction &fn, llvm::LLVMContext &context)
+static std::unique_ptr<CompiledLLVM> compile_ir_to_tuple_call(SharedFunction &fn,
+ llvm::LLVMContext &context)
{
llvm::Module *module = new llvm::Module(fn->name(), context);
llvm::Function *function = insert_tuple_call_function(fn, module);
auto compiled = CompiledLLVM::FromIR(module, function);
- return new LLVMTupleCall(std::move(compiled));
+ return compiled;
}
void derive_TupleCallBody_from_LLVMBuildIRBody(SharedFunction &fn)
@@ -135,7 +136,7 @@ void derive_TupleCallBody_from_LLVMBuildIRBody(SharedFunction &fn)
BLI_assert(!fn->has_body<TupleCallBody>());
auto *context = aquire_llvm_context();
- fn->add_body(compile_ir_to_tuple_call(fn, *context));
+ fn->add_body<LLVMTupleCall>(compile_ir_to_tuple_call(fn, *context));
release_llvm_context(context);
}
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 9c06ff727e2..aa4ef14d319 100644
--- a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
+++ b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
@@ -607,7 +607,7 @@ class ExecuteFGraph_Simple : public TupleCallBody {
void fgraph_add_TupleCallBody(SharedFunction &fn, FunctionGraph &fgraph)
{
try_ensure_tuple_call_bodies(fgraph.graph());
- fn->add_body(new ExecuteFGraph(fgraph));
+ fn->add_body<ExecuteFGraph>(fgraph);
}
} /* namespace FN */
diff --git a/source/blender/functions/backends/tuple_call/lazy_to_normal.cpp b/source/blender/functions/backends/tuple_call/lazy_to_normal.cpp
index 6860cda1c66..c4c4de97ef2 100644
--- a/source/blender/functions/backends/tuple_call/lazy_to_normal.cpp
+++ b/source/blender/functions/backends/tuple_call/lazy_to_normal.cpp
@@ -30,7 +30,7 @@ void derive_TupleCallBody_from_LazyInTupleCallBody(SharedFunction &fn)
BLI_assert(fn->has_body<LazyInTupleCallBody>());
BLI_assert(!fn->has_body<TupleCallBody>());
- fn->add_body(new MakeEagerBody(fn->body<LazyInTupleCallBody>()));
+ fn->add_body<MakeEagerBody>(fn->body<LazyInTupleCallBody>());
}
} /* namespace FN */
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index e1e5e34f762..5d552647e1e 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -32,11 +32,6 @@ class FunctionBody {
{
return m_owner;
}
-
- bool has_owner()
- {
- return m_owner != nullptr;
- }
};
class Function final : public RefCountedBase {
@@ -69,24 +64,34 @@ class Function final : public RefCountedBase {
return m_signature;
}
- template<typename T> inline T *body() const
+ template<typename T> inline bool has_body() const
{
- return m_bodies.get<T>();
+ std::lock_guard<std::mutex> lock(m_body_mutex);
+ static_assert(std::is_base_of<FunctionBody, T>::value, "");
+ return this->m_bodies.has<T>();
}
- template<typename T> void add_body(T *body)
+ template<typename T> inline T *body() const
{
+ std::lock_guard<std::mutex> lock(m_body_mutex);
static_assert(std::is_base_of<FunctionBody, T>::value, "");
- BLI_assert(m_bodies.get<T>() == nullptr);
- BLI_assert(!body->has_owner());
- m_bodies.add(body);
- body->set_owner(this);
+ return m_bodies.get<T>();
}
- template<typename T> inline bool has_body() const
+ template<typename T, typename... Args> bool add_body(Args &&... args)
{
+ std::lock_guard<std::mutex> lock(m_body_mutex);
static_assert(std::is_base_of<FunctionBody, T>::value, "");
- return this->body<T>() != nullptr;
+
+ if (m_bodies.has<T>()) {
+ return false;
+ }
+ else {
+ T *new_body = new T(std::forward<Args>(args)...);
+ new_body->set_owner(this);
+ m_bodies.add(new_body);
+ return true;
+ }
}
void print() const;
@@ -95,6 +100,7 @@ class Function final : public RefCountedBase {
const std::string m_name;
Signature m_signature;
Composition m_bodies;
+ mutable std::mutex m_body_mutex;
};
using SharedFunction = AutoRefCount<Function>;
diff --git a/source/blender/functions/core/type.hpp b/source/blender/functions/core/type.hpp
index 983863646d4..8a66e44ba3c 100644
--- a/source/blender/functions/core/type.hpp
+++ b/source/blender/functions/core/type.hpp
@@ -22,6 +22,10 @@ class TypeExtension {
}
public:
+ virtual ~TypeExtension()
+ {
+ }
+
Type *owner() const
{
return m_owner;
@@ -43,6 +47,7 @@ class Type final : public RefCountedBase {
template<typename T> bool has_extension() const
{
std::lock_guard<std::mutex> lock(m_extension_mutex);
+ static_assert(std::is_base_of<TypeExtension, T>::value, "");
return m_extensions.has<T>();
}
@@ -52,6 +57,7 @@ class Type final : public RefCountedBase {
* Since extensions can't be removed, it might be
* to access existing extensions without a lock. */
std::lock_guard<std::mutex> lock(m_extension_mutex);
+ static_assert(std::is_base_of<TypeExtension, T>::value, "");
return m_extensions.get<T>();
}
diff --git a/source/blender/functions/frontends/data_flow_nodes/inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
index fab7df6538a..dd85622e0b3 100644
--- a/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
@@ -134,7 +134,7 @@ DFGB_SocketVector GraphInserters::insert_sockets(BTreeGraphBuilder &builder, BSo
}
auto fn = SharedFunction::New("Input Sockets", Signature({}, outputs));
- fn->add_body(new SocketLoaderBody(builder.btree(), bsockets, loaders));
+ fn->add_body<SocketLoaderBody>(builder.btree(), bsockets, loaders);
DFGB_Node *node = builder.insert_function(fn);
DFGB_SocketVector sockets;
diff --git a/source/blender/functions/functions/auto_vectorization.cpp b/source/blender/functions/functions/auto_vectorization.cpp
index 8a01c447f78..65017011cc4 100644
--- a/source/blender/functions/functions/auto_vectorization.cpp
+++ b/source/blender/functions/functions/auto_vectorization.cpp
@@ -405,8 +405,8 @@ SharedFunction to_vectorized_function(SharedFunction &original_fn,
std::string name = original_fn->name() + " (Vectorized)";
auto fn = SharedFunction::New(name, Signature(inputs, outputs));
- // fn->add_body(new AutoVectorization(original_fn, vectorize_input));
- fn->add_body(new AutoVectorizationGen(original_fn, vectorize_input));
+ // fn->add_body<AutoVectorization>(original_fn, vectorize_input);
+ fn->add_body<AutoVectorizationGen>(original_fn, vectorize_input);
return fn;
}
diff --git a/source/blender/functions/functions/lists.cpp b/source/blender/functions/functions/lists.cpp
index 3b81c2aeb4b..126bf074f75 100644
--- a/source/blender/functions/functions/lists.cpp
+++ b/source/blender/functions/functions/lists.cpp
@@ -26,7 +26,7 @@ SharedFunction build_create_empty_list_function(SharedType &base_type, SharedTyp
{
OutputParameter("List", list_type),
}));
- fn->add_body(new CreateEmptyList<T>());
+ fn->add_body<CreateEmptyList<T>>();
return fn;
}
@@ -53,7 +53,7 @@ SharedFunction build_create_single_element_list_function(SharedType &base_type,
{
OutputParameter("List", list_type),
}));
- fn->add_body(new CreateSingleElementList<T>());
+ fn->add_body<CreateSingleElementList<T>>();
return fn;
}
@@ -83,7 +83,7 @@ SharedFunction build_append_function(SharedType &base_type, SharedType &list_typ
{
OutputParameter("List", list_type
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list