26 #if ! (defined (DOXYGEN) || JUCE_EXCEPTIONS_DISABLED) 27 namespace HeapBlockHelper
29 template <
bool shouldThrow>
30 struct ThrowOnFail {
static void checkPointer (
void*) {} };
33 struct ThrowOnFail<true> {
static void checkPointer (
void* data) {
if (data ==
nullptr)
throw std::bad_alloc(); } };
85 template <
class ElementType,
bool throwOnFailure = false>
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;
110 template <
typename SizeType>
112 : data (static_cast<ElementType*> (
std::malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
114 throwOnAllocationFailure();
122 template <
typename SizeType>
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))))
128 throwOnAllocationFailure();
143 other.data =
nullptr;
149 std::swap (data, other.data);
157 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
159 : data (reinterpret_cast<ElementType*> (other.data))
161 other.data =
nullptr;
168 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
172 data =
reinterpret_cast<ElementType*
> (other.data);
173 other.data =
nullptr;
182 inline operator ElementType*()
const noexcept {
return data; }
188 inline ElementType*
get()
const noexcept {
return data; }
194 inline ElementType*
getData() const noexcept {
return data; }
200 inline operator void*()
const noexcept {
return static_cast<void*
> (data); }
206 inline operator const void*()
const noexcept {
return static_cast<const void*
> (data); }
212 inline ElementType*
operator->() const noexcept {
return data; }
218 template <
typename IndexType>
219 ElementType& operator[] (IndexType index)
const noexcept {
return data [index]; }
224 template <
typename IndexType>
225 ElementType* operator+ (IndexType index)
const noexcept {
return data + index; }
231 inline bool operator== (
const ElementType* otherPointer)
const noexcept {
return otherPointer == data; }
236 inline bool operator!= (
const ElementType* otherPointer)
const noexcept {
return otherPointer != data; }
251 template <
typename SizeType>
252 void malloc (SizeType newNumElements,
size_t elementSize =
sizeof (ElementType))
255 data =
static_cast<ElementType*
> (std::malloc (static_cast<size_t> (newNumElements) * elementSize));
256 throwOnAllocationFailure();
262 template <
typename SizeType>
263 void calloc (SizeType newNumElements,
const size_t elementSize =
sizeof (ElementType))
266 data =
static_cast<ElementType*
> (std::calloc (static_cast<size_t> (newNumElements), elementSize));
267 throwOnAllocationFailure();
274 template <
typename SizeType>
275 void allocate (SizeType newNumElements,
bool initialiseToZero)
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)));
281 throwOnAllocationFailure();
289 template <
typename SizeType>
290 void realloc (SizeType newNumElements,
size_t elementSize =
sizeof (ElementType))
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));
294 throwOnAllocationFailure();
309 template <
bool otherBlockThrows>
312 std::swap (data, other.data);
319 template <
typename SizeType>
320 void clear (SizeType numElements) noexcept
322 zeromem (data,
sizeof (ElementType) * static_cast<size_t> (numElements));
330 ElementType* data =
nullptr;
332 void throwOnAllocationFailure()
const 334 #if JUCE_EXCEPTIONS_DISABLED 335 jassert (data !=
nullptr);
337 HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (data);
341 template <
class OtherElementType,
bool otherThrowOnFailure>
344 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD)) 346 JUCE_PREVENT_HEAP_ALLOCATION
void allocate(SizeType newNumElements, bool initialiseToZero)
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
ElementType * getData() const noexcept
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))
HeapBlock(HeapBlock &&other) noexcept
HeapBlock(HeapBlock< OtherElementType, otherThrowOnFailure > &&other) noexcept
ElementType * operator->() const noexcept
void clear(SizeType numElements) noexcept
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
HeapBlock(SizeType numElements)
HeapBlock(SizeType numElements, bool initialiseToZero)