[Bf-blender-cvs] [1a8bc27052b] functions: Use Blender's guarded allocators

Jacques Lucke noreply at git.blender.org
Tue Mar 26 16:56:37 CET 2019


Commit: 1a8bc27052ba76895a0ab8b4e2d55f1e98af00e9
Author: Jacques Lucke
Date:   Tue Mar 26 16:56:23 2019 +0100
Branches: functions
https://developer.blender.org/rB1a8bc27052ba76895a0ab8b4e2d55f1e98af00e9

Use Blender's guarded allocators

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

M	source/blender/blenlib/BLI_lazy_init.hpp
M	source/blender/blenlib/BLI_mempool.hpp
M	source/blender/blenlib/BLI_small_vector.hpp
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
M	source/blender/functions/backends/tuple_call/tuple.hpp

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

diff --git a/source/blender/blenlib/BLI_lazy_init.hpp b/source/blender/blenlib/BLI_lazy_init.hpp
index 1e7cb9af72f..a49b979ffe2 100644
--- a/source/blender/blenlib/BLI_lazy_init.hpp
+++ b/source/blender/blenlib/BLI_lazy_init.hpp
@@ -1,3 +1,9 @@
+/* These macros help to define functions that initialize
+ * some data the first time it is used.
+ * Currently the memory is freed after Blender prints information
+ * about not-freed blocks, which are false positive memory leaks.
+ * I'm not sure what the best solution to this is, yet. */
+
 #define LAZY_INIT__NO_ARG(final_ret_type, builder_ret_type, func_name) \
 	static builder_ret_type func_name##_impl(void); \
 	final_ret_type func_name(void) \
diff --git a/source/blender/blenlib/BLI_mempool.hpp b/source/blender/blenlib/BLI_mempool.hpp
index ee81389daeb..dfdeffa77df 100644
--- a/source/blender/blenlib/BLI_mempool.hpp
+++ b/source/blender/blenlib/BLI_mempool.hpp
@@ -19,7 +19,7 @@ namespace BLI {
 		~MemPool()
 		{
 			for (void *ptr : m_start_pointers) {
-				std::free(ptr);
+				MEM_freeN(ptr);
 			}
 		}
 
@@ -47,8 +47,7 @@ namespace BLI {
 		void allocate_more()
 		{
 			uint new_amount = 1 << (m_start_pointers.size() + 4);
-			uint byte_size = new_amount * m_element_size;
-			void *ptr = std::malloc(byte_size);
+			void *ptr = MEM_malloc_arrayN(new_amount, m_element_size, __func__);
 
 			for (uint i = 0; i < new_amount; i++) {
 				m_free_stack.push((char *)ptr + i * m_element_size);
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 8e86cb53861..025534351d2 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "BLI_utildefines.h"
+#include "MEM_guardedalloc.h"
 #include <cstdlib>
 #include <cstring>
 #include <memory>
@@ -244,7 +245,7 @@ namespace BLI {
 
 			m_capacity = min_capacity;
 
-			T *new_array = (T *)std::malloc(sizeof(T) * m_capacity);
+			T *new_array = (T *)MEM_malloc_arrayN(m_capacity, sizeof(T), __func__);
 			std::uninitialized_copy(
 				std::make_move_iterator(this->begin()),
 				std::make_move_iterator(this->end()),
@@ -253,7 +254,7 @@ namespace BLI {
 			this->destruct_elements_but_keep_memory();
 
 			if (!this->is_small()) {
-				std::free(m_elements);
+				MEM_freeN(m_elements);
 			}
 
 			m_elements = new_array;
@@ -265,7 +266,7 @@ namespace BLI {
 				m_elements = this->small_buffer();
 			}
 			else {
-				m_elements = (T *)std::malloc(sizeof(T) * other.m_capacity);
+				m_elements = (T *)MEM_malloc_arrayN(other.m_capacity, sizeof(T), __func__);
 			}
 
 			std::uninitialized_copy(other.begin(), other.end(), m_elements);
@@ -298,7 +299,7 @@ namespace BLI {
 			if (!this->is_small()) {
 				/* Can be nullptr when previously stolen. */
 				if (m_elements != nullptr) {
-					std::free(m_elements);
+					MEM_freeN(m_elements);
 				}
 			}
 		}
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 719a21d3cd0..6a9a19f6e56 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -6,6 +6,7 @@ set(INC
 	../blenkernel
 	../depsgraph
 	../windowmanager
+	../../../intern/guardedalloc
 )
 
 set(INC_SYS
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 9ebaf2575a5..a36dee7f13b 100644
--- a/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
+++ b/source/blender/functions/backends/tuple_call/fgraph_tuple_call.cpp
@@ -410,6 +410,9 @@ 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);
 			}
 		}
 	};
@@ -536,6 +539,9 @@ 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>();
@@ -561,6 +567,9 @@ 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 {
 					BLI_assert(false);
diff --git a/source/blender/functions/backends/tuple_call/tuple.hpp b/source/blender/functions/backends/tuple_call/tuple.hpp
index 506d517a17a..92b83bcd170 100644
--- a/source/blender/functions/backends/tuple_call/tuple.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple.hpp
@@ -65,8 +65,9 @@ namespace FN {
 		Tuple(SharedTupleMeta meta)
 			: m_meta(std::move(meta))
 		{
-			m_initialized = (bool *)std::calloc(m_meta->element_amount(), sizeof(bool));
-			m_data = std::malloc(m_meta->total_data_size());
+			m_initialized = (bool *)MEM_calloc_arrayN(
+				m_meta->element_amount(), sizeof(bool), __func__);
+			m_data = MEM_mallocN(m_meta->total_data_size(), __func__);
 			m_owns_mem = true;
 		}
 
@@ -109,8 +110,8 @@ namespace FN {
 		{
 			this->destruct_all();
 			if (m_owns_mem) {
-				std::free(m_data);
-				std::free(m_initialized);
+				MEM_freeN(m_data);
+				MEM_freeN(m_initialized);
 			}
 		}
 
@@ -389,3 +390,6 @@ namespace FN {
 	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();



More information about the Bf-blender-cvs mailing list