MusicBuffer.cxx 1.92 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21 22
#include "MusicBuffer.hxx"
#include "MusicChunk.hxx"
23
#include "thread/Mutex.hxx"
24
#include "util/SliceBuffer.hxx"
25
#include "mpd_error.h"
26 27 28

#include <assert.h>

29
struct music_buffer : public SliceBuffer<music_chunk>  {
30
	/** a mutex which protects #available */
31
	Mutex mutex;
32

33
	music_buffer(unsigned num_chunks)
34
		:SliceBuffer(num_chunks) {
35 36 37
		if (IsOOM())
			MPD_ERROR("Failed to allocate buffer");
	}
38
};
39

40 41 42 43
struct music_buffer *
music_buffer_new(unsigned num_chunks)
{
	return new music_buffer(num_chunks);
44 45 46 47 48
}

void
music_buffer_free(struct music_buffer *buffer)
{
49
	delete buffer;
50 51 52 53 54
}

unsigned
music_buffer_size(const struct music_buffer *buffer)
{
55
	return buffer->GetCapacity();
56 57 58 59 60
}

struct music_chunk *
music_buffer_allocate(struct music_buffer *buffer)
{
61 62
	const ScopeLock protect(buffer->mutex);
	return buffer->Allocate();
63 64 65 66 67 68 69 70
}

void
music_buffer_return(struct music_buffer *buffer, struct music_chunk *chunk)
{
	assert(buffer != NULL);
	assert(chunk != NULL);

71
	const ScopeLock protect(buffer->mutex);
72

73 74 75 76
	if (chunk->other != nullptr) {
		assert(chunk->other->other == nullptr);
		buffer->Free(chunk->other);
	}
77

78
	buffer->Free(chunk);
79
}