libktorrent  2.2.0
bufferpool.h
1 /***************************************************************************
2  * Copyright (C) 2011 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef BUFFERPOOL_H_
22 #define BUFFERPOOL_H_
23 
24 #include <map>
25 #include <list>
26 #include <boost/shared_array.hpp>
27 #include <QMutex>
28 #include <QWeakPointer>
29 #include <QSharedPointer>
30 #include <ktorrent_export.h>
31 #include <util/constants.h>
32 
33 namespace bt
34 {
35  class BufferPool;
36 
40  class KTORRENT_EXPORT Buffer
41  {
42  public:
43  typedef QSharedPointer<Buffer> Ptr;
44  typedef boost::shared_array<bt::Uint8> Data;
45 
46  Buffer(Data data, bt::Uint32 fill, bt::Uint32 cap, QWeakPointer<BufferPool> pool);
47  virtual ~Buffer();
48 
50  bt::Uint32 capacity() const {return cap;}
51 
53  bt::Uint32 size() const {return fill;}
54 
56  void setSize(bt::Uint32 s) {fill = s;}
57 
59  bt::Uint8* get() {return data.get();}
60 
61  private:
62  Data data;
63  bt::Uint32 fill;
64  bt::Uint32 cap;
65  QWeakPointer<BufferPool> pool;
66  };
67 
71  class KTORRENT_EXPORT BufferPool
72  {
73  public:
74  BufferPool();
75  virtual ~BufferPool();
76 
81  void setWeakPointer(QWeakPointer<BufferPool> wp) {self = wp;}
82 
89  Buffer::Ptr get(bt::Uint32 min_size);
90 
96  void release(Buffer::Data data, bt::Uint32 size);
97 
101  void clear();
102 
103  typedef QSharedPointer<BufferPool> Ptr;
104 
105  private:
106  typedef std::map<bt::Uint32, std::list<Buffer::Data> > FreeBufferMap;
107  QMutex mutex;
108  FreeBufferMap free_buffers;
109  QWeakPointer<BufferPool> self;
110  };
111 } /* namespace bt */
112 
113 #endif /* BUFFERPOOL_H_ */
bt::BufferPool::get
Buffer::Ptr get(bt::Uint32 min_size)
bt::BufferPool::release
void release(Buffer::Data data, bt::Uint32 size)
bt::BufferPool
Definition: bufferpool.h:72
bt::Buffer::capacity
bt::Uint32 capacity() const
Get the buffers capacity.
Definition: bufferpool.h:50
bt::Buffer
Definition: bufferpool.h:41
bt::Buffer::get
bt::Uint8 * get()
Get a pointer to the data.
Definition: bufferpool.h:59
bt::BufferPool::clear
void clear()
bt::BufferPool::setWeakPointer
void setWeakPointer(QWeakPointer< BufferPool > wp)
Definition: bufferpool.h:81
bt::Buffer::setSize
void setSize(bt::Uint32 s)
Set the current size.
Definition: bufferpool.h:56
bt::Buffer::size
bt::Uint32 size() const
Get the current size.
Definition: bufferpool.h:53