LMMS
Loading...
Searching...
No Matches
HeapBlock.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_HEAPBLOCK_H_INCLUDED
27#define WATER_HEAPBLOCK_H_INCLUDED
28
29#include "Memory.h"
31
32namespace water {
33
34//==============================================================================
75template <class ElementType>
77{
78public:
79 //==============================================================================
85 HeapBlock() noexcept : data (nullptr)
86 {
87 }
88
93 {
94 std::free (data);
95 }
96
97 //==============================================================================
102 inline operator ElementType*() const noexcept { return data; }
103
108 inline ElementType* getData() const noexcept { return data; }
109
114 inline const ElementType* getConstData() const noexcept { return data; }
115
120 inline operator void*() const noexcept { return static_cast<void*> (data); }
121
126 inline operator const void*() const noexcept { return static_cast<const void*> (data); }
127
132 inline ElementType* operator->() const noexcept { return data; }
133
138 template <typename IndexType>
139 inline ElementType& operator[] (IndexType index) const noexcept { return data [index]; }
140
144 template <typename IndexType>
145 inline ElementType* operator+ (IndexType index) const noexcept { return data + index; }
146
147 //==============================================================================
151 inline bool operator== (const ElementType* const otherPointer) const noexcept { return otherPointer == data; }
152
156 inline bool operator!= (const ElementType* const otherPointer) const noexcept { return otherPointer != data; }
157
158 //==============================================================================
171 bool malloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
172 {
173 std::free (data);
174 data = static_cast<ElementType*> (std::malloc (newNumElements * elementSize));
175
176 return data != nullptr;
177 }
178
182 bool calloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
183 {
184 std::free (data);
185 data = static_cast<ElementType*> (std::calloc (newNumElements, elementSize));
186
187 return data != nullptr;
188 }
189
194 bool allocate (const size_t newNumElements, bool initialiseToZero) noexcept
195 {
196 std::free (data);
197 data = static_cast<ElementType*> (initialiseToZero
198 ? std::calloc (newNumElements, sizeof (ElementType))
199 : std::malloc (newNumElements * sizeof (ElementType)));
200
201 return data != nullptr;
202 }
203
209 bool realloc (const size_t newNumElements, const size_t elementSize = sizeof (ElementType)) noexcept
210 {
211 data = static_cast<ElementType*> (data == nullptr ? std::malloc (newNumElements * elementSize)
212 : std::realloc (data, newNumElements * elementSize));
213 return data != nullptr;
214 }
215
220 {
221 std::free (data);
222 data = nullptr;
223 }
224
228 void swapWith (HeapBlock<ElementType>& other) noexcept
229 {
230 std::swap (data, other.data);
231 }
232
237 void clear (size_t numElements) noexcept
238 {
239 zeromem (data, sizeof (ElementType) * numElements);
240 }
241
243 ElementType* release() noexcept
244 {
245 ElementType* const r = data;
246 data = nullptr;
247 return r;
248 }
249
251 typedef ElementType Type;
252
253private:
254 //==============================================================================
255 ElementType* data;
256
258};
259
260}
261
262#endif // WATER_HEAPBLOCK_H_INCLUDED
#define CARLA_DECLARE_NON_COPYABLE(ClassName)
Definition CarlaDefines.h:242
#define noexcept
Definition DistrhoDefines.h:72
ElementType * data
Definition HeapBlock.h:255
const ElementType * getConstData() const noexcept
Definition HeapBlock.h:114
bool operator==(const ElementType *const otherPointer) const noexcept
Definition HeapBlock.h:151
bool malloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType)) noexcept
Definition HeapBlock.h:171
ElementType & operator[](IndexType index) const noexcept
Definition HeapBlock.h:139
ElementType * getData() const noexcept
Definition HeapBlock.h:108
ElementType * operator+(IndexType index) const noexcept
Definition HeapBlock.h:145
HeapBlock() noexcept
Definition HeapBlock.h:85
void clear(size_t numElements) noexcept
Definition HeapBlock.h:237
bool calloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType)) noexcept
Definition HeapBlock.h:182
void swapWith(HeapBlock< ElementType > &other) noexcept
Definition HeapBlock.h:228
ElementType Type
Definition HeapBlock.h:251
bool allocate(const size_t newNumElements, bool initialiseToZero) noexcept
Definition HeapBlock.h:194
bool operator!=(const ElementType *const otherPointer) const noexcept
Definition HeapBlock.h:156
ElementType * operator->() const noexcept
Definition HeapBlock.h:132
bool realloc(const size_t newNumElements, const size_t elementSize=sizeof(ElementType)) noexcept
Definition HeapBlock.h:209
~HeapBlock() noexcept
Definition HeapBlock.h:92
ElementType * release() noexcept
Definition HeapBlock.h:243
void free() noexcept
Definition HeapBlock.h:219
Definition AudioSampleBuffer.h:33
void zeromem(void *memory, size_t numBytes) noexcept
Definition Memory.h:37
int r
Definition crypt.c:458
#define const
Definition zconf.h:137