Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow Namespace Reference

Classes

class  ArenaMemoryResource
 A linear arena memory resource that allocates memory sequentially from a fixed buffer. Throws std::bad_alloc if there is insufficient space for the requested allocation. More...
class  BinaryHeap
 A binary min-heap container. Supports custom comparator and allocator. More...
class  ConcurrentFlexQueue
 A thread-safe concurrent queue with flexible push and pop operations. More...
class  ConcurrentQueue
class  CountedValueViewIterator
 Iterator that returns a constant value a fixed number of times. Useful for creating a virtual range of repeated values without overhead. More...
class  DebugClass
 Debug class to track copy/move operations. Some operations may be optimized away in release builds. More...
class  DefaultMemoryResource
 A default memory resource that wraps global ::operator new and ::operator delete. More...
class  MemoryResource
 A memory resource holder interface for the PolymorphicAllocator. Responsible for allocate and deallocate raw memory. More...
class  MultiQueueThreadPool
 A work-stealing multiqueue threadpool. Each worker thread has a thread_local task queue to reduce thread contention. More...
class  PolymorphicAllocator
 Polymorphic allocator wrapping a non-owning memory resource. Allocation strategy depends on the memory resource implementation. More...
class  PoolMemoryResource
 A pool memory resource that manages fixed-size memory blocks from a pre-allocated buffer. The allocation size must be less or equal to the block size. The allocation alignment must be less or equal to the block alignment. Throws std::bad_alloc if the constraint is not met or run out of memory. More...
class  SimpleThreadPool
 A simple thread pool with fixed number of worker threads. All the threads poll tasks from a single queue. If all the threads are waiting, then the user must manually call runPendingTask() to resolve deadlock. More...
class  StackMemoryResource
 A stack-based memory resource that allocates memory in a LIFO order from a fixed buffer. Deallocation must happen in reverse order of allocation. Throws std::bad_alloc if there is insufficient space for an allocation. More...
class  ThreadTask
 A task that can be execute by a thread. Internally, it uses type erasure to store a std::packaged_task. The memory is managed by std::unique_ptr. More...
class  Timer
 A simple timer to record timelapses. Uses steady_clock by default. More...
class  Tuple
class  Tuple< T, Ts... >
class  Vector
struct  VectorGrowthStrategy
class  WorkStealingQueue

Concepts

concept  GrowthStrategy

Functions

void enableMemoryGuard ()
 Enable MSVC native memory leak checker in debug mode. Not compatible with address sanitizer.
template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt uninitializedForward (AllocatorType &allocator, InputIt first, InputIt last, OutputIt dest)
 Forward elements from a source range to uninitialized memory.
template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt uninitializedMove (AllocatorType &allocator, InputIt first, InputIt last, OutputIt dest) noexcept
 Moves elements from a source range to uninitialized memory.
template<typename AllocatorType, std::forward_iterator OutputIt, typename ... Args>
OutputIt uninitializedEmplace (AllocatorType &allocator, OutputIt first, OutputIt last, const Args &... args)
 Constructs objects in uninitialized memory by copying arguments to their constructor. Intentional copy of arguments to prevent reuse of moved objects.
template<typename AllocatorType, std::forward_iterator OutputIt, typename T>
OutputIt uninitializedFill (AllocatorType &allocator, OutputIt first, OutputIt last, const T &value)
 Fills uninitialized memory with copies of a value.
template<typename AllocatorType, std::forward_iterator InputIt>
void destroyElements (AllocatorType &allocator, InputIt first, InputIt last) noexcept
 Destroys a range of constructed objects in memory.
template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt uninitializedForwardN (AllocatorType &allocator, InputIt first, std::size_t count, OutputIt dest)
 Forward count elements from a source range to uninitialized memory.
template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt uninitializedMoveN (AllocatorType &allocator, InputIt first, std::size_t count, OutputIt dest) noexcept
 Moves count elements from a source range to uninitialized memory.
template<typename AllocatorType, std::forward_iterator OutputIt, typename ... Args>
OutputIt uninitializedEmplaceN (AllocatorType &allocator, OutputIt first, std::size_t count, const Args &... args)
 Constructs a specified number of objects in uninitialized memory by copying constructor arguments. Intentional copy of arguments to prevent reuse of moved objects.
template<typename AllocatorType, std::forward_iterator OutputIt, typename T>
OutputIt uninitializedFillN (AllocatorType &allocator, OutputIt first, std::size_t count, const T &value)
 Fills count elements in uninitialized memory with a value.
template<typename AllocatorType, std::forward_iterator InputIt>
void destroyElementsN (AllocatorType &allocator, InputIt first, std::size_t count) noexcept
 Destroys count objects in a range.
template<typename AllocatorType, typename T>
void deleteBuffer (AllocatorType &allocator, T *buffer, std::size_t size, std::size_t capacity) noexcept
 Destroys and deallocates the buffer.
template<typename T, typename U>
std::size_t pointerDistance (const T *first, const U *last)
 Calculate the distance in bytes from the first pointer to the last pointer.
template<typename Header>
Header * alignWithHeader (std::size_t alignment, std::size_t size, void *&buffer, std::size_t &capacity) noexcept
 Align the header + buffer to their corresponding alignments. If the capacity is not big enough, returns nullptr.
template<typename T>
requires std::integral<T> || std::floating_point<T>
getRandomNumber (T lower, T upper)
 Produces a random number uniformly distributed on the closed interval [lower, upper].
template<std::random_access_iterator It>
void shuffle (It begin, It end)
 Randomly shuffle the elements in the range [begin, end).
template<typename T, typename ... Ts>
 Tuple (T &&, Ts &&...) -> Tuple< T, Ts... >

Function Documentation

◆ alignWithHeader()

template<typename Header>
Header * flow::alignWithHeader ( std::size_t alignment,
std::size_t size,
void *& buffer,
std::size_t & capacity )
noexcept

Align the header + buffer to their corresponding alignments. If the capacity is not big enough, returns nullptr.

Parameters
alignmentRequired alignment.
sizeSize of the buffer to align.
bufferReference to pointer to the buffer to be aligned.
capacityReference to the capacity of the buffer.
Returns
Aligned header pointer followed by the aligned buffer, or nullptr on failure.

Definition at line 161 of file flow_memory_algorithm.h.

161 {
162
163 // https://stackoverflow.com/questions/46457449/is-it-always-the-case-that-sizeoft-alignoft-for-all-object-types-t
164 assert(size >= alignment && "size is smaller than its alignment");
165 if (capacity < sizeof(Header) + size) {
166 return nullptr;
167 }
168
169 // Set the block alignment to be at least as big as the header alignment.
170 alignment = std::max(alignment, alignof(Header));
171
172 // Reserve at least sizeof(Header) before the allocated block.
173 void* allocatedBlock = reinterpret_cast<Header*>(buffer) + 1;
174 std::size_t capacityAfterHeader = capacity - sizeof(Header);
175 if (!std::align(alignment, size, allocatedBlock, capacityAfterHeader)) {
176 return nullptr;
177 }
178
179 // Calculate the address of the header.
180 Header* header = reinterpret_cast<Header*>(allocatedBlock) - 1;
181 assert(reinterpret_cast<std::uintptr_t>(header) % alignof(Header) == 0 && "allocated buffer is not aligned with the header");
182
183 capacity -= pointerDistance(buffer, header);
184 buffer = header;
185 return header;
186 }
std::size_t pointerDistance(const T *first, const U *last)
Calculate the distance in bytes from the first pointer to the last pointer.

References pointerDistance().

Referenced by flow::StackMemoryResource::allocateImp(), and flow::PoolMemoryResource::PoolMemoryResource().

◆ deleteBuffer()

template<typename AllocatorType, typename T>
void flow::deleteBuffer ( AllocatorType & allocator,
T * buffer,
std::size_t size,
std::size_t capacity )
noexcept

Destroys and deallocates the buffer.

Parameters
allocatorAllocator for destruction and deallocation.
bufferPointer to the buffer.
sizeNumber of constructed elements.
capacityTotal buffer capacity.

Definition at line 138 of file flow_memory_algorithm.h.

138 {
139 destroyElementsN(allocator, buffer, size);
140 std::allocator_traits<AllocatorType>::deallocate(allocator, buffer, capacity);
141 }
void destroyElementsN(AllocatorType &allocator, InputIt first, std::size_t count) noexcept
Destroys count objects in a range.

References destroyElementsN().

Referenced by flow::Vector< T, Allocator >::updateBuffer(), and flow::Vector< T, Allocator >::~Vector().

◆ destroyElements()

template<typename AllocatorType, std::forward_iterator InputIt>
void flow::destroyElements ( AllocatorType & allocator,
InputIt first,
InputIt last )
noexcept

Destroys a range of constructed objects in memory.

Parameters
allocatorAllocator for destruction.
firstStart of range.
lastEnd of range.

Definition at line 64 of file flow_memory_algorithm.h.

64 {
65 for (; first != last; ++first) {
66 std::allocator_traits<AllocatorType>::destroy(allocator, std::addressof(*first));
67 }
68 }

Referenced by flow::Vector< T, Allocator >::erase(), and flow::Vector< T, Allocator >::resizeImp().

◆ destroyElementsN()

template<typename AllocatorType, std::forward_iterator InputIt>
void flow::destroyElementsN ( AllocatorType & allocator,
InputIt first,
std::size_t count )
noexcept

Destroys count objects in a range.

Parameters
allocatorAllocator for destruction.
firstStart of range.
countNumber of elements to destroy.

Definition at line 126 of file flow_memory_algorithm.h.

126 {
127 for (std::size_t i = 0; i < count; ++i, ++first) {
128 std::allocator_traits<AllocatorType>::destroy(allocator, std::addressof(*first));
129 }
130 }

Referenced by flow::Vector< T, Allocator >::clear(), and deleteBuffer().

◆ enableMemoryGuard()

void flow::enableMemoryGuard ( )
inline

Enable MSVC native memory leak checker in debug mode. Not compatible with address sanitizer.

Definition at line 15 of file flow_debug_memory.h.

15 {
16 #ifdef _CRTDBG_MAP_ALLOC
17 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
18 #endif
19 }

◆ getRandomNumber()

template<typename T>
requires std::integral<T> || std::floating_point<T>
T flow::getRandomNumber ( T lower,
T upper )

Produces a random number uniformly distributed on the closed interval [lower, upper].

Parameters
lowerThe lower bound of the interval.
upperThe upper bound of the interval.
Returns
A random number in the closed interval [lower, upper].

Definition at line 14 of file flow_random_algorithm.h.

14 {
15 static std::random_device device{};
16 static std::mt19937_64 engine(device());
17 if constexpr (std::integral<T>) {
18 return std::uniform_int_distribution<T>(lower, upper)(engine);
19 } else {
20 return std::uniform_real_distribution<T>(lower, upper)(engine);
21 }
22 }

◆ pointerDistance()

template<typename T, typename U>
std::size_t flow::pointerDistance ( const T * first,
const U * last )

Calculate the distance in bytes from the first pointer to the last pointer.

Parameters
firstPointer to the first element.
lastPointer to one-past-last element.
Returns
The address distance in bytes.

Definition at line 148 of file flow_memory_algorithm.h.

148 {
149 assert(first <= last && "first pointer address must be smaller than the last pointer address");
150 return reinterpret_cast<const std::byte*>(last) - reinterpret_cast<const std::byte*>(first);
151 }

Referenced by alignWithHeader(), and flow::StackMemoryResource::deallocateImp().

◆ shuffle()

template<std::random_access_iterator It>
void flow::shuffle ( It begin,
It end )

Randomly shuffle the elements in the range [begin, end).

Parameters
beginIterator to the beginning of the range.
endIterator to one past the end of the range.

Definition at line 28 of file flow_random_algorithm.h.

28 {
29 static std::random_device device{};
30 static std::mt19937_64 engine(device());
31 std::shuffle(begin, end, engine);
32 }

◆ Tuple()

template<typename T, typename ... Ts>
flow::Tuple ( T && ,
Ts && ... ) -> Tuple< T, Ts... >

◆ uninitializedEmplace()

template<typename AllocatorType, std::forward_iterator OutputIt, typename ... Args>
OutputIt flow::uninitializedEmplace ( AllocatorType & allocator,
OutputIt first,
OutputIt last,
const Args &... args )

Constructs objects in uninitialized memory by copying arguments to their constructor. Intentional copy of arguments to prevent reuse of moved objects.

Parameters
allocatorAllocator for memory management.
firstStart of the destination range.
lastEnd of the destination range.
argsConstructor arguments.
Returns
One past the last constructed element.

Definition at line 41 of file flow_memory_algorithm.h.

41 {
42 for (; first != last; ++first) {
43 std::allocator_traits<AllocatorType>::construct(allocator, std::addressof(*first), args...);
44 }
45 return first;
46 }

Referenced by flow::Vector< T, Allocator >::resizeImp(), and uninitializedFill().

◆ uninitializedEmplaceN()

template<typename AllocatorType, std::forward_iterator OutputIt, typename ... Args>
OutputIt flow::uninitializedEmplaceN ( AllocatorType & allocator,
OutputIt first,
std::size_t count,
const Args &... args )

Constructs a specified number of objects in uninitialized memory by copying constructor arguments. Intentional copy of arguments to prevent reuse of moved objects.

Parameters
allocatorAllocator for memory management.
firstStart of the destination range.
countNumber of objects to construct.
argsConstructor arguments.
Returns
One past the last constructed element.

Definition at line 103 of file flow_memory_algorithm.h.

103 {
104 for (std::size_t i = 0; i < count; ++i, ++first) {
105 std::allocator_traits<AllocatorType>::construct(allocator, std::addressof(*first), args...);
106 }
107 return first;
108 }

Referenced by uninitializedFillN().

◆ uninitializedFill()

template<typename AllocatorType, std::forward_iterator OutputIt, typename T>
OutputIt flow::uninitializedFill ( AllocatorType & allocator,
OutputIt first,
OutputIt last,
const T & value )

Fills uninitialized memory with copies of a value.

Parameters
allocatorAllocator for construction.
firstStart of destination range.
lastEnd of destination range.
valueValue to fill with.
Returns
One past the last constructed element.

Definition at line 55 of file flow_memory_algorithm.h.

55 {
56 return uninitializedEmplace(allocator, first, last, value);
57 }
OutputIt uninitializedEmplace(AllocatorType &allocator, OutputIt first, OutputIt last, const Args &... args)
Constructs objects in uninitialized memory by copying arguments to their constructor....

References uninitializedEmplace().

◆ uninitializedFillN()

template<typename AllocatorType, std::forward_iterator OutputIt, typename T>
OutputIt flow::uninitializedFillN ( AllocatorType & allocator,
OutputIt first,
std::size_t count,
const T & value )

Fills count elements in uninitialized memory with a value.

Parameters
allocatorAllocator for construction.
firstStart of destination range.
countNumber of elements to fill.
valueValue to fill with.
Returns
One past the last constructed element.

Definition at line 117 of file flow_memory_algorithm.h.

117 {
118 return uninitializedEmplaceN(allocator, first, count, value);
119 }
OutputIt uninitializedEmplaceN(AllocatorType &allocator, OutputIt first, std::size_t count, const Args &... args)
Constructs a specified number of objects in uninitialized memory by copying constructor arguments....

References uninitializedEmplaceN().

◆ uninitializedForward()

template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt flow::uninitializedForward ( AllocatorType & allocator,
InputIt first,
InputIt last,
OutputIt dest )

Forward elements from a source range to uninitialized memory.

Parameters
allocatorAllocator for construction.
firstStart of source range.
lastEnd of source range.
destStart of destination range.
Returns
One past the last constructed element.

Definition at line 15 of file flow_memory_algorithm.h.

15 {
16 for (; first != last; ++first, ++dest) {
17 std::allocator_traits<AllocatorType>::construct(allocator, std::addressof(*dest), *first);
18 }
19 return dest;
20 }

Referenced by flow::Vector< T, Allocator >::insert(), uninitializedMove(), and flow::Vector< T, Allocator >::Vector().

◆ uninitializedForwardN()

template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt flow::uninitializedForwardN ( AllocatorType & allocator,
InputIt first,
std::size_t count,
OutputIt dest )

Forward count elements from a source range to uninitialized memory.

Parameters
allocatorAllocator for construction.
firstStart of source range.
countNumber of elements to copy.
destStart of destination range.
Returns
One past the last constructed element.

Definition at line 77 of file flow_memory_algorithm.h.

77 {
78 for (std::size_t i = 0; i < count; ++i, ++first, ++dest) {
79 std::allocator_traits<AllocatorType>::construct(allocator, std::addressof(*dest), *first);
80 }
81 return dest;
82 }

Referenced by flow::Vector< T, Allocator >::insert(), and uninitializedMoveN().

◆ uninitializedMove()

template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt flow::uninitializedMove ( AllocatorType & allocator,
InputIt first,
InputIt last,
OutputIt dest )
noexcept

Moves elements from a source range to uninitialized memory.

Parameters
allocatorAllocator for construction.
firstStart of source range.
lastEnd of source range.
destStart of destination range.
Returns
One past the last constructed element.

Definition at line 29 of file flow_memory_algorithm.h.

29 {
30 return uninitializedForward(allocator, std::move_iterator(first), std::move_iterator(last), dest);
31 }
OutputIt uninitializedForward(AllocatorType &allocator, InputIt first, InputIt last, OutputIt dest)
Forward elements from a source range to uninitialized memory.

References uninitializedForward().

Referenced by flow::Vector< T, Allocator >::insert(), flow::Vector< T, Allocator >::relocateBuffer(), and flow::Vector< T, Allocator >::relocateBufferWithHoles().

◆ uninitializedMoveN()

template<typename AllocatorType, std::input_iterator InputIt, std::forward_iterator OutputIt>
OutputIt flow::uninitializedMoveN ( AllocatorType & allocator,
InputIt first,
std::size_t count,
OutputIt dest )
noexcept

Moves count elements from a source range to uninitialized memory.

Parameters
allocatorAllocator for construction.
firstStart of source range.
countNumber of elements to move.
destStart of destination range.
Returns
One past the last constructed element.

Definition at line 91 of file flow_memory_algorithm.h.

91 {
92 return uninitializedForwardN(allocator, std::move_iterator(first), count, dest);
93 }
OutputIt uninitializedForwardN(AllocatorType &allocator, InputIt first, std::size_t count, OutputIt dest)
Forward count elements from a source range to uninitialized memory.

References uninitializedForwardN().