29 #ifndef JUCE_HEAPBLOCK_H_INCLUDED
30 #define JUCE_HEAPBLOCK_H_INCLUDED
33 namespace HeapBlockHelper
35 template <
bool shouldThrow>
89 template <
class ElementType,
bool throwOnFailure = false>
112 : data (static_cast<ElementType*> (std::
malloc (numElements * sizeof (ElementType))))
114 throwOnAllocationFailure();
122 HeapBlock (
const size_t numElements,
const bool initialiseToZero)
123 : data (static_cast<ElementType*> (initialiseToZero
124 ? std::
calloc (numElements, sizeof (ElementType))
125 : std::
malloc (numElements * sizeof (ElementType))))
127 throwOnAllocationFailure();
138 #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
142 other.data =
nullptr;
147 std::swap (data, other.data);
157 inline operator ElementType*()
const noexcept {
return data; }
169 inline operator void*()
const noexcept {
return static_cast<void*
> (data); }
175 inline operator const void*()
const noexcept {
return static_cast<const void*
> (data); }
187 template <
typename IndexType>
193 template <
typename IndexType>
200 inline bool operator== (
const ElementType*
const otherPointer)
const noexcept {
return otherPointer == data; }
205 inline bool operator!= (
const ElementType*
const otherPointer)
const noexcept {
return otherPointer != data; }
220 void malloc (
const size_t newNumElements,
const size_t elementSize =
sizeof (ElementType))
223 data =
static_cast<ElementType*
> (std::malloc (newNumElements * elementSize));
224 throwOnAllocationFailure();
230 void calloc (
const size_t newNumElements,
const size_t elementSize =
sizeof (ElementType))
233 data =
static_cast<ElementType*
> (std::calloc (newNumElements, elementSize));
234 throwOnAllocationFailure();
241 void allocate (
const size_t newNumElements,
bool initialiseToZero)
244 data =
static_cast<ElementType*
> (initialiseToZero
245 ? std::calloc (newNumElements,
sizeof (ElementType))
246 : std::malloc (newNumElements *
sizeof (ElementType)));
247 throwOnAllocationFailure();
255 void realloc (
const size_t newNumElements,
const size_t elementSize =
sizeof (ElementType))
257 data =
static_cast<ElementType*
> (data ==
nullptr ? std::malloc (newNumElements * elementSize)
258 : std::realloc (data, newNumElements * elementSize));
259 throwOnAllocationFailure();
274 template <
bool otherBlockThrows>
277 std::swap (data, other.data);
286 zeromem (data,
sizeof (ElementType) * numElements);
296 void throwOnAllocationFailure()
const
301 #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
308 #endif // JUCE_HEAPBLOCK_H_INCLUDED
bool operator==(const ElementType *const otherPointer) const noexcept
Definition: juce_HeapBlock.h:200
HeapBlock(const size_t numElements)
Definition: juce_HeapBlock.h:111
#define true
Definition: ordinals.h:82
bool operator!=(const ElementType *const otherPointer) const noexcept
Definition: juce_HeapBlock.h:205
#define noexcept
Definition: juce_CompilerSupport.h:141
HeapBlock(const size_t numElements, const bool initialiseToZero)
Definition: juce_HeapBlock.h:122
void zeromem(void *memory, size_t numBytes) noexcept
Definition: juce_Memory.h:34
void malloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType))
Definition: juce_HeapBlock.h:220
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Definition: juce_HeapBlock.h:275
ElementType Type
Definition: juce_HeapBlock.h:290
Definition: juce_HeapBlock.h:36
void realloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType))
Definition: juce_HeapBlock.h:255
ElementType * operator->() const noexcept
Definition: juce_HeapBlock.h:181
ElementType & operator[](IndexType index) const noexcept
Definition: juce_HeapBlock.h:188
static void check(void *data)
Definition: juce_HeapBlock.h:39
void clear(size_t numElements) noexcept
Definition: juce_HeapBlock.h:284
ElementType * operator+(IndexType index) const noexcept
Definition: juce_HeapBlock.h:194
void calloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType))
Definition: juce_HeapBlock.h:230
Definition: juce_HeapBlock.h:90
~HeapBlock()
Definition: juce_HeapBlock.h:133
ElementType * getData() const noexcept
Definition: juce_HeapBlock.h:163
HeapBlock() noexcept
Definition: juce_HeapBlock.h:99
JSAMPIMAGE data
Definition: jpeglib.h:945
void allocate(const size_t newNumElements, bool initialiseToZero)
Definition: juce_HeapBlock.h:241
static void check(void *)
Definition: juce_HeapBlock.h:36
void free() noexcept
Definition: juce_HeapBlock.h:265