sfizz/sources/Buffer.h

139 lines
4.6 KiB
C
Raw Normal View History

2019-07-29 02:13:03 +02:00
#pragma once
#include "Globals.h"
#include "Helpers.h"
#include <cstdlib>
2019-08-24 11:32:19 +02:00
#include <cstring>
#include <memory>
2019-08-24 11:32:19 +02:00
#include <type_traits>
#include <utility>
template <class Type, unsigned int Alignment = SIMDConfig::defaultAlignment>
2019-07-29 02:13:03 +02:00
class Buffer
{
public:
2019-08-21 01:00:07 +02:00
using value_type = std::remove_cv_t<Type>;
2019-08-24 11:32:19 +02:00
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
2019-08-21 01:00:07 +02:00
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = size_t;
using difference_type = ptrdiff_t;
2019-08-24 11:32:19 +02:00
Buffer()
{
}
Buffer(size_t size)
{
resize(size);
}
bool resize(size_t newSize)
2019-07-29 02:13:03 +02:00
{
if (newSize == 0)
{
clear();
return true;
}
2019-07-29 02:13:03 +02:00
2019-07-31 01:45:58 +02:00
auto tempSize = newSize + 2 * AlignmentMask; // To ensure that we have leeway at the beginning and at the end
2019-08-24 11:32:19 +02:00
auto *newData = paddedData != nullptr ? std::realloc(paddedData, tempSize * sizeof(value_type)) : std::malloc(tempSize * sizeof(value_type));
if (newData == nullptr)
2019-08-24 11:32:19 +02:00
{
return false;
2019-08-24 11:32:19 +02:00
}
largerSize = tempSize;
alignedSize = newSize;
2019-08-21 01:00:07 +02:00
paddedData = static_cast<pointer>(newData);
normalData = static_cast<pointer>(std::align(Alignment, alignedSize, newData, tempSize));
2019-08-03 18:14:58 +02:00
normalEnd = normalData + alignedSize;
if (auto endMisalignment = (alignedSize & TypeAlignmentMask); endMisalignment != 0)
_alignedEnd = normalEnd + Alignment - endMisalignment;
2019-08-07 23:39:35 +02:00
else
_alignedEnd = normalEnd;
2019-08-24 15:32:06 +02:00
return true;
}
void clear()
{
largerSize = 0;
alignedSize = 0;
2019-08-03 18:14:58 +02:00
std::free(paddedData);
normalData = nullptr;
normalEnd = nullptr;
_alignedEnd = nullptr;
2019-07-29 02:13:03 +02:00
}
~Buffer()
{
2019-08-03 18:14:58 +02:00
std::free(paddedData);
2019-07-29 02:13:03 +02:00
}
2019-08-24 11:32:19 +02:00
Buffer(const Buffer<Type> &other)
{
if (resize(other.size()))
{
std::memcpy(this->data(), other.data(), other.size() * sizeof(value_type));
}
}
Buffer(Buffer<Type> &&other)
{
largerSize = std::exchange(other.largerSize, 0);
alignedSize = std::exchange(other.alignedSize, 0);
paddedData = std::exchange(other.paddedData, nullptr);
normalData = std::exchange(other.normalData, nullptr);
normalEnd = std::exchange(other.normalEnd, nullptr);
_alignedEnd = std::exchange(other._alignedEnd, nullptr);
}
Buffer<Type> &operator=(const Buffer<Type> &other)
{
if (this != &other)
{
if (resize(other.size()))
std::memcpy(this->data(), other.data(), other.size() * sizeof(value_type));
}
return *this;
}
Buffer<Type> &operator=(Buffer<Type> &&other)
{
if (this != &other)
{
std::free(paddedData);
largerSize = std::exchange(other.largerSize, 0);
alignedSize = std::exchange(other.alignedSize, 0);
paddedData = std::exchange(other.paddedData, nullptr);
normalData = std::exchange(other.normalData, nullptr);
normalEnd = std::exchange(other.normalEnd, nullptr);
_alignedEnd = std::exchange(other._alignedEnd, nullptr);
}
return *this;
}
Type &operator[](int idx) { return *(normalData + idx); }
2019-08-21 01:00:07 +02:00
constexpr pointer data() const noexcept { return normalData; }
constexpr size_type size() const noexcept { return alignedSize; }
constexpr bool empty() const noexcept { return alignedSize == 0; }
constexpr iterator begin() noexcept { return data(); }
constexpr iterator end() noexcept { return normalEnd; }
constexpr pointer alignedEnd() noexcept { return _alignedEnd; }
2019-08-24 11:32:19 +02:00
2019-07-29 02:13:03 +02:00
private:
2019-08-24 11:32:19 +02:00
static constexpr auto AlignmentMask{Alignment - 1};
static constexpr auto TypeAlignment{Alignment / sizeof(value_type)};
static constexpr auto TypeAlignmentMask{TypeAlignment - 1};
2019-08-21 01:00:07 +02:00
static_assert(std::is_arithmetic<value_type>::value, "Type should be arithmetic");
2019-08-03 18:14:58 +02:00
static_assert(Alignment == 0 || Alignment == 4 || Alignment == 8 || Alignment == 16, "Bad alignment value");
2019-08-21 01:00:07 +02:00
static_assert(TypeAlignment * sizeof(value_type) == Alignment, "The alignment does not appear to be divided by the size of the Type");
2019-08-24 11:32:19 +02:00
size_type largerSize{0};
size_type alignedSize{0};
pointer normalData{nullptr};
pointer paddedData{nullptr};
pointer normalEnd{nullptr};
pointer _alignedEnd{nullptr};
LEAK_DETECTOR(Buffer);
2019-07-29 02:13:03 +02:00
};