Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/util/util_guarded_allocator.h
| Show All 11 Lines | |||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| * limitations under the License. | * limitations under the License. | ||||
| */ | */ | ||||
| #ifndef __UTIL_GUARDED_ALLOCATOR_H__ | #ifndef __UTIL_GUARDED_ALLOCATOR_H__ | ||||
| #define __UTIL_GUARDED_ALLOCATOR_H__ | #define __UTIL_GUARDED_ALLOCATOR_H__ | ||||
| /* Define this in order to use Blender's guarded allocator to keep | #include <cstddef> | ||||
| * 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 <memory> | #include <memory> | ||||
| #include "util_debug.h" | |||||
| #include "util_types.h" | #include "util_types.h" | ||||
| #ifdef WITH_BLENDER_GUARDEDALLOC | #ifdef WITH_BLENDER_GUARDEDALLOC | ||||
| # include "../../guardedalloc/MEM_guardedalloc.h" | # include "../../guardedalloc/MEM_guardedalloc.h" | ||||
| #endif | #endif | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| /* Internal use only. */ | /* Internal use only. */ | ||||
| void util_guarded_mem_alloc(size_t n); | void util_guarded_mem_alloc(size_t n); | ||||
| void util_guarded_mem_free(size_t n); | void util_guarded_mem_free(size_t n); | ||||
| /* Guarded allocator for the use with STL. */ | /* Guarded allocator for the use with STL. */ | ||||
| template <typename T> | template <typename T> | ||||
| class GuardedAllocator : public std::allocator<T> { | class GuardedAllocator { | ||||
| public: | public: | ||||
| template<typename _Tp1> | typedef size_t size_type; | ||||
| struct rebind { | typedef ptrdiff_t difference_type; | ||||
| typedef GuardedAllocator<_Tp1> other; | 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) | T *allocate(size_t n, const void *hint = 0) | ||||
| { | { | ||||
| util_guarded_mem_alloc(n * sizeof(T)); | util_guarded_mem_alloc(n * sizeof(T)); | ||||
| #ifdef WITH_BLENDER_GUARDEDALLOC | |||||
| (void)hint; | (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 | #else | ||||
| return std::allocator<T>::allocate(n, hint); | return (T*)malloc(n * sizeof(T)); | ||||
| #endif | #endif | ||||
| } | } | ||||
| void deallocate(T *p, size_t n) | void deallocate(T *p, size_t n) | ||||
| { | { | ||||
| util_guarded_mem_free(n * sizeof(T)); | util_guarded_mem_free(n * sizeof(T)); | ||||
| if(p != NULL) { | |||||
| #ifdef WITH_BLENDER_GUARDEDALLOC | #ifdef WITH_BLENDER_GUARDEDALLOC | ||||
| MEM_freeN((void*)p); | MEM_freeN(p); | ||||
| #else | #else | ||||
| std::allocator<T>::deallocate(p, n); | free(p); | ||||
| #endif | #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); | |||||
| } | |||||
| template <class U> | |||||
| struct rebind { | |||||
| typedef GuardedAllocator<U> other; | |||||
| }; | |||||
| template <class U> | |||||
| GuardedAllocator(const GuardedAllocator<U>&) {} | |||||
| GuardedAllocator() : std::allocator<T>() { } | |||||
| GuardedAllocator(const GuardedAllocator &a) : std::allocator<T>(a) { } | |||||
| template <class U> | template <class U> | ||||
| GuardedAllocator(const GuardedAllocator<U> &a) : std::allocator<T>(a) { } | GuardedAllocator& operator=(const GuardedAllocator<U>&) { return *this; } | ||||
| ~GuardedAllocator() { } | |||||
| }; | }; | ||||
| /* Get memory usage and peak from the guarded STL allocator. */ | /* Get memory usage and peak from the guarded STL allocator. */ | ||||
| size_t util_guarded_get_mem_used(void); | size_t util_guarded_get_mem_used(void); | ||||
| size_t util_guarded_get_mem_peak(void); | size_t util_guarded_get_mem_peak(void); | ||||
| CCL_NAMESPACE_END | CCL_NAMESPACE_END | ||||
| #endif /* __UTIL_GUARDED_ALLOCATOR_H__ */ | #endif /* __UTIL_GUARDED_ALLOCATOR_H__ */ | ||||