mallocn.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. /** \file
  17. * \ingroup MEM
  18. *
  19. * Guarded memory allocation, and boundary-write detection.
  20. */
  21. #include "MEM_guardedalloc.h"
  22. /* to ensure strict conversions */
  23. #include "../../source/blender/blenlib/BLI_strict_flags.h"
  24. #include <assert.h>
  25. #include "mallocn_intern.h"
  26. #ifdef WITH_JEMALLOC_CONF
  27. /* If jemalloc is used, it reads this global variable and enables background
  28. * threads to purge dirty pages. Otherwise we release memory too slowly or not
  29. * at all if the thread that did the allocation stays inactive. */
  30. const char *malloc_conf = "background_thread:true,dirty_decay_ms:4000";
  31. #endif
  32. size_t (*MEM_allocN_len)(const void *vmemh) = MEM_lockfree_allocN_len;
  33. void (*MEM_freeN)(void *vmemh) = MEM_lockfree_freeN;
  34. void *(*MEM_dupallocN)(const void *vmemh) = MEM_lockfree_dupallocN;
  35. void *(*MEM_reallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id;
  36. void *(*MEM_recallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id;
  37. void *(*MEM_callocN)(size_t len, const char *str) = MEM_lockfree_callocN;
  38. void *(*MEM_calloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_calloc_arrayN;
  39. void *(*MEM_mallocN)(size_t len, const char *str) = MEM_lockfree_mallocN;
  40. void *(*MEM_malloc_arrayN)(size_t len, size_t size, const char *str) = MEM_lockfree_malloc_arrayN;
  41. void *(*MEM_mallocN_aligned)(size_t len,
  42. size_t alignment,
  43. const char *str) = MEM_lockfree_mallocN_aligned;
  44. void *(*MEM_mapallocN)(size_t len, const char *str) = MEM_lockfree_mapallocN;
  45. void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
  46. void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
  47. void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
  48. void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
  49. void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
  50. bool (*MEM_consistency_check)(void) = MEM_lockfree_consistency_check;
  51. void (*MEM_set_lock_callback)(void (*lock)(void),
  52. void (*unlock)(void)) = MEM_lockfree_set_lock_callback;
  53. void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
  54. size_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
  55. size_t (*MEM_get_mapped_memory_in_use)(void) = MEM_lockfree_get_mapped_memory_in_use;
  56. unsigned int (*MEM_get_memory_blocks_in_use)(void) = MEM_lockfree_get_memory_blocks_in_use;
  57. void (*MEM_reset_peak_memory)(void) = MEM_lockfree_reset_peak_memory;
  58. size_t (*MEM_get_peak_memory)(void) = MEM_lockfree_get_peak_memory;
  59. #ifndef NDEBUG
  60. const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr;
  61. #endif
  62. void *aligned_malloc(size_t size, size_t alignment)
  63. {
  64. #ifdef _WIN32
  65. return _aligned_malloc(size, alignment);
  66. #elif defined(__APPLE__)
  67. /* On Mac OS X, both the heap and the stack are guaranteed 16-byte aligned so
  68. * they work natively with SSE types with no further work.
  69. */
  70. assert(alignment == 16);
  71. (void)alignment;
  72. return malloc(size);
  73. #elif defined(__FreeBSD__) || defined(__NetBSD__)
  74. void *result;
  75. if (posix_memalign(&result, alignment, size)) {
  76. /* non-zero means allocation error
  77. * either no allocation or bad alignment value
  78. */
  79. return NULL;
  80. }
  81. return result;
  82. #else /* This is for Linux. */
  83. return memalign(alignment, size);
  84. #endif
  85. }
  86. void aligned_free(void *ptr)
  87. {
  88. #ifdef _WIN32
  89. _aligned_free(ptr);
  90. #else
  91. free(ptr);
  92. #endif
  93. }
  94. void MEM_use_guarded_allocator(void)
  95. {
  96. MEM_allocN_len = MEM_guarded_allocN_len;
  97. MEM_freeN = MEM_guarded_freeN;
  98. MEM_dupallocN = MEM_guarded_dupallocN;
  99. MEM_reallocN_id = MEM_guarded_reallocN_id;
  100. MEM_recallocN_id = MEM_guarded_recallocN_id;
  101. MEM_callocN = MEM_guarded_callocN;
  102. MEM_calloc_arrayN = MEM_guarded_calloc_arrayN;
  103. MEM_mallocN = MEM_guarded_mallocN;
  104. MEM_malloc_arrayN = MEM_guarded_malloc_arrayN;
  105. MEM_mallocN_aligned = MEM_guarded_mallocN_aligned;
  106. MEM_mapallocN = MEM_guarded_mapallocN;
  107. MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
  108. MEM_printmemlist = MEM_guarded_printmemlist;
  109. MEM_callbackmemlist = MEM_guarded_callbackmemlist;
  110. MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
  111. MEM_set_error_callback = MEM_guarded_set_error_callback;
  112. MEM_consistency_check = MEM_guarded_consistency_check;
  113. MEM_set_lock_callback = MEM_guarded_set_lock_callback;
  114. MEM_set_memory_debug = MEM_guarded_set_memory_debug;
  115. MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
  116. MEM_get_mapped_memory_in_use = MEM_guarded_get_mapped_memory_in_use;
  117. MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
  118. MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
  119. MEM_get_peak_memory = MEM_guarded_get_peak_memory;
  120. #ifndef NDEBUG
  121. MEM_name_ptr = MEM_guarded_name_ptr;
  122. #endif
  123. }