SliceBuffer.hxx 3.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_SLICE_BUFFER_HXX
#define MPD_SLICE_BUFFER_HXX

23
#include "HugeAllocator.hxx"
24
#include "Compiler.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include <utility>
#include <new>

#include <assert.h>
#include <stddef.h>

/**
 * This class pre-allocates a certain number of objects, and allows
 * callers to allocate and free these objects ("slices").
 */
template<typename T>
class SliceBuffer {
	union Slice {
		Slice *next;

		T value;
	};

44
	HugeArray<Slice> buffer;
45

46 47 48 49 50
	/**
	 * The number of slices that are initialized.  This is used to
	 * avoid page faulting on the new allocation, so the kernel
	 * does not need to reserve physical memory pages.
	 */
51
	unsigned n_initialized = 0;
52

53 54 55
	/**
	 * The number of slices currently allocated.
	 */
56
	unsigned n_allocated = 0;
57 58 59 60

	/**
	 * Pointer to the first free element in the chain.
	 */
61
	Slice *available = nullptr;
62 63 64

public:
	SliceBuffer(unsigned _count)
65 66
		:buffer(_count) {
		buffer.ForkCow(false);
67 68
	}

69
	~SliceBuffer() noexcept {
70 71 72 73 74 75 76 77
		/* all slices must be freed explicitly, and this
		   assertion checks for leaks */
		assert(n_allocated == 0);
	}

	SliceBuffer(const SliceBuffer &other) = delete;
	SliceBuffer &operator=(const SliceBuffer &other) = delete;

78
	unsigned GetCapacity() const noexcept {
79
		return buffer.size();
80 81
	}

82
	bool empty() const noexcept {
83 84 85
		return n_allocated == 0;
	}

86
	bool IsFull() const noexcept {
87
		return n_allocated == buffer.size();
88 89
	}

90 91 92 93 94
	void DiscardMemory() noexcept {
		assert(empty());

		n_initialized = 0;
		buffer.Discard();
95
		available = nullptr;
96 97
	}

98 99
	template<typename... Args>
	T *Allocate(Args&&... args) {
100
		assert(n_initialized <= buffer.size());
101
		assert(n_allocated <= n_initialized);
102 103

		if (available == nullptr) {
104
			if (n_initialized == buffer.size()) {
105
				/* out of (internal) memory, buffer is full */
106
				assert(n_allocated == buffer.size());
107 108 109
				return nullptr;
			}

110
			available = &buffer[n_initialized++];
111
			available->next = nullptr;
112 113 114 115 116 117 118 119 120 121 122
		}

		/* allocate a slice */
		T *value = &available->value;
		available = available->next;
		++n_allocated;

		/* construct the object */
		return ::new((void *)value) T(std::forward<Args>(args)...);
	}

123
	void Free(T *value) noexcept {
124
		assert(n_initialized <= buffer.size());
125
		assert(n_allocated > 0);
126
		assert(n_allocated <= n_initialized);
127 128

		Slice *slice = reinterpret_cast<Slice *>(value);
129
		assert(slice >= &buffer.front() && slice <= &buffer.back());
130 131 132 133 134 135 136 137

		/* destruct the object */
		value->~T();

		/* insert the slice in the "available" linked list */
		slice->next = available;
		available = slice;
		--n_allocated;
138 139 140 141

		/* give memory back to the kernel when the last slice
		   was freed */
		if (n_allocated == 0) {
142
			DiscardMemory();
143
		}
144 145 146 147
	}
};

#endif