00001 // Copyright (c) 2000-2001 Brad Hughes <bhughes@trolltech.com> 00002 // 00003 // Use, modification and distribution is allowed without limitation, 00004 // warranty, or liability of any kind. 00005 // 00006 00007 #include "recycler.h" 00008 #include "constants.h" 00009 #include "buffer.h" 00010 00011 #include <iostream> 00012 using namespace std; 00013 00014 Recycler::Recycler(unsigned int sz) 00015 : add_index(0), done_index(0), current_count(0) 00016 { 00017 buffer_count = (sz / Buffer::size()); 00018 if (buffer_count < 1) { 00019 buffer_count = 1; 00020 } 00021 00022 buffers = new Buffer*[buffer_count]; 00023 00024 for (unsigned int i = 0; i < buffer_count; i ++) 00025 { 00026 buffers[i] = new Buffer; 00027 } 00028 } 00029 00030 00031 Recycler::~Recycler() 00032 { 00033 for (unsigned int i = 0; i < buffer_count; i++) 00034 { 00035 delete buffers[i]; 00036 buffers[i] = 0; 00037 } 00038 00039 delete [] buffers; 00040 } 00041 00042 00043 bool Recycler::full() const 00044 { 00045 return current_count == buffer_count; 00046 } 00047 00048 00049 bool Recycler::empty() const 00050 { 00051 return current_count == 0; 00052 } 00053 00054 00055 int Recycler::available() const 00056 { 00057 return buffer_count - current_count; 00058 } 00059 00060 00061 int Recycler::used() const 00062 { 00063 return current_count; 00064 } 00065 00066 00067 Buffer *Recycler::get() 00068 { 00069 if (full()) 00070 return 0; 00071 return buffers[add_index]; 00072 } 00073 00074 00075 void Recycler::add() 00076 { 00077 add_index = ++add_index % buffer_count; 00078 current_count++; 00079 } 00080 00081 00082 Buffer *Recycler::next() 00083 { 00084 return buffers[done_index]; 00085 } 00086 00087 00088 void Recycler::done() 00089 { 00090 done_index = ++done_index % buffer_count; 00091 current_count--; 00092 } 00093 00094 00095 void Recycler::clear() 00096 { 00097 add_index = done_index = current_count = 0; 00098 } 00099 00100 00101 unsigned int Recycler::size() const 00102 { 00103 return buffer_count * Buffer::size(); 00104 }
1.6.3