185 lines
6.2 KiB
C++
185 lines
6.2 KiB
C++
/**
|
|
This file is a part of rexy's general purpose library
|
|
Copyright (C) 2020-2022 rexy712
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef REXY_DEFAULT_ALLOCATOR_HPP
|
|
#define REXY_DEFAULT_ALLOCATOR_HPP
|
|
|
|
#include <cstddef> //ptrdiff_t, size_t
|
|
#include <type_traits> //true_type, false_type, is_constant_evaluated
|
|
#include <new>
|
|
#include <limits> //numeric_limits
|
|
|
|
#if __cplusplus >= 202002L
|
|
#include <memory> //allocator
|
|
#endif
|
|
|
|
#ifdef __cpp_concepts
|
|
#include "concepts/allocator.hpp"
|
|
#define REXY_ALLOCATOR_CONCEPT Allocator
|
|
#else
|
|
#define REXY_ALLOCATOR_CONCEPT class
|
|
#endif
|
|
|
|
#include "rexy.hpp"
|
|
#include "compat/standard.hpp"
|
|
|
|
#include <type_traits> //declval
|
|
|
|
namespace rexy{
|
|
|
|
template<class T>
|
|
struct 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<class U>
|
|
struct rebind{
|
|
using other = allocator<U>;
|
|
};
|
|
|
|
private:
|
|
constexpr bool has_overflow(size_type n)const{
|
|
return n > max_size();
|
|
}
|
|
|
|
public:
|
|
REXY_CPP20_CONSTEXPR allocator(void) = default;
|
|
REXY_CPP20_CONSTEXPR allocator(const allocator&) = default;
|
|
REXY_CPP20_CONSTEXPR allocator(allocator&&) = default;
|
|
template<class U>
|
|
constexpr allocator(const allocator<U>&)noexcept{}
|
|
REXY_CPP20_CONSTEXPR ~allocator(void) = default;
|
|
|
|
//'::operator new' is never a constexpr call as of C++23. However, 'std::allocator::allocate' *is* transiently constexpr as of C++20,
|
|
//even though it directly calls '::operator new' as is stated in the standard. Therefore, when evaluating this call with support for
|
|
//'if consteval', we can use the 'std::allocator::allocate' constexpr-ness when this is in a constant evaluation context.
|
|
#if __cplusplus >= 202002L
|
|
REXY_CPP20_CONSTEXPR pointer allocate(size_type n){
|
|
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
|
|
|
|
#ifdef __cpp_if_consteval
|
|
if consteval{ //} //makes my braces matcher in nano not upset ;)
|
|
#else
|
|
if(std::is_constant_evaluated()){
|
|
#endif //__cpp_if_consteval
|
|
std::allocator<value_type> a;
|
|
const size_type num_items = n / sizeof(value_type);
|
|
return a.allocate(num_items);
|
|
}else{
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
return reinterpret_cast<pointer>(::operator new(bytes));
|
|
}else{
|
|
return reinterpret_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T))));
|
|
}
|
|
} //if consteval
|
|
}
|
|
|
|
|
|
REXY_CPP20_CONSTEXPR void deallocate(pointer p, size_type n){
|
|
#ifdef __cpp_if_consteval
|
|
if consteval{ //} //makes my braces matcher in nano not upset ;)
|
|
#else
|
|
if(std::is_constant_evaluated()){
|
|
#endif
|
|
std::allocator<value_type> a;
|
|
const size_type num_items = n / sizeof(value_type);
|
|
return a.deallocate(p, num_items);
|
|
}else{
|
|
|
|
#if !defined(__cpp_sized_deallocation)
|
|
//clang does not enable ::operator delete(void* ptr, std::size_t sz) by default for some reason
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
::operator delete(p);
|
|
}else{
|
|
::operator delete(p, static_cast<std::align_val_t>(alignof(T)));
|
|
}
|
|
#else //__cpp_sized_deallocation
|
|
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
::operator delete(p, bytes);
|
|
}else{
|
|
::operator delete(p, bytes, static_cast<std::align_val_t>(alignof(T)));
|
|
}
|
|
#endif //__cpp_sized_deallocation
|
|
|
|
} //if consteval
|
|
|
|
}
|
|
#else //__cplusplus
|
|
REXY_CPP20_CONSTEXPR pointer allocate(size_type n){
|
|
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
|
|
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
return reinterpret_cast<pointer>(::operator new(bytes));
|
|
}else{
|
|
return reinterpret_cast<pointer>(::operator new(bytes, static_cast<std::align_val_t>(alignof(T))));
|
|
}
|
|
}
|
|
REXY_CPP20_CONSTEXPR void deallocate(pointer p, size_type n){
|
|
#if !defined(__cpp_sized_deallocation)
|
|
//clang does not enable ::operator delete(void* ptr, std::size_t sz) by default for some reason
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
::operator delete(p);
|
|
}else{
|
|
::operator delete(p, static_cast<std::align_val_t>(alignof(T)));
|
|
}
|
|
#else //__cpp_sized_deallocation
|
|
size_type bytes = has_overflow(n) ? std::numeric_limits<size_type>::max() : n*sizeof(T);
|
|
if constexpr(alignof(T) <= __STDCPP_DEFAULT_NEW_ALIGNMENT__){
|
|
::operator delete(p, bytes);
|
|
}else{
|
|
::operator delete(p, bytes, static_cast<std::align_val_t>(alignof(T)));
|
|
}
|
|
#endif //__cpp_sized_deallocation
|
|
|
|
}
|
|
#endif //__cplusplus
|
|
constexpr size_type max_size(void)const{
|
|
return std::numeric_limits<size_type>::max()/sizeof(T);
|
|
}
|
|
};
|
|
template<class T, class U>
|
|
constexpr bool operator==(const allocator<T>&, const allocator<U>&){
|
|
return true;
|
|
}
|
|
template<class T, class U>
|
|
constexpr bool operator!=(const allocator<T>&, const allocator<U>&){
|
|
return false;
|
|
}
|
|
|
|
template<REXY_ALLOCATOR_CONCEPT T>
|
|
struct is_nothrow_allocator{
|
|
static constexpr bool value = noexcept(std::declval<T>().allocate(0)) && noexcept(std::declval<T>().deallocate(nullptr, 0));
|
|
};
|
|
template<REXY_ALLOCATOR_CONCEPT T>
|
|
static constexpr bool is_nothrow_allocator_v = is_nothrow_allocator<T>::value;
|
|
|
|
}
|
|
|
|
#endif
|