Class anira::MemoryBlock¶
-
template<typename T>
class MemoryBlock¶ Template class for managing contiguous memory blocks with automatic resource management.
The MemoryBlock class provides a low-level, efficient container for managing contiguous memory allocation with automatic cleanup and move semantics. It serves as a foundation for higher-level data structures like Buffer.
Key features:
Automatic memory management with proper cleanup
Move semantics for efficient memory transfers
Zero-copy data swapping for trivially copyable types
Direct memory access with array-style indexing
Resize capabilities with memory reallocation
Template-based design supporting any data type
This class is designed for performance-critical applications where direct memory control is needed while maintaining memory safety.
See also
Note
This class uses malloc/free for memory management to allow for efficient reallocation operations and to avoid constructor/destructor calls for POD types.
- Template Parameters:
T – The data type to store in the memory block
Public Functions
-
inline MemoryBlock(std::size_t size = 0)¶
Constructor that allocates a memory block of specified size.
Allocates contiguous memory for the specified number of elements. If allocation fails, an error is logged and the block remains in an invalid state.
Note
Memory is allocated but not initialized. For non-POD types, consider using appropriate initialization after construction.
- Parameters:
size – Number of elements to allocate (default: 0 for empty block)
-
inline ~MemoryBlock() noexcept¶
Destructor that automatically frees allocated memory.
Safely deallocates the memory block using free(). Marked noexcept to guarantee no exceptions during destruction, which is essential for RAII.
-
inline MemoryBlock(const MemoryBlock &other)¶
Copy constructor that creates a deep copy of another memory block.
Allocates new memory and copies all data from the source block. If allocation fails, an error is logged and the block remains in an invalid state.
- Parameters:
other – The source memory block to copy from
-
inline MemoryBlock &operator=(const MemoryBlock &other)¶
Copy assignment operator that replaces this block’s content with another’s data.
Deallocates existing memory, allocates new memory matching the source size, and copies all data. Self-assignment is safely handled.
- Parameters:
other – The source memory block to copy from
- Returns:
Reference to this memory block after the copy operation
-
inline MemoryBlock(MemoryBlock &&other) noexcept¶
Move constructor that transfers ownership from another memory block.
Efficiently transfers the memory ownership from the source block to this block without copying data. The source block is left in a valid but empty state.
Note
Marked noexcept to guarantee no exceptions, essential for move semantics
- Parameters:
other – The source memory block to move from (will be left empty)
-
inline MemoryBlock &operator=(MemoryBlock &&other) noexcept¶
Move assignment operator that transfers ownership from another memory block.
Deallocates any existing memory, then efficiently transfers ownership from the source block. The source block is left in a valid but empty state.
Note
Marked noexcept to guarantee no exceptions, essential for move semantics
- Parameters:
other – The source memory block to move from (will be left empty)
- Returns:
Reference to this memory block after the move operation
-
inline T &operator[](size_t index)¶
Array subscript operator for mutable element access.
Provides direct access to elements in the memory block using array-style indexing. No bounds checking is performed for performance reasons.
Warning
No bounds checking is performed. Accessing out-of-bounds indices results in undefined behavior.
- Parameters:
index – The index of the element to access (0-based)
- Returns:
Reference to the element at the specified index
-
inline const T &operator[](size_t index) const¶
Array subscript operator for const element access.
Provides direct read-only access to elements in the memory block using array-style indexing. No bounds checking is performed for performance reasons.
Warning
No bounds checking is performed. Accessing out-of-bounds indices results in undefined behavior.
- Parameters:
index – The index of the element to access (0-based)
- Returns:
Const reference to the element at the specified index
-
inline T *data()¶
Gets a pointer to the raw memory data.
Returns a direct pointer to the underlying memory block. This can be used for interfacing with C APIs or for performance-critical operations that need direct memory access.
- Returns:
Pointer to the first element in the memory block
-
inline size_t size() const¶
Gets the number of elements in the memory block.
- Returns:
The number of elements currently allocated in the memory block
-
inline void resize(size_t size)¶
Resizes the memory block to a new size.
Changes the size of the memory block, potentially reallocating memory. If the new size is larger, the additional memory is uninitialized. If smaller, data beyond the new size is lost.
Note
This operation may invalidate existing pointers to the data. For performance, realloc is used when possible to avoid unnecessary copying.
- Parameters:
size – New number of elements to allocate
-
inline void clear()¶
Clears the memory block by setting all bytes to zero.
Efficiently zeros out all memory in the block using memset. This is a fast operation suitable for clearing numerical data or preparing memory for fresh use.
Note
This operation sets all bytes to zero, which may not be appropriate for all data types (e.g., types with non-trivial constructors).
-
template<typename U = T, std::enable_if_t<std::is_trivially_copyable_v<U>, bool> = true>
inline void swap_data(MemoryBlock &other)¶ Swaps data with another memory block without copying (for trivially copyable types)
Efficiently exchanges the data pointers between this block and another block of the same size. This is a zero-copy operation that only swaps pointers. The function is only available for trivially copyable types to ensure safety.
Template parameter U is used with SFINAE (Substitution Failure Is Not An Error) to enable this function only for trivially copyable types, ensuring memory safety for complex types that may have special copying requirements.
Note
Both blocks must have identical sizes for the swap to succeed. This function is only available for trivially copyable types.
- Template Parameters:
U – Template parameter defaulting to T, used for SFINAE type checking
- Parameters:
other – The memory block to swap data with
-
inline void swap_data(T *&data, size_t size)¶
Swaps data with a raw memory pointer without copying.
Efficiently exchanges the internal data pointer with the provided raw pointer. This is a zero-copy operation that transfers ownership of the memory.
Note
The provided memory size must match this block’s current size. After the swap, the caller assumes ownership of this block’s original memory.
- Parameters:
data – Reference to the raw memory pointer to swap with
size – Size of the raw memory in number of elements