LMMS
Loading...
Searching...
No Matches
ArrayAllocationBase.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_ARRAYALLOCATIONBASE_H_INCLUDED
27#define WATER_ARRAYALLOCATIONBASE_H_INCLUDED
28
29#include "../memory/HeapBlock.h"
30
31#include "CarlaUtils.hpp"
32
33namespace water {
34
35//==============================================================================
44template <class ElementType>
46{
47public:
48 //==============================================================================
55
60
61 //==============================================================================
69 bool setAllocatedSize (const size_t numNewElements) noexcept
70 {
71 if (numAllocated != numNewElements)
72 {
73 if (numNewElements > 0)
74 {
75 if (! elements.realloc ((size_t) numNewElements))
76 return false;
77 }
78 else
79 {
80 elements.free();
81 }
82
83 numAllocated = numNewElements;
84 }
85
86 return true;
87 }
88
97 bool ensureAllocatedSize (const size_t minNumElements) noexcept
98 {
99 if (minNumElements > numAllocated)
100 return setAllocatedSize ((minNumElements + minNumElements / 2U + 8U) & ~7U);
101
102 return true;
103 }
104
108 bool shrinkToNoMoreThan (const size_t maxNumElements) noexcept
109 {
110 if (maxNumElements < numAllocated)
111 return setAllocatedSize (maxNumElements);
112
113 return true;
114 }
115
118 {
119 elements.swapWith (other.elements);
120 std::swap (numAllocated, other.numAllocated);
121 }
122
123 void moveMemory (ElementType* target, const ElementType* source, const size_t numElements) noexcept
124 {
125 CARLA_SAFE_ASSERT_RETURN(target != nullptr,);
126 CARLA_SAFE_ASSERT_RETURN(source != nullptr,);
127 CARLA_SAFE_ASSERT_RETURN(target != source,);
128 CARLA_SAFE_ASSERT_RETURN(numElements != 0,);
129
130 std::memmove (target, source, ((size_t) numElements) * sizeof (ElementType));
131 }
132
133 //==============================================================================
136
138};
139
140}
141
142#endif // WATER_ARRAYALLOCATIONBASE_H_INCLUDED
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
#define CARLA_DECLARE_NON_COPYABLE(ClassName)
Definition CarlaDefines.h:242
#define noexcept
Definition DistrhoDefines.h:72
void swapWith(ArrayAllocationBase< ElementType > &other) noexcept
Definition ArrayAllocationBase.h:117
size_t numAllocated
Definition ArrayAllocationBase.h:135
~ArrayAllocationBase() noexcept
Definition ArrayAllocationBase.h:57
bool setAllocatedSize(const size_t numNewElements) noexcept
Definition ArrayAllocationBase.h:69
HeapBlock< ElementType > elements
Definition ArrayAllocationBase.h:134
ArrayAllocationBase() noexcept
Definition ArrayAllocationBase.h:50
bool ensureAllocatedSize(const size_t minNumElements) noexcept
Definition ArrayAllocationBase.h:97
bool shrinkToNoMoreThan(const size_t maxNumElements) noexcept
Definition ArrayAllocationBase.h:108
void moveMemory(ElementType *target, const ElementType *source, const size_t numElements) noexcept
Definition ArrayAllocationBase.h:123
Definition HeapBlock.h:77
Definition AudioSampleBuffer.h:33