configure.ac: Move OggVorbis Encoder to Encoder Plugins.
[mpd-mk.git] / src / buffer.c
blob27a0bf05cd3e92a825e3d92e81bc8fb557fdc0fc
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "buffer.h"
22 #include "chunk.h"
23 #include "poison.h"
25 #include <glib.h>
27 #include <assert.h>
29 struct music_buffer {
30 struct music_chunk *chunks;
31 unsigned num_chunks;
33 struct music_chunk *available;
35 /** a mutex which protects #available */
36 GMutex *mutex;
38 #ifndef NDEBUG
39 unsigned num_allocated;
40 #endif
43 struct music_buffer *
44 music_buffer_new(unsigned num_chunks)
46 struct music_buffer *buffer;
47 struct music_chunk *chunk;
49 assert(num_chunks > 0);
51 buffer = g_new(struct music_buffer, 1);
53 buffer->chunks = g_new(struct music_chunk, num_chunks);
54 buffer->num_chunks = num_chunks;
56 chunk = buffer->available = buffer->chunks;
57 poison_undefined(chunk, sizeof(*chunk));
59 for (unsigned i = 1; i < num_chunks; ++i) {
60 chunk->next = &buffer->chunks[i];
61 chunk = chunk->next;
62 poison_undefined(chunk, sizeof(*chunk));
65 chunk->next = NULL;
67 buffer->mutex = g_mutex_new();
69 #ifndef NDEBUG
70 buffer->num_allocated = 0;
71 #endif
73 return buffer;
76 void
77 music_buffer_free(struct music_buffer *buffer)
79 assert(buffer->chunks != NULL);
80 assert(buffer->num_chunks > 0);
81 assert(buffer->num_allocated == 0);
83 g_mutex_free(buffer->mutex);
84 g_free(buffer->chunks);
85 g_free(buffer);
88 unsigned
89 music_buffer_size(const struct music_buffer *buffer)
91 return buffer->num_chunks;
94 struct music_chunk *
95 music_buffer_allocate(struct music_buffer *buffer)
97 struct music_chunk *chunk;
99 g_mutex_lock(buffer->mutex);
101 chunk = buffer->available;
102 if (chunk != NULL) {
103 buffer->available = chunk->next;
104 music_chunk_init(chunk);
106 #ifndef NDEBUG
107 ++buffer->num_allocated;
108 #endif
111 g_mutex_unlock(buffer->mutex);
112 return chunk;
115 void
116 music_buffer_return(struct music_buffer *buffer, struct music_chunk *chunk)
118 assert(buffer != NULL);
119 assert(chunk != NULL);
121 g_mutex_lock(buffer->mutex);
123 music_chunk_free(chunk);
124 poison_undefined(chunk, sizeof(*chunk));
126 chunk->next = buffer->available;
127 buffer->available = chunk;
129 #ifndef NDEBUG
130 --buffer->num_allocated;
131 #endif
133 g_mutex_unlock(buffer->mutex);