4#ifndef WPI_MEMORY_JOINT_ALLOCATOR_HPP_INCLUDED
5#define WPI_MEMORY_JOINT_ALLOCATOR_HPP_INCLUDED
10#include <initializer_list>
25 template <
typename T,
class RawAllocator>
38 : stack_(
static_cast<char*
>(mem)), end_(
static_cast<char*
>(mem) + cap)
42 void*
allocate(std::size_t size, std::size_t alignment)
noexcept
44 return stack_.
allocate(end_, size, alignment, 0u);
47 bool bump(std::size_t offset)
noexcept
49 if (offset > std::size_t(end_ - stack_.
top()))
60 const char*
top() const noexcept
70 std::size_t
capacity(
const char* mem)
const noexcept
72 return std::size_t(end_ - mem);
77 return std::size_t(end_ -
top());
82 return std::size_t(
top() - mem);
103 joint(std::size_t cap) noexcept : capacity(cap) {}
105 std::size_t capacity;
107 template <
typename T,
class RawAllocator>
109 template <
typename T>
129 template <
typename T>
144 template <
typename U>
146 template <
typename U>
152 template <
typename T>
158 template <
typename T>
164 template <
typename T>
167 auto mem =
static_cast<void*
>(&obj);
168 return static_cast<char*
>(mem) +
sizeof(T);
171 template <
typename T>
174 auto mem =
static_cast<const void*
>(&obj);
175 return static_cast<const char*
>(mem) +
sizeof(T);
180 template <
typename T>
211 template <
typename T,
class RawAllocator>
212 class joint_ptr : WPI_EBO(allocator_reference<RawAllocator>)
214 static_assert(std::is_base_of<joint_type<T>, T>::value,
215 "T must be derived of joint_type<T>");
239 template <
typename... Args>
246 template <
typename... Args>
259 other.ptr_ =
nullptr;
300 (**this).~element_type();
301 this->deallocate_node(ptr_,
313 explicit operator bool() const noexcept
315 return ptr_ !=
nullptr;
351 template <
typename... Args>
352 void create(std::size_t additional_size, Args&&... args)
354 auto mem = this->allocate_node(
sizeof(
element_type) + additional_size,
358#if WPI_HAS_EXCEPTION_SUPPORT
366 this->deallocate_node(mem,
sizeof(
element_type) + additional_size,
386 template <
typename T,
class RawAllocator>
392 template <
typename T,
class RawAllocator>
395 return ptr ==
nullptr;
403 template <
typename T,
class RawAllocator>
406 return ptr.get() == p;
409 template <
typename T,
class RawAllocator>
420 template <
typename T,
class RawAllocator>
421 bool operator!=(
const joint_ptr<T, RawAllocator>&
ptr, std::nullptr_t)
423 return !(
ptr ==
nullptr);
426 template <
typename T,
class RawAllocator>
429 return ptr !=
nullptr;
437 template <
typename T,
class RawAllocator>
443 template <
typename T,
class RawAllocator>
454 template <
typename T,
class RawAllocator,
typename... Args>
462 template <
typename T,
class RawAllocator,
typename... Args>
475 template <
class RawAllocator,
typename T>
482 static_cast<const T&
>(
joint));
485 template <
class RawAllocator,
typename T>
492 static_cast<const T&
>(
joint));
505#if defined(__GNUC__) && (!defined(_GLIBCXX_USE_CXX11_ABI) || _GLIBCXX_USE_CXX11_ABI == 0)
512 template <
typename T>
527 auto mem = stack_->
allocate(size, alignment);
538 auto end =
static_cast<char*
>(
ptr) + size;
539 if (end == stack_->
top())
549 detail::joint_stack* stack_;
559 return lhs.stack_ == rhs.stack_;
564 return !(lhs == rhs);
586 template <
class RawAllocator>
605 template <
class AllocReference>
608 static_assert(always_false<AllocReference>::value,
609 "you must not use the regular copy constructor");
613 template <
typename T>
614 struct always_false : std::false_type
628 template <
typename T>
641 template <
typename Jo
intType>
651 template <
typename Jo
intType>
661 template <
typename Jo
intType>
671 template <
typename InIter,
typename JointType,
672 typename =
decltype(*std::declval<InIter&>()++)>
684 template <
typename Jo
intType>
696 template <
typename Jo
intType>
706 for (std::size_t i = 0u; i != size_; ++i)
771 std::size_t
size() const noexcept
787 joint_array(allocate_only, detail::joint_stack& stack, std::size_t size)
788 : ptr_(nullptr), size_(0u)
790 ptr_ =
static_cast<T*
>(stack.allocate(size *
sizeof(T),
alignof(T)));
792 WPI_THROW(out_of_fixed_memory(info(), size *
sizeof(T)));
798 builder(detail::joint_stack& stack, T*
ptr) noexcept
799 : stack_(&stack), objects_(
ptr), size_(0u)
805 for (std::size_t i = 0u; i != size_; ++i)
812 builder(builder&&) =
delete;
813 builder& operator=(builder&&) =
delete;
815 template <
typename... Args>
816 T* create(Args&&... args)
818 auto ptr = ::new (
static_cast<void*
>(&objects_[size_]))
824 std::size_t size() const noexcept
829 std::size_t release() noexcept
837 detail::joint_stack* stack_;
842 joint_array(detail::joint_stack& stack, std::size_t size)
845 builder
b(stack, ptr_);
846 for (
auto i = 0u; i !=
size; ++i)
854 builder
b(stack, ptr_);
855 for (
auto i = 0u; i !=
size; ++i)
860 joint_array(detail::joint_stack& stack, std::initializer_list<value_type> ilist)
861 :
joint_array(allocate_only{}, stack, ilist.size())
863 builder
b(stack, ptr_);
864 for (
auto& elem : ilist)
870 :
joint_array(allocate_only{}, stack, other.size())
872 builder
b(stack, ptr_);
873 for (
auto& elem : other)
879 :
joint_array(allocate_only{}, stack, other.size())
881 builder
b(stack, ptr_);
882 for (
auto& elem : other)
887 template <
typename InIter>
888 joint_array(detail::joint_stack& stack, InIter begin, InIter end)
889 : ptr_(nullptr), size_(0u)
894 ptr_ =
static_cast<T*
>(stack.allocate(
sizeof(T),
alignof(T)));
896 WPI_THROW(out_of_fixed_memory(info(),
sizeof(T)));
898 builder
b(stack, ptr_);
901 for (
auto last = ptr_; begin !=
end; ++
begin)
904 if (!stack.bump(
sizeof(T)))
905 WPI_THROW(out_of_fixed_memory(info(),
b.size() *
sizeof(T)));
907 auto cur =
b.create(*begin);
915 allocator_info info() const noexcept
Class template wpi::memory::allocator_storage, some policies and resulting typedefs.
An alias template for allocator_storage using the reference_storage policy.
Definition allocator_storage.hpp:900
typename reference_storage< RawAllocator >::allocator_type allocator_type
Definition allocator_storage.hpp:106
auto get_allocator() noexcept -> decltype(std::declval< storage_policy >().get_allocator())
Definition allocator_storage.hpp:277
Definition memory_stack.hpp:22
void unwind(char *top) noexcept
Definition memory_stack.hpp:100
void bump(std::size_t offset) noexcept
Definition memory_stack.hpp:47
void * allocate(const char *end, std::size_t size, std::size_t alignment, std::size_t fence_size=debug_fence_size) noexcept
Definition memory_stack.hpp:71
char * top() const noexcept
Definition memory_stack.hpp:107
Definition joint_allocator.hpp:35
void * allocate(std::size_t size, std::size_t alignment) noexcept
Definition joint_allocator.hpp:42
char * top() noexcept
Definition joint_allocator.hpp:55
bool bump(std::size_t offset) noexcept
Definition joint_allocator.hpp:47
std::size_t capacity_used(const char *mem) const noexcept
Definition joint_allocator.hpp:80
const char * top() const noexcept
Definition joint_allocator.hpp:60
void unwind(void *ptr) noexcept
Definition joint_allocator.hpp:65
std::size_t capacity_left() const noexcept
Definition joint_allocator.hpp:75
std::size_t capacity(const char *mem) const noexcept
Definition joint_allocator.hpp:70
joint_stack(void *mem, std::size_t cap) noexcept
Definition joint_allocator.hpp:37
A RawAllocator that uses the additional joint memory for its allocation.
Definition joint_allocator.hpp:503
joint_allocator & operator=(const joint_allocator &other) noexcept=default
void deallocate_node(void *ptr, std::size_t size, std::size_t) noexcept
Definition joint_allocator.hpp:535
joint_allocator(const joint_allocator &other) noexcept=default
joint_allocator(joint_type< T > &j) noexcept
Definition joint_allocator.hpp:513
void * allocate_node(std::size_t size, std::size_t alignment)
Definition joint_allocator.hpp:524
friend bool operator==(const joint_allocator &lhs, const joint_allocator &rhs) noexcept
A zero overhead dynamic array using joint memory.
Definition joint_allocator.hpp:630
joint_array(joint_array &&other, joint_type< JointType > &j)
Definition joint_allocator.hpp:697
const value_type * const_iterator
Definition joint_allocator.hpp:634
value_type & operator[](std::size_t i) noexcept
Definition joint_allocator.hpp:717
joint_array & operator=(const joint_array &)=delete
iterator end() noexcept
Definition joint_allocator.hpp:759
~joint_array() noexcept
Definition joint_allocator.hpp:704
value_type * iterator
Definition joint_allocator.hpp:633
const_iterator end() const noexcept
Definition joint_allocator.hpp:764
joint_array(const joint_array &other, joint_type< JointType > &j)
Definition joint_allocator.hpp:685
value_type * data() noexcept
Definition joint_allocator.hpp:733
joint_array(std::size_t size, joint_type< JointType > &j)
Definition joint_allocator.hpp:642
joint_array(const joint_array &)=delete
T value_type
Definition joint_allocator.hpp:632
bool empty() const noexcept
Definition joint_allocator.hpp:777
joint_array(joint_array &&)=delete
std::size_t size() const noexcept
Definition joint_allocator.hpp:771
joint_array(std::size_t size, const value_type &val, joint_type< JointType > &j)
Definition joint_allocator.hpp:652
const value_type * data() const noexcept
Definition joint_allocator.hpp:738
const_iterator begin() const noexcept
Definition joint_allocator.hpp:751
joint_array & operator=(joint_array &&)=delete
const value_type & operator[](std::size_t i) const noexcept
Definition joint_allocator.hpp:723
joint_array(std::initializer_list< value_type > ilist, joint_type< JointType > &j)
Definition joint_allocator.hpp:662
joint_array(InIter begin, InIter end, joint_type< JointType > &j)
Definition joint_allocator.hpp:673
iterator begin() noexcept
Definition joint_allocator.hpp:746
A pointer to an object where all allocations are joint.
Definition joint_allocator.hpp:213
friend void swap(joint_ptr &a, joint_ptr &b) noexcept
Definition joint_allocator.hpp:286
typename allocator_reference< RawAllocator >::allocator_type allocator_type
Definition joint_allocator.hpp:219
joint_ptr(allocator_type &alloc, joint_size additional_size, Args &&... args)
Definition joint_allocator.hpp:240
T element_type
Definition joint_allocator.hpp:218
auto get_allocator() const noexcept -> decltype(std::declval< allocator_reference< allocator_type > >().get_allocator())
Definition joint_allocator.hpp:344
joint_ptr(const allocator_type &alloc, joint_size additional_size, Args &&... args)
Definition joint_allocator.hpp:247
void reset() noexcept
Definition joint_allocator.hpp:296
joint_ptr & operator=(std::nullptr_t) noexcept
Definition joint_allocator.hpp:279
element_type * operator->() const noexcept
Definition joint_allocator.hpp:330
joint_ptr(joint_ptr &&other) noexcept
Definition joint_allocator.hpp:256
joint_ptr(allocator_type &alloc) noexcept
Definition joint_allocator.hpp:224
element_type & operator*() const noexcept
Definition joint_allocator.hpp:321
joint_ptr(const allocator_type &alloc) noexcept
Definition joint_allocator.hpp:229
joint_ptr & operator=(joint_ptr &&other) noexcept
Definition joint_allocator.hpp:271
~joint_ptr() noexcept
Definition joint_allocator.hpp:263
element_type * get() const noexcept
Definition joint_allocator.hpp:338
CRTP base class for all objects that want to use joint memory.
Definition joint_allocator.hpp:131
joint_type(joint_type &&)=delete
joint_type(const joint_type &)=delete
joint_type(joint j) noexcept
Definition joint_allocator.hpp:181
Tag type that can't be created.
Definition joint_allocator.hpp:102
A special case of out_of_memory errors thrown when a low-level allocator with a fixed size runs out o...
Definition error.hpp:121
#define WPI_MEMORY_LOG_PREFIX
Definition config.hpp:46
#define WPI_THROW(Ex)
Definition config.hpp:33
The typedef wpi::memory::default_allocator.
auto allocate_joint(RawAllocator &alloc, joint_size additional_size, Args &&... args) -> joint_ptr< T, RawAllocator >
Definition joint_allocator.hpp:455
auto clone_joint(RawAllocator &alloc, const joint_type< T > &joint) -> joint_ptr< T, RawAllocator >
Definition joint_allocator.hpp:476
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
void adl_swap(T &a, T &b) noexcept
Definition utility.hpp:60
T && forward(typename std::remove_reference< T >::type &t) noexcept
Definition utility.hpp:31
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
char * get_memory(joint_type< T > &obj) noexcept
Definition joint_allocator.hpp:165
detail::joint_stack & get_stack(joint_type< T > &obj) noexcept
Definition joint_allocator.hpp:153
Memory namespace.
Definition heap_allocator.hpp:20
bool operator!=(const memory_resource_allocator &lhs, const memory_resource_allocator &rhs) noexcept
Definition memory_resource_adapter.hpp:216
bool operator==(std::nullptr_t, const joint_ptr< T, RawAllocator > &ptr)
Definition joint_allocator.hpp:393
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
Specifies whether or not a RawAllocator has shared semantics.
Definition allocator_storage.hpp:534
Specifies whether or not a RawAllocator is thread safe as-is.
Definition threading.hpp:46
Tag type to make the joint size more explicit.
Definition joint_allocator.hpp:118
joint_size(std::size_t s) noexcept
Definition joint_allocator.hpp:121
std::size_t size
Definition joint_allocator.hpp:119
std::false_type propagate_on_container_swap
Definition joint_allocator.hpp:601
std::false_type propagate_on_container_copy_assignment
Definition joint_allocator.hpp:603
static AllocReference select_on_container_copy_construction(const AllocReference &)
Definition joint_allocator.hpp:606
std::false_type propagate_on_container_move_assignment
Definition joint_allocator.hpp:602
Controls the propagation of a std_allocator for a certain RawAllocator.
Definition std_allocator.hpp:50
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46