File: malloc_aligned.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 (331 lines) | stat: -rw-r--r-- 7,053 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//  functions for aligned memory allocation
//  Copyright (C) 2009 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_MALLOC_ALIGNED_HPP
#define UTILITIES_MALLOC_ALIGNED_HPP

#include <cstdlib>
#include <cstring>

#include <new>     // for std::bad_alloc
#include <utility> // for std::forward

#include <boost/noncopyable.hpp>

#ifdef __SSE2__
#include <xmmintrin.h>
#elif defined(HAVE_TBB)
#include <tbb/cache_aligned_allocator.h>
#endif /* HAVE_TBB */

#ifdef _MSC_VER
#include <malloc.h>
#endif

#include "function_attributes.h"

namespace nova {

#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
/* we have posix_memalign */

/* memory alignment constraints:
 *
 * - 16 byte for SSE operations
 * - the cache lines size of modern x86 cpus is 64 bytes (pentium-m, pentium 4, core, k8)
 */
const int malloc_memory_alignment = 64;

inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    void * ret;
    int status = posix_memalign(&ret, malloc_memory_alignment, nbytes);
    if (!status)
        return ret;
    else
        return 0;
}

inline void free_aligned(void *ptr)
{
    free(ptr);
}

#elif defined(__APPLE__)

const int malloc_memory_alignment = 64;

/* apple's malloc implementation returns 16-byte aligned chunks */
inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    return malloc(nbytes);
}

inline void free_aligned(void *ptr)
{
    free(ptr);
}


#elif defined(__SSE2__)

const int malloc_memory_alignment = 64;

inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    return _mm_malloc(nbytes, malloc_memory_alignment);
}

inline void free_aligned(void *ptr)
{
    _mm_free(ptr);
}

#elif defined(_MSC_VER)

const int malloc_memory_alignment = 64;

inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    return _aligned_malloc(nbytes, malloc_memory_alignment);
}

inline void free_aligned(void *ptr)
{
    _aligned_free(ptr);
}

#elif defined(HAVE_TBB)

inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    tbb::cache_aligned_allocator<void*> ca_alloc;
    return static_cast<void*>(ca_alloc.allocate(nbytes));
}

inline void free_aligned(void *ptr)
{
    tbb::cache_aligned_allocator<void*> ca_alloc;
    ca_alloc.deallocate(static_cast<void**>(ptr), 0);
}

#else

/* on other systems, we use the aligned memory allocation taken
 * from thomas grill's implementation for pd */
#define VECTORALIGNMENT 128
inline void* MALLOC malloc_aligned(std::size_t nbytes)
{
    void* vec = malloc(nbytes+ (VECTORALIGNMENT/8-1) + sizeof(void *));

    if (vec != NULL) {
        /* get alignment of first possible signal vector byte */
        long alignment = ((long)vec+sizeof(void *))&(VECTORALIGNMENT/8-1);
        /* calculate aligned pointer */
        void *ret = (unsigned char *)vec+sizeof(void *)+(alignment == 0?0:VECTORALIGNMENT/8-alignment);
        /* save original memory location */
        *(void **)((unsigned char *)ret-sizeof(void *)) = vec;
        return ret;
    } else
        return 0;
}

inline void free_aligned(void *ptr)
{
    if (ptr == NULL)
        return;

    /* get original memory location */
    void *ori = *(void **)((unsigned char *)ptr-sizeof(void *));
    free(ori);
}

#undef VECTORALIGNMENT

#endif

inline void * calloc_aligned(std::size_t nbytes)
{
    void * ret = malloc_aligned(nbytes);
    if (ret)
        std::memset(ret, 0, nbytes);
    return ret;
}

template <typename T>
T* malloc_aligned(std::size_t n)
{
    return static_cast<T*>(malloc_aligned(n * sizeof(T)));
}

template <typename T>
T* calloc_aligned(std::size_t n)
{
    return static_cast<T*>(calloc_aligned(n * sizeof(T)));
}


/** aligned allocator. uses malloc_aligned and free_aligned internally
 *  */
template <class T>
class aligned_allocator
{
public:
    typedef std::size_t size_type;
    typedef std::ptrdiff_t difference_type;
    typedef T*        pointer;
    typedef const T*  const_pointer;
    typedef T&        reference;
    typedef const T&  const_reference;
    typedef T         value_type;

    template <class U> struct rebind
    {
        typedef aligned_allocator<U> other;
    };

    aligned_allocator(void)
    {}

    template <class U>
    aligned_allocator(aligned_allocator<U> const & rhs)
    {}

    pointer address(reference x) const
    {
        return &x;
    }

    const_pointer address(const_reference x) const
    {
        return &x;
    }

    pointer allocate(size_type n, const void* hint = 0)
    {
        pointer ret = malloc_aligned<T>(n);
        if (ret == 0)
            throw std::bad_alloc();
        return ret;
    }

    void deallocate(pointer p, size_type n)
    {
        return free_aligned(p);
    }

    size_type max_size() const throw()
    {
        return size_type(-1) / sizeof(T);
    }

    void construct(pointer p, const T& val = T())
    {
        ::new(p) T(val);
    }

#if (__cplusplus >= 201103L)
    template< class U, class... Args >
    void construct(U * p, Args&& ... args)
    {
        ::new(p) U(std::forward<Args>(args)...);
    }
#endif

    void destroy(pointer p)
    {
        p->~T();
    }
};


template<typename T, typename U>
bool operator==( aligned_allocator<T> const& left, aligned_allocator<U> const& right )
{
    return !(left != right);
}

template<typename T, typename U>
bool operator!=( aligned_allocator<T> const& left, aligned_allocator<U> const& right )
{
    return true;
}


/** smart-pointer, freeing the managed pointer via free_aligned */
template<class T, bool managed = true>
class aligned_storage_ptr
{
public:
    explicit aligned_storage_ptr(T * p = 0):
        ptr(p)
    {}

    ~aligned_storage_ptr(void)
    {
        if (managed && ptr)
            free_aligned(ptr);
    }

    void reset(T * p = 0)
    {
        if (managed && ptr)
            free_aligned(ptr);
        ptr = p;
    }

    T & operator*() const
    {
        return *ptr;
    }

    T * operator->() const
    {
        return ptr;
    }

    T * get() const
    {
        return ptr;
    }

    aligned_storage_ptr & operator=(T * p)
    {
        reset(p);
        return *this;
    }

    operator bool() const
    {
        return bool(ptr);
    }

    void swap(aligned_storage_ptr & b)
    {
        T * p = ptr;
        ptr = b.ptr;
        b.ptr = p;
    }

private:
    T * ptr;
};

} /* namespace nova */

#endif /* UTILITIES_MALLOC_ALIGNED_HPP */