PeakBuffer.hxx 2.08 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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_PEAK_BUFFER_HXX
#define MPD_PEAK_BUFFER_HXX

23
#include "Compiler.h"
24

25
#include <cstddef>
26

27 28
template<typename T> struct WritableBuffer;
template<typename T> class DynamicFifoBuffer;
29 30 31 32 33 34 35

/**
 * A FIFO-like buffer that will allocate more memory on demand to
 * allow large peaks.  This second buffer will be given back to the
 * kernel when it has been consumed.
 */
class PeakBuffer {
36
	std::size_t normal_size, peak_size;
37

38
	DynamicFifoBuffer<std::byte> *normal_buffer, *peak_buffer;
39 40

public:
41
	PeakBuffer(std::size_t _normal_size, std::size_t _peak_size) noexcept
42 43 44
		:normal_size(_normal_size), peak_size(_peak_size),
		 normal_buffer(nullptr), peak_buffer(nullptr) {}

45
	PeakBuffer(PeakBuffer &&other) noexcept
46 47 48 49 50 51 52
		:normal_size(other.normal_size), peak_size(other.peak_size),
		 normal_buffer(other.normal_buffer),
		 peak_buffer(other.peak_buffer) {
		other.normal_buffer = nullptr;
		other.peak_buffer = nullptr;
	}

53
	~PeakBuffer() noexcept;
54 55 56 57

	PeakBuffer(const PeakBuffer &) = delete;
	PeakBuffer &operator=(const PeakBuffer &) = delete;

58 59 60 61
	std::size_t max_size() const noexcept {
		return normal_size + peak_size;
	}

62
	gcc_pure
63
	bool empty() const noexcept;
64

65
	gcc_pure
66
	WritableBuffer<void> Read() const noexcept;
67

68
	void Consume(std::size_t length) noexcept;
69

70
	bool Append(const void *data, std::size_t length);
71 72 73
};

#endif