Skip to content

Commit

Permalink
Refactor more meta functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KredeGC committed Jun 23, 2023
1 parent 39c5e94 commit f5209b3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
20 changes: 12 additions & 8 deletions include/ktl/allocators/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ namespace ktl
};

public:
freelist() noexcept :
template<typename A = Alloc>
freelist()
noexcept(std::is_nothrow_default_constructible_v<A>) :
m_Alloc(),
m_Free(nullptr) {}

Expand All @@ -48,13 +50,14 @@ namespace ktl
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
explicit freelist(Args&&... args)
noexcept(noexcept(Alloc(std::declval<Args>() ...))) :
noexcept(std::is_nothrow_constructible_v<Alloc, Args...>) :
m_Alloc(std::forward<Args>(args)...),
m_Free(nullptr) {}

freelist(const freelist&) = delete;

freelist(freelist&& other) noexcept :
freelist(freelist&& other)
noexcept(std::is_nothrow_move_constructible<Alloc>) :
m_Alloc(std::move(other.m_Alloc)),
m_Free(other.m_Free)
{
Expand All @@ -71,7 +74,8 @@ namespace ktl

freelist& operator=(const freelist&) = delete;

freelist& operator=(freelist&& rhs) noexcept
freelist& operator=(freelist&& rhs)
noexcept(std::is_nothrow_move_constructible<Alloc>)
{
release();

Expand Down Expand Up @@ -107,7 +111,7 @@ namespace ktl
* @return A location in memory that is at least @p n bytes big or nullptr if it could not be allocated
*/
void* allocate(size_type n)
noexcept(noexcept(m_Alloc.allocate(Max)))
noexcept(detail::has_nothrow_allocate_v<Alloc>)
{
if (n > Min && n <= Max)
{
Expand Down Expand Up @@ -154,7 +158,7 @@ namespace ktl
template<typename T, typename... Args>
typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
construct(T* p, Args&&... args)
noexcept(detail::has_noexcept_construct_v<Alloc, T*, Args...>)
noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
{
m_Alloc.construct(p, std::forward<Args>(args)...);
}
Expand All @@ -167,7 +171,7 @@ namespace ktl
template<typename T>
typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
destroy(T* p)
noexcept(detail::has_noexcept_destroy_v<Alloc, T*>)
noexcept(detail::has_nothrow_destroy_v<Alloc, T*>)
{
m_Alloc.destroy(p);
}
Expand Down Expand Up @@ -222,7 +226,7 @@ namespace ktl

private:
void release()
noexcept(noexcept(m_Alloc.deallocate(m_Free, Max)))
noexcept(detail::has_nothrow_deallocate_v<Alloc>)
{
link* next = m_Free;
while (next)
Expand Down
6 changes: 3 additions & 3 deletions include/ktl/allocators/overflow.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace ktl
*/
template<typename... Args,
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
std::is_constructible_v<Alloc, Args...>>>
explicit overflow(Stream& stream, Args&&... args)
noexcept(std::is_nothrow_constructible_v<Alloc, Args...>) :
m_Stream(stream),
Expand Down Expand Up @@ -102,7 +102,7 @@ namespace ktl
* @return A location in memory that is at least @p n bytes big or nullptr if it could not be allocated
*/
void* allocate(size_type n)
noexcept(noexcept(m_Alloc.allocate(n)))
noexcept(detail::has_nothrow_allocate_v<Alloc>)
{
m_Allocs += n;

Expand All @@ -124,7 +124,7 @@ namespace ktl
* @param n The size that was initially allocated
*/
void deallocate(void* p, size_type n)
noexcept(noexcept(m_Alloc.deallocate(p, n)))
noexcept(detail::has_nothrow_deallocate_v<Alloc>)
{
KTL_ASSERT(p != nullptr);

Expand Down
13 changes: 5 additions & 8 deletions include/ktl/allocators/segragator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ namespace ktl
public:
typedef typename detail::get_size_type_t<P> size_type;

segragator()
noexcept(std::is_nothrow_default_constructible_v<P>&& std::is_nothrow_default_constructible_v<F>) :
m_Primary(),
m_Fallback() {}
segragator() = default;

/**
* @brief Constructor for forwarding a single argument to the primary allocator
Expand Down Expand Up @@ -98,7 +95,7 @@ namespace ktl

#pragma region Allocation
void* allocate(size_t n)
noexcept(noexcept(m_Primary.allocate(n)) && noexcept(m_Fallback.allocate(n)))
noexcept(detail::has_nothrow_allocate_v<P> && detail::has_nothrow_allocate_v<F>)
{
if (n <= Threshold)
return m_Primary.allocate(n);
Expand All @@ -107,7 +104,7 @@ namespace ktl
}

void deallocate(void* p, size_t n)
noexcept(noexcept(m_Primary.deallocate(p, n)) && noexcept(m_Fallback.deallocate(p, n)))
noexcept(detail::has_nothrow_deallocate_v<P> && detail::has_nothrow_deallocate_v<F>)
{
if (n <= Threshold)
return m_Primary.deallocate(p, n);
Expand Down Expand Up @@ -182,15 +179,15 @@ namespace ktl
template<typename Primary = P, typename Fallback = F>
typename std::enable_if<detail::has_max_size_v<Primary> && detail::has_max_size_v<Fallback>, size_type>::type
max_size() const
noexcept(detail::has_nothrow_max_size_v<P> && detail::has_nothrow_max_size_v<F>)
noexcept(detail::has_nothrow_max_size_v<Primary> && detail::has_nothrow_max_size_v<Fallback>)
{
return (std::max)(m_Primary.max_size(), m_Fallback.max_size());
}

template<typename Primary = P, typename Fallback = F>
typename std::enable_if<detail::has_owns_v<Primary> && detail::has_owns_v<Fallback>, bool>::type
owns(void* p) const
noexcept(detail::has_nothrow_owns_v<P> && detail::has_nothrow_owns_v<F>)
noexcept(detail::has_nothrow_owns_v<Primary> && detail::has_nothrow_owns_v<Fallback>)
{
if (m_Primary.owns(p))
return true;
Expand Down
21 changes: 11 additions & 10 deletions include/ktl/allocators/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ namespace ktl
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
block(Args&&... alloc)
noexcept(noexcept(Alloc(std::declval<Args>()...))) :
noexcept(std::is_nothrow_constructible_v<Alloc, Args...>) :
Allocator(std::forward<Args>(alloc)...),
UseCount(1) {}
};

public:
typedef typename detail::get_size_type_t<Alloc> size_type;

template<typename A = Alloc>
shared()
noexcept(noexcept(block())) :
noexcept(std::is_nothrow_default_constructible_v<block>) :
m_Block(detail::aligned_new<block>(detail::ALIGNMENT)) {}

/**
Expand All @@ -45,7 +46,7 @@ namespace ktl
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
explicit shared(Args&&... alloc)
noexcept(noexcept(block(std::declval<Args>()...))) :
noexcept(std::is_nothrow_constructible_v<block, Args...>) :
m_Block(detail::aligned_new<block>(detail::ALIGNMENT, std::forward<Args>(alloc)...)) {}

shared(const shared& other) noexcept :
Expand Down Expand Up @@ -103,13 +104,13 @@ namespace ktl

#pragma region Allocation
void* allocate(size_t n)
noexcept(noexcept(m_Block->Allocator.allocate(n)))
noexcept(detail::has_nothrow_allocate_v<Alloc>)
{
return m_Block->Allocator.allocate(n);
}

void deallocate(void* p, size_t n)
noexcept(noexcept(m_Block->Allocator.deallocate(p, n)))
noexcept(detail::has_nothrow_deallocate_v<Alloc>)
{
m_Block->Allocator.deallocate(p, n);
}
Expand All @@ -119,15 +120,15 @@ namespace ktl
template<typename T, typename... Args>
typename std::enable_if<detail::has_construct_v<Alloc, T*, Args...>, void>::type
construct(T* p, Args&&... args)
noexcept(detail::has_noexcept_construct_v<Alloc, T*, Args...>)
noexcept(detail::has_nothrow_construct_v<Alloc, T*, Args...>)
{
m_Block->Allocator.construct(p, std::forward<Args>(args)...);
}

template<typename T>
typename std::enable_if<detail::has_destroy_v<Alloc, T*>, void>::type
destroy(T* p)
noexcept(detail::has_noexcept_destroy_v<Alloc, T*>)
noexcept(detail::has_nothrow_destroy_v<Alloc, T*>)
{
m_Block->Allocator.destroy(p);
}
Expand All @@ -137,15 +138,15 @@ namespace ktl
template<typename A = Alloc>
typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
max_size() const
noexcept(noexcept(std::declval<A&>().max_size()))
noexcept(detail::has_nothrow_max_size_v<A>)
{
return m_Block->Allocator.max_size();
}

template<typename A = Alloc>
typename std::enable_if<detail::has_owns_v<A>, bool>::type
owns(void* p) const
noexcept(noexcept(std::declval<A&>().owns(p)))
noexcept(detail::has_nothrow_owns_v<A>)
{
return m_Block->Allocator.owns(p);
}
Expand All @@ -170,7 +171,7 @@ namespace ktl
}

void decrement()
noexcept(noexcept(detail::aligned_delete(m_Block)))
noexcept(std::is_nothrow_destructible_v<Alloc>)
{
if (!m_Block) return;

Expand Down
13 changes: 7 additions & 6 deletions include/ktl/allocators/threaded.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ktl
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
block(Args&&... alloc)
noexcept(noexcept(Alloc(std::declval<Args>()...))) :
noexcept(std::is_nothrow_constructible_v<Alloc, Args...>) :
Allocator(std::forward<Args>(alloc)...),
UseCount(1),
Lock() {}
Expand All @@ -38,8 +38,9 @@ namespace ktl
public:
typedef typename detail::get_size_type_t<Alloc> size_type;

template<typename A = Alloc>
threaded()
noexcept(noexcept(block())) :
noexcept(std::is_nothrow_default_constructible_v<block>) :
m_Block(detail::aligned_new<block>(detail::ALIGNMENT)) {}

/**
Expand All @@ -49,7 +50,7 @@ namespace ktl
typename = std::enable_if_t<
detail::can_construct_v<Alloc, Args...>>>
explicit threaded(Args&&... alloc)
noexcept(noexcept(block(std::declval<Args>()...))) :
noexcept(std::is_nothrow_constructible_v<block, Args...>) :
m_Block(detail::aligned_new<block>(detail::ALIGNMENT, std::forward<Args>(alloc)...)) {}

threaded(const threaded& other) noexcept :
Expand Down Expand Up @@ -145,15 +146,15 @@ namespace ktl
template<typename A = Alloc>
typename std::enable_if<detail::has_max_size_v<A>, size_type>::type
max_size() const
noexcept(noexcept(m_Block->Allocator.max_size()))
noexcept(detail::has_nothrow_max_size_v<A>)
{
return m_Block->Allocator.max_size();
}

template<typename A = Alloc>
typename std::enable_if<detail::has_owns_v<A>, bool>::type
owns(void* p) const
noexcept(noexcept(m_Block->Allocator.owns(p)))
noexcept(detail::has_nothrow_owns_v<A>)
{
return m_Block->Allocator.owns(p);
}
Expand All @@ -178,7 +179,7 @@ namespace ktl
}

void decrement()
noexcept(noexcept(detail::aligned_delete(m_Block)))
noexcept(std::is_nothrow_destructible_v<Alloc>)
{
if (!m_Block) return;

Expand Down

0 comments on commit f5209b3

Please sign in to comment.