|
| 1 | +//------------------------------------------------------------------------ |
| 2 | +/* This source code is free software. It comes without any warranty, to |
| 3 | + * the extent permitted by applicable law. You can redistribute it |
| 4 | + * and/or modify it under the terms of the Do What The Fuck You Want |
| 5 | + * To Public License, Version 2, as published by Sam Hocevar. See |
| 6 | + * http://sam.zoy.org/wtfpl/COPYING for more details. */ |
| 7 | +//------------------------------------------------------------------------ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cstdlib> |
| 12 | +#include <algorithm> |
| 13 | +#include <cassert> |
| 14 | + |
| 15 | +#ifdef _MSC_VER |
| 16 | +#include <malloc.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#if __APPLE__ |
| 20 | +#include <AvailabilityMacros.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +//------------------------------------------------------------------------ |
| 24 | +namespace vst3utils { |
| 25 | + |
| 26 | +//------------------------------------------------------------------------ |
| 27 | +/** standard allocator using malloc and free */ |
| 28 | +struct standard_allocator |
| 29 | +{ |
| 30 | + static void* allocate (size_t numBytes) { return std::malloc (numBytes); } |
| 31 | + static void deallocate (void* ptr, size_t numButes) { std::free (ptr); } |
| 32 | +}; |
| 33 | + |
| 34 | +//------------------------------------------------------------------------ |
| 35 | +/** simple RAII buffer implementation */ |
| 36 | +template<typename T, typename allocatorT = standard_allocator> |
| 37 | +struct buffer final |
| 38 | +{ |
| 39 | + buffer (size_t num_elements = 0) { allocate (num_elements); } |
| 40 | + ~buffer () noexcept { deallocate (); } |
| 41 | + |
| 42 | + /** allocate */ |
| 43 | + void allocate (size_t num_elements) |
| 44 | + { |
| 45 | + deallocate (); |
| 46 | + if (num_elements == 0u) |
| 47 | + return; |
| 48 | + buffer_ptr = reinterpret_cast<T*> (allocatorT::allocate (num_elements * element_size ())); |
| 49 | + if (buffer_ptr) |
| 50 | + num_buffer_elements = num_elements; |
| 51 | + } |
| 52 | + |
| 53 | + /** fill all elements in the buffer with the same value */ |
| 54 | + void fill (T value) |
| 55 | + { |
| 56 | + assert (buffer_ptr); |
| 57 | + std::fill_n (buffer_ptr, num_buffer_elements, value); |
| 58 | + } |
| 59 | + |
| 60 | + /** access specified element */ |
| 61 | + T& operator[] (size_t index) |
| 62 | + { |
| 63 | + assert (index < num_buffer_elements); |
| 64 | + return buffer_ptr[index]; |
| 65 | + } |
| 66 | + |
| 67 | + /** access specified element */ |
| 68 | + const T& operator[] (size_t index) const |
| 69 | + { |
| 70 | + assert (index < num_buffer_elements); |
| 71 | + return buffer_ptr[index]; |
| 72 | + } |
| 73 | + |
| 74 | + /** returns a pointer to the first element */ |
| 75 | + T* data () { return buffer_ptr; } |
| 76 | + /** returns a pointer to the first element */ |
| 77 | + const T* data () const { return buffer_ptr; } |
| 78 | + |
| 79 | + /** returns the number of elements */ |
| 80 | + size_t size () const { return num_buffer_elements; } |
| 81 | + /** returns the byte size of one element */ |
| 82 | + constexpr size_t element_size () const { return sizeof (T); } |
| 83 | + |
| 84 | + /** returns an iterator to the beginning */ |
| 85 | + T* begin () { return &buffer_ptr[0]; } |
| 86 | + /** returns an iterator to the end */ |
| 87 | + T* end () { return &buffer_ptr[size ()]; } |
| 88 | + |
| 89 | + /** returns an iterator to the beginning */ |
| 90 | + const T* begin () const { return &buffer_ptr[0]; } |
| 91 | + /** returns an iterator to the end */ |
| 92 | + const T* end () const { return &buffer_ptr[size ()]; } |
| 93 | + |
| 94 | +private: |
| 95 | + void deallocate () |
| 96 | + { |
| 97 | + if (!buffer_ptr) |
| 98 | + return; |
| 99 | + allocatorT::deallocate (buffer_ptr, num_buffer_elements * element_size ()); |
| 100 | + buffer_ptr = nullptr; |
| 101 | + num_buffer_elements = 0u; |
| 102 | + } |
| 103 | + |
| 104 | + T* buffer_ptr {nullptr}; |
| 105 | + size_t num_buffer_elements {0u}; |
| 106 | +}; |
| 107 | + |
| 108 | +//------------------------------------------------------------------------ |
| 109 | +/** allocator that uses memory aligned allocations */ |
| 110 | +template<size_t alignment> |
| 111 | +struct alignment_allocator |
| 112 | +{ |
| 113 | + static void* allocate (size_t numBytes) |
| 114 | + { |
| 115 | + if (alignment == 0) |
| 116 | + return malloc (numBytes); |
| 117 | + void* data {nullptr}; |
| 118 | +#if __APPLE__ && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_15 |
| 119 | + posix_memalign (&data, alignment, numBytes); |
| 120 | +#elif defined(_MSC_VER) |
| 121 | + data = _aligned_malloc (numBytes, alignment); |
| 122 | +#else |
| 123 | + size_t d = numBytes % alignment; |
| 124 | + if (d != 0u) |
| 125 | + numBytes += (alignment - d); |
| 126 | + data = std::aligned_alloc (alignment, numBytes); |
| 127 | +#endif |
| 128 | + return data; |
| 129 | + } |
| 130 | + static void deallocate (void* ptr, size_t numButes) |
| 131 | + { |
| 132 | + if (alignment == 0) |
| 133 | + std::free (ptr); |
| 134 | + else |
| 135 | + { |
| 136 | +#if defined(_MSC_VER) |
| 137 | + _aligned_free (ptr); |
| 138 | +#else |
| 139 | + std::free (ptr); |
| 140 | +#endif |
| 141 | + } |
| 142 | + } |
| 143 | +}; |
| 144 | + |
| 145 | +//------------------------------------------------------------------------ |
| 146 | +template<typename T, size_t alignment> |
| 147 | +using aligned_buffer = buffer<T, alignment_allocator<alignment>>; |
| 148 | + |
| 149 | +//------------------------------------------------------------------------ |
| 150 | +} // vst3utils |
0 commit comments