30 #if ! (defined (DOXYGEN) || JUCE_EXCEPTIONS_DISABLED)
31 namespace HeapBlockHelper
33 template <
bool shouldThrow>
34 struct ThrowOnFail {
static void checkPointer (
void*) {} };
37 struct ThrowOnFail<true> {
static void checkPointer (
void* data) {
if (data ==
nullptr)
throw std::bad_alloc(); } };
89 template <
class ElementType,
bool throwOnFailure = false>
93 template <
class OtherElementType>
94 using AllowConversion =
typename std::enable_if<std::is_base_of<typename std::remove_pointer<ElementType>::type,
95 typename std::remove_pointer<OtherElementType>::type>::value>::type;
114 template <
typename SizeType>
116 : data (static_cast<ElementType*> (std::
malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
118 throwOnAllocationFailure();
126 template <
typename SizeType>
128 : data (static_cast<ElementType*> (initialiseToZero
129 ? std::
calloc (static_cast<size_t> (numElements), sizeof (ElementType))
130 : std::
malloc (static_cast<size_t> (numElements) * sizeof (ElementType))))
132 throwOnAllocationFailure();
147 other.data =
nullptr;
153 std::swap (data, other.data);
161 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
163 : data (
reinterpret_cast<ElementType*
> (other.data))
165 other.data =
nullptr;
172 template <
class OtherElementType,
bool otherThrowOnFailure,
typename = AllowConversion<OtherElementType>>
176 data =
reinterpret_cast<ElementType*
> (other.data);
177 other.data =
nullptr;
186 inline operator ElementType*()
const noexcept {
return data; }
192 inline ElementType*
get() const noexcept {
return data; }
198 inline ElementType*
getData() const noexcept {
return data; }
204 inline operator void*()
const noexcept {
return static_cast<void*
> (data); }
210 inline operator const void*()
const noexcept {
return static_cast<const void*
> (data); }
216 inline ElementType*
operator->() const noexcept {
return data; }
222 template <
typename IndexType>
223 ElementType&
operator[] (IndexType index)
const noexcept {
return data [index]; }
228 template <
typename IndexType>
229 ElementType*
operator+ (IndexType index)
const noexcept {
return data + index; }
235 inline bool operator== (
const ElementType* otherPointer)
const noexcept {
return otherPointer == data; }
240 inline bool operator!= (
const ElementType* otherPointer)
const noexcept {
return otherPointer != data; }
255 template <
typename SizeType>
256 void malloc (SizeType newNumElements,
size_t elementSize =
sizeof (ElementType))
259 data =
static_cast<ElementType*
> (std::malloc (
static_cast<size_t> (newNumElements) * elementSize));
260 throwOnAllocationFailure();
266 template <
typename SizeType>
267 void calloc (SizeType newNumElements,
const size_t elementSize =
sizeof (ElementType))
270 data =
static_cast<ElementType*
> (std::calloc (
static_cast<size_t> (newNumElements), elementSize));
271 throwOnAllocationFailure();
278 template <
typename SizeType>
279 void allocate (SizeType newNumElements,
bool initialiseToZero)
282 data =
static_cast<ElementType*
> (initialiseToZero
283 ? std::calloc (
static_cast<size_t> (newNumElements),
sizeof (ElementType))
284 : std::malloc (
static_cast<size_t> (newNumElements) *
sizeof (ElementType)));
285 throwOnAllocationFailure();
293 template <
typename SizeType>
294 void realloc (SizeType newNumElements,
size_t elementSize =
sizeof (ElementType))
296 data =
static_cast<ElementType*
> (data ==
nullptr ? std::malloc (
static_cast<size_t> (newNumElements) * elementSize)
297 : std::realloc (data,
static_cast<size_t> (newNumElements) * elementSize));
298 throwOnAllocationFailure();
313 template <
bool otherBlockThrows>
316 std::swap (data, other.data);
323 template <
typename SizeType>
324 void clear (SizeType numElements) noexcept
326 zeromem (data,
sizeof (ElementType) *
static_cast<size_t> (numElements));
334 ElementType* data =
nullptr;
336 void throwOnAllocationFailure()
const
338 #if JUCE_EXCEPTIONS_DISABLED
339 jassert (data !=
nullptr);
341 HeapBlockHelper::ThrowOnFail<throwOnFailure>::checkPointer (data);
345 template <
class OtherElementType,
bool otherThrowOnFailure>
348 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
350 JUCE_PREVENT_HEAP_ALLOCATION
Very simple container class to hold a pointer to some data on the heap.
HeapBlock & operator=(HeapBlock &&other) noexcept
Move assignment operator.
HeapBlock(HeapBlock< OtherElementType, otherThrowOnFailure > &&other) noexcept
Converting move constructor.
ElementType * get() const noexcept
Returns a raw pointer to the allocated data.
ElementType Type
This typedef can be used to get the type of the heapblock's elements.
void clear(SizeType numElements) noexcept
This fills the block with zeros, up to the number of elements specified.
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Swaps this object's data with the data of another HeapBlock.
bool operator!=(const ElementType *otherPointer) const noexcept
Compares the pointer with another pointer.
HeapBlock(SizeType numElements, bool initialiseToZero)
Creates a HeapBlock containing a number of elements.
void malloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory.
ElementType * getData() const noexcept
Returns a raw pointer to the allocated data.
HeapBlock(HeapBlock &&other) noexcept
Move constructor.
ElementType * operator+(IndexType index) const noexcept
Returns a pointer to a data element at an offset from the start of the array.
HeapBlock()=default
Creates a HeapBlock which is initially just a null pointer.
void allocate(SizeType newNumElements, bool initialiseToZero)
Allocates a specified amount of memory and optionally clears it.
HeapBlock(SizeType numElements)
Creates a HeapBlock containing a number of elements.
bool operator==(const ElementType *otherPointer) const noexcept
Compares the pointer with another pointer.
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Re-allocates a specified amount of memory.
ElementType * operator->() const noexcept
Lets you use indirect calls to the first element in the array.
void free() noexcept
Frees any currently-allocated data.
ElementType & operator[](IndexType index) const noexcept
Returns a reference to one of the data elements.
void calloc(SizeType newNumElements, const size_t elementSize=sizeof(ElementType))
Allocates a specified amount of memory and clears it.