OpenShot Library | libopenshot-audio  0.2.0
juce_ArrayAllocationBase.h
1 
2 /** @weakgroup juce_core-containers
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Implements some basic array storage allocation functions.
33 
34  This class isn't really for public use - it used to be part of the
35  container classes but has since been superseded by ArrayBase. Eventually
36  it will be removed from the API.
37 
38  @tags{Core}
39 */
40 template <class ElementType, class TypeOfCriticalSectionToUse>
41 class ArrayAllocationBase : public TypeOfCriticalSectionToUse
42 {
43 public:
44  //==============================================================================
45  /** Creates an empty array. */
46  ArrayAllocationBase() = default;
47 
48  /** Destructor. */
49  ~ArrayAllocationBase() = default;
50 
51  ArrayAllocationBase (ArrayAllocationBase&& other) noexcept
52  : elements (std::move (other.elements)),
53  numAllocated (other.numAllocated)
54  {
55  }
56 
57  ArrayAllocationBase& operator= (ArrayAllocationBase&& other) noexcept
58  {
59  elements = std::move (other.elements);
60  numAllocated = other.numAllocated;
61  return *this;
62  }
63 
64  //==============================================================================
65  /** Changes the amount of storage allocated.
66 
67  This will retain any data currently held in the array, and either add or
68  remove extra space at the end.
69 
70  @param numElements the number of elements that are needed
71  */
72  void setAllocatedSize (int numElements)
73  {
74  if (numAllocated != numElements)
75  {
76  if (numElements > 0)
77  elements.realloc ((size_t) numElements);
78  else
79  elements.free();
80 
81  numAllocated = numElements;
82  }
83  }
84 
85  /** Increases the amount of storage allocated if it is less than a given amount.
86 
87  This will retain any data currently held in the array, but will add
88  extra space at the end to make sure there it's at least as big as the size
89  passed in. If it's already bigger, no action is taken.
90 
91  @param minNumElements the minimum number of elements that are needed
92  */
93  void ensureAllocatedSize (int minNumElements)
94  {
95  if (minNumElements > numAllocated)
96  setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
97 
98  jassert (numAllocated <= 0 || elements != nullptr);
99  }
100 
101  /** Minimises the amount of storage allocated so that it's no more than
102  the given number of elements.
103  */
104  void shrinkToNoMoreThan (int maxNumElements)
105  {
106  if (maxNumElements < numAllocated)
107  setAllocatedSize (maxNumElements);
108  }
109 
110  /** Swap the contents of two objects. */
111  void swapWith (ArrayAllocationBase& other) noexcept
112  {
113  elements.swapWith (other.elements);
114  std::swap (numAllocated, other.numAllocated);
115  }
116 
117  //==============================================================================
118  HeapBlock<ElementType> elements;
119  int numAllocated = 0;
120 
121 private:
122  JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase)
123 };
124 
125 } // namespace juce
126 
127 /** @}*/
juce::ArrayAllocationBase::shrinkToNoMoreThan
void shrinkToNoMoreThan(int maxNumElements)
Minimises the amount of storage allocated so that it's no more than the given number of elements.
Definition: juce_ArrayAllocationBase.h:104
juce::HeapBlock::free
void free() noexcept
Frees any currently-allocated data.
Definition: juce_HeapBlock.h:304
juce::HeapBlock< ElementType >
juce::ArrayAllocationBase::ensureAllocatedSize
void ensureAllocatedSize(int minNumElements)
Increases the amount of storage allocated if it is less than a given amount.
Definition: juce_ArrayAllocationBase.h:93
juce::ArrayAllocationBase::setAllocatedSize
void setAllocatedSize(int numElements)
Changes the amount of storage allocated.
Definition: juce_ArrayAllocationBase.h:72
juce::HeapBlock::realloc
void realloc(SizeType newNumElements, size_t elementSize=sizeof(ElementType))
Re-allocates a specified amount of memory.
Definition: juce_HeapBlock.h:294
juce::ArrayAllocationBase::~ArrayAllocationBase
~ArrayAllocationBase()=default
Destructor.
juce::ArrayAllocationBase::swapWith
void swapWith(ArrayAllocationBase &other) noexcept
Swap the contents of two objects.
Definition: juce_ArrayAllocationBase.h:111
juce::HeapBlock::swapWith
void swapWith(HeapBlock< ElementType, otherBlockThrows > &other) noexcept
Swaps this object's data with the data of another HeapBlock.
Definition: juce_HeapBlock.h:314
juce::ArrayAllocationBase::ArrayAllocationBase
ArrayAllocationBase()=default
Creates an empty array.
juce::ArrayAllocationBase
Implements some basic array storage allocation functions.
Definition: juce_ArrayAllocationBase.h:41