switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / pool_alloc_mixin.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief pool_alloc_mixin public header */
30
31 #ifndef HH_SENF_Utils_pool_alloc_mixin_
32 #define HH_SENF_Utils_pool_alloc_mixin_ 1
33
34 // Custom includes
35 #include <boost/pool/singleton_pool.hpp>
36
37 //#include "pool_alloc_mixin.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace senf {
41
42     struct pool_alloc_mixin_tag;
43
44     /** \brief Mixin to assign pool allocator to a class
45
46         This mixin will overload a classes <tt>operator new</tt> and <tt>operator delete</tt> so as
47         to make the class use the <a
48         href="http://www.boost.org/doc/libs/release/libs/pool/doc/index.html">Boost.Pool</a> memory allocator by
49         default. Using this allocator does however introduce a few restrictions:
50
51         \li The operator is defined for a fixed size. Therefore if you derive from the class <b>you
52             must not change it's size</b>.
53         \li If you change the size of the class in a derived class you have to derive from
54             pool_alloc_mixin again.
55
56         Usage:
57         \code
58           class SomeClass
59               : public senf::pool_alloc_mixin<SomeClass>
60           {
61               // ...
62           };
63         \endcode
64
65         \note pool_alloc_mixin uses the <a
66             href="http://www.boost.org/doc/libs/release/libs/pool/doc/index.html">Boost.Pool</a> <i>singleton
67             pool</i> interface with the tag <tt>pool_alloc_mixin_tag</tt>. This class is accessible
68             via the <tt>pool</tt> member. Using this member, it is simple to call relevant pool
69             functions, e.g. <tt>SomeClass::pool<>::release_memory()</tt>.
70      */
71     template <class Self>
72     class pool_alloc_mixin
73     {
74     public:
75         /** \brief Templated typedef for the pool used
76
77             Since the instantiation of the typedef must be delayed until Self is completely defined,
78             the simple typedef is replaced with a nested struct.
79          */
80         template <class T=void>
81         struct pool
82             : public boost::singleton_pool< pool_alloc_mixin_tag, sizeof(Self) >
83         {
84             typedef boost::singleton_pool< pool_alloc_mixin_tag, sizeof(Self) > type;
85         };
86
87         static void * operator new (size_t size);
88                                         ///< Operator new utilizing pool allocation
89         static void operator delete (void *p, size_t size);
90                                         ///< Operator delete utilizing pool allocation
91
92 #ifdef SENF_DEBUG
93         static unsigned long allocCounter();
94     private:
95         static unsigned long allocCounter(long delta);
96 #endif
97     };
98
99 }
100
101 //-/////////////////////////////////////////////////////////////////////////////////////////////////
102 //#include "pool_alloc_mixin.cci"
103 //#include "pool_alloc_mixin.ct"
104 #include "pool_alloc_mixin.cti"
105 #endif
106
107 \f
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40
116 // End: