2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_MEMORY_JUCEHEADER__
27 #define __JUCE_MEMORY_JUCEHEADER__
29 //==============================================================================
30 #if JUCE_MSVC || DOXYGEN
31 /** This is a compiler-independent way of declaring a variable as being thread-local.
35 juce_ThreadLocal int myVariable;
38 #define juce_ThreadLocal __declspec(thread)
40 #define juce_ThreadLocal __thread
43 //==============================================================================
45 /** This allocator is not defined in mingw gcc. */
46 #define alloca __builtin_alloca
49 //==============================================================================
50 /** Fills a block of memory with zeros. */
51 inline void zeromem (void* memory
, size_t numBytes
) noexcept
{ memset (memory
, 0, numBytes
); }
53 /** Overwrites a structure or object with zeros. */
54 template <typename Type
>
55 inline void zerostruct (Type
& structure
) noexcept
{ memset (&structure
, 0, sizeof (structure
)); }
57 /** Delete an object pointer, and sets the pointer to null.
59 Remember that it's not good c++ practice to use delete directly - always try to use a ScopedPointer
60 or other automatic lieftime-management system rather than resorting to deleting raw pointers!
62 template <typename Type
>
63 inline void deleteAndZero (Type
& pointer
) { delete pointer
; pointer
= nullptr; }
65 /** A handy function which adds a number of bytes to any type of pointer and returns the result.
66 This can be useful to avoid casting pointers to a char* and back when you want to move them by
67 a specific number of bytes,
69 template <typename Type
>
70 inline Type
* addBytesToPointer (Type
* pointer
, int bytes
) noexcept
{ return (Type
*) (((char*) pointer
) + bytes
); }
72 /** A handy function which returns the difference between any two pointers, in bytes.
73 The address of the second pointer is subtracted from the first, and the difference in bytes is returned.
75 template <typename Type1
, typename Type2
>
76 inline int getAddressDifference (Type1
* pointer1
, Type2
* pointer2
) noexcept
{ return (int) (((const char*) pointer1
) - (const char*) pointer2
); }
79 //==============================================================================
80 /* In a win32 DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for
81 allocating all the objects - that way all juce objects in the DLL and in the host will live in the same heap,
82 avoiding problems when an object is created in one module and passed across to another where it is deleted.
83 By piggy-backing on the JUCE_LEAK_DETECTOR macro, these allocators can be injected into most juce classes.
85 #if JUCE_MSVC && defined (JUCE_DLL) && ! DOXYGEN
86 extern JUCE_API
void* juceDLL_malloc (size_t);
87 extern JUCE_API
void juceDLL_free (void*);
89 #define JUCE_LEAK_DETECTOR(OwnerClass) public:\
90 static void* operator new (size_t sz) { return JUCE_NAMESPACE::juceDLL_malloc ((int) sz); } \
91 static void* operator new (size_t, void* p) { return p; } \
92 static void operator delete (void* p) { JUCE_NAMESPACE::juceDLL_free (p); } \
93 static void operator delete (void*, void*) {}
96 //==============================================================================
97 /** (Deprecated) This was a win32-specific way of checking for object leaks - now please
98 use the JUCE_LEAK_DETECTOR instead.
100 #ifndef juce_UseDebuggingNewOperator
101 #define juce_UseDebuggingNewOperator
105 #endif // __JUCE_MEMORY_JUCEHEADER__