Class thl::core::MemoryBlock#

template<typename T>
class MemoryBlock#

Contiguous, resizable block of raw storage for size() elements of T.

Elements are never constructed or destroyed by the block: it hands out malloc’d memory, which makes it usable for non-copyable element types (e.g. std::atomic<float>) as long as the caller treats the storage as raw.

Failure semantics (no logging, no platform dependencies):

  • Allocation failure throws std::bad_alloc and leaves the block unchanged.

  • swap_data() with a size mismatch is a contract violation: it asserts in debug builds and is a no-op in release builds.

Public Functions

inline MemoryBlock(std::size_t size = 0)#
inline ~MemoryBlock() noexcept#
inline MemoryBlock(const MemoryBlock &other)#
inline MemoryBlock &operator=(const MemoryBlock &other)#
inline MemoryBlock(MemoryBlock &&other) noexcept#
inline MemoryBlock &operator=(MemoryBlock &&other) noexcept#
inline T &operator[](size_t index)#
inline const T &operator[](size_t index) const#
inline T *data()#
inline const T *data() const#
inline size_t size() const#
inline void resize(size_t size)#

Resize the block, preserving the first min(old, new) elements.

On allocation failure the block is left unchanged (old data and old size) and std::bad_alloc is thrown, so size() never claims more elements than data() actually holds.

inline void clear()#
template<typename U = T>
inline void swap_data(MemoryBlock &other)#

Exchange storage with another block of the same size.

Precondition: size() == other.size(); a mismatch asserts in debug and is a no-op in release.

inline void swap_data(T *&data, size_t size)#

Exchange storage with a raw malloc’d pointer holding size elements.

Precondition: size() == size; a mismatch asserts in debug and is a no-op in release.