MPD: util/SliceBuffer.hxx Source File

MPD 0.20.18

SliceBuffer.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_SLICE_BUFFER_HXX
21 #define MPD_SLICE_BUFFER_HXX
22
23 #include "HugeAllocator.hxx"
24 #include "Compiler.h"
25
26 #include <utility>
27 #include <new>
28
29 #include <assert.h>
30 #include <stddef.h>
31
36 template<typename T>
37 class SliceBuffer {
38 union Slice {
39 Slice *next;
40
41 T value;
42 };
43
47 const unsigned n_max;
48
54 unsigned n_initialized;
55
59 unsigned n_allocated;
60
61 Slice *const data;
62
66 Slice *available;
67
68 size_t CalcAllocationSize() const {
69 return n_max * sizeof(Slice);
70 }
71
72 public:
73 SliceBuffer(unsigned _count)
74 :n_max(_count), n_initialized(0), n_allocated(0),
75 data((Slice *)HugeAllocate(CalcAllocationSize())),
76 available(nullptr) {
77 assert(n_max > 0);
78 }
79
80 ~SliceBuffer() {
81 /* all slices must be freed explicitly, and this
82 assertion checks for leaks */
83 assert(n_allocated == 0);
84
85 HugeFree(data, CalcAllocationSize());
86 }
87
88 SliceBuffer(const SliceBuffer &other) = delete;
89 SliceBuffer &operator=(const SliceBuffer &other) = delete;
90
91 unsigned GetCapacity() const {
92 return n_max;
93 }
94
95 bool IsEmpty() const {
96 return n_allocated == 0;
97 }
98
99 bool IsFull() const {
100 return n_allocated == n_max;
101 }
102
103 template<typename... Args>
104 T *Allocate(Args&&... args) {
105 assert(n_initialized <= n_max);
106 assert(n_allocated <= n_initialized);
107
108 if (available == nullptr) {
109 if (n_initialized == n_max) {
110 /* out of (internal) memory, buffer is full */
111 assert(n_allocated == n_max);
112 return nullptr;
113 }
114
115 available = &data[n_initialized++];
116 available->next = nullptr;
117 }
118
119 /* allocate a slice */
120 T *value = &available->value;
121 available = available->next;
122 ++n_allocated;
123
124 /* construct the object */
125 return ::new((void *)value) T(std::forward<Args>(args)...);
126 }
127
128 void Free(T *value) {
129 assert(n_initialized <= n_max);
130 assert(n_allocated > 0);
131 assert(n_allocated <= n_initialized);
132
133 Slice *slice = reinterpret_cast<Slice *>(value);
134 assert(slice >= data && slice < data + n_max);
135
136 /* destruct the object */
137 value->~T();
138
139 /* insert the slice in the "available" linked list */
140 slice->next = available;
141 available = slice;
142 --n_allocated;
143
144 /* give memory back to the kernel when the last slice
145 was freed */
146 if (n_allocated == 0) {
147 HugeDiscard(data, CalcAllocationSize());
148 n_initialized = 0;
149 available = nullptr;
150 }
151 }
152 };
153
154 #endif
SliceBuffer::IsFull
bool IsFull() const
Definition: SliceBuffer.hxx:99
SliceBuffer::SliceBuffer
SliceBuffer(unsigned _count)
Definition: SliceBuffer.hxx:73
SliceBuffer::~SliceBuffer
~SliceBuffer()
Definition: SliceBuffer.hxx:80
HugeAllocator.hxx
SliceBuffer::IsEmpty
bool IsEmpty() const
Definition: SliceBuffer.hxx:95
SliceBuffer::Free
void Free(T *value)
Definition: SliceBuffer.hxx:128
HugeDiscard
static void HugeDiscard(void *, size_t) noexcept
Definition: HugeAllocator.hxx:108
SliceBuffer::GetCapacity
unsigned GetCapacity() const
Definition: SliceBuffer.hxx:91
Compiler.h
SliceBuffer::operator=
SliceBuffer & operator=(const SliceBuffer &other)=delete
SliceBuffer
This class pre-allocates a certain number of objects, and allows callers to allocate and free these o...
Definition: SliceBuffer.hxx:37
HugeFree
static void HugeFree(void *_p, size_t) noexcept
Definition: HugeAllocator.hxx:101
SliceBuffer::Allocate
T * Allocate(Args &&... args)
Definition: SliceBuffer.hxx:104
HugeAllocate
static gcc_malloc void * HugeAllocate(size_t size)
Definition: HugeAllocator.hxx:95