From 2d622fd9bcb8da9dd3f3206e296cd6a701fc9d12 Mon Sep 17 00:00:00 2001 From: vovanec Date: Sat, 23 Jun 2007 16:48:01 +0000 Subject: moved into qmmp dir git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@12 90c681e8-e032-0410-971d-27865f9a5e38 --- lib/recycler.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lib/recycler.cpp (limited to 'lib/recycler.cpp') diff --git a/lib/recycler.cpp b/lib/recycler.cpp new file mode 100644 index 000000000..515652fb0 --- /dev/null +++ b/lib/recycler.cpp @@ -0,0 +1,103 @@ +// Copyright (c) 2000-2001 Brad Hughes +// +// Use, modification and distribution is allowed without limitation, +// warranty, or liability of any kind. +// + +#include "recycler.h" +#include "constants.h" +#include "buffer.h" + + +Recycler::Recycler ( unsigned int sz ) + : add_index ( 0 ), done_index ( 0 ), current_count ( 0 ) +{ + buffer_count = ( sz / Buffer::size() ); + if ( buffer_count < 1 ) + { + buffer_count = 1; + } + + buffers = new Buffer*[buffer_count]; + + for ( unsigned int i = 0; i < buffer_count; i ++ ) + { + buffers[i] = new Buffer; + } +} + + +Recycler::~Recycler() +{ + for ( unsigned int i = 0; i < buffer_count; i++ ) + { + delete buffers[i]; + buffers[i] = 0; + } + + delete [] buffers; +} + + +bool Recycler::full() const +{ + return current_count == buffer_count; +} + + +bool Recycler::empty() const +{ + return current_count == 0; +} + + +int Recycler::available() const +{ + return buffer_count - current_count; +} + + +int Recycler::used() const +{ + return current_count; +} + + +Buffer *Recycler::get() +{ + if ( full() ) + return 0; + return buffers[add_index]; +} + + +void Recycler::add() +{ + add_index = ++add_index % buffer_count; + current_count++; +} + + +Buffer *Recycler::next() +{ + return buffers[done_index]; +} + + +void Recycler::done() +{ + done_index = ++done_index % buffer_count; + current_count--; +} + + +void Recycler::clear() +{ + add_index = done_index = current_count = 0; +} + + +unsigned int Recycler::size() const +{ + return buffer_count * Buffer::size(); +} -- cgit v1.2.3-13-gbd6f