MPD: util/ReusableArray.hxx Source File

MPD 0.20.18

ReusableArray.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013-2017 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef REUSABLE_ARRAY_HXX
31 #define REUSABLE_ARRAY_HXX
32
33 #include "Compiler.h"
34
35 #include <utility>
36
37 #include <stddef.h>
38
47 template<typename T, size_t M=1>
48 class ReusableArray {
49 T *buffer = nullptr;
50 size_t capacity = 0;
51
52 public:
53 ReusableArray() = default;
54
55 ReusableArray(ReusableArray &&src)
56 :buffer(std::exchange(src.buffer, nullptr)),
57 capacity(std::exchange(src.capacity, 0)) {}
58
59 ReusableArray &operator=(ReusableArray &&src) {
60 std::swap(buffer, src.buffer);
61 std::swap(capacity, src.capacity);
62 return *this;
63 }
64
65 ~ReusableArray() {
66 delete[] buffer;
67 }
68
69 size_t GetCapacity() const {
70 return capacity;
71 }
72
77 void Clear() {
78 delete[] buffer;
79 buffer = nullptr;
80 capacity = 0;
81 }
82
87 gcc_malloc
88 T *Get(size_t size) {
89 if (gcc_unlikely(size > capacity)) {
90 /* too small: grow */
91 delete[] buffer;
92
93 capacity = ((size - 1) | (M - 1)) + 1;
94 buffer = new T[capacity];
95 }
96
97 return buffer;
98 }
99 };
100
101 #endif
ReusableArray::Clear
void Clear()
Free resources allocated by this object.
Definition: ReusableArray.hxx:77
gcc_malloc
#define gcc_malloc
Definition: Compiler.h:112
gcc_unlikely
#define gcc_unlikely(x)
Definition: Compiler.h:125
ReusableArray::ReusableArray
ReusableArray()=default
Compiler.h
ReusableArray
Manager for a temporary array which grows as needed.
Definition: ReusableArray.hxx:48
ReusableArray::GetCapacity
size_t GetCapacity() const
Definition: ReusableArray.hxx:69
ReusableArray::Get
gcc_malloc T * Get(size_t size)
Get the buffer, and guarantee a minimum size.
Definition: ReusableArray.hxx:88
ReusableArray::ReusableArray
ReusableArray(ReusableArray &&src)
Definition: ReusableArray.hxx:55
ReusableArray::operator=
ReusableArray & operator=(ReusableArray &&src)
Definition: ReusableArray.hxx:59
ReusableArray::~ReusableArray
~ReusableArray()
Definition: ReusableArray.hxx:65