util_guarded_allocator.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2011-2015 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_GUARDED_ALLOCATOR_H__
  17. #define __UTIL_GUARDED_ALLOCATOR_H__
  18. #include <cstddef>
  19. #include <memory>
  20. #ifdef WITH_BLENDER_GUARDEDALLOC
  21. # include "../../guardedalloc/MEM_guardedalloc.h"
  22. #endif
  23. CCL_NAMESPACE_BEGIN
  24. /* Internal use only. */
  25. void util_guarded_mem_alloc(size_t n);
  26. void util_guarded_mem_free(size_t n);
  27. /* Guarded allocator for the use with STL. */
  28. template<typename T> class GuardedAllocator {
  29. public:
  30. typedef size_t size_type;
  31. typedef ptrdiff_t difference_type;
  32. typedef T *pointer;
  33. typedef const T *const_pointer;
  34. typedef T &reference;
  35. typedef const T &const_reference;
  36. typedef T value_type;
  37. GuardedAllocator()
  38. {
  39. }
  40. GuardedAllocator(const GuardedAllocator &)
  41. {
  42. }
  43. T *allocate(size_t n, const void *hint = 0)
  44. {
  45. (void)hint;
  46. size_t size = n * sizeof(T);
  47. util_guarded_mem_alloc(size);
  48. if (n == 0) {
  49. return NULL;
  50. }
  51. T *mem;
  52. #ifdef WITH_BLENDER_GUARDEDALLOC
  53. /* C++ standard requires allocation functions to allocate memory suitably
  54. * aligned for any standard type. This is 16 bytes for 64 bit platform as
  55. * far as i concerned. We might over-align on 32bit here, but that should
  56. * be all safe actually.
  57. */
  58. mem = (T *)MEM_mallocN_aligned(size, 16, "Cycles Alloc");
  59. #else
  60. mem = (T *)malloc(size);
  61. #endif
  62. if (mem == NULL) {
  63. throw std::bad_alloc();
  64. }
  65. return mem;
  66. }
  67. void deallocate(T *p, size_t n)
  68. {
  69. util_guarded_mem_free(n * sizeof(T));
  70. if (p != NULL) {
  71. #ifdef WITH_BLENDER_GUARDEDALLOC
  72. MEM_freeN(p);
  73. #else
  74. free(p);
  75. #endif
  76. }
  77. }
  78. T *address(T &x) const
  79. {
  80. return &x;
  81. }
  82. const T *address(const T &x) const
  83. {
  84. return &x;
  85. }
  86. GuardedAllocator<T> &operator=(const GuardedAllocator &)
  87. {
  88. return *this;
  89. }
  90. size_t max_size() const
  91. {
  92. return size_t(-1);
  93. }
  94. template<class U> struct rebind {
  95. typedef GuardedAllocator<U> other;
  96. };
  97. template<class U> GuardedAllocator(const GuardedAllocator<U> &)
  98. {
  99. }
  100. template<class U> GuardedAllocator &operator=(const GuardedAllocator<U> &)
  101. {
  102. return *this;
  103. }
  104. inline bool operator==(GuardedAllocator const & /*other*/) const
  105. {
  106. return true;
  107. }
  108. inline bool operator!=(GuardedAllocator const &other) const
  109. {
  110. return !operator==(other);
  111. }
  112. #ifdef _MSC_VER
  113. /* Welcome to the black magic here.
  114. *
  115. * The issue is that MSVC C++ allocates container proxy on any
  116. * vector initialization, including static vectors which don't
  117. * have any data yet. This leads to several issues:
  118. *
  119. * - Static objects initialization fiasco (global_stats from
  120. * util_stats.h might not be initialized yet).
  121. * - If main() function changes allocator type (for example,
  122. * this might happen with `blender --debug-memory`) nobody
  123. * will know how to convert already allocated memory to a new
  124. * guarded allocator.
  125. *
  126. * Here we work this around by making it so container proxy does
  127. * not use guarded allocation. A bit fragile, unfortunately.
  128. */
  129. template<> struct rebind<std::_Container_proxy> {
  130. typedef std::allocator<std::_Container_proxy> other;
  131. };
  132. operator std::allocator<std::_Container_proxy>() const
  133. {
  134. return std::allocator<std::_Container_proxy>();
  135. }
  136. #endif
  137. };
  138. /* Get memory usage and peak from the guarded STL allocator. */
  139. size_t util_guarded_get_mem_used();
  140. size_t util_guarded_get_mem_peak();
  141. /* Call given function and keep track if it runs out of memory.
  142. *
  143. * If it does run out f memory, stop execution and set progress
  144. * to do a global cancel.
  145. *
  146. * It's not fully robust, but good enough to catch obvious issues
  147. * when running out of memory.
  148. */
  149. #define MEM_GUARDED_CALL(progress, func, ...) \
  150. do { \
  151. try { \
  152. (func)(__VA_ARGS__); \
  153. } \
  154. catch (std::bad_alloc &) { \
  155. fprintf(stderr, "Error: run out of memory!\n"); \
  156. fflush(stderr); \
  157. (progress)->set_error("Out of memory"); \
  158. } \
  159. } while (false)
  160. CCL_NAMESPACE_END
  161. #endif /* __UTIL_GUARDED_ALLOCATOR_H__ */