rev: 1dfb319fdb2c605b0c4b516f11efb41e198ee30b bqvec/bqvec/Allocators.h -rw-r--r-- 11.3 KiB View raw Log this file
1dfb319fdb2c — Chris Cannam Fix case where oldchannels < channels a month ago
                                                                                
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    bqvec

    A small library for vector arithmetic and allocation in C++ using
    raw C pointer arrays.

    Copyright 2007-2017 Particular Programs Ltd.

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy,
    modify, merge, publish, distribute, sublicense, and/or sell copies
    of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Except as contained in this notice, the names of Chris Cannam and
    Particular Programs Ltd shall not be used in advertising or
    otherwise to promote the sale, use or other dealings in this
    Software without prior written authorization.
*/

#ifndef BQVEC_ALLOCATORS_H
#define BQVEC_ALLOCATORS_H

/*
 * Aligned and per-channel allocators and deallocators for raw C array
 * buffers and C++ STL vectors.
 */

#include <new>
#include <stdlib.h>
#include <stddef.h>
#include <stdexcept>

#ifndef HAVE_POSIX_MEMALIGN
#ifndef LACK_POSIX_MEMALIGN
#ifndef _WIN32
#ifndef __APPLE__
#define HAVE_POSIX_MEMALIGN
#endif
#endif
#endif
#endif

#ifndef MALLOC_IS_ALIGNED
#ifndef MALLOC_IS_NOT_ALIGNED
#ifdef __APPLE__
#define MALLOC_IS_ALIGNED
#endif
#endif
#endif

#ifndef HAVE__ALIGNED_MALLOC
#ifndef LACK__ALIGNED_MALLOC
#ifdef _WIN32
#define HAVE__ALIGNED_MALLOC
#endif
#endif
#endif

#ifndef USE_OWN_ALIGNED_MALLOC
#ifndef AVOID_OWN_ALIGNED_MALLOC
#ifndef HAVE_POSIX_MEMALIGN
#ifndef MALLOC_IS_ALIGNED
#ifndef HAVE__ALIGNED_MALLOC
#define USE_OWN_ALIGNED_MALLOC
#endif
#endif
#endif
#endif
#endif

#ifdef HAVE_POSIX_MEMALIGN
#include <sys/mman.h>
#include <errno.h>
#endif

#ifndef NO_EXCEPTIONS
#ifdef LACK_BAD_ALLOC
namespace std {
    struct bad_alloc { };
}
#endif
#endif

namespace breakfastquay {

template <typename T>
T *allocate(size_t count)
{
    void *ptr = 0;

    // We'd like to check HAVE_IPP first and, if it's defined, call
    // ippsMalloc_8u(count * sizeof(T)). But that isn't a general
    // replacement for malloc() because it only takes an int
    // argument. So we save it for the specialisations of
    // allocate<float> and allocate<double> below, where we're more
    // likely to get away with it.

#ifdef MALLOC_IS_ALIGNED
    ptr = malloc(count * sizeof(T));
#else /* !MALLOC_IS_ALIGNED */

    // That's the "sufficiently aligned" functions dealt with, the
    // rest need a specific alignment provided to the call. 32-byte
    // alignment is required for at least OpenMAX
    static const int alignment = 32;

#ifdef HAVE__ALIGNED_MALLOC
    ptr = _aligned_malloc(count * sizeof(T), alignment);
#else /* !HAVE__ALIGNED_MALLOC */

#ifdef HAVE_POSIX_MEMALIGN
    int rv = posix_memalign(&ptr, alignment, count * sizeof(T));
    if (rv) {
#ifndef NO_EXCEPTIONS
        if (rv == EINVAL) {
            throw std::logic_error("Internal error: invalid alignment");
         } else {
            throw std::bad_alloc();
        }
#else
        abort();
#endif
    }
#else /* !HAVE_POSIX_MEMALIGN */
    
#ifdef USE_OWN_ALIGNED_MALLOC
#pragma message("Rolling own aligned malloc: this is unlikely to perform as well as the alternatives")

    // Alignment must be a power of two, bigger than the pointer
    // size. Stuff the actual malloc'd pointer in just before the
    // returned value.  This is the least desirable way to do this --
    // the other options below are all better
    size_t allocd = count * sizeof(T) + alignment;
    void *buf = malloc(allocd);
    if (buf) {
        char *adj = (char *)buf;
        while ((unsigned long long)adj & (alignment-1)) --adj;
        ptr = ((char *)adj) + alignment;
        new (((void **)ptr)[-1]) (void *);
        ((void **)ptr)[-1] = buf;
    }

#else /* !USE_OWN_ALIGNED_MALLOC */

#error "No aligned malloc available: define MALLOC_IS_ALIGNED to use system malloc, HAVE_POSIX_MEMALIGN if posix_memalign is available, HAVE__ALIGNED_MALLOC if _aligned_malloc is available, or USE_OWN_ALIGNED_MALLOC to roll our own"

#endif /* !USE_OWN_ALIGNED_MALLOC */
#endif /* !HAVE_POSIX_MEMALIGN */
#endif /* !HAVE__ALIGNED_MALLOC */
#endif /* !MALLOC_IS_ALIGNED */

    if (!ptr) {
#ifndef NO_EXCEPTIONS
        throw std::bad_alloc();
#else
        abort();
#endif
    }

    T *typed_ptr = static_cast<T *>(ptr);
    for (size_t i = 0; i < count; ++i) {
        new (typed_ptr + i) T;
    }
    return typed_ptr;
}

#ifdef HAVE_IPP

template <>
float *allocate(size_t count);

template <>
double *allocate(size_t count);

#endif
	
template <typename T>
T *allocate_and_zero(size_t count)
{
    T *ptr = allocate<T>(count);
    for (size_t i = 0; i < count; ++i) {
        ptr[i] = T();
    }
    return ptr;
}

template <typename T>
void deallocate(T *ptr)
{
    if (!ptr) return;
    
#ifdef MALLOC_IS_ALIGNED
    free((void *)ptr);
#else /* !MALLOC_IS_ALIGNED */

#ifdef HAVE__ALIGNED_MALLOC
    _aligned_free((void *)ptr);
#else /* !HAVE__ALIGNED_MALLOC */

#ifdef HAVE_POSIX_MEMALIGN
    free((void *)ptr);
#else /* !HAVE_POSIX_MEMALIGN */
    
#ifdef USE_OWN_ALIGNED_MALLOC
    free(((void **)ptr)[-1]);
#else /* !USE_OWN_ALIGNED_MALLOC */

#error "No aligned malloc available: define MALLOC_IS_ALIGNED to use system malloc, HAVE_POSIX_MEMALIGN if posix_memalign is available, or USE_OWN_ALIGNED_MALLOC to roll our own"

#endif /* !USE_OWN_ALIGNED_MALLOC */
#endif /* !HAVE_POSIX_MEMALIGN */
#endif /* !HAVE__ALIGNED_MALLOC */
#endif /* !MALLOC_IS_ALIGNED */
}

#ifdef HAVE_IPP

template <>
void deallocate(float *);

template <>
void deallocate(double *);

#endif

/// Reallocate preserving contents but leaving additional memory uninitialised	
template <typename T>
T *reallocate(T *ptr, size_t oldcount, size_t count)
{
    T *newptr = allocate<T>(count);
    if (oldcount && ptr) {
        size_t tocopy = oldcount;
        if (count < oldcount) tocopy = count;
        for (size_t i = 0; i < tocopy; ++i) {
            newptr[i] = ptr[i];
        }
    }
    if (ptr) deallocate<T>(ptr);
    return newptr;
}

/// Reallocate, zeroing all contents
template <typename T>
T *reallocate_and_zero(T *ptr, size_t oldcount, size_t count)
{
    ptr = reallocate(ptr, oldcount, count);
    for (size_t i = 0; i < count; ++i) {
        ptr[i] = T();
    }
    return ptr;
}
	
/// Reallocate preserving contents and zeroing any additional memory	
template <typename T>
T *reallocate_and_zero_extension(T *ptr, size_t oldcount, size_t count)
{
    ptr = reallocate(ptr, oldcount, count);
    if (count > oldcount) {
        for (size_t i = oldcount; i < count; ++i) {
            ptr[i] = T();
        }
    }
    return ptr;
}

template <typename T>
T **allocate_channels(size_t channels, size_t count)
{
    // We don't want to use the aligned allocate for the channel
    // pointers, it might even make things slower
    T **ptr = new T *[channels];
    for (size_t c = 0; c < channels; ++c) {
        ptr[c] = allocate<T>(count);
    }
    return ptr;
}
	
template <typename T>
T **allocate_and_zero_channels(size_t channels, size_t count)
{
    // We don't want to use the aligned allocate for the channel
    // pointers, it might even make things slower
    T **ptr = new T *[channels];
    for (size_t c = 0; c < channels; ++c) {
        ptr[c] = allocate_and_zero<T>(count);
    }
    return ptr;
}

template <typename T>
void deallocate_channels(T **ptr, size_t channels)
{
    if (!ptr) return;
    for (size_t c = 0; c < channels; ++c) {
        deallocate<T>(ptr[c]);
    }
    delete[] ptr;
}
	
template <typename T>
T **reallocate_channels(T **ptr,
                        size_t oldchannels, size_t oldcount,
                        size_t channels, size_t count)
{
    T **newptr = allocate_channels<T>(channels, count);
    if (oldcount && ptr) {
        for (size_t c = 0; c < oldchannels && c < channels; ++c) {
            for (size_t i = 0; i < oldcount && i < count; ++i) {
                newptr[c][i] = ptr[c][i];
            }
        }
    } 
    if (ptr) deallocate_channels<T>(ptr, oldchannels);
    return newptr;
}
	
template <typename T>
T **reallocate_and_zero_extend_channels(T **ptr,
                                        size_t oldchannels, size_t oldcount,
                                        size_t channels, size_t count)
{
    T **newptr = allocate_and_zero_channels<T>(channels, count);
    if (oldcount && ptr) {
        for (size_t c = 0; c < oldchannels && c < channels; ++c) {
            for (size_t i = 0; i < oldcount && i < count; ++i) {
                newptr[c][i] = ptr[c][i];
            }
        }
    } 
    if (ptr) deallocate_channels<T>(ptr, oldchannels);
    return newptr;
}


/** Trivial RAII class to call deallocate() on destruction.
 */
template <typename T>
class Deallocator
{
public:
    Deallocator(T *t) : m_t(t) { }
    ~Deallocator() { deallocate<T>(m_t); }

private:
    T *m_t;
};


/** Allocator for use with STL classes, e.g. vector, to ensure
 *  alignment.  Based on example code by Stephan T. Lavavej.
 *
 *  e.g. std::vector<float, StlAllocator<float> > v;
 */
template <typename T>
class StlAllocator
{
public:
    typedef T *pointer;
    typedef const T *const_pointer;
    typedef T &reference;
    typedef const T &const_reference;
    typedef T value_type;
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;

    StlAllocator() { }
    StlAllocator(const StlAllocator&) { }
    template <typename U> StlAllocator(const StlAllocator<U>&) { }
    ~StlAllocator() { }

    T *
    allocate(const size_t n) const {
        if (n == 0) return 0;
        if (n > max_size()) {
#ifndef NO_EXCEPTIONS
            throw std::length_error("Size overflow in StlAllocator::allocate()");
#else
            abort();
#endif
        }
        return ::breakfastquay::allocate<T>(n);
    }

    void
    deallocate(T *const p, const size_t) const {
        ::breakfastquay::deallocate(p);
    }

    template <typename U>
    T *
    allocate(const size_t n, const U *) const {
        return allocate(n);
    }

    T *
    address(T &r) const {
        return &r;
    }

    const T *
    address(const T &s) const {
        return &s;
    }

    size_t
    max_size() const {
        return (static_cast<size_t>(0) - static_cast<size_t>(1)) / sizeof(T);
    }

    template <typename U> struct rebind {
        typedef StlAllocator<U> other;
    };
    
    bool
    operator==(const StlAllocator &) const {
        return true;
    }

    bool
    operator!=(const StlAllocator &) const {
        return false;
    }

    void
    construct(T *const p, const T &t) const {
        void *const pv = static_cast<void *>(p);
        new (pv) T(t);
    }

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

private:
    StlAllocator& operator=(const StlAllocator&);
};

}

#endif