[Bf-blender-cvs] [74fba99cc64] functions: initial resource storage for functions
Jacques Lucke
noreply at git.blender.org
Thu Sep 26 16:51:35 CEST 2019
Commit: 74fba99cc6470fdf50b80e198eb7c0316bdbdd4f
Author: Jacques Lucke
Date: Thu Sep 26 13:35:06 2019 +0200
Branches: functions
https://developer.blender.org/rB74fba99cc6470fdf50b80e198eb7c0316bdbdd4f
initial resource storage for functions
===================================================================
M source/blender/functions/core/function.cpp
M source/blender/functions/core/function.hpp
===================================================================
diff --git a/source/blender/functions/core/function.cpp b/source/blender/functions/core/function.cpp
index 30940272c61..c9a0382532e 100644
--- a/source/blender/functions/core/function.cpp
+++ b/source/blender/functions/core/function.cpp
@@ -24,12 +24,18 @@ Function::Function(StringRefNull name,
Function::~Function()
{
- MEM_freeN((void *)m_strings);
for (uint i = 0; i < ARRAY_SIZE(m_bodies); i++) {
if (m_bodies[i] != nullptr) {
delete m_bodies[i];
}
}
+
+ for (int i = m_resources.size() - 1; i >= 0; i--) {
+ OwnedResource &resource = m_resources[i];
+ resource.free(resource.data);
+ }
+
+ MEM_freeN((void *)m_strings);
}
void Function::print()
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index 7f11968e1be..611d4fbce04 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -19,10 +19,13 @@
* should be created using the corresponding builder class.
*/
-#include "type.hpp"
+#include <functional>
+
#include "BLI_chained_strings.h"
#include "BLI_utility_mixins.h"
+#include "type.hpp"
+
namespace FN {
class Function;
@@ -149,16 +152,30 @@ class Function final : public RefCounter {
*/
void print();
+ /**
+ * Add a resource that is owned by the function. All resources will be freed in reverse order.
+ */
+ template<typename T> void add_resource(std::unique_ptr<T> resource, const char *name);
+
private:
- StringRefNull m_name;
- std::mutex m_add_body_mutex;
FunctionBody *m_bodies[FunctionBody::BODY_TYPE_AMOUNT] = {0};
+ StringRefNull m_name;
+
Vector<StringRefNull> m_input_names;
Vector<Type *> m_input_types;
Vector<StringRefNull> m_output_names;
Vector<Type *> m_output_types;
+ struct OwnedResource {
+ void *data;
+ void (*free)(void *data);
+ const char *name;
+ };
+
+ Vector<OwnedResource, 1> m_resources;
+ std::mutex m_modify_mutex;
+
const char *m_strings;
};
@@ -193,7 +210,7 @@ template<typename T, typename... Args> inline T *Function::add_body(Args &&... a
{
STATIC_ASSERT_BODY_TYPE(T);
- std::lock_guard<std::mutex> lock(m_add_body_mutex);
+ std::lock_guard<std::mutex> lock(m_modify_mutex);
if (m_bodies[T::FUNCTION_BODY_ID] == nullptr) {
T *new_body = new T(std::forward<Args>(args)...);
new_body->set_owner(this);
@@ -270,6 +287,20 @@ inline ArrayRef<Type *> Function::output_types() const
return m_output_types;
}
+template<typename T> void Function::add_resource(std::unique_ptr<T> resource, const char *name)
+{
+ std::lock_guard<std::mutex> lock(m_modify_mutex);
+
+ OwnedResource owned_resource;
+ owned_resource.name = name;
+ owned_resource.data = static_cast<void *>(resource.release());
+ owned_resource.free = [](void *data) {
+ T *typed_data = static_cast<T *>(data);
+ delete typed_data;
+ };
+ m_resources.append(owned_resource);
+}
+
/* Function Body inline functions
********************************************/
More information about the Bf-blender-cvs
mailing list