JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_Memory.h
Go to the documentation of this file.
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
23namespace juce
24{
25
26//==============================================================================
28inline void zeromem (void* memory, size_t numBytes) noexcept { memset (memory, 0, numBytes); }
29
31template <typename Type>
32inline void zerostruct (Type& structure) noexcept { memset ((void*) &structure, 0, sizeof (structure)); }
33
39template <typename Type>
40inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
41
44template <typename Type, typename IntegerType>
46{
47 return (Type*) ((((size_t) basePointer) + (alignmentBytes - 1)) & ~(alignmentBytes - 1));
48}
49
53template <typename Type1, typename Type2>
54inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
55
59template <class Type>
60inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
61
62//==============================================================================
64template <typename Type>
65inline Type readUnaligned (const void* srcPtr) noexcept
66{
67 Type value;
68 memcpy (&value, srcPtr, sizeof (Type));
69 return value;
70}
71
73template <typename Type>
74inline void writeUnaligned (void* dstPtr, Type value) noexcept
75{
76 memcpy (dstPtr, &value, sizeof (Type));
77}
78
79//==============================================================================
87template <typename Type>
88inline Type unalignedPointerCast (void* ptr) noexcept
89{
90 static_assert (std::is_pointer_v<Type>);
91 return reinterpret_cast<Type> (ptr);
92}
93
101template <typename Type>
102inline Type unalignedPointerCast (const void* ptr) noexcept
103{
104 static_assert (std::is_pointer_v<Type>);
105 return reinterpret_cast<Type> (ptr);
106}
107
112template <typename Type, typename IntegerType>
113inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept
114{
115 return unalignedPointerCast<Type*> (reinterpret_cast<char*> (basePointer) + bytes);
116}
117
122template <typename Type, typename IntegerType>
123inline 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
137 {
138 public:
141
142 private:
143 void* pool;
144
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
196template <typename T>
198{
199 return std::unique_ptr<T> (ptr);
200}
201
202} // namespace juce
A handy C++ wrapper that creates and deletes an NSAutoreleasePool object using RAII.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
memcpy
memset
JUCE Namespace.
Type * createCopyIfNotNull(const Type *objectToCopy)
If a pointer is non-null, this returns a new copy of the object that it points to,...
Definition juce_Memory.h:60
void zerostruct(Type &structure) noexcept
Overwrites a structure or object with zeros.
Definition juce_Memory.h:32
int getAddressDifference(Type1 *pointer1, Type2 *pointer2) noexcept
A handy function which returns the difference between any two pointers, in bytes.
Definition juce_Memory.h:54
Type readUnaligned(const void *srcPtr) noexcept
A handy function to read un-aligned memory without a performance penalty or bus-error.
Definition juce_Memory.h:65
void deleteAndZero(Type &pointer)
Delete an object pointer, and sets the pointer to null.
Definition juce_Memory.h:40
Type * addBytesToPointer(Type *basePointer, IntegerType bytes) noexcept
A handy function which adds a number of bytes to any type of pointer and returns the result.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
void writeUnaligned(void *dstPtr, Type value) noexcept
A handy function to write un-aligned memory without a performance penalty or bus-error.
Definition juce_Memory.h:74
Type * snapPointerToAlignment(Type *basePointer, IntegerType alignmentBytes) noexcept
A handy function to round up a pointer to the nearest multiple of a given number of bytes.
Definition juce_Memory.h:45
std::unique_ptr< T > rawToUniquePtr(T *ptr)
Converts an owning raw pointer into a unique_ptr, deriving the type of the unique_ptr automatically.
void zeromem(void *memory, size_t numBytes) noexcept
Fills a block of memory with zeros.
Definition juce_Memory.h:28
typedef size_t