Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Utils / pool_alloc_mixin.hh
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief pool_alloc_mixin public header */
23
24 #ifndef HH_pool_alloc_mixin_
25 #define HH_pool_alloc_mixin_ 1
26
27 // Custom includes
28 #include <boost/pool/singleton_pool.hpp>
29
30 //#include "pool_alloc_mixin.mpp"
31 ///////////////////////////////hh.p////////////////////////////////////////
32
33 namespace senf {
34
35     struct pool_alloc_mixin_tag;
36
37     /** \brief Mixin to assign pool allocator to a class
38
39         This mixin will overload a classes <tt>operator new</tt> and <tt>operator delete</tt> so as
40         to make the class use the <a
41         href="http://www.boost.org/libs/pool/doc/index.html">Boost.Pool</a> memory allocator by
42         default. Using this allocator does however introduce a few restrictions:
43
44         \li The operator is defined for a fixed size. Therefore if you derive from the class <b>you
45             must not change it's size</t>.
46         \li If you change the size of the class in a derived class you have to derive from
47             pool_alloc_mixin again.
48
49         Usage:
50         \code
51           class SomeClass
52               : public senf::pool_alloc_mixin<SomeClass>
53           {
54               // ...
55           };
56         \endcode
57
58         \note pool_alloc_mixin uses the <a
59             href="http://www.boost.org/libs/pool/doc/index.html">Boost.Pool</a> <i>singleton
60             pool</i> interface with the tag <tt>pool_alloc_mixin_tag</tt>. This class is accessible
61             via the <tt>pool</tt> member. Using this member, it is simple to call relevant pool
62             functions, e.g. <tt>SomeClass::pool<>::release_memory()</tt>.
63      */
64     template <class Self>
65     class pool_alloc_mixin
66     {
67     public:
68         /** \brief Templated typedef for the pool used
69
70             Since the instantiation of the typedef must be delayed until Self is completely defined,
71             the simple typedef is replaced with a nested struct.
72          */
73         template <class T=void>
74         struct pool 
75             : public boost::singleton_pool< pool_alloc_mixin_tag, sizeof(Self) >
76         {
77             typedef boost::singleton_pool< pool_alloc_mixin_tag, sizeof(Self) > type;
78         };
79
80         static void * operator new (size_t size);
81                                         ///< Operator new utilizing pool allocation
82         static void operator delete (void *p, size_t size);
83                                         ///< Operator delete utilizing pool allocation
84
85 #ifndef NDEBUG
86         static unsigned long allocCounter();
87     private:
88         static unsigned long allocCounter(long delta);
89 #endif
90     };
91
92 }
93
94 ///////////////////////////////hh.e////////////////////////////////////////
95 //#include "pool_alloc_mixin.cci"
96 //#include "pool_alloc_mixin.ct"
97 #include "pool_alloc_mixin.cti"
98 #endif
99
100 \f
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // c-file-style: "senf"
105 // indent-tabs-mode: nil
106 // ispell-local-dictionary: "american"
107 // End: