change allocator

This commit is contained in:
Christian Zimmermann 2019-03-18 11:33:33 +01:00
parent de8d456f73
commit 389e4ec0a3
2 changed files with 14 additions and 17 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
project(multi_array) project(multi_array)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -march=native -funroll-loops -fopenmp") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -g -Wpedantic -Ofast -march=native -faligned-new -funroll-loops -fopenmp")
enable_testing() enable_testing()

View file

@ -18,7 +18,12 @@ namespace MultiArrayHelper
typedef T value_type; typedef T value_type;
static constexpr size_t type_size = sizeof(value_type); static constexpr size_t type_size = sizeof(value_type);
static constexpr size_t N = 32; static constexpr size_t N = 32;
struct VX
{
alignas(N) char x[N];
};
Allocator() = default; Allocator() = default;
template <typename U> template <typename U>
@ -26,26 +31,18 @@ namespace MultiArrayHelper
T* allocate(size_t n) T* allocate(size_t n)
{ {
//constexpr size_t NN = N*type_size;
const size_t nn = n*type_size; const size_t nn = n*type_size;
if(n > static_cast<size_t>(-1-N)) throw std::bad_alloc(); const size_t off = nn%N;
auto p = static_cast<char*>(std::malloc(nn + N)); const size_t nnx = (off == 0) ? nn : nn + N - off;
if(not p) throw std::bad_alloc(); const size_t nnd = nnx/4;
auto ip = reinterpret_cast<std::intptr_t>(p); VX* vx = new VX[nnd];
auto diff = N - (ip % N); return reinterpret_cast<T*>(vx);
p += diff;
//auto ipx = reinterpret_cast<std::intptr_t>(p);
//std::cout << "ixp: " << ipx << std::endl;
//std::cout << "ixp %: " << ipx % N << std::endl;
auto ps = reinterpret_cast<std::intptr_t*>(p);
ps[-1] = diff;
return reinterpret_cast<T*>(p);
} }
void deallocate(T* p, size_t n) void deallocate(T* p, size_t n)
{ {
auto ps = reinterpret_cast<std::intptr_t*>(p); VX* vx = reinterpret_cast<VX*>(p);
std::free(reinterpret_cast<char*>(p)-ps[-1]); delete [] vx;
} }
}; };