mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-02-05 03:50:07 +00:00
Added a HeapBlock constructor.
This commit is contained in:
parent
085b08611b
commit
8092d7d695
1 changed files with 28 additions and 14 deletions
|
|
@ -87,6 +87,13 @@ template <class ElementType, bool throwOnFailure = false>
|
|||
class HeapBlock
|
||||
{
|
||||
public:
|
||||
/** Flags used to indicate whether a newly allocated block should be cleared or not. */
|
||||
enum InitialisationState
|
||||
{
|
||||
leaveUnitialised = 0,
|
||||
clearToZero
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a HeapBlock which is initially just a null pointer.
|
||||
|
||||
|
|
@ -102,7 +109,8 @@ public:
|
|||
The contents of the block are undefined, as it will have been created by a
|
||||
malloc call.
|
||||
|
||||
If you want an array of zero values, you can use the calloc() method instead.
|
||||
If you want an array of zero values, you can use the calloc() method or the
|
||||
other constructor that takes an InitialisationState parameter.
|
||||
*/
|
||||
explicit HeapBlock (const size_t numElements)
|
||||
: data (static_cast <ElementType*> (std::malloc (numElements * sizeof (ElementType))))
|
||||
|
|
@ -110,8 +118,20 @@ public:
|
|||
throwOnAllocationFailure();
|
||||
}
|
||||
|
||||
/** Destructor.
|
||||
/** Creates a HeapBlock containing a number of elements.
|
||||
|
||||
The initState parameter determines whether the new memory should be cleared, or
|
||||
left uninitialised.
|
||||
*/
|
||||
HeapBlock (const size_t numElements, InitialisationState initState)
|
||||
: data (static_cast <ElementType*> (initState == leaveUnitialised
|
||||
? std::malloc (numElements * sizeof (ElementType))
|
||||
: std::calloc (numElements, sizeof (ElementType))))
|
||||
{
|
||||
throwOnAllocationFailure();
|
||||
}
|
||||
|
||||
/** Destructor.
|
||||
This will free the data, if any has been allocated.
|
||||
*/
|
||||
~HeapBlock()
|
||||
|
|
@ -222,15 +242,12 @@ public:
|
|||
This does the same job as either malloc() or calloc(), depending on the
|
||||
initialiseToZero parameter.
|
||||
*/
|
||||
void allocate (const size_t newNumElements, const bool initialiseToZero)
|
||||
void allocate (const size_t newNumElements, bool initialiseToZero)
|
||||
{
|
||||
std::free (data);
|
||||
|
||||
if (initialiseToZero)
|
||||
data = static_cast <ElementType*> (std::calloc (newNumElements, sizeof (ElementType)));
|
||||
else
|
||||
data = static_cast <ElementType*> (std::malloc (newNumElements * sizeof (ElementType)));
|
||||
|
||||
data = static_cast <ElementType*> (initialiseToZero
|
||||
? std::calloc (newNumElements, sizeof (ElementType))
|
||||
: std::malloc (newNumElements * sizeof (ElementType)));
|
||||
throwOnAllocationFailure();
|
||||
}
|
||||
|
||||
|
|
@ -241,11 +258,8 @@ public:
|
|||
*/
|
||||
void realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType))
|
||||
{
|
||||
if (data == nullptr)
|
||||
data = static_cast <ElementType*> (std::malloc (newNumElements * elementSize));
|
||||
else
|
||||
data = static_cast <ElementType*> (std::realloc (data, newNumElements * elementSize));
|
||||
|
||||
data = static_cast <ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
|
||||
: std::realloc (data, newNumElements * elementSize));
|
||||
throwOnAllocationFailure();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue