1 #ifndef SIPLASPLAS_TYPEERASURE_ANYSTORAGE_DEADPOOL_HPP 2 #define SIPLASPLAS_TYPEERASURE_ANYSTORAGE_DEADPOOL_HPP 4 #include <siplasplas/utility/memory_manip.hpp> 5 #include <siplasplas/typeerasure/typeinfo.hpp> 24 template<std::
size_t PreallocatedSize, std::
size_t PreallocatedAlignment = alignof(std::u
int8_t)>
28 static_assert(PreallocatedSize >=
sizeof(
void*),
29 "At least sizeof(void*) bytes of preallocated storage is needed");
33 if(dynamicAllocStorage() &&
34 _dynamicAllocStorageSize == typeInfo.
sizeOf() &&
37 return dynamicAllocStoragePointer();
41 if(dynamicAllocStorage())
46 char* storagePtr =
reinterpret_cast<char*
>(&_preallocatedStorage);
52 if(alignedPointerToFixedStorage + typeInfo.
sizeOf() <= storagePtr + PreallocatedSize)
54 _dynamicAllocStorageSize = 0;
55 return alignedPointerToFixedStorage;
59 dynamicAllocStoragePointer(
63 _dynamicAllocStorageSize = typeInfo.
sizeOf();
65 return dynamicAllocStoragePointer();
71 constexpr
bool objectFitsInStorage()
const 78 if(dynamicAllocStorage())
84 std::size_t dynamicAllocStorageSize()
const 86 return _dynamicAllocStorageSize;
90 mutable std::size_t _dynamicAllocStorageSize = 0;
91 mutable std::aligned_storage_t<PreallocatedSize, PreallocatedAlignment> _preallocatedStorage;
93 bool dynamicAllocStorage()
const 95 return _dynamicAllocStorageSize > 0;
98 void* dynamicAllocStoragePointer()
const 100 return cpp::detail::read_at<void*>(&_preallocatedStorage);
103 void dynamicAllocStoragePointer(
void* pointer)
const 105 cpp::detail::write_at(&_preallocatedStorage, pointer);
111 #endif // SIPLASPLAS_TYPEERASURE_ANYSTORAGE_DEADPOOL_HPP SIPLASPLAS_UTILITY_EXPORT void * aligned_malloc(std::size_t size, std::size_t alignment, std::size_t offset=0)
Allocates a block of memory of memory aligned to an specific boundary.
Definition: canary_allocator.hpp:7
SIPLASPLAS_UTILITY_EXPORT char * aligned_ptr(char *pointer, std::size_t alignment)
Returns an address aligned to an specific boundary.
std::size_t alignment() const
returns the alignment of the type
Definition: typeinfo.hpp:275
Implements fixed-size with fallback to dynamic allocation.
Definition: deadpool.hpp:25
Contains minimal information to execute the value semantics operations of a type. ...
Definition: typeinfo.hpp:112
std::size_t sizeOf() const
Returns the size of the type.
Definition: typeinfo.hpp:130
SIPLASPLAS_UTILITY_EXPORT bool is_aligned(char *pointer, std::size_t alignment)
Checks if an address is aligned to a given boundary.
SIPLASPLAS_UTILITY_EXPORT void aligned_free(void *pointer, std::size_t offset=0)
Deallocates a block allocated by cpp::aligned_malloc()