class library: Volume - use ServerBoot to send synthdef
[supercollider.git] / server / supernova / utilities / tl_allocator.hpp
blob616ec0bfb4c2cf2938990afbc28505bd074f9338
1 // thread-local real-time safe allocator class
2 // Copyright (C) 2008 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_TL_ALLOCATOR_HPP
20 #define UTILITIES_TL_ALLOCATOR_HPP
22 extern "C"
24 #include "tlsf.h"
27 #include <exception>
28 #include <cstring>
29 #include <array>
31 #include <boost/static_assert.hpp>
32 #include <boost/mpl/modulus.hpp>
33 #include <boost/mpl/arithmetic.hpp>
34 #include <boost/thread/tss.hpp>
36 #include "branch_hints.hpp"
38 namespace nova {
40 namespace detail {
42 #ifdef __GNUC__
43 template <std::size_t bytes>
44 class tl_allocator
46 BOOST_STATIC_ASSERT((boost::mpl::modulus<boost::mpl::int_<bytes>, boost::mpl::int_<sizeof(long)> >::value == 0));
48 public:
50 static const std::size_t poolsize = bytes/sizeof(long);
51 typedef std::size_t size_type;
52 typedef std::ptrdiff_t difference_type;
54 tl_allocator(void) throw()
56 if (likely(initialized))
57 return;
59 memset(pool, bytes, 0);
60 init_memory_pool(bytes, pool);
63 void * allocate(size_type n)
65 void * ret = malloc_ex(n, pool);
67 if (ret == 0)
68 throw std::bad_alloc();
70 return ret;
73 void deallocate(void * p)
75 free_ex(p, pool);
78 size_type max_size() const throw()
80 return bytes;
83 static __thread long pool[poolsize];
84 static __thread bool initialized;
87 template <std::size_t bytes>
88 long __thread tl_allocator<bytes>::pool[];
90 template <std::size_t bytes>
91 bool __thread tl_allocator<bytes>::initialized = false;
93 #else
95 template <std::size_t bytes>
96 class tl_allocator
98 BOOST_STATIC_ASSERT((boost::mpl::modulus<boost::mpl::int_<bytes>, boost::mpl::int_<sizeof(long)> >::value == 0));
99 static const std::size_t poolsize = bytes/sizeof(long);
101 struct pool_t
104 pool_t(void)
106 pool.assign(0);
107 init_memory_pool(bytes, pool.begin());
110 ~pool_t(void)
112 destroy_memory_pool(pool.begin());
115 std::array<long, poolsize> pool;
118 public:
119 typedef std::size_t size_type;
120 typedef std::ptrdiff_t difference_type;
122 tl_allocator(void) throw()
124 if (likely(pool.get()))
125 return;
126 pool.reset(new pool_t());
129 tl_allocator(tl_allocator const & rhs) throw()
132 void * allocate(size_type n)
134 void * ret = malloc_ex(n, pool->pool.begin());
136 if (ret == 0)
137 throw std::bad_alloc();
139 return ret;
142 void deallocate(void * p)
144 free_ex(p, pool->pool.begin());
147 size_type max_size() const throw()
149 return bytes;
152 typedef boost::thread_specific_ptr<pool_t> pool_ptr;
153 static pool_ptr pool;
156 template <std::size_t bytes>
157 typename tl_allocator<bytes>::pool_ptr tl_allocator<bytes>::pool;
159 #endif
165 * realtime allocator:
166 * allocates from a thread-local memory pool via the malloc implementation of tlsf
168 * it is not completely stl compliant
169 * memory allocated from one instance can be deallocated from each other instance
170 * of this class as long as it is done from the same thread
172 * */
173 template <typename T, std::size_t bytes = 8 * 1024 * 1024>
174 class tl_allocator
176 public:
177 typedef std::size_t size_type;
178 typedef std::ptrdiff_t difference_type;
179 typedef T* pointer;
180 typedef const T* const_pointer;
181 typedef T& reference;
182 typedef const T& const_reference;
183 typedef T value_type;
185 template <class U> struct rebind
187 typedef tl_allocator<U, bytes> other;
190 tl_allocator(void) throw()
193 template <class U, std::size_t bytes_>
194 tl_allocator(tl_allocator<U, bytes_> const & rhs) throw()
197 pointer address(reference x) const
199 return &x;
202 const_pointer address(const_reference x) const
204 return &x;
207 pointer allocate(size_type n,
208 const_pointer hint = 0)
210 return static_cast<pointer> (allocator.allocate(n * sizeof(T)));
213 void deallocate(pointer p, size_type n)
215 allocator.deallocate(p);
218 size_type max_size() const throw()
220 return bytes;
223 void construct(pointer p, const T& val)
225 ::new(p) T(val);
228 void destroy(pointer p)
230 p->~T();
233 private:
234 detail::tl_allocator<bytes> allocator;
237 } /* namespace nova */
239 #endif /* UTILITIES_TL_ALLOCATOR_HPP */