2009-03-06 00:42:03 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2009-03-06 00:42:03 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-06 00:42:03 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicBuffer.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2018-09-21 16:41:32 +02:00
|
|
|
MusicBuffer::MusicBuffer(unsigned num_chunks)
|
2022-04-26 20:34:19 +02:00
|
|
|
:buffer(num_chunks)
|
|
|
|
{
|
|
|
|
buffer.SetName("MusicBuffer");
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-30 18:00:40 +01:00
|
|
|
MusicChunkPtr
|
2017-05-08 14:44:49 +02:00
|
|
|
MusicBuffer::Allocate() noexcept
|
2009-03-06 00:42:03 +01:00
|
|
|
{
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2021-11-11 11:40:47 +01:00
|
|
|
return {buffer.Allocate(), MusicChunkDeleter(*this)};
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-08 14:44:49 +02:00
|
|
|
MusicBuffer::Return(MusicChunk *chunk) noexcept
|
2009-03-06 00:42:03 +01:00
|
|
|
{
|
2013-09-26 22:09:42 +02:00
|
|
|
assert(chunk != nullptr);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2018-06-23 18:39:50 +02:00
|
|
|
/* these attributes need to be cleared before locking the
|
|
|
|
mutex, because they might recursively call this method,
|
|
|
|
causing a deadlock */
|
|
|
|
chunk->next.reset();
|
|
|
|
chunk->other.reset();
|
|
|
|
|
2021-11-12 01:19:32 +01:00
|
|
|
const std::scoped_lock<Mutex> protect(mutex);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2017-12-30 18:00:40 +01:00
|
|
|
assert(!chunk->other || !chunk->other->other);
|
2009-03-06 00:42:03 +01:00
|
|
|
|
2013-09-26 22:09:42 +02:00
|
|
|
buffer.Free(chunk);
|
2009-03-06 00:42:03 +01:00
|
|
|
}
|