From f0be6a4b9edf193a018f0e671106616f6f8dd30c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 4 Jan 2013 17:03:50 +0100 Subject: MusicBuffer: allocate with new/delete --- src/MusicBuffer.cxx | 60 ++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'src/MusicBuffer.cxx') diff --git a/src/MusicBuffer.cxx b/src/MusicBuffer.cxx index 59e59a953..e90344aed 100644 --- a/src/MusicBuffer.cxx +++ b/src/MusicBuffer.cxx @@ -37,49 +37,49 @@ struct music_buffer { #ifndef NDEBUG unsigned num_allocated; #endif -}; -struct music_buffer * -music_buffer_new(unsigned num_chunks) -{ - struct music_buffer *buffer; - struct music_chunk *chunk; - - assert(num_chunks > 0); - - buffer = g_new(struct music_buffer, 1); + music_buffer(unsigned _num_chunks) + :chunks(g_new(struct music_chunk, _num_chunks)), + num_chunks(_num_chunks), + available(chunks), + mutex(g_mutex_new()) +#ifndef NDEBUG + , num_allocated(0) +#endif + { + assert(num_chunks > 0); - buffer->chunks = g_new(struct music_chunk, num_chunks); - buffer->num_chunks = num_chunks; + struct music_chunk *chunk; + chunk = available = chunks; - chunk = buffer->available = buffer->chunks; + for (unsigned i = 1; i < num_chunks; ++i) { + chunk->next = &chunks[i]; + chunk = chunk->next; + } - for (unsigned i = 1; i < num_chunks; ++i) { - chunk->next = &buffer->chunks[i]; - chunk = chunk->next; + chunk->next = nullptr; } - chunk->next = NULL; + ~music_buffer() { + assert(chunks != nullptr); + assert(num_chunks > 0); + assert(num_allocated == 0); - buffer->mutex = g_mutex_new(); - -#ifndef NDEBUG - buffer->num_allocated = 0; -#endif + g_mutex_free(mutex); + g_free(chunks); + } +}; - return buffer; +struct music_buffer * +music_buffer_new(unsigned num_chunks) +{ + return new music_buffer(num_chunks); } void music_buffer_free(struct music_buffer *buffer) { - assert(buffer->chunks != NULL); - assert(buffer->num_chunks > 0); - assert(buffer->num_allocated == 0); - - g_mutex_free(buffer->mutex); - g_free(buffer->chunks); - g_free(buffer); + delete buffer; } unsigned -- cgit v1.2.3