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.

167 lines
8.2KB

  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. #define juce_malloc(numBytes) _malloc_dbg (numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  31. /** This should be used instead of calling calloc directly. */
  32. #define juce_calloc(numBytes) _calloc_dbg (1, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  33. /** This should be used instead of calling realloc directly. */
  34. #define juce_realloc(location, numBytes) _realloc_dbg (location, numBytes, _NORMAL_BLOCK, __FILE__, __LINE__)
  35. /** This should be used instead of calling free directly. */
  36. #define juce_free(location) _free_dbg (location, _NORMAL_BLOCK)
  37. #else
  38. //==============================================================================
  39. // Win32 debug DLL versions..
  40. // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
  41. // way all juce calls in the DLL and in the host API will all use the same allocator.
  42. extern JUCE_API void* juce_DebugMalloc (const int size, const char* file, const int line);
  43. extern JUCE_API void* juce_DebugCalloc (const int size, const char* file, const int line);
  44. extern JUCE_API void* juce_DebugRealloc (void* const block, const int size, const char* file, const int line);
  45. extern JUCE_API void juce_DebugFree (void* const block);
  46. /** This should be used instead of calling malloc directly. */
  47. #define juce_malloc(numBytes) JUCE_NAMESPACE::juce_DebugMalloc (numBytes, __FILE__, __LINE__)
  48. /** This should be used instead of calling calloc directly. */
  49. #define juce_calloc(numBytes) JUCE_NAMESPACE::juce_DebugCalloc (numBytes, __FILE__, __LINE__)
  50. /** This should be used instead of calling realloc directly. */
  51. #define juce_realloc(location, numBytes) JUCE_NAMESPACE::juce_DebugRealloc (location, numBytes, __FILE__, __LINE__)
  52. /** This should be used instead of calling free directly. */
  53. #define juce_free(location) JUCE_NAMESPACE::juce_DebugFree (location)
  54. #endif
  55. #if ! defined (_AFXDLL)
  56. /** This macro can be added to classes to add extra debugging information to the memory
  57. allocated for them, so you can see the type of objects involved when there's a dump
  58. of leaked objects at program shutdown. (Only works on win32 at the moment).
  59. */
  60. #define juce_UseDebuggingNewOperator \
  61. static void* operator new (size_t sz) { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
  62. static void* operator new (size_t sz, void* p) { return ::operator new (sz, p); } \
  63. static void operator delete (void* p) { juce_free (p); }
  64. #endif
  65. #elif defined (JUCE_DLL)
  66. //==============================================================================
  67. // Win32 DLL (release) versions..
  68. // For the DLL, we'll define some functions in the DLL that will be used for allocation - that
  69. // way all juce calls in the DLL and in the host API will all use the same allocator.
  70. extern JUCE_API void* juce_Malloc (const int size);
  71. extern JUCE_API void* juce_Calloc (const int size);
  72. extern JUCE_API void* juce_Realloc (void* const block, const int size);
  73. extern JUCE_API void juce_Free (void* const block);
  74. /** This should be used instead of calling malloc directly. */
  75. #define juce_malloc(numBytes) JUCE_NAMESPACE::juce_Malloc (numBytes)
  76. /** This should be used instead of calling calloc directly. */
  77. #define juce_calloc(numBytes) JUCE_NAMESPACE::juce_Calloc (numBytes)
  78. /** This should be used instead of calling realloc directly. */
  79. #define juce_realloc(location, numBytes) JUCE_NAMESPACE::juce_Realloc (location, numBytes)
  80. /** This should be used instead of calling free directly. */
  81. #define juce_free(location) JUCE_NAMESPACE::juce_Free (location)
  82. #define juce_UseDebuggingNewOperator \
  83. static void* operator new (size_t sz) { void* const p = juce_malloc ((int) sz); return (p != 0) ? p : ::operator new (sz); } \
  84. static void* operator new (size_t sz, void* p) { return ::operator new (sz, p); } \
  85. static void operator delete (void* p) { juce_free (p); }
  86. #else
  87. //==============================================================================
  88. // Mac, Linux and Win32 (release) versions..
  89. /** This should be used instead of calling malloc directly. */
  90. #define juce_malloc(numBytes) malloc (numBytes)
  91. /** This should be used instead of calling calloc directly. */
  92. #define juce_calloc(numBytes) calloc (1, numBytes)
  93. /** This should be used instead of calling realloc directly. */
  94. #define juce_realloc(location, numBytes) realloc (location, numBytes)
  95. /** This should be used instead of calling free directly. */
  96. #define juce_free(location) free (location)
  97. #endif
  98. //==============================================================================
  99. /** This macro can be added to classes to add extra debugging information to the memory
  100. allocated for them, so you can see the type of objects involved when there's a dump
  101. of leaked objects at program shutdown. (Only works on win32 at the moment).
  102. Note that if you create a class that inherits from a class that uses this macro,
  103. your class must also use the macro, otherwise you'll probably get compile errors
  104. because of ambiguous new operators.
  105. Most of the JUCE classes use it, so see these for examples of where it should go.
  106. */
  107. #ifndef juce_UseDebuggingNewOperator
  108. #define juce_UseDebuggingNewOperator
  109. #endif
  110. //==============================================================================
  111. #if JUCE_MSVC
  112. /** This is a compiler-indenpendent way of declaring a variable as being thread-local.
  113. E.g.
  114. @code
  115. juce_ThreadLocal int myVariable;
  116. @endcode
  117. */
  118. #define juce_ThreadLocal __declspec(thread)
  119. #else
  120. #define juce_ThreadLocal __thread
  121. #endif
  122. //==============================================================================
  123. /** Clears a block of memory. */
  124. #define zeromem(memory, numBytes) memset (memory, 0, numBytes)
  125. /** Clears a reference to a local structure. */
  126. #define zerostruct(structure) memset (&structure, 0, sizeof (structure))
  127. /** A handy macro that calls delete on a pointer if it's non-zero, and
  128. then sets the pointer to null.
  129. */
  130. #define deleteAndZero(pointer) { delete (pointer); (pointer) = 0; }
  131. #endif // __JUCE_MEMORY_JUCEHEADER__