Align wrapper for array.
This commit is contained in:
committed by
Matthew Parkinson
parent
8824622136
commit
fd7b040823
@@ -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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user