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.
30 struct music_chunk
*chunks
;
33 struct music_chunk
*available
;
35 /** a mutex which protects #available */
39 unsigned num_allocated
;
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
];
62 poison_undefined(chunk
, sizeof(*chunk
));
67 buffer
->mutex
= g_mutex_new();
70 buffer
->num_allocated
= 0;
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
);
89 music_buffer_size(const struct music_buffer
*buffer
)
91 return buffer
->num_chunks
;
95 music_buffer_allocate(struct music_buffer
*buffer
)
97 struct music_chunk
*chunk
;
99 g_mutex_lock(buffer
->mutex
);
101 chunk
= buffer
->available
;
103 buffer
->available
= chunk
->next
;
104 music_chunk_init(chunk
);
107 ++buffer
->num_allocated
;
111 g_mutex_unlock(buffer
->mutex
);
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
;
130 --buffer
->num_allocated
;
133 g_mutex_unlock(buffer
->mutex
);