2 // Copyright (C) 2009, 2010 Tim Blechmann
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; see the file COPYING. If not, write to
16 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 // Boston, MA 02111-1307, USA.
19 #ifndef UTILITIES_SIMPLE_POOL_HPP
20 #define UTILITIES_SIMPLE_POOL_HPP
28 #include <cstring> /* for std::memset */
30 #include <boost/array.hpp>
31 #include <boost/noncopyable.hpp>
32 #include <boost/static_assert.hpp>
33 #include <boost/mpl/arithmetic.hpp>
34 #include <boost/mpl/modulus.hpp>
36 #include "nova-tt/spin_lock.hpp"
37 #include "nova-tt/dummy_mutex.hpp"
38 #include "nova-tt/mlock.hpp"
40 #include "function_attributes.h"
45 * simple memory pool class, based on tlsf.
47 * its size has to be set at runtime before it can be used.
50 template <bool blocking
= false>
54 typedef typename
boost::mpl::if_c
<blocking
,
56 dummy_mutex
>::type mutex_type
;
58 typedef typename
mutex_type::scoped_lock scoped_lock
;
67 void init(std::size_t size
, bool lock
)
69 pool
= (char*) operator new(size
);
72 std::memset(pool
, 0, size
);
73 init_memory_pool(size
, pool
);
78 scoped_lock
lock(*this);
80 destroy_memory_pool(pool
);
81 operator delete(pool
);
92 simple_pool(std::size_t size
, bool lock
= false)
97 void init(std::size_t size
, bool lock
= false)
99 #ifndef NOVA_MEMORY_DEBUGGING
100 assert(size
% sizeof(long) == 0);
101 data_
.init(size
, lock
);
105 ~simple_pool() throw()
108 #ifdef NOVA_MEMORY_DEBUGGING
109 void * MALLOC
malloc(std::size_t size
)
111 return ::malloc(size
);
114 void * MALLOC
realloc(void * p
, std::size_t size
)
116 return ::realloc(p
, size
);
124 std::size_t get_max_size(void)
126 return std::numeric_limits
<std::size_t>::max();
129 void * MALLOC
malloc(std::size_t size
)
131 scoped_lock
lock(data_
);
132 return malloc_ex(size
, data_
.pool
);
135 void * MALLOC
realloc(void * p
, std::size_t size
)
137 scoped_lock
lock(data_
);
138 return realloc_ex(p
, size
, data_
.pool
);
143 scoped_lock
lock(data_
);
144 free_ex(p
, data_
.pool
);
147 std::size_t get_max_size(void)
149 return ::get_max_size(data_
.pool
);
156 } /* namespace nova */
158 #endif /* UTILITIES_SIMPLE_POOL_HPP */