class library: Volume - use ServerBoot to send synthdef
[supercollider.git] / server / supernova / utilities / utils.hpp
bloba5ce79e078fe20df56dd804dff626896d6382eba
1 // utility functions
2 // Copyright (C) 2005-2012 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_UTILS_HPP
20 #define UTILITIES_UTILS_HPP
22 #include <cstddef>
23 #include <cassert>
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"
42 namespace nova {
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);
61 template <>
62 struct is_power_of_two<2>
64 static const bool val = true;
67 inline int log2(int n)
69 if (unlikely(n <= 0))
70 return(0);
72 #ifdef __GNUC__
73 return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz( n );
74 #else
75 int r = -1;
76 while (n)
78 r++;
79 n >>= 1;
81 return r;
82 #endif
85 inline int nextpoweroftwo(int n)
87 n = n - 1;
88 const uint bitspace = sizeof(int) * 8 / 2;
89 for (uint i = 1; i != bitspace; i *= 2)
90 n = n | (n >> i);
92 return n + 1;
95 using std::size_t;
97 /** \brief base class for a callback function */
98 template <typename t>
99 class runnable
101 public:
102 virtual ~runnable(void)
105 virtual t run(void) = 0;
108 /** \brief tag for denoting a deleteable class */
109 class deleteable
113 template <class T>
114 struct default_deleter
116 void operator()(T * t)
118 delete t;
122 struct delayed_deleter
124 template <typename T>
125 inline void operator()(T *);
128 struct checked_deleter
130 template<class T>
131 void operator()(T * x) const
133 boost::checked_delete(x);
138 template <typename deleter = checked_deleter >
139 struct intrusive_refcountable:
140 public noncopyable,
141 public deleter
143 intrusive_refcountable(void):
144 use_count_(0)
147 virtual ~intrusive_refcountable(void)
150 void add_ref(void)
152 ++use_count_;
155 void release(void)
157 if(--use_count_ == 0)
158 deleter::operator()(this);
161 inline friend void intrusive_ptr_add_ref(intrusive_refcountable * p)
163 p->add_ref();
166 inline friend void intrusive_ptr_release(intrusive_refcountable * p)
168 p->release();
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)
180 assert(lhs and rhs);
181 compare cmp;
182 return cmp(*lhs, *rhs);
187 PURE inline std::size_t string_hash(const char * str)
189 std::size_t ret = 0;
191 // sdbm hash ... later try another function!
192 int c;
193 while ((c = *str++))
194 ret = c + (ret << 6) + (ret << 16) - ret;
196 return ret;
199 struct linear_allocator
201 linear_allocator(char * chunk):
202 chunk(chunk)
205 template <typename T>
206 T * alloc(int count = 1)
208 T * ret = reinterpret_cast<T*>(chunk);
209 chunk += count * sizeof(T);
210 return ret;
213 private:
214 char * chunk;
217 } /* namespace nova */
219 #endif /* UTILITIES_UTILS_HPP */