Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

bufferpool.hh

Go to the documentation of this file.
00001 /*
00002  * PIAVE - PIAVE Is A Video Editor
00003  *
00004  * Copyright (C) 2002 Rolf Dubitzky, rolf@dubitzky.de
00005  */
00006 
00007 /*
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00028 #if ! defined ( BUFFERPOOL_HH )
00029 #define BUFFERPOOL_HH
00030 
00031 // std c
00032 #include <time.h> 
00033 
00034 // std c++
00035 #include <string>
00036 #include <vector>
00037 #include <map>
00038 
00039 // PIAVE
00040 #include "piave_base.hh"
00041 
00042 namespace PIAVE {
00043 
00044     class Pool;
00045     /*  simple refcounted buffer  */
00046     class Buffer
00047     {
00048       public:
00049         Buffer() : 
00050           _refCount( 0 ), _b( 0 ), _pool( 0 ) {};
00051         uint8_t    * buf() const { return _b; }
00052         void         setBuffer( uint8_t * b ) { _b=b; }
00053         int          getRefCount() const { return _refCount; }
00054         int          incRef() { return ++_refCount; }
00055         int          dropRef() { if (_refCount==0) { WARN("!!");return 0;} return --_refCount; }
00056         void         dropAllRefs() { _refCount = 0; }
00057         Pool       * getPool() const { return _pool; }
00058         void         setPool( Pool * p ) { _pool = p; }
00059       private:
00060         int        _refCount;
00061         uint8_t  * _b;
00062         Pool     * _pool;
00063         /* hide */
00064         Buffer( const Buffer & o ) {};
00065         Buffer & operator = ( const Buffer & o ) {};
00066     };
00067 
00068 // FIXME replace all this stuff by somethning less embarassing
00069 #define FBUFSIZE 1024
00070     struct Pool 
00071     {
00072         Pool( int s ) : size( s ) { 
00073             for ( int i=0; i<FBUFSIZE; i++ ) {
00074                 pool[i].setPool( this );
00075             }
00076         }
00077 //            void clear() {  memset( pool, 0, sizeof(pool) ); }
00078         Buffer pool[FBUFSIZE];
00079         int   size;
00080     };
00081     typedef std::map< int, Pool* > PoolMap;
00082 
00083     /*
00084      *  BufferPool this thing works because we usually always
00085      *  need amny buffers of the same size once we allocte a certain
00086      *  size, we can probably use a buffer of same size later
00087      */
00088     class BufferPool
00089     {
00090       public:
00091         static BufferPool * Inst() { 
00092             if (!_instance) {
00093                 _instance = new BufferPool();
00094             }
00095             return _instance;
00096         }
00098         static void printStats();
00100 //        static void clearAll();
00101 
00102         // get a buffer if size size, allocate if not available
00103         static Buffer * getNewBuffer( size_t size );
00104 
00105       private:
00106         BufferPool();
00107         ~BufferPool();
00108 
00109         PoolMap _poolMap;
00110 
00112         static BufferPool * _instance;
00113     };
00114 
00115 };
00116 
00117 #endif 

Generated on Tue Oct 14 20:45:36 2003 for piave by doxygen1.2.18