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 buffers[i] = new Buffer; 00026 } 00027 } 00028 00029 00030 Recycler::~Recycler() 00031 { 00032 for (unsigned int i = 0; i < buffer_count; i++) { 00033 delete buffers[i]; 00034 buffers[i] = 0; 00035 } 00036 00037 delete [] buffers; 00038 } 00039 00040 00041 bool Recycler::full() const 00042 { 00043 return current_count == buffer_count; 00044 } 00045 00046 00047 bool Recycler::empty() const 00048 { 00049 return current_count == 0; 00050 } 00051 00052 00053 int Recycler::available() const 00054 { 00055 return buffer_count - current_count; 00056 } 00057 00058 00059 int Recycler::used() const 00060 { 00061 return current_count; 00062 } 00063 00064 00065 Buffer *Recycler::get() 00066 { 00067 if (full()) 00068 return 0; 00069 return buffers[add_index]; 00070 } 00071 00072 00073 void Recycler::add() 00074 { 00075 add_index = ++add_index % buffer_count; 00076 current_count++; 00077 } 00078 00079 00080 Buffer *Recycler::next() 00081 { 00082 return buffers[done_index]; 00083 } 00084 00085 00086 void Recycler::done() 00087 { 00088 done_index = ++done_index % buffer_count; 00089 current_count--; 00090 } 00091 00092 00093 void Recycler::clear() 00094 { 00095 add_index = done_index = current_count = 0; 00096 } 00097 00098 00099 unsigned int Recycler::size() const 00100 { 00101 return buffer_count * Buffer::size(); 00102 }
1.5.5