2 // Copyright (C) 2005-2012 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_UTILS_HPP
20 #define UTILITIES_UTILS_HPP
25 #include <type_traits>
26 #include <boost/static_assert.hpp>
28 #include "boost/noncopyable.hpp"
30 #include <boost/intrusive_ptr.hpp>
31 #include <boost/detail/atomic_count.hpp>
33 #include "branch_hints.hpp"
35 typedef unsigned int uint
;
37 #include "malloc_aligned.hpp"
39 #include "function_attributes.h"
44 /* we're using some member of the boost namespace */
45 using boost::noncopyable
;
46 using boost::intrusive_ptr
;
48 /* some basic math functions */
49 inline bool ispoweroftwo(int i
)
51 return (i
& (i
- 1)) == 0;
55 template <unsigned int n
>
56 struct is_power_of_two
58 static const bool val
= (n
%2==0) && (is_power_of_two
<(n
>>1)>::val
);
62 struct is_power_of_two
<2>
64 static const bool val
= true;
67 inline int log2(int n
)
73 return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz( n
);
85 inline int nextpoweroftwo(int n
)
88 const uint bitspace
= sizeof(int) * 8 / 2;
89 for (uint i
= 1; i
!= bitspace
; i
*= 2)
97 /** \brief base class for a callback function */
102 virtual ~runnable(void)
105 virtual t
run(void) = 0;
108 /** \brief tag for denoting a deleteable class */
114 struct default_deleter
116 void operator()(T
* t
)
122 struct delayed_deleter
124 template <typename T
>
125 inline void operator()(T
*);
128 struct checked_deleter
131 void operator()(T
* x
) const
133 boost::checked_delete(x
);
138 template <typename deleter
= checked_deleter
>
139 struct intrusive_refcountable
:
143 intrusive_refcountable(void):
147 virtual ~intrusive_refcountable(void)
157 if(--use_count_
== 0)
158 deleter::operator()(this);
161 inline friend void intrusive_ptr_add_ref(intrusive_refcountable
* p
)
166 inline friend void intrusive_ptr_release(intrusive_refcountable
* p
)
171 boost::detail::atomic_count use_count_
;
175 template <class t
, class compare
= std::less
<t
> >
176 struct compare_by_instance
178 bool operator()(const t
* lhs
, const t
* rhs
)
182 return cmp(*lhs
, *rhs
);
187 PURE
inline std::size_t string_hash(const char * str
)
191 // sdbm hash ... later try another function!
194 ret
= c
+ (ret
<< 6) + (ret
<< 16) - ret
;
199 struct linear_allocator
201 linear_allocator(char * chunk
):
205 template <typename T
>
206 T
* alloc(int count
= 1)
208 T
* ret
= reinterpret_cast<T
*>(chunk
);
209 chunk
+= count
* sizeof(T
);
217 } /* namespace nova */
219 #endif /* UTILITIES_UTILS_HPP */