Generic memory buffer descriptor with alignment and memory space support. More...
#include <buffer.h>
Public Types | |
| using | Ptr = SharedPtr< Buffer > |
| Shared pointer type for Buffer. | |
| using | List = promeki::List< Buffer > |
| List of Buffer values. | |
| using | PtrList = promeki::List< Ptr > |
| List of shared Buffer pointers. | |
Public Member Functions | |
| Buffer ()=default | |
| Constructs an invalid (empty) buffer. | |
| Buffer (size_t sz, size_t an=DefaultAlign, const MemSpace &ms=MemSpace::Default) | |
| Allocates a buffer of the given size. | |
| Buffer (void *p, size_t sz, size_t an=0, bool own=false, const MemSpace &ms=MemSpace::Default) | |
| Wraps an existing memory pointer as a buffer. | |
| Buffer (const Buffer &o) | |
| Copy constructor. | |
| Buffer & | operator= (const Buffer &o) |
| Copy assignment operator. Performs a deep copy. | |
| Buffer (Buffer &&o) noexcept | |
| Move constructor. Transfers ownership from the source buffer. | |
| Buffer & | operator= (Buffer &&o) noexcept |
| Move assignment operator. Transfers ownership from the source buffer. | |
| ~Buffer () | |
| Destructor. Releases owned memory. | |
| bool | isValid () const |
| Returns true if the buffer has been allocated or assigned memory. | |
| void * | data () const |
| Returns a pointer to the buffer data. | |
| void * | odata () const |
| Returns the original allocation pointer. | |
| size_t | size () const |
| Returns the logical content size in bytes. | |
| void | setSize (size_t s) const |
| Sets the logical content size. | |
| size_t | availSize () const |
| Returns the available space from data() to the end of the allocation. | |
| size_t | allocSize () const |
| Returns the total allocation size in bytes. | |
| size_t | align () const |
| Returns the alignment of the buffer in bytes. | |
| const MemSpace & | memSpace () const |
| Returns the memory space this buffer was allocated from. | |
| const MemAllocation & | allocation () const |
| Returns the underlying allocation descriptor. | |
| void | shiftData (size_t bytes) |
| Shifts the data pointer forward from the allocation base. | |
| bool | isHostAccessible () const |
| Returns true if the buffer memory is directly accessible from the host CPU. | |
| void | setOwnershipEnabled (bool val) |
| Enables or disables ownership of the underlying memory. | |
| Error | fill (char value) const |
| Fills the entire buffer with the given byte value. | |
| Error | copyFrom (const void *src, size_t bytes, size_t offset=0) const |
| Copies data from an external source into the buffer. | |
Static Public Member Functions | |
| static size_t | getPageSize () |
| Returns the system memory page size in bytes. | |
Static Public Attributes | |
| static const size_t | DefaultAlign |
| Default memory alignment in bytes. | |
Generic memory buffer descriptor with alignment and memory space support.
Buffer is a uniform tracking object for contiguous memory regions that may reside in different address spaces: local system RAM, GPU device memory, or even remote memory on another machine (e.g. RDMA). It does not assume the memory is host-accessible — use isHostAccessible() before dereferencing data(). All memory operations (allocation, release, copy, fill) are delegated through the buffer's MemSpace, allowing each memory space to provide appropriate implementations.
|
inline |
Allocates a buffer of the given size.
| sz | Size in bytes to allocate. |
| an | Alignment in bytes (defaults to DefaultAlign). |
| ms | Memory space to allocate from. |
|
inline |
Wraps an existing memory pointer as a buffer.
| p | Pointer to existing memory. |
| sz | Size of the memory region in bytes. |
| an | Alignment of the pointer (0 if unknown). |
| own | If true, the buffer takes ownership and will free the memory on destruction. |
| ms | Memory space the pointer belongs to. |
Copy constructor.
Performs a deep copy of the buffer data, including any data shift offset and the logical size.
|
inline |
Returns the alignment of the buffer in bytes.
|
inline |
Returns the underlying allocation descriptor.
|
inline |
Returns the total allocation size in bytes.
This always returns the full allocation size regardless of any shiftData() calls or the logical content size.
|
inline |
Returns the available space from data() to the end of the allocation.
This is the maximum number of bytes that can be stored starting from data(). Equal to allocSize() minus any data shift.
Copies data from an external source into the buffer.
Copies bytes from src into the buffer starting at offset bytes from data(). The buffer must be valid and host-accessible, and offset + bytes must not exceed availSize().
| src | Pointer to the source data. |
| bytes | Number of bytes to copy. |
| offset | Destination offset from data() (default 0). |
|
inline |
Returns a pointer to the buffer data.
Fills the entire buffer with the given byte value.
| value | The byte value to fill with. |
Returns the system memory page size in bytes.
|
inline |
Returns true if the buffer memory is directly accessible from the host CPU.
|
inline |
Returns true if the buffer has been allocated or assigned memory.
Returns the memory space this buffer was allocated from.
|
inline |
Returns the original allocation pointer.
Unlike data(), this always points to the start of the allocated region regardless of any shiftData() calls. This is the pointer that was returned by the MemSpace allocator.
Enables or disables ownership of the underlying memory.
| val | If true, the buffer will free memory on destruction; if false, it will not. |
Sets the logical content size.
Indicates how many bytes of meaningful data are in the buffer, starting from data(). The value must not exceed availSize().
| s | The new logical content size. |
Shifts the data pointer forward from the allocation base.
Sets the data pointer to allocation base + bytes, creating a sub-buffer view. The shift is always relative to the original allocation pointer, not the current data pointer.
| bytes | Number of bytes from the allocation base. |
|
inline |