MusicPipe.cxx 2.25 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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 23
#include "MusicPipe.hxx"
#include "MusicBuffer.hxx"
#include "MusicChunk.hxx"
24 25

#ifndef NDEBUG
26 27

bool
28
MusicPipe::Contains(const music_chunk *chunk) const
29
{
30
	const ScopeLock protect(mutex);
31

32
	for (const struct music_chunk *i = head; i != nullptr; i = i->next)
33
		if (i == chunk)
34 35 36 37 38
			return true;

	return false;
}

39 40
#endif

41 42
music_chunk *
MusicPipe::Shift()
43
{
44
	const ScopeLock protect(mutex);
45

46 47
	music_chunk *chunk = head;
	if (chunk != nullptr) {
48
		assert(!chunk->IsEmpty());
49

50 51
		head = chunk->next;
		--size;
52

53 54 55
		if (head == nullptr) {
			assert(size == 0);
			assert(tail_r == &chunk->next);
56

57
			tail_r = &head;
58
		} else {
59 60
			assert(size > 0);
			assert(tail_r != &chunk->next);
61
		}
62 63 64

#ifndef NDEBUG
		/* poison the "next" reference */
65
		chunk->next = (music_chunk *)(void *)0x01010101;
66

67 68
		if (size == 0)
			audio_format.Clear();
69
#endif
70
	}
71

72
	return chunk;
73 74
}

75
void
76
MusicPipe::Clear(MusicBuffer &buffer)
77
{
78
	music_chunk *chunk;
79

80
	while ((chunk = Shift()) != nullptr)
81
		buffer.Return(chunk);
82
}
83

84
void
85
MusicPipe::Push(music_chunk *chunk)
86
{
87
	assert(!chunk->IsEmpty());
88
	assert(chunk->length == 0 || chunk->audio_format.IsValid());
89

90
	const ScopeLock protect(mutex);
91

92 93 94
	assert(size > 0 || !audio_format.IsDefined());
	assert(!audio_format.IsDefined() ||
	       chunk->CheckFormat(audio_format));
95 96

#ifndef NDEBUG
97 98
	if (!audio_format.IsDefined() && chunk->length > 0)
		audio_format = chunk->audio_format;
99 100
#endif

101 102 103
	chunk->next = nullptr;
	*tail_r = chunk;
	tail_r = &chunk->next;
104

105
	++size;
106
}