Fix scvim regsitry file for updated filename (thanks Carlo Capocasa)
[supercollider.git] / server / supernova / utilities / static_pool.hpp
blob2ba2a82592a64db2f84b4a49be1e20b034ad7e96
1 // static pool class
2 // Copyright (C) 2009 Tim Blechmann
3 //
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.
8 //
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
22 extern "C"
24 #include "tlsf.h"
27 #include <boost/array.hpp>
28 #include <boost/noncopyable.hpp>
29 #include <boost/static_assert.hpp>
30 #include <boost/mpl/arithmetic.hpp>
31 #include <boost/mpl/modulus.hpp>
33 #include "nova-tt/spin_lock.hpp"
34 #include "nova-tt/dummy_mutex.hpp"
35 #include "nova-tt/mlock.hpp"
37 namespace nova
40 template <std::size_t bytes,
41 bool blocking = false>
42 class static_pool:
43 boost::noncopyable
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,
50 spin_lock,
51 dummy_mutex>::type mutex_type;
53 typedef typename mutex_type::scoped_lock scoped_lock;
55 struct data:
56 mutex_type
58 boost::array<long, poolsize> pool;
61 void lock_memory(void)
63 mlock(data_.pool.begin(), bytes);
66 public:
67 static_pool(bool lock = false) throw()
69 /* first lock, then assign */
70 if (lock)
71 lock_memory();
73 data_.pool.assign(0);
74 std::size_t status = init_memory_pool(bytes, data_.pool.begin());
75 assert(status > 0);
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());
91 void free(void * p)
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());
102 private:
103 data data_;
106 } /* namespace nova */
108 #endif /* UTILITIES_STATIC_POOL_HPP */