OpenShot Audio Library | OpenShotAudio  0.6.0
juce_Memory.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
28 inline void zeromem (void* memory, size_t numBytes) noexcept { memset (memory, 0, numBytes); }
29 
31 template <typename Type>
32 inline void zerostruct (Type& structure) noexcept { memset ((void*) &structure, 0, sizeof (structure)); }
33 
39 template <typename Type>
40 inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
41 
44 template <typename Type, typename IntegerType>
45 inline Type* snapPointerToAlignment (Type* basePointer, IntegerType alignmentBytes) noexcept
46 {
47  return (Type*) ((((size_t) basePointer) + (alignmentBytes - 1)) & ~(alignmentBytes - 1));
48 }
49 
53 template <typename Type1, typename Type2>
54 inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
55 
59 template <class Type>
60 inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
61 
62 //==============================================================================
64 template <typename Type>
65 inline Type readUnaligned (const void* srcPtr) noexcept
66 {
67  Type value;
68  memcpy (&value, srcPtr, sizeof (Type));
69  return value;
70 }
71 
73 template <typename Type>
74 inline void writeUnaligned (void* dstPtr, Type value) noexcept
75 {
76  memcpy (dstPtr, &value, sizeof (Type));
77 }
78 
79 //==============================================================================
87 template <typename Type>
88 inline Type unalignedPointerCast (void* ptr) noexcept
89 {
90  static_assert (std::is_pointer_v<Type>);
91  return reinterpret_cast<Type> (ptr);
92 }
93 
101 template <typename Type>
102 inline Type unalignedPointerCast (const void* ptr) noexcept
103 {
104  static_assert (std::is_pointer_v<Type>);
105  return reinterpret_cast<Type> (ptr);
106 }
107 
112 template <typename Type, typename IntegerType>
113 inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept
114 {
115  return unalignedPointerCast<Type*> (reinterpret_cast<char*> (basePointer) + bytes);
116 }
117 
122 template <typename Type, typename IntegerType>
123 inline const Type* addBytesToPointer (const Type* basePointer, IntegerType bytes) noexcept
124 {
125  return unalignedPointerCast<const Type*> (reinterpret_cast<const char*> (basePointer) + bytes);
126 }
127 
128 //==============================================================================
129 #if JUCE_MAC || JUCE_IOS || DOXYGEN
130 
136  class JUCE_API ScopedAutoReleasePool
137  {
138  public:
139  ScopedAutoReleasePool();
140  ~ScopedAutoReleasePool();
141 
142  private:
143  void* pool;
144 
145  JUCE_DECLARE_NON_COPYABLE (ScopedAutoReleasePool)
146  };
147 
153 #if (JUCE_COMPILER_SUPPORTS_ARC && defined (__OBJC__)) || DOXYGEN
154  #define JUCE_AUTORELEASEPOOL @autoreleasepool
155 #else
156  #define JUCE_AUTORELEASEPOOL const juce::ScopedAutoReleasePool JUCE_JOIN_MACRO (autoReleasePool_, __LINE__);
157 #endif
158 
159 #else
160  #define JUCE_AUTORELEASEPOOL
161 #endif
162 
163 //==============================================================================
164 /* In a Windows DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for
165  allocating all the objects - that way all juce objects in the DLL and in the host will live in the same heap,
166  avoiding problems when an object is created in one module and passed across to another where it is deleted.
167  By piggy-backing on the JUCE_LEAK_DETECTOR macro, these allocators can be injected into most juce classes.
168 */
169 #if JUCE_MSVC && (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD)) && ! (JUCE_DISABLE_DLL_ALLOCATORS || DOXYGEN)
170  extern JUCE_API void* juceDLL_malloc (size_t);
171  extern JUCE_API void juceDLL_free (void*);
172 
173  #define JUCE_LEAK_DETECTOR(OwnerClass) public:\
174  static void* operator new (size_t sz) { return juce::juceDLL_malloc (sz); } \
175  static void* operator new (size_t, void* p) { return p; } \
176  static void operator delete (void* p) { juce::juceDLL_free (p); } \
177  static void operator delete (void*, void*) {}
178 #endif
179 
180 //==============================================================================
184 #ifndef juce_UseDebuggingNewOperator
185  #define juce_UseDebuggingNewOperator
186 #endif
187 
196 template <typename T>
197 std::unique_ptr<T> rawToUniquePtr (T* ptr)
198 {
199  return std::unique_ptr<T> (ptr);
200 }
201 
202 } // namespace juce