openshot-audio  0.1.6
juce_HeapBlock.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_HEAPBLOCK_H_INCLUDED
30 #define JUCE_HEAPBLOCK_H_INCLUDED
31 
32 #ifndef DOXYGEN
33 namespace HeapBlockHelper
34 {
35  template <bool shouldThrow>
36  struct ThrowOnFail { static void check (void*) {} };
37 
38  template<>
39  struct ThrowOnFail<true> { static void check (void* data) { if (data == nullptr) throw std::bad_alloc(); } };
40 }
41 #endif
42 
43 //==============================================================================
89 template <class ElementType, bool throwOnFailure = false>
90 class HeapBlock
91 {
92 public:
93  //==============================================================================
99  HeapBlock() noexcept : data (nullptr)
100  {
101  }
102 
111  explicit HeapBlock (const size_t numElements)
112  : data (static_cast<ElementType*> (std::malloc (numElements * sizeof (ElementType))))
113  {
114  throwOnAllocationFailure();
115  }
116 
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))))
126  {
127  throwOnAllocationFailure();
128  }
129 
134  {
135  std::free (data);
136  }
137 
138  #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
139  HeapBlock (HeapBlock&& other) noexcept
140  : data (other.data)
141  {
142  other.data = nullptr;
143  }
144 
145  HeapBlock& operator= (HeapBlock&& other) noexcept
146  {
147  std::swap (data, other.data);
148  return *this;
149  }
150  #endif
151 
152  //==============================================================================
157  inline operator ElementType*() const noexcept { return data; }
158 
163  inline ElementType* getData() const noexcept { return data; }
164 
169  inline operator void*() const noexcept { return static_cast<void*> (data); }
170 
175  inline operator const void*() const noexcept { return static_cast<const void*> (data); }
176 
181  inline ElementType* operator->() const noexcept { return data; }
182 
187  template <typename IndexType>
188  inline ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
189 
193  template <typename IndexType>
194  inline ElementType* operator+ (IndexType index) const noexcept { return data + index; }
195 
196  //==============================================================================
200  inline bool operator== (const ElementType* const otherPointer) const noexcept { return otherPointer == data; }
201 
205  inline bool operator!= (const ElementType* const otherPointer) const noexcept { return otherPointer != data; }
206 
207  //==============================================================================
220  void malloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType))
221  {
222  std::free (data);
223  data = static_cast<ElementType*> (std::malloc (newNumElements * elementSize));
224  throwOnAllocationFailure();
225  }
226 
230  void calloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType))
231  {
232  std::free (data);
233  data = static_cast<ElementType*> (std::calloc (newNumElements, elementSize));
234  throwOnAllocationFailure();
235  }
236 
241  void allocate (const size_t newNumElements, bool initialiseToZero)
242  {
243  std::free (data);
244  data = static_cast<ElementType*> (initialiseToZero
245  ? std::calloc (newNumElements, sizeof (ElementType))
246  : std::malloc (newNumElements * sizeof (ElementType)));
247  throwOnAllocationFailure();
248  }
249 
255  void realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType))
256  {
257  data = static_cast<ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
258  : std::realloc (data, newNumElements * elementSize));
259  throwOnAllocationFailure();
260  }
261 
265  void free() noexcept
266  {
267  std::free (data);
268  data = nullptr;
269  }
270 
274  template <bool otherBlockThrows>
276  {
277  std::swap (data, other.data);
278  }
279 
284  void clear (size_t numElements) noexcept
285  {
286  zeromem (data, sizeof (ElementType) * numElements);
287  }
288 
290  typedef ElementType Type;
291 
292 private:
293  //==============================================================================
294  ElementType* data;
295 
296  void throwOnAllocationFailure() const
297  {
299  }
300 
301  #if ! (defined (JUCE_DLL) || defined (JUCE_DLL_BUILD))
303  JUCE_PREVENT_HEAP_ALLOCATION // Creating a 'new HeapBlock' would be missing the point!
304  #endif
305 };
306 
307 
308 #endif // JUCE_HEAPBLOCK_H_INCLUDED
HeapBlock(const size_t numElements)
Definition: juce_HeapBlock.h:111
#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
#define JUCE_PREVENT_HEAP_ALLOCATION
Definition: juce_PlatformDefs.h:205
ElementType * operator->() const noexcept
Definition: juce_HeapBlock.h:181
static void check(void *data)
Definition: juce_HeapBlock.h:39
void clear(size_t numElements) noexcept
Definition: juce_HeapBlock.h:284
bool operator==(const var &v1, const var &v2) noexcept
Definition: juce_Variant.cpp:565
Definition: juce_HeapBlock.h:33
#define JUCE_DECLARE_NON_COPYABLE(className)
Definition: juce_PlatformDefs.h:191
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
void allocate(const size_t newNumElements, bool initialiseToZero)
Definition: juce_HeapBlock.h:241
static void check(void *)
Definition: juce_HeapBlock.h:36
bool operator!=(const var &v1, const var &v2) noexcept
Definition: juce_Variant.cpp:566
void free() noexcept
Definition: juce_HeapBlock.h:265