diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-02-14 16:03:39 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2015-02-15 00:01:48 +0300 |
commit | a445e491860c0853c398dbb4662c4c21cdb3696d (patch) | |
tree | 3198f8c7a72aea367f792015ee12140d36762fe3 /intern/cycles/util | |
parent | 01067fe51c4f3df06fa6e8744da2344253201ec6 (diff) |
Cycles: Implement guarded allocator for STL classes
The commit implements a guarded allocator which can be used by STL classes
such as vectors, maps and so on. This allocator will keep track of current
and peak memory usage which then can be queried.
New code for allocator is only active when building Cycles with debug flag
(WITH_CYCLES_DEBUG) and doesn't distort regular builds too much.
Additionally now we're using own subclass of std::vector which allows us
to implement shrink_to_fit() method which would ensure capacity of the
vector is as big as it should be (without this making vector smaller will
still use all previous memory allocated).
Diffstat (limited to 'intern/cycles/util')
-rw-r--r-- | intern/cycles/util/CMakeLists.txt | 5 | ||||
-rw-r--r-- | intern/cycles/util/util_guarded_allocator.cpp | 49 | ||||
-rw-r--r-- | intern/cycles/util/util_guarded_allocator.h | 64 | ||||
-rw-r--r-- | intern/cycles/util/util_vector.h | 56 |
4 files changed, 173 insertions, 1 deletions
diff --git a/intern/cycles/util/CMakeLists.txt b/intern/cycles/util/CMakeLists.txt index e0a0921f88d..bfe46ce6f67 100644 --- a/intern/cycles/util/CMakeLists.txt +++ b/intern/cycles/util/CMakeLists.txt @@ -74,6 +74,11 @@ 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.cpp b/intern/cycles/util/util_guarded_allocator.cpp new file mode 100644 index 00000000000..8de6e254cbf --- /dev/null +++ b/intern/cycles/util/util_guarded_allocator.cpp @@ -0,0 +1,49 @@ +/* + * Copyright 2011-2015 Blender Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "util_guarded_allocator.h" +#include "util_stats.h" + +CCL_NAMESPACE_BEGIN + +static Stats global_stats; + +/* Internal API. */ + +void util_guarded_mem_alloc(size_t n) +{ + global_stats.mem_alloc(n); +} + +void util_guarded_mem_free(size_t n) +{ + global_stats.mem_free(n); +} + +/* Public API. */ + +size_t util_guarded_get_mem_used(void) +{ + return global_stats.mem_used; +} + +size_t util_guarded_get_mem_peak(void) +{ + return global_stats.mem_peak; +} + + +CCL_NAMESPACE_END diff --git a/intern/cycles/util/util_guarded_allocator.h b/intern/cycles/util/util_guarded_allocator.h new file mode 100644 index 00000000000..4a7e0825388 --- /dev/null +++ b/intern/cycles/util/util_guarded_allocator.h @@ -0,0 +1,64 @@ +/* + * Copyright 2011-2015 Blender Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __UTIL_GUARDED_ALLOCATOR_H__ +#define __UTIL_GUARDED_ALLOCATOR_H__ + +#include <memory> + +#include "util_types.h" + +CCL_NAMESPACE_BEGIN + +/* Internal use only. */ +void util_guarded_mem_alloc(size_t n); +void util_guarded_mem_free(size_t n); + +/* Guarded allocator for the use with STL. */ +template <typename T> +class GuardedAllocator: public std::allocator<T> { +public: + template<typename _Tp1> + struct rebind { + typedef GuardedAllocator<_Tp1> other; + }; + + T *allocate(size_t n, const void *hint = 0) + { + util_guarded_mem_alloc(n * sizeof(T)); + return std::allocator<T>::allocate(n, hint); + } + + void deallocate(T *p, size_t n) + { + util_guarded_mem_free(n * sizeof(T)); + return std::allocator<T>::deallocate(p, n); + } + + 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() { } +}; + +/* Get memory usage and peak from the guarded STL allocator. */ +size_t util_guarded_get_mem_used(void); +size_t util_guarded_get_mem_peak(void); + +CCL_NAMESPACE_END + +#endif /* __UTIL_GUARDED_ALLOCATOR_H__ */ diff --git a/intern/cycles/util/util_vector.h b/intern/cycles/util/util_vector.h index 710b0690256..8d1bd7407ec 100644 --- a/intern/cycles/util/util_vector.h +++ b/intern/cycles/util/util_vector.h @@ -25,9 +25,63 @@ #include "util_aligned_malloc.h" #include "util_types.h" +#ifdef WITH_CYCLES_DEBUG +# include "util_guarded_allocator.h" +#endif + CCL_NAMESPACE_BEGIN -using std::vector; +/* 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. + * + * - Have method to ensure capacity is re-set to 0. + */ +template<typename value_type> +class vector : public std::vector<value_type +#ifdef WITH_CYCLES_DEBUG + , GuardedAllocator<value_type> +#endif + > +{ +public: +#ifdef WITH_CYCLES_DEBUG + typedef GuardedAllocator<value_type> allocator_type; +#else + typedef std::allocator<value_type> allocator_type; +#endif + + /* Default constructor. */ + explicit vector() : std::vector<value_type, allocator_type>() { } + + /* Fill constructor. */ + explicit vector(size_t n, const value_type& val = value_type()) + : std::vector<value_type, allocator_type>(n, val) { } + + /* Range constructor. */ + template <class InputIterator> + vector (InputIterator first, InputIterator last) + : std::vector<value_type, allocator_type>(first, last) { } + + /* Copy constructor. */ + vector(const vector &x) : std::vector<value_type, allocator_type>(x) { } + +#if __cplusplus < 201103L + void shrink_to_fit(void) + { + vector<value_type>().swap(*this); + } +#endif + + /* Some external API might demand working with std::vector. */ + operator std::vector<value_type>() + { + return std::vector<value_type>(*this); + } +}; /* Array * |