class library: Volume - use ServerBoot to send synthdef
[supercollider.git] / server / supernova / utilities / simple_pool.hpp
blob37bcd4e5bed1007427c8e09cdb731ad13fadbf63
1 // simple pool class
2 // Copyright (C) 2009, 2010 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_SIMPLE_POOL_HPP
20 #define UTILITIES_SIMPLE_POOL_HPP
22 extern "C"
24 #include "tlsf.h"
27 #include <cstdlib>
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"
42 namespace nova {
44 /**
45 * simple memory pool class, based on tlsf.
47 * its size has to be set at runtime before it can be used.
49 * */
50 template <bool blocking = false>
51 class simple_pool:
52 boost::noncopyable
54 typedef typename boost::mpl::if_c<blocking,
55 spin_lock,
56 dummy_mutex>::type mutex_type;
58 typedef typename mutex_type::scoped_lock scoped_lock;
60 struct data:
61 mutex_type
63 data(void):
64 pool(0)
67 void init(std::size_t size, bool lock)
69 pool = (char*) operator new(size);
70 mlock(pool, size);
72 std::memset(pool, 0, size);
73 init_memory_pool(size, pool);
76 ~data(void)
78 scoped_lock lock(*this);
79 if(pool) {
80 destroy_memory_pool(pool);
81 operator delete(pool);
85 char* pool;
88 public:
89 simple_pool(void)
92 simple_pool(std::size_t size, bool lock = false)
94 init(size, lock);
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);
102 #endif
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);
119 void free(void * p)
121 return ::free(p);
124 std::size_t get_max_size(void)
126 return std::numeric_limits<std::size_t>::max();
128 #else
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);
141 void free(void * p)
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);
151 #endif
152 private:
153 data data_;
156 } /* namespace nova */
158 #endif /* UTILITIES_SIMPLE_POOL_HPP */