/**
This file is a part of rexy's general purpose library
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
#ifndef REXY_DEFAULT_ALLOCATOR_HPP
#define REXY_DEFAULT_ALLOCATOR_HPP
#include //size_t
#include //true_type, false_type
#include
namespace rexy{
namespace detail{
template
struct default_allocator
{
using pointer = T*;
using const_pointer = const T*;
using void_pointer = void*;
using const_void_pointer = const void*;
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using is_always_equal = std::true_type;
using propagate_on_container_copy_assignment = std::false_type;
using propagate_on_container_move_assignment = std::false_type;
using propagate_on_container_swap = std::false_type;
template
struct rebind{
using other = default_allocator;
};
private:
constexpr bool has_overflow(size_type n)const{
return n > max_size();
}
public:
default_allocator(void) = default;
default_allocator(const default_allocator&) = default;
default_allocator(default_allocator&&) = default;
template
constexpr default_allocator(const default_allocator&)noexcept{}
~default_allocator(void) = default;
pointer allocate(size_type n){
size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
return reinterpret_cast(::operator new(bytes));
}else{
return reinterpret_cast(::operator new(bytes, static_cast(alignof(T))));
}
}
void deallocate(pointer p, size_type n){
size_type bytes = has_overflow(n) ? size_type{-1} : n*sizeof(T);
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
::operator delete(p, bytes);
}else{
::operator delete(p, bytes, static_cast(alignof(T)));
}
}
constexpr size_type max_size(void)const{
return size_type{-1}/sizeof(T);
}
};
template
constexpr bool operator==(const default_allocator&, const default_allocator&){
return true;
}
template
constexpr bool operator!=(const default_allocator&, const default_allocator&){
return false;
}
}
}
#endif