Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Sharybin <sergey.vfx@gmail.com>2016-02-07 01:40:41 +0300
committerSergey Sharybin <sergey.vfx@gmail.com>2016-02-12 17:43:26 +0300
commitc8d2bc78902422c89607a5778857de958e3bb837 (patch)
tree73355a2912eb64be5ecbe52f2d42b1da60d10f0c /intern/cycles/util
parent28604c46a137c1288cc7a494b36ed72e44a0ab8b (diff)
Cycles: Always use guarded allocator of vectors
We don't have vectors re-allocation happening multiple times from inside a loop anymore, so we can safely switch to a memory guarded allocator for vectors and keep track on the memory usage at various stages of rendering. Additionally, when building from inside Blender repository, Cycles will use Blender's guarded allocator, so actual memory usage will be displayed in the Space Info header. There are couple of tricky aspects of the patch: - TaskScheduler::exit() now explicitly frees memory used by `threads`. This is needed because `threads` is a static member which destructor isn't getting called on Blender's exit which caused memory leak print to happen. This shouldn't give any measurable speed issues, reallocation of that vector is only one of fewzillion other allocations happening during synchronization. - Use regular guarded malloc (not aligned one). No idea why it was made to be aligned in the first place. Perhaps some corner case tests or so. Vector was never expected to be aligned anyway. Let's see if we'll have actual bugs with this. Reviewers: dingto, lukasstockner97, juicyfruit, brecht Reviewed By: brecht Differential Revision: https://developer.blender.org/D1774
Diffstat (limited to 'intern/cycles/util')
-rw-r--r--intern/cycles/util/CMakeLists.txt11
-rw-r--r--intern/cycles/util/util_guarded_allocator.h85
-rw-r--r--intern/cycles/util/util_task.cpp6
-rw-r--r--intern/cycles/util/util_task.h1
-rw-r--r--intern/cycles/util/util_vector.h15
5 files changed, 74 insertions, 44 deletions
diff --git a/intern/cycles/util/CMakeLists.txt b/intern/cycles/util/CMakeLists.txt
index 8367d21bfc6..d4f6a49953b 100644
--- a/intern/cycles/util/CMakeLists.txt
+++ b/intern/cycles/util/CMakeLists.txt
@@ -40,8 +40,10 @@ set(SRC_HEADERS
util_atomic.h
util_boundbox.h
util_debug.h
+ util_guarded_allocator.cpp
util_foreach.h
util_function.h
+ util_guarded_allocator.h
util_half.h
util_hash.h
util_image.h
@@ -77,15 +79,6 @@ set(SRC_HEADERS
util_xml.h
)
-if(WITH_CYCLES_DEBUG)
- list(APPEND SRC
- util_guarded_allocator.cpp
- )
- list(APPEND SRC_HEADERS
- util_guarded_allocator.h
- )
-endif()
-
include_directories(${INC})
include_directories(SYSTEM ${INC_SYS})
diff --git a/intern/cycles/util/util_guarded_allocator.h b/intern/cycles/util/util_guarded_allocator.h
index 2df717253e3..2cef1494369 100644
--- a/intern/cycles/util/util_guarded_allocator.h
+++ b/intern/cycles/util/util_guarded_allocator.h
@@ -17,17 +17,10 @@
#ifndef __UTIL_GUARDED_ALLOCATOR_H__
#define __UTIL_GUARDED_ALLOCATOR_H__
-/* Define this in order to use Blender's guarded allocator to keep
- * track of allocated buffers, their sizes and peak memory usage.
- *
- * This is usually a bad level call, but it's really handy to keep
- * track of overall peak memory consumption during the scene
- * synchronization step.
- */
-#undef WITH_BLENDER_GUARDEDALLOC
-
+#include <cstddef>
#include <memory>
+#include "util_debug.h"
#include "util_types.h"
#ifdef WITH_BLENDER_GUARDEDALLOC
@@ -42,39 +35,85 @@ void util_guarded_mem_free(size_t n);
/* Guarded allocator for the use with STL. */
template <typename T>
-class GuardedAllocator : public std::allocator<T> {
+class GuardedAllocator {
public:
- template<typename _Tp1>
- struct rebind {
- typedef GuardedAllocator<_Tp1> other;
- };
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+ typedef T *pointer;
+ typedef const T *const_pointer;
+ typedef T& reference;
+ typedef const T& const_reference;
+ typedef T value_type;
+
+ GuardedAllocator() {}
+ GuardedAllocator(const GuardedAllocator&) {}
T *allocate(size_t n, const void *hint = 0)
{
util_guarded_mem_alloc(n * sizeof(T));
-#ifdef WITH_BLENDER_GUARDEDALLOC
(void)hint;
- return (T*)MEM_mallocN_aligned(n * sizeof(T), 16, "Cycles Alloc");
+#ifdef WITH_BLENDER_GUARDEDALLOC
+ if(n == 0) {
+ return NULL;
+ }
+ return (T*)MEM_mallocN(n * sizeof(T), "Cycles Alloc");
#else
- return std::allocator<T>::allocate(n, hint);
+ return (T*)malloc(n * sizeof(T));
#endif
}
void deallocate(T *p, size_t n)
{
util_guarded_mem_free(n * sizeof(T));
+ if(p != NULL) {
#ifdef WITH_BLENDER_GUARDEDALLOC
- MEM_freeN((void*)p);
+ MEM_freeN(p);
#else
- std::allocator<T>::deallocate(p, n);
+ free(p);
#endif
+ }
+ }
+
+ T *address(T& x) const
+ {
+ return &x;
+ }
+
+ const T *address(const T& x) const
+ {
+ return &x;
+ }
+
+ GuardedAllocator<T>& operator=(const GuardedAllocator&)
+ {
+ return *this;
+ }
+
+ void construct(T *p, const T& val)
+ {
+ new ((T *)p) T(val);
+ }
+
+ void destroy(T *p)
+ {
+ p->~T();
+ }
+
+ size_t max_size() const
+ {
+ return size_t(-1);
}
- GuardedAllocator() : std::allocator<T>() { }
- GuardedAllocator(const GuardedAllocator &a) : std::allocator<T>(a) { }
template <class U>
- GuardedAllocator(const GuardedAllocator<U> &a) : std::allocator<T>(a) { }
- ~GuardedAllocator() { }
+ struct rebind {
+ typedef GuardedAllocator<U> other;
+ };
+
+ template <class U>
+ GuardedAllocator(const GuardedAllocator<U>&) {}
+
+ template <class U>
+ GuardedAllocator& operator=(const GuardedAllocator<U>&) { return *this; }
};
/* Get memory usage and peak from the guarded STL allocator. */
diff --git a/intern/cycles/util/util_task.cpp b/intern/cycles/util/util_task.cpp
index d56553d1d4a..5523f373824 100644
--- a/intern/cycles/util/util_task.cpp
+++ b/intern/cycles/util/util_task.cpp
@@ -219,6 +219,12 @@ void TaskScheduler::exit()
}
}
+void TaskScheduler::free_memory()
+{
+ assert(users == 0);
+ threads.free_memory();
+}
+
bool TaskScheduler::thread_wait_pop(Entry& entry)
{
thread_scoped_lock queue_lock(queue_mutex);
diff --git a/intern/cycles/util/util_task.h b/intern/cycles/util/util_task.h
index debcff3b776..a8e1963252a 100644
--- a/intern/cycles/util/util_task.h
+++ b/intern/cycles/util/util_task.h
@@ -91,6 +91,7 @@ class TaskScheduler
public:
static void init(int num_threads = 0);
static void exit();
+ static void free_memory();
/* number of threads that can work on task */
static int num_threads() { return threads.size(); }
diff --git a/intern/cycles/util/util_vector.h b/intern/cycles/util/util_vector.h
index 623436483a0..830aa15291d 100644
--- a/intern/cycles/util/util_vector.h
+++ b/intern/cycles/util/util_vector.h
@@ -24,30 +24,21 @@
#include <vector>
#include "util_aligned_malloc.h"
+#include "util_guarded_allocator.h"
#include "util_types.h"
-#ifdef WITH_CYCLES_DEBUG
-# include "util_guarded_allocator.h"
-#endif
-
CCL_NAMESPACE_BEGIN
/* Vector
*
* Own subclass-ed vestion of std::vector. Subclass is needed because:
*
- * - When building with WITH_CYCLES_DEBUG we need to use own allocator which
- * keeps track of used/peak memory.
+ * - Use own allocator which keeps track of used/peak memory.
*
* - Have method to ensure capacity is re-set to 0.
*/
template<typename value_type,
-#ifdef WITH_CYCLES_DEBUG
- typename allocator_type = GuardedAllocator<value_type>
-#else
- typename allocator_type = std::allocator<value_type>
-#endif
- >
+ typename allocator_type = GuardedAllocator<value_type> >
class vector : public std::vector<value_type, allocator_type>
{
public: