2 // Copyright (C) 2005-2010 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 /** \todo later use std::tr1 type_traits */
26 #include <boost/type_traits.hpp>
27 #include <boost/static_assert.hpp>
29 #include "boost/noncopyable.hpp"
31 /** \todo later use std::tr1 smart pointers */
32 #include <boost/shared_ptr.hpp>
33 #include <boost/scoped_ptr.hpp>
34 #include <boost/weak_ptr.hpp>
35 #include <boost/intrusive_ptr.hpp>
36 #include <boost/detail/atomic_count.hpp>
38 #include "branch_hints.hpp"
40 #include <boost/foreach.hpp>
41 #define foreach BOOST_FOREACH
43 typedef unsigned int uint
;
45 #include "malloc_aligned.hpp"
47 #include "function_attributes.h"
52 /* we're using some member of the boost namespace */
53 using boost::noncopyable
;
55 using boost::shared_ptr
;
56 using boost::weak_ptr
;
57 using boost::scoped_ptr
;
58 using boost::intrusive_ptr
;
60 /* some basic math functions */
61 inline bool ispoweroftwo(int i
)
63 return (i
& (i
- 1)) == 0;
67 template <unsigned int n
>
68 struct is_power_of_two
70 static const bool val
= (n
%2==0) && (is_power_of_two
<(n
>>1)>::val
);
74 struct is_power_of_two
<2>
76 static const bool val
= true;
79 inline int log2(int n
)
85 return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz( n
);
97 inline int nextpoweroftwo(int n
)
100 const uint bitspace
= sizeof(int) * 8 / 2;
101 for (uint i
= 1; i
!= bitspace
; i
*= 2)
109 /** \brief base class for a callback function */
110 template <typename t
>
114 virtual ~runnable(void)
117 virtual t
run(void) = 0;
120 /** \brief tag for denoting a deleteable class */
126 struct default_deleter
128 void operator()(T
* t
)
134 struct delayed_deleter
136 template <typename T
>
137 inline void operator()(T
*);
140 struct checked_deleter
143 void operator()(T
* x
) const
145 boost::checked_delete(x
);
150 template <typename deleter
= checked_deleter
>
151 struct intrusive_refcountable
:
155 intrusive_refcountable(void):
159 virtual ~intrusive_refcountable(void)
169 if(--use_count_
== 0)
170 deleter::operator()(this);
173 inline friend void intrusive_ptr_add_ref(intrusive_refcountable
* p
)
178 inline friend void intrusive_ptr_release(intrusive_refcountable
* p
)
183 boost::detail::atomic_count use_count_
;
187 template <class t
, class compare
= std::less
<t
> >
188 struct compare_by_instance
190 bool operator()(const t
* lhs
, const t
* rhs
)
194 return cmp(*lhs
, *rhs
);
199 PURE
inline std::size_t string_hash(const char * str
)
203 // sdbm hash ... later try another function!
206 ret
= c
+ (ret
<< 6) + (ret
<< 16) - ret
;
212 } /* namespace nova */
214 #endif /* UTILITIES_UTILS_HPP */