1 // thread-local real-time safe allocator class
2 // Copyright (C) 2008 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_TL_ALLOCATOR_HPP
20 #define UTILITIES_TL_ALLOCATOR_HPP
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"
43 template <std::size_t bytes
>
46 BOOST_STATIC_ASSERT((boost::mpl::modulus
<boost::mpl::int_
<bytes
>, boost::mpl::int_
<sizeof(long)> >::value
== 0));
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
))
59 memset(pool
, bytes
, 0);
60 init_memory_pool(bytes
, pool
);
63 void * allocate(size_type n
)
65 void * ret
= malloc_ex(n
, pool
);
68 throw std::bad_alloc();
73 void deallocate(void * p
)
78 size_type
max_size() const throw()
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;
95 template <std::size_t bytes
>
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);
107 init_memory_pool(bytes
, pool
.begin());
112 destroy_memory_pool(pool
.begin());
115 std::array
<long, poolsize
> pool
;
119 typedef std::size_t size_type
;
120 typedef std::ptrdiff_t difference_type
;
122 tl_allocator(void) throw()
124 if (likely(pool
.get()))
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());
137 throw std::bad_alloc();
142 void deallocate(void * p
)
144 free_ex(p
, pool
->pool
.begin());
147 size_type
max_size() const throw()
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
;
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
173 template <typename T
, std::size_t bytes
= 8 * 1024 * 1024>
177 typedef std::size_t size_type
;
178 typedef std::ptrdiff_t difference_type
;
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
202 const_pointer
address(const_reference x
) const
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()
223 void construct(pointer p
, const T
& val
)
228 void destroy(pointer p
)
234 detail::tl_allocator
<bytes
> allocator
;
237 } /* namespace nova */
239 #endif /* UTILITIES_TL_ALLOCATOR_HPP */