2 // Copyright (C) 2009 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_STATIC_POOL_HPP
20 #define UTILITIES_STATIC_POOL_HPP
29 #include <boost/noncopyable.hpp>
30 #include <boost/static_assert.hpp>
31 #include <boost/mpl/arithmetic.hpp>
32 #include <boost/mpl/modulus.hpp>
34 #include "nova-tt/spin_lock.hpp"
35 #include "nova-tt/dummy_mutex.hpp"
36 #include "nova-tt/mlock.hpp"
40 template <std::size_t bytes
,
41 bool blocking
= false>
45 BOOST_STATIC_ASSERT((boost::mpl::modulus
<boost::mpl::int_
<bytes
>, boost::mpl::int_
<sizeof(long)> >::value
== 0));
47 static const std::size_t poolsize
= bytes
/sizeof(long);
49 typedef typename
boost::mpl::if_c
<blocking
,
51 dummy_mutex
>::type mutex_type
;
53 typedef typename
mutex_type::scoped_lock scoped_lock
;
58 std::array
<long, poolsize
> pool
;
61 void lock_memory(void)
63 mlock(data_
.pool
.data(), poolsize
* sizeof(long));
67 static_pool(bool lock
= false) throw()
69 /* first lock, then assign */
74 std::size_t status
= init_memory_pool(bytes
, data_
.pool
.begin());
79 ~static_pool() throw()
81 scoped_lock
lock(data_
);
82 destroy_memory_pool(data_
.pool
.begin());
85 void * malloc(std::size_t size
)
87 scoped_lock
lock(data_
);
88 return malloc_ex(size
, data_
.pool
.begin());
93 scoped_lock
lock(data_
);
94 free_ex(p
, data_
.pool
.begin());
97 std::size_t get_max_size(void)
99 return ::get_max_size(data_
.pool
.begin());
106 } /* namespace nova */
108 #endif /* UTILITIES_STATIC_POOL_HPP */