Align wrapper for array.

This commit is contained in:
Matthew Parkinson
2021-03-19 15:25:13 +00:00
committed by Matthew Parkinson
parent 8824622136
commit fd7b040823

View File

@@ -73,18 +73,26 @@ namespace snmalloc
template<size_t length, typename T> template<size_t length, typename T>
class ModArray class ModArray
{ {
/**
* Align the elements, so that access is cheaper.
*/
struct alignas(bits::next_pow2_const(sizeof(T))) TWrap
{
T v;
};
static constexpr size_t rlength = bits::next_pow2_const(length); static constexpr size_t rlength = bits::next_pow2_const(length);
T array[rlength]; TWrap array[rlength];
public: public:
constexpr const T& operator[](const size_t i) const constexpr const T& operator[](const size_t i) const
{ {
return array[i & (rlength - 1)]; return array[i & (rlength - 1)].v;
} }
constexpr T& operator[](const size_t i) constexpr T& operator[](const size_t i)
{ {
return array[i & (rlength - 1)]; return array[i & (rlength - 1)].v;
} }
}; };