2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-01-04 10:16:16 +01:00
|
|
|
#ifndef MPD_MUSIC_BUFFER_HXX
|
|
|
|
#define MPD_MUSIC_BUFFER_HXX
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
#include "MusicChunk.hxx"
|
2017-12-30 18:00:40 +01:00
|
|
|
#include "MusicChunkPtr.hxx"
|
2013-09-26 22:09:42 +02:00
|
|
|
#include "util/SliceBuffer.hxx"
|
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
|
2009-03-06 00:42:03 +01:00
|
|
|
/**
|
2014-08-12 15:56:41 +02:00
|
|
|
* An allocator for #MusicChunk objects.
|
2009-03-06 00:42:03 +01:00
|
|
|
*/
|
2013-09-26 22:09:42 +02:00
|
|
|
class MusicBuffer {
|
|
|
|
/** a mutex which protects #buffer */
|
2018-09-21 19:05:25 +02:00
|
|
|
mutable Mutex mutex;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2014-08-12 15:56:41 +02:00
|
|
|
SliceBuffer<MusicChunk> buffer;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Creates a new #MusicBuffer object.
|
|
|
|
*
|
2014-08-12 15:56:41 +02:00
|
|
|
* @param num_chunks the number of #MusicChunk reserved in
|
2013-09-26 22:09:42 +02:00
|
|
|
* this buffer
|
|
|
|
*/
|
2018-09-21 16:41:32 +02:00
|
|
|
explicit MusicBuffer(unsigned num_chunks);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 22:41:07 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/**
|
|
|
|
* Check whether the buffer is empty. This call is not
|
|
|
|
* protected with the mutex, and may only be used while this
|
|
|
|
* object is inaccessible to other threads.
|
|
|
|
*/
|
|
|
|
bool IsEmptyUnsafe() const {
|
2017-11-10 19:24:33 +01:00
|
|
|
return buffer.empty();
|
2013-09-26 22:41:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-09-21 19:05:25 +02:00
|
|
|
bool IsFull() const noexcept {
|
2024-05-23 20:43:31 +02:00
|
|
|
const std::scoped_lock protect{mutex};
|
2018-09-21 19:05:25 +02:00
|
|
|
return buffer.IsFull();
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
/**
|
|
|
|
* Returns the total number of reserved chunks in this buffer. This
|
|
|
|
* is the same value which was passed to the constructor
|
|
|
|
* music_buffer_new().
|
|
|
|
*/
|
2021-10-13 11:28:04 +02:00
|
|
|
[[gnu::pure]]
|
2017-06-03 21:33:44 +02:00
|
|
|
unsigned GetSize() const noexcept {
|
2013-09-26 22:09:42 +02:00
|
|
|
return buffer.GetCapacity();
|
|
|
|
}
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
/**
|
|
|
|
* Allocates a chunk from the buffer. When it is not used anymore,
|
|
|
|
* call Return().
|
|
|
|
*
|
|
|
|
* @return an empty chunk or nullptr if there are no chunks
|
|
|
|
* available
|
|
|
|
*/
|
2017-12-30 18:00:40 +01:00
|
|
|
MusicChunkPtr Allocate() noexcept;
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
/**
|
|
|
|
* Returns a chunk to the buffer. It can be reused by
|
|
|
|
* Allocate() then.
|
|
|
|
*/
|
2017-05-08 14:44:49 +02:00
|
|
|
void Return(MusicChunk *chunk) noexcept;
|
2013-09-26 22:09:42 +02:00
|
|
|
};
|
2009-03-06 00:42:03 +01:00
|
|
|
|
|
|
|
#endif
|