LMMS
Loading...
Searching...
No Matches
juce_HeapBlock.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#if ! (DOXYGEN || JUCE_EXCEPTIONS_DISABLED)
28{
29 template <bool shouldThrow>
30 struct ThrowOnFail { static void checkPointer (void*) {} };
31
32 template <>
33 struct ThrowOnFail<true> { static void checkPointer (void* data) { if (data == nullptr) throw std::bad_alloc(); } };
34}
35#endif
36
37//==============================================================================
85template <class ElementType, bool throwOnFailure = false>
87{
88private:
89 template <class OtherElementType>
90 using AllowConversion = typename std::enable_if<std::is_base_of<typename std::remove_pointer<ElementType>::type,
91 typename std::remove_pointer<OtherElementType>::type>::value>::type;
92
93public:
94 //==============================================================================
100 HeapBlock() = default;
101
110 template <typename SizeType, std::enable_if_t<std::is_convertible<SizeType, int>::value, int> = 0>
111 explicit HeapBlock (SizeType numElements)
112 : data (static_cast<ElementType*> (std::malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
113 {
115 }
116
122 template <typename SizeType, std::enable_if_t<std::is_convertible<SizeType, int>::value, int> = 0>
123 HeapBlock (SizeType numElements, bool initialiseToZero)
124 : data (static_cast<ElementType*> (initialiseToZero
125 ? std::calloc (static_cast<size_t> (numElements), sizeof (ElementType))
126 : std::malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
127 {
129 }
130
135 {
136 std::free (data);
137 }
138
141 : data (other.data)
142 {
143 other.data = nullptr;
144 }
145
147 HeapBlock& operator= (HeapBlock&& other) noexcept
148 {
149 std::swap (data, other.data);
150 return *this;
151 }
152
157 template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
159 : data (reinterpret_cast<ElementType*> (other.data))
160 {
161 other.data = nullptr;
162 }
163
168 template <class OtherElementType, bool otherThrowOnFailure, typename = AllowConversion<OtherElementType>>
170 {
171 free();
172 data = reinterpret_cast<ElementType*> (other.data);
173 other.data = nullptr;
174 return *this;
175 }
176
177 //==============================================================================
182 inline operator ElementType*() const noexcept { return data; }
183
188 inline ElementType* get() const noexcept { return data; }
189
194 inline ElementType* getData() const noexcept { return data; }
195
200 inline operator void*() const noexcept { return static_cast<void*> (data); }
201
206 inline operator const void*() const noexcept { return static_cast<const void*> (data); }
207
212 inline ElementType* operator->() const noexcept { return data; }
213
218 template <typename IndexType>
219 ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
220
224 template <typename IndexType>
225 ElementType* operator+ (IndexType index) const noexcept { return data + index; }
226
227 //==============================================================================
231 inline bool operator== (const ElementType* otherPointer) const noexcept { return otherPointer == data; }
232
236 inline bool operator!= (const ElementType* otherPointer) const noexcept { return otherPointer != data; }
237
238 //==============================================================================
251 template <typename SizeType>
252 void malloc (SizeType newNumElements, size_t elementSize = sizeof (ElementType))
253 {
254 std::free (data);
255 data = static_cast<ElementType*> (std::malloc (static_cast<size_t> (newNumElements) * elementSize));
257 }
258
262 template <typename SizeType>
263 void calloc (SizeType newNumElements, const size_t elementSize = sizeof (ElementType))
264 {
265 std::free (data);
266 data = static_cast<ElementType*> (std::calloc (static_cast<size_t> (newNumElements), elementSize));
268 }
269
274 template <typename SizeType>
275 void allocate (SizeType newNumElements, bool initialiseToZero)
276 {
277 std::free (data);
278 data = static_cast<ElementType*> (initialiseToZero
279 ? std::calloc (static_cast<size_t> (newNumElements), sizeof (ElementType))
280 : std::malloc (static_cast<size_t> (newNumElements) * sizeof (ElementType)));
282 }
283
289 template <typename SizeType>
290 void realloc (SizeType newNumElements, size_t elementSize = sizeof (ElementType))
291 {
292 data = static_cast<ElementType*> (data == nullptr ? std::malloc (static_cast<size_t> (newNumElements) * elementSize)
293 : std::realloc (data, static_cast<size_t> (newNumElements) * elementSize));
295 }
296
301 {
302 std::free (data);
303 data = nullptr;
304 }
305
309 template <bool otherBlockThrows>
311 {
312 std::swap (data, other.data);
313 }
314
319 template <typename SizeType>
320 void clear (SizeType numElements) noexcept
321 {
322 zeromem (data, sizeof (ElementType) * static_cast<size_t> (numElements));
323 }
324
326 using Type = ElementType;
327
328private:
329 //==============================================================================
330 ElementType* data = nullptr;
331
333 {
334 #if JUCE_EXCEPTIONS_DISABLED
335 jassert (data != nullptr); // without exceptions, you'll need to find a better way to handle this failure case.
336 #else
338 #endif
339 }
340
341 template <class OtherElementType, bool otherThrowOnFailure>
342 friend class HeapBlock;
343
344 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
346 JUCE_PREVENT_HEAP_ALLOCATION // Creating a 'new HeapBlock' would be missing the point!
347 #endif
348};
349
350} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
char * data
Definition juce_HeapBlock.h:330
~HeapBlock()
Definition juce_HeapBlock.h:134
HeapBlock(SizeType numElements)
Definition juce_HeapBlock.h:111
HeapBlock(HeapBlock< OtherElementType, otherThrowOnFailure > &&other) noexcept
Definition juce_HeapBlock.h:158
ElementType Type
Definition juce_HeapBlock.h:326
void clear(SizeType numElements) noexcept
Definition juce_HeapBlock.h:320
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Definition juce_HeapBlock.h:310
typename std::enable_if< std::is_base_of< typename std::remove_pointer< ElementType >::type, typename std::remove_pointer< OtherElementType >::type >::value >::type AllowConversion
Definition juce_HeapBlock.h:90
void malloc(SizeType newNumElements, size_t elementSize=sizeof(char))
Definition juce_HeapBlock.h:252
friend class HeapBlock
Definition juce_HeapBlock.h:342
HeapBlock(HeapBlock &&other) noexcept
Definition juce_HeapBlock.h:140
void throwOnAllocationFailure() const
Definition juce_HeapBlock.h:332
ElementType * get() const noexcept
Definition juce_HeapBlock.h:188
ElementType * getData() const noexcept
Definition juce_HeapBlock.h:194
HeapBlock()=default
void allocate(SizeType newNumElements, bool initialiseToZero)
Definition juce_HeapBlock.h:275
HeapBlock(SizeType numElements, bool initialiseToZero)
Definition juce_HeapBlock.h:123
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Definition juce_HeapBlock.h:290
void free() noexcept
Definition juce_HeapBlock.h:300
ElementType * operator->() const noexcept
Definition juce_HeapBlock.h:212
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(char))
Definition juce_HeapBlock.h:263
static PuglViewHint int value
Definition pugl.h:1708
JSAMPIMAGE data
Definition jpeglib.h:945
#define jassert(expression)
#define JUCE_DECLARE_NON_COPYABLE(className)
#define JUCE_PREVENT_HEAP_ALLOCATION
Definition juce_HeapBlock.h:28
Definition carla_juce.cpp:31
void zeromem(void *memory, size_t numBytes) noexcept
Definition juce_Memory.h:28
Definition juce_Uuid.h:141
#define true
Definition ordinals.h:82
static void checkPointer(void *data)
Definition juce_HeapBlock.h:33
Definition juce_HeapBlock.h:30
static void checkPointer(void *)
Definition juce_HeapBlock.h:30
#define const
Definition zconf.h:137