From 8092d7d695d6feeec829f654b9cb521b7f5dcaff Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 21 Jun 2012 11:59:56 +0100 Subject: [PATCH] Added a HeapBlock constructor. --- modules/juce_core/memory/juce_HeapBlock.h | 42 +++++++++++++++-------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/modules/juce_core/memory/juce_HeapBlock.h b/modules/juce_core/memory/juce_HeapBlock.h index 4f6a66eaaa..ee58e6c1f9 100644 --- a/modules/juce_core/memory/juce_HeapBlock.h +++ b/modules/juce_core/memory/juce_HeapBlock.h @@ -87,6 +87,13 @@ template 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 (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 (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 (std::calloc (newNumElements, sizeof (ElementType))); - else - data = static_cast (std::malloc (newNumElements * sizeof (ElementType))); - + data = static_cast (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 (std::malloc (newNumElements * elementSize)); - else - data = static_cast (std::realloc (data, newNumElements * elementSize)); - + data = static_cast (data == nullptr ? std::malloc (newNumElements * elementSize) + : std::realloc (data, newNumElements * elementSize)); throwOnAllocationFailure(); }