31 lines
649 B
C++
31 lines
649 B
C++
#ifndef RAII_DEFAULT_ALLOCATOR_HPP
|
|
#define RAII_DEFAULT_ALLOCATOR_HPP
|
|
|
|
#include <cstdlib> //size_t
|
|
#include <cstring> //memcpy
|
|
#include <new>
|
|
|
|
namespace raii{
|
|
|
|
namespace detail{
|
|
|
|
template<size_t Alignment = __STDCPP_DEFAULT_NEW_ALIGNMENT__>
|
|
struct default_allocator
|
|
{
|
|
static void free(void* data){
|
|
::operator delete(data, std::align_val_t{Alignment});
|
|
}
|
|
static void* allocate(size_t size){
|
|
return ::operator new(size, std::align_val_t{Alignment});
|
|
}
|
|
static void* copy(const void* c, size_t size){
|
|
char* tmp = reinterpret_cast<char*>(allocate(size));
|
|
memcpy(tmp, c, size);
|
|
return tmp;
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
#endif
|