[Bf-blender-cvs] [3bca19b38ab] functions: separate owned resources container from function
Jacques Lucke
noreply at git.blender.org
Thu Sep 26 18:08:05 CEST 2019
Commit: 3bca19b38ab6f5819ef9a7f30281ee9b9bb5bf29
Author: Jacques Lucke
Date: Thu Sep 26 17:23:16 2019 +0200
Branches: functions
https://developer.blender.org/rB3bca19b38ab6f5819ef9a7f30281ee9b9bb5bf29
separate owned resources container from function
===================================================================
A source/blender/blenlib/BLI_owned_resources.h
M source/blender/functions/core/function.cpp
M source/blender/functions/core/function.hpp
===================================================================
diff --git a/source/blender/blenlib/BLI_owned_resources.h b/source/blender/blenlib/BLI_owned_resources.h
new file mode 100644
index 00000000000..efd226b7077
--- /dev/null
+++ b/source/blender/blenlib/BLI_owned_resources.h
@@ -0,0 +1,51 @@
+#ifndef __BLI_OWNED_RESOURCES_H__
+#define __BLI_OWNED_RESOURCES_H__
+
+#include "BLI_vector.h"
+#include "BLI_utility_mixins.h"
+
+namespace BLI {
+
+class OwnedResources : NonCopyable {
+ private:
+ struct ResourceData {
+ void *data;
+ void (*free)(void *data);
+ const char *name;
+ };
+
+ Vector<ResourceData> m_resources;
+
+ public:
+ OwnedResources() = default;
+
+ ~OwnedResources()
+ {
+ for (int i = m_resources.size() - 1; i >= 0; i--) {
+ ResourceData &data = m_resources[i];
+ data.free(data.data);
+ }
+ }
+
+ /**
+ * Add another object that will be freed when this container is freed. Objects are freed in
+ * reverse order.
+ */
+ template<typename T> void add(std::unique_ptr<T> resource, const char *name)
+ {
+ BLI_assert(resource.get() != nullptr);
+
+ ResourceData data;
+ data.name = name;
+ data.data = static_cast<void *>(resource.release());
+ data.free = [](void *data) {
+ T *typed_data = static_cast<T *>(data);
+ delete typed_data;
+ };
+ m_resources.append(data);
+ }
+};
+
+} // namespace BLI
+
+#endif /* __BLI_OWNED_RESOURCES_H__ */
diff --git a/source/blender/functions/core/function.cpp b/source/blender/functions/core/function.cpp
index c9a0382532e..f2f042015df 100644
--- a/source/blender/functions/core/function.cpp
+++ b/source/blender/functions/core/function.cpp
@@ -30,11 +30,6 @@ Function::~Function()
}
}
- for (int i = m_resources.size() - 1; i >= 0; i--) {
- OwnedResource &resource = m_resources[i];
- resource.free(resource.data);
- }
-
MEM_freeN((void *)m_strings);
}
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index a4bcd5c9322..3d41c379339 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -23,6 +23,7 @@
#include "BLI_chained_strings.h"
#include "BLI_utility_mixins.h"
+#include "BLI_owned_resources.h"
#include "type.hpp"
@@ -167,15 +168,8 @@ class Function final : public RefCounter {
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;
-
+ OwnedResources m_resources;
const char *m_strings;
};
@@ -297,15 +291,7 @@ inline ArrayRef<Type *> Function::output_types() const
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);
+ m_resources.add(std::move(resource), name);
}
/* Function Body inline functions
More information about the Bf-blender-cvs
mailing list