2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
28 Implements some basic array storage allocation functions.
30 This class isn't really for public use - it used to be part of the
31 container classes but has since been superseded by ArrayBase. Eventually
32 it will be removed from the API.
36 template <class ElementType
, class TypeOfCriticalSectionToUse
>
37 class ArrayAllocationBase
: public TypeOfCriticalSectionToUse
40 //==============================================================================
41 /** Creates an empty array. */
42 ArrayAllocationBase() = default;
45 ~ArrayAllocationBase() = default;
47 ArrayAllocationBase (ArrayAllocationBase
&& other
) noexcept
48 : elements (std::move (other
.elements
)),
49 numAllocated (other
.numAllocated
)
53 ArrayAllocationBase
& operator= (ArrayAllocationBase
&& other
) noexcept
55 elements
= std::move (other
.elements
);
56 numAllocated
= other
.numAllocated
;
60 //==============================================================================
61 /** Changes the amount of storage allocated.
63 This will retain any data currently held in the array, and either add or
64 remove extra space at the end.
66 @param numElements the number of elements that are needed
68 void setAllocatedSize (int numElements
)
70 if (numAllocated
!= numElements
)
73 elements
.realloc ((size_t) numElements
);
77 numAllocated
= numElements
;
81 /** Increases the amount of storage allocated if it is less than a given amount.
83 This will retain any data currently held in the array, but will add
84 extra space at the end to make sure there it's at least as big as the size
85 passed in. If it's already bigger, no action is taken.
87 @param minNumElements the minimum number of elements that are needed
89 void ensureAllocatedSize (int minNumElements
)
91 if (minNumElements
> numAllocated
)
92 setAllocatedSize ((minNumElements
+ minNumElements
/ 2 + 8) & ~7);
94 jassert (numAllocated
<= 0 || elements
!= nullptr);
97 /** Minimises the amount of storage allocated so that it's no more than
98 the given number of elements.
100 void shrinkToNoMoreThan (int maxNumElements
)
102 if (maxNumElements
< numAllocated
)
103 setAllocatedSize (maxNumElements
);
106 /** Swap the contents of two objects. */
107 void swapWith (ArrayAllocationBase
& other
) noexcept
109 elements
.swapWith (other
.elements
);
110 std::swap (numAllocated
, other
.numAllocated
);
113 //==============================================================================
114 HeapBlock
<ElementType
> elements
;
115 int numAllocated
= 0;
118 JUCE_DECLARE_NON_COPYABLE (ArrayAllocationBase
)