Utilities
Misc helper functionality.
likely
-
likely(expr)
Marks the given expression to likely be evaluated to true.
This may help compiler optimization.
- Returns:
the evaluated
expr
-
unlikely(expr)
Marks the given expression to likely be evaluated to true.
This may help compiler optimization.
- Returns:
the evaluated
expr
saturated_cast
stored_assert
-
stored_assert(expr)
Like
assert()
, but only emits code when stored::Config::EnableAssert.
stored::Fifo
Warning
doxygenclass: Cannot find class “stored::Fifo” in doxygen xml output for project “doxygen” from directory: /home/runner/work/libstored/libstored/dist/ubuntu/build/sphinx/doxygen/xml/
stored::memcmp_swap
-
int stored::memcmp_swap(void const *a, void const *b, size_t len) noexcept
memcmp() with endianness swapping.
-
void stored::memcpy_swap(void *dst, void const *src, size_t len) noexcept
memcpy()
with endianness swapping.
stored::MessageFifo
Warning
doxygenclass: Cannot find class “stored::MessageFifo” in doxygen xml output for project “doxygen” from directory: /home/runner/work/libstored/libstored/dist/ubuntu/build/sphinx/doxygen/xml/
stored::Scratchpad
-
template<size_t MaxSize = 0xffff>
class ScratchPad Memory that uses bump-alloc for a very fast short-lived heap.
The ScratchPad grows automatically, but it is more efficient to manage the capacity() on beforehand. The capacity is determined while using the ScratchPad, which may cause some more overhead at the start of the application.
There is no overhead per alloc(), but padding bytes may be inserted to word-align allocs. Heap fragmentation is not possible.
Alloc is very fast, but dealloc or free is not possible. Bump-alloc is like a stack; you can reset() it, or make a snapshot(), which you can rollback to.
- Template Parameters:
MaxSize – the maximum total size to be allocated, which is used to determine the type of the internal counters.
Public Types
Public Functions
-
inline explicit ScratchPad(size_t reserve = 0)
Ctor.
- Parameters:
reserve – number of bytes to reserve during construction
-
inline ~ScratchPad() noexcept
Dtor.
-
template<typename T>
inline T *alloc(size_t count = 1, size_t align = sizeof(T)) Allocate memory.
- Template Parameters:
T – the type of object to allocate
- Parameters:
count – number of objects, which is allocated as an array of
T
align – alignment requirement (maximized to word size)
- Returns:
a pointer to the allocated memory, which remains uninitialized and cannot be
nullptr
-
inline constexpr size_t capacity() const noexcept
Returns the total capacity currently available within the ScratchPad.
-
inline constexpr size_t chunks() const noexcept
Returns the number of chunks of the ScratchPad.
You would want to have only one chunk, but during the first moments of running, the ScratchPad has to determine how much memory the application uses. During this time, there may exist multiple chunks. Call reset() to optimize memory usage.
-
inline constexpr bool empty() const noexcept
Checks if the ScratchPad is empty.
-
inline constexpr size_t max() const noexcept
Returns the maximum size.
To reset this value, use shrink_to_fit().
-
inline void reserve(size_t more)
Reserves memory to save the additional given amount of bytes.
-
inline void reset() noexcept
Resets the content of the ScratchPad.
Coalesce chunks when required. It leaves max() untouched. To actually free all used memory, call shrink_to_fit() afterwards.
-
inline void shrink_to_fit() noexcept
Releases all unused memory back to the OS, if possible.
-
inline constexpr size_t size() const noexcept
Returns the total amount of allocated memory.
This includes padding because of alignment requirements of alloc().
-
inline Snapshot snapshot() noexcept
Get a snapshot of the ScratchPad.
See also
Friends
- friend class Snapshot
-
class Snapshot
A snapshot of the ScratchPad, which can be rolled back to.
A Snapshot remains valid, until the ScratchPad is reset, or an earlier snapshot is rolled back. An invalid snapshot cannot be rolled back, and cannot be destructed, as that implies a rollback. Make sure to reset the snapshot before destruction, if it may have become invalid.
Normally, you would let a snapshot go out of scope before doing anything with older snapshots.
See also
Public Functions
-
inline Snapshot(Snapshot const &s) noexcept
Move ctor.
Even though
s
isconst
, it will be reset anyway by this ctor.
-
inline ~Snapshot() noexcept
Dtor, which implies a rollback.
-
inline void reset() noexcept
Detach from the ScratchPad.
Cannot rollback afterwards.
-
inline void rollback() noexcept
Perform a rollback of the corresponding ScratchPad.
-
inline Snapshot(Snapshot const &s) noexcept
stored::Signal
Warning
doxygenclass: Cannot find class “stored::Signal” in doxygen xml output for project “doxygen” from directory: /home/runner/work/libstored/libstored/dist/ubuntu/build/sphinx/doxygen/xml/
stored::Signalling
Warning
doxygenclass: Cannot find class “stored::Signalling” in doxygen xml output for project “doxygen” from directory: /home/runner/work/libstored/libstored/dist/ubuntu/build/sphinx/doxygen/xml/
stored::string_literal
-
String::type stored::string_literal(void const *buffer, size_t len, char const *prefix)
Converts the given buffer to a string literal.
This comes in handy for verbose output of binary data, like protocol messages.
stored::strncmp
-
int stored::strncmp(char const *str1, size_t len1, char const *str2, size_t len2) noexcept
Like
::strncmp()
, but handles non zero-terminated strings.
stored::strncpy
-
size_t stored::strncpy(char *dst, char const *src, size_t len) noexcept
Like
::strncpy()
, but without padding and returning the length of the string.
stored::swap_endian
-
template<typename T>
static inline T stored::swap_endian(T value) noexcept Swap endianness of the given value.
-
void stored::swap_endian(void *buffer, size_t len) noexcept
Swap endianness of the given buffer.