OpenShot Library | libopenshot-audio 0.2.0
juce_Memory.h
1
2/** @weakgroup juce_core-memory
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 The code included in this file is provided under the terms of the ISC license
15 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16 To use, copy, modify, and/or distribute this software for any purpose with or
17 without fee is hereby granted provided that the above copyright notice and
18 this permission notice appear in all copies.
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29
30//==============================================================================
31/** Fills a block of memory with zeros. */
32inline void zeromem (void* memory, size_t numBytes) noexcept { memset (memory, 0, numBytes); }
33
34/** Overwrites a structure or object with zeros. */
35template <typename Type>
36inline void zerostruct (Type& structure) noexcept { memset ((void*) &structure, 0, sizeof (structure)); }
37
38/** Delete an object pointer, and sets the pointer to null.
39
40 Remember that it's not good c++ practice to use delete directly - always try to use a std::unique_ptr
41 or other automatic lifetime-management system rather than resorting to deleting raw pointers!
42*/
43template <typename Type>
44inline void deleteAndZero (Type& pointer) { delete pointer; pointer = nullptr; }
45
46/** A handy function which adds a number of bytes to any type of pointer and returns the result.
47 This can be useful to avoid casting pointers to a char* and back when you want to move them by
48 a specific number of bytes,
49*/
50template <typename Type, typename IntegerType>
51inline Type* addBytesToPointer (Type* basePointer, IntegerType bytes) noexcept { return (Type*) (const_cast<char*> (reinterpret_cast<const char*> (basePointer)) + bytes); }
52
53/** A handy function to round up a pointer to the nearest multiple of a given number of bytes.
54 alignmentBytes must be a power of two. */
55template <typename Type, typename IntegerType>
56inline Type* snapPointerToAlignment (Type* basePointer, IntegerType alignmentBytes) noexcept
57{
58 return (Type*) ((((size_t) basePointer) + (alignmentBytes - 1)) & ~(alignmentBytes - 1));
59}
60
61/** A handy function which returns the difference between any two pointers, in bytes.
62 The address of the second pointer is subtracted from the first, and the difference in bytes is returned.
63*/
64template <typename Type1, typename Type2>
65inline int getAddressDifference (Type1* pointer1, Type2* pointer2) noexcept { return (int) (((const char*) pointer1) - (const char*) pointer2); }
66
67/** If a pointer is non-null, this returns a new copy of the object that it points to, or safely returns
68 nullptr if the pointer is null.
69*/
70template <class Type>
71inline Type* createCopyIfNotNull (const Type* objectToCopy) { return objectToCopy != nullptr ? new Type (*objectToCopy) : nullptr; }
72
73//==============================================================================
74/** A handy function to read un-aligned memory without a performance penalty or bus-error. */
75template <typename Type>
76inline Type readUnaligned (const void* srcPtr) noexcept
77{
78 Type value;
79 memcpy (&value, srcPtr, sizeof (Type));
80 return value;
81}
82
83/** A handy function to write un-aligned memory without a performance penalty or bus-error. */
84template <typename Type>
85inline void writeUnaligned (void* dstPtr, Type value) noexcept
86{
87 memcpy (dstPtr, &value, sizeof (Type));
88}
89
90//==============================================================================
91#if JUCE_MAC || JUCE_IOS || DOXYGEN
92
93 /** A handy C++ wrapper that creates and deletes an NSAutoreleasePool object using RAII.
94 You should use the JUCE_AUTORELEASEPOOL macro to create a local auto-release pool on the stack.
95
96 @tags{Core}
97 */
99 {
100 public:
103
104 private:
105 void* pool;
106
107 JUCE_DECLARE_NON_COPYABLE (ScopedAutoReleasePool)
108 };
109
110 /** A macro that can be used to easily declare a local ScopedAutoReleasePool
111 object for RAII-based obj-C autoreleasing.
112 Because this may use the \@autoreleasepool syntax, you must follow the macro with
113 a set of braces to mark the scope of the pool.
114 */
115#if (JUCE_COMPILER_SUPPORTS_ARC && defined (__OBJC__)) || DOXYGEN
116 #define JUCE_AUTORELEASEPOOL @autoreleasepool
117#else
118 #define JUCE_AUTORELEASEPOOL const juce::ScopedAutoReleasePool JUCE_JOIN_MACRO (autoReleasePool_, __LINE__);
119#endif
120
121#else
122 #define JUCE_AUTORELEASEPOOL
123#endif
124
125//==============================================================================
126/* In a Windows DLL build, we'll expose some malloc/free functions that live inside the DLL, and use these for
127 allocating all the objects - that way all juce objects in the DLL and in the host will live in the same heap,
128 avoiding problems when an object is created in one module and passed across to another where it is deleted.
129 By piggy-backing on the JUCE_LEAK_DETECTOR macro, these allocators can be injected into most juce classes.
130*/
131#if JUCE_MSVC && (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD)) && ! (JUCE_DISABLE_DLL_ALLOCATORS || DOXYGEN)
132 extern JUCE_API void* juceDLL_malloc (size_t);
133 extern JUCE_API void juceDLL_free (void*);
134
135 #define JUCE_LEAK_DETECTOR(OwnerClass) public:\
136 static void* operator new (size_t sz) { return juce::juceDLL_malloc (sz); } \
137 static void* operator new (size_t, void* p) { return p; } \
138 static void operator delete (void* p) { juce::juceDLL_free (p); } \
139 static void operator delete (void*, void*) {}
140#endif
141
142//==============================================================================
143/** (Deprecated) This was a Windows-specific way of checking for object leaks - now please
144 use the JUCE_LEAK_DETECTOR instead.
145*/
146#ifndef juce_UseDebuggingNewOperator
147 #define juce_UseDebuggingNewOperator
148#endif
149
150} // namespace juce
151
152/** @}*/
A handy C++ wrapper that creates and deletes an NSAutoreleasePool object using RAII.
Definition juce_Memory.h:99
#define JUCE_API
This macro is added to all JUCE public class declarations.