#ifndef __ma_allocator__ #define __ma_allocator__ #include #include #include #include #include #include namespace MultiArrayHelper { template struct Allocator { typedef T value_type; static constexpr size_t type_size = sizeof(value_type); static constexpr size_t N = 32; Allocator() = default; template Allocator(const Allocator& x) {} T* allocate(size_t n) { //constexpr size_t NN = N*type_size; const size_t nn = n*type_size; if(n > static_cast(-1-N)) throw std::bad_alloc(); auto p = static_cast(std::malloc(nn + N)); if(not p) throw std::bad_alloc(); auto ip = reinterpret_cast(p); auto diff = N - (ip % N); p += diff; //auto ipx = reinterpret_cast(p); //std::cout << "ixp: " << ipx << std::endl; //std::cout << "ixp %: " << ipx % N << std::endl; auto ps = reinterpret_cast(p); ps[-1] = diff; return reinterpret_cast(p); } void deallocate(T* p, size_t n) { auto ps = reinterpret_cast(p); std::free(reinterpret_cast(p)-ps[-1]); } }; template bool operator==(const Allocator& a, const Allocator& b) { return true; } template bool operator!=(const Allocator& a, const Allocator& b) { return false; } } // namespace MultiArrayHelper namespace MultiArrayTools { template using vector = std::vector>; } // namespace MultiArrayTools #endif