class library: Volume - use ServerBoot to send synthdef
[supercollider.git] / server / supernova / utilities / static_pool.hpp
blob306c5075993855b5c788a9e75a166ea11a14c990
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 <array>
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"
38 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 std::array<long, poolsize> pool;
61 void lock_memory(void)
63 mlock(data_.pool.data(), poolsize * sizeof(long));
66 public:
67 static_pool(bool lock = false) throw()
69 /* first lock, then assign */
70 if (lock)
71 lock_memory();
73 data_.pool.fill(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 */