File: utils.hpp

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (219 lines) | stat: -rw-r--r-- 4,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//  utility functions
//  Copyright (C) 2005-2012  Tim Blechmann
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#ifndef UTILITIES_UTILS_HPP
#define UTILITIES_UTILS_HPP

#include <cstddef>
#include <cassert>

#include <type_traits>
#include <boost/static_assert.hpp>

#include "boost/noncopyable.hpp"

#include <boost/intrusive_ptr.hpp>
#include <boost/detail/atomic_count.hpp>

#include "branch_hints.hpp"

typedef unsigned int uint;

#include "malloc_aligned.hpp"

#include "function_attributes.h"


namespace nova {

/* we're using some member of the boost namespace */
using boost::noncopyable;
using boost::intrusive_ptr;

/* some basic math functions */
inline bool ispoweroftwo(int i)
{
    return (i & (i - 1)) == 0;
}


template <unsigned int n>
struct is_power_of_two
{
    static const bool val = (n%2==0) && (is_power_of_two<(n>>1)>::val);
};

template <>
struct is_power_of_two<2>
{
    static const bool val = true;
};

inline int log2(int n)
{
    if (unlikely(n <= 0))
        return(0);

#ifdef __GNUC__
    return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz( n );
#else
    int r = -1;
    while (n)
    {
        r++;
        n >>= 1;
    }
    return r;
#endif
}

inline int nextpoweroftwo(int n)
{
    n = n - 1;
    const uint bitspace = sizeof(int) * 8 / 2;
    for (uint i = 1; i != bitspace; i *= 2)
        n = n | (n >> i);

    return n + 1;
}

using std::size_t;

/** \brief base class for a callback function */
template <typename t>
class runnable
{
public:
    virtual ~runnable(void)
    {}

    virtual t run(void) = 0;
};

/** \brief tag for denoting a deleteable class */
class deleteable
{};


template <class T>
struct default_deleter
{
    void operator()(T * t)
    {
        delete t;
    }
};

struct delayed_deleter
{
    template <typename T>
    inline void operator()(T *);
};

struct checked_deleter
{
    template<class T>
    void operator()(T * x) const
    {
        boost::checked_delete(x);
    }
};


template <typename deleter = checked_deleter >
struct intrusive_refcountable:
    public noncopyable,
    public deleter
{
    intrusive_refcountable(void):
        use_count_(0)
    {}

    virtual ~intrusive_refcountable(void)
    {}

    void add_ref(void)
    {
        ++use_count_;
    }

    void release(void)
    {
        if(--use_count_ == 0)
            deleter::operator()(this);
    }

    inline friend void intrusive_ptr_add_ref(intrusive_refcountable * p)
    {
        p->add_ref();
    }

    inline friend void intrusive_ptr_release(intrusive_refcountable * p)
    {
        p->release();
    }

    boost::detail::atomic_count use_count_;
};


template <class t, class compare = std::less<t> >
struct compare_by_instance
{
    bool operator()(const t * lhs, const t * rhs)
    {
        assert(lhs and rhs);
        compare cmp;
        return cmp(*lhs, *rhs);
    }
};


PURE inline std::size_t string_hash(const char * str)
{
    std::size_t ret = 0;

    // sdbm hash ... later try another function!
    int c;
    while ((c = *str++))
        ret = c + (ret << 6) + (ret << 16) - ret;

    return ret;
}

struct linear_allocator
{
    linear_allocator(char * chunk):
        chunk(chunk)
    {}

    template <typename T>
    T * alloc(int count = 1)
    {
        T * ret = reinterpret_cast<T*>(chunk);
        chunk += count * sizeof(T);
        return ret;
    }

private:
    char * chunk;
};

} /* namespace nova */

#endif /* UTILITIES_UTILS_HPP */