The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MEMORY_JUCEHEADER__
  19. #define __JUCE_MEMORY_JUCEHEADER__
  20. //==============================================================================
  21. /*
  22. This file defines the various juce_malloc(), juce_free() macros that should be used in
  23. preference to the standard calls.
  24. */
  25. #if defined (JUCE_DEBUG) && JUCE_MSVC && JUCE_CHECK_MEMORY_LEAKS
  26. #ifndef JUCE_DLL
  27. //==============================================================================
  28. // Win32 debug non-DLL versions..
  29. /** This should be used instead of calling malloc directly.
  30. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  31. */
  32. #define juce_malloc(numBytes) _malloc_dbg (numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  33. /** This should be used instead of calling calloc directly.
  34. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  35. */
  36. #define juce_calloc(numBytes) _calloc_dbg (1, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  37. /** This should be used instead of calling realloc directly.
  38. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  39. */
  40. #define juce_realloc(location, numBytes) _realloc_dbg (location, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  41. /** This should be used instead of calling free directly.
  42. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  43. */
  44. #define juce_free(location) _free_dbg (location, _NORMAL_BLOCK)
  45. #else
  46. //==============================================================================
  47. // Win32 debug DLL versions..
  48. // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
  49. // way all juce calls in the DLL and in the host API will all use the same allocator.
  50. extern JUCE_API void* juce_DebugMalloc (const int size, const char* file, const int line);
  51. extern JUCE_API void* juce_DebugCalloc (const int size, const char* file, const int line);
  52. extern JUCE_API void* juce_DebugRealloc (void* const block, const int size, const char* file, const int line);
  53. extern JUCE_API void juce_DebugFree (void* const block);
  54. /** This should be used instead of calling malloc directly.
  55. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  56. */
  57. #define juce_malloc(numBytes) JUCE_NAMESPACE::juce_DebugMalloc (numBytes, __FILE__, __LINE__)
  58. /** This should be used instead of calling calloc directly.
  59. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  60. */
  61. #define juce_calloc(numBytes) JUCE_NAMESPACE::juce_DebugCalloc (numBytes, __FILE__, __LINE__)
  62. /** This should be used instead of calling realloc directly.
  63. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  64. */
  65. #define juce_realloc(location, numBytes) JUCE_NAMESPACE::juce_DebugRealloc (location, numBytes, __FILE__, __LINE__)
  66. /** This should be used instead of calling free directly.
  67. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  68. */
  69. #define juce_free(location) JUCE_NAMESPACE::juce_DebugFree (location)
  70. #endif
  71. #if ! defined (_AFXDLL)
  72. /** This macro can be added to classes to add extra debugging information to the memory
  73. allocated for them, so you can see the type of objects involved when there's a dump
  74. of leaked objects at program shutdown. (Only works on win32 at the moment).
  75. */
  76. #define juce_UseDebuggingNewOperator \
  77. static void* operator new (size_t sz) { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
  78. static void* operator new (size_t sz, void* p) { return ::operator new (sz, p); } \
  79. static void operator delete (void* p) { juce_free (p); }
  80. #endif
  81. #elif defined (JUCE_DLL)
  82. //==============================================================================
  83. // Win32 DLL (release) versions..
  84. // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
  85. // way all juce calls in the DLL and in the host API will all use the same allocator.
  86. extern JUCE_API void* juce_Malloc (const int size);
  87. extern JUCE_API void* juce_Calloc (const int size);
  88. extern JUCE_API void* juce_Realloc (void* const block, const int size);
  89. extern JUCE_API void juce_Free (void* const block);
  90. /** This should be used instead of calling malloc directly.
  91. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  92. */
  93. #define juce_malloc(numBytes) JUCE_NAMESPACE::juce_Malloc (numBytes)
  94. /** This should be used instead of calling calloc directly.
  95. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  96. */
  97. #define juce_calloc(numBytes) JUCE_NAMESPACE::juce_Calloc (numBytes)
  98. /** This should be used instead of calling realloc directly.
  99. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  100. */
  101. #define juce_realloc(location, numBytes) JUCE_NAMESPACE::juce_Realloc (location, numBytes)
  102. /** This should be used instead of calling free directly.
  103. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  104. */
  105. #define juce_free(location) JUCE_NAMESPACE::juce_Free (location)
  106. #define juce_UseDebuggingNewOperator \
  107. static void* operator new (size_t sz) { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
  108. static void* operator new (size_t sz, void* p) { return ::operator new (sz, p); } \
  109. static void operator delete (void* p) { juce_free (p); }
  110. #else
  111. //==============================================================================
  112. // Mac, Linux and Win32 (release) versions..
  113. /** This should be used instead of calling malloc directly.
  114. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  115. */
  116. #define juce_malloc(numBytes) malloc (numBytes)
  117. /** This should be used instead of calling calloc directly.
  118. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  119. */
  120. #define juce_calloc(numBytes) calloc (1, numBytes)
  121. /** This should be used instead of calling realloc directly.
  122. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  123. */
  124. #define juce_realloc(location, numBytes) realloc (location, numBytes)
  125. /** This should be used instead of calling free directly.
  126. Only use direct memory allocation if there's really no way to use a HeapBlock object instead!
  127. */
  128. #define juce_free(location) free (location)
  129. #endif
  130. //==============================================================================
  131. /** This macro can be added to classes to add extra debugging information to the memory
  132. allocated for them, so you can see the type of objects involved when there's a dump
  133. of leaked objects at program shutdown. (Only works on win32 at the moment).
  134. Note that if you create a class that inherits from a class that uses this macro,
  135. your class must also use the macro, otherwise you'll probably get compile errors
  136. because of ambiguous new operators.
  137. Most of the JUCE classes use it, so see these for examples of where it should go.
  138. */
  139. #ifndef juce_UseDebuggingNewOperator
  140. #define juce_UseDebuggingNewOperator
  141. #endif
  142. //==============================================================================
  143. #if JUCE_MSVC
  144. /** This is a compiler-independent way of declaring a variable as being thread-local.
  145. E.g.
  146. @code
  147. juce_ThreadLocal int myVariable;
  148. @endcode
  149. */
  150. #define juce_ThreadLocal __declspec(thread)
  151. #else
  152. #define juce_ThreadLocal __thread
  153. #endif
  154. //==============================================================================
  155. /** Clears a block of memory. */
  156. #define zeromem(memory, numBytes) memset (memory, 0, numBytes)
  157. /** Clears a reference to a local structure. */
  158. #define zerostruct(structure) memset (&structure, 0, sizeof (structure))
  159. /** A handy macro that calls delete on a pointer if it's non-zero, and
  160. then sets the pointer to null.
  161. */
  162. #define deleteAndZero(pointer) { delete (pointer); (pointer) = 0; }
  163. #endif // __JUCE_MEMORY_JUCEHEADER__