|
VLink 2.0.0
A high-performance communication middleware
|
Versatile 128-byte byte buffer with SBO, five ownership modes and compression helpers. 更多...
#include <bytes.h>
Public 成员函数 | |
| Bytes () noexcept | |
Constructs an empty, unallocated Bytes object. | |
| Bytes (const Bytes &target) noexcept | |
| Copy constructor. | |
| Bytes (Bytes &&target) noexcept | |
| Move constructor. | |
| Bytes (const std::initializer_list< uint8_t > &list) noexcept | |
Constructs an owned Bytes from an initialiser list of byte values. | |
| Bytes (const std::vector< uint8_t > &data) noexcept | |
Constructs an owned Bytes by deep-copying a std::vector<uint8_t>. | |
| ~Bytes () noexcept | |
Destructor. Frees owned memory if is_owner() is true and is_loaned() is false. | |
| Bytes & | operator= (const Bytes &target) noexcept |
| Copy assignment operator. | |
| Bytes & | operator= (Bytes &&target) noexcept |
| Move assignment operator. | |
| Bytes & | operator= (const std::vector< uint8_t > &data) noexcept |
Assignment from a std::vector<uint8_t> (deep copy). | |
| bool | operator== (const Bytes &target) const noexcept |
| Equality comparison – compares byte content. | |
| bool | operator!= (const Bytes &target) const noexcept |
| Inequality comparison – compares byte content. | |
| bool | operator== (const std::vector< uint8_t > &data) const noexcept |
Equality comparison with a std::vector<uint8_t>. | |
| bool | operator!= (const std::vector< uint8_t > &data) const noexcept |
Inequality comparison with a std::vector<uint8_t>. | |
| uint8_t & | operator[] (size_t index) noexcept |
| Mutable subscript operator. | |
| const uint8_t & | operator[] (size_t index) const noexcept |
| Read-only subscript operator. | |
| uint8_t * | data () noexcept |
| Returns a pointer to the start of the user data region (after the prefix offset). | |
| const uint8_t * | data () const noexcept |
| Returns a const pointer to the start of the user data region. | |
| uint8_t * | real_data () noexcept |
| Returns a pointer to the very beginning of the backing buffer (before the offset). | |
| const uint8_t * | real_data () const noexcept |
| Returns a const pointer to the very beginning of the backing buffer. | |
| size_t | size () const noexcept |
| Returns the number of usable bytes (excluding the prefix offset region). | |
| size_t | real_size () const noexcept |
| Returns the total backing-buffer size including the prefix offset region. | |
| size_t | capacity () const noexcept |
| Returns the allocated capacity of the backing buffer. | |
| uint8_t | offset () const noexcept |
| Returns the size of the prefix offset region in bytes. | |
| bool | is_owner () const noexcept |
Returns true if this object owns and is responsible for freeing its buffer. | |
| bool | is_loaned () const noexcept |
Returns true if this object holds an iceoryx loaned chunk. | |
| bool | empty () const noexcept |
Returns true if the buffer is empty (no data pointer and size == 0). | |
| uint8_t * | begin () noexcept |
Iterator begin (mutable) – same as data(). | |
| const uint8_t * | begin () const noexcept |
Iterator begin (const) – same as data() const. | |
| uint8_t * | end () noexcept |
| Iterator end (mutable) – one past the last usable byte. | |
| const uint8_t * | end () const noexcept |
| Iterator end (const) – one past the last usable byte. | |
| uint8_t * | real_begin () noexcept |
Iterator begin for the full backing buffer (mutable) – same as real_data(). | |
| const uint8_t * | real_begin () const noexcept |
| Iterator begin for the full backing buffer (const). | |
| uint8_t * | real_end () noexcept |
| Iterator end for the full backing buffer (mutable). | |
| const uint8_t * | real_end () const noexcept |
| Iterator end for the full backing buffer (const). | |
| bool | is_ptr () const noexcept |
Returns true if the object holds only an opaque pointer (size == 0 and not owner). | |
| std::vector< uint8_t > | to_raw_data () const noexcept |
Copies the usable byte region into a new std::vector<uint8_t>. | |
| std::string | to_string () const noexcept |
Returns the usable byte region as a std::string. | |
| std::string_view | to_string_view () const noexcept |
Returns a zero-copy std::string_view over the usable byte region. | |
| template<typename T = void> | |
| T * | to_ptr () const noexcept |
Reinterprets the backing buffer as a pointer to T. | |
| void | clear () noexcept |
| Releases the buffer and resets the object to the empty state. | |
| bool | shrink_to (size_t size) noexcept |
| Reduces the logical size of the buffer without reallocating. | |
| bool | reserve (size_t new_capacity) noexcept |
Ensures the backing buffer can hold at least new_capacity bytes. | |
| bool | resize (size_t size) noexcept |
Resizes the logical data region to size bytes. | |
| Bytes & | shallow_copy (const Bytes &bytes) noexcept |
Makes this object a non-owning alias of bytes (shallow copy in-place). | |
| Bytes & | deep_copy (const Bytes &bytes) noexcept |
Replaces this object with a fully owned deep copy of bytes. | |
| Bytes & | deep_copy_self () noexcept |
| Converts this object from a non-owning alias to a fully owned deep copy of its own data. | |
静态 Public 成员函数 | |
| static void | init_memory_pool () noexcept |
Initialises the global thread-safe memory pool for Bytes allocations. | |
| static void | release_memory_pool () noexcept |
| Releases the global memory pool and returns its memory to the OS. | |
| static uint8_t * | bytes_malloc (size_t size) noexcept |
| Allocates a raw byte buffer from the memory pool (or heap if pool is unavailable). | |
| static void | bytes_free (uint8_t *ptr, size_t size) noexcept |
Frees a buffer previously allocated by bytes_malloc(). | |
| static Bytes | create (size_t size, uint8_t offset=0) noexcept |
Creates an owned Bytes buffer of the given size. | |
| static Bytes | shallow_copy (uint8_t *data, size_t size) noexcept |
Creates a non-owning Bytes alias pointing to an external mutable buffer. | |
| static Bytes | shallow_copy (const uint8_t *data, size_t size) noexcept |
Creates a non-owning Bytes alias pointing to an external read-only buffer. | |
| static Bytes | shallow_copy_ptr (void *data) noexcept |
| Wraps an opaque pointer without associating a byte size. | |
| static Bytes | deep_copy (uint8_t *data, size_t size, uint8_t offset=0) noexcept |
| Creates an owned deep copy of an external mutable buffer. | |
| static Bytes | deep_copy (const uint8_t *data, size_t size, uint8_t offset=0) noexcept |
| Creates an owned deep copy of an external read-only buffer. | |
| static Bytes | loan_internal (uint8_t *data, size_t size) noexcept |
| Creates a loaned (non-owning) alias for an iceoryx zero-copy payload (mutable). | |
| static Bytes | loan_internal (const uint8_t *data, size_t size) noexcept |
| Creates a loaned (non-owning) alias for an iceoryx zero-copy payload (read-only). | |
| static Bytes | from_string (const std::string &str, uint8_t offset=0) noexcept |
Constructs a Bytes buffer from a std::string by deep-copying its contents. | |
| static Bytes | from_user_input (const std::string &str, bool *ok=nullptr) noexcept |
Parses a user-provided hex or binary string literal into a Bytes buffer. | |
| static std::string | convert_to_hex_str (const uint8_t *value, size_t size) noexcept |
| Converts a raw byte array to an uppercase hex string with spaces. | |
| static Bytes | reverse_order (const Bytes &target) noexcept |
Returns a new Bytes object with the byte order of target reversed. | |
| static std::string | encode_to_base64 (const Bytes &target) noexcept |
Encodes a Bytes buffer as a standard Base-64 ASCII string. | |
| static Bytes | decode_from_base64 (const std::string &target) noexcept |
Decodes a Base-64 ASCII string into a Bytes buffer. | |
| static uint32_t | get_crc_32 (const Bytes &target) noexcept |
Computes the CRC-32 checksum of a Bytes buffer. | |
| static constexpr uint8_t | stack_size () noexcept |
| Returns the size of the inline stack storage in bytes. | |
| static constexpr bool | is_little_endian () noexcept |
Returns true if the platform uses little-endian byte order. | |
| static constexpr bool | is_big_endian () noexcept |
Returns true if the platform uses big-endian byte order. | |
| static bool | is_compress_data (const uint8_t *data, size_t size) noexcept |
| Checks whether a raw byte buffer contains LZAV-compressed VLink data. | |
| static Bytes | compress_data (const uint8_t *data, size_t size, bool high_ratio=false) noexcept |
| Compresses a raw byte buffer using the LZAV algorithm. | |
| static Bytes | uncompress_data (const uint8_t *data, size_t size, bool check_valid=true) noexcept |
Decompresses a LZAV-compressed Bytes buffer. | |
友元 | |
| VLINK_EXPORT friend std::ostream & | operator<< (std::ostream &ostream, const Bytes &target) noexcept |
| Stream insertion operator – prints bytes as space-separated hex pairs. | |
Versatile 128-byte byte buffer with SBO, five ownership modes and compression helpers.
The total object size is always 128 bytes (96-byte inline stack storage + metadata). Allocations larger than 96 bytes spill to the memory pool or system heap.
|
noexcept |
Constructs an empty, unallocated Bytes object.
data() returns nullptr and size() returns 0. is_owner() and is_loaned() are both false.
|
noexcept |
Copy constructor.
If target is an owner, a deep copy of its data is made. If target is a shallow alias (non-owner, non-loaned), the alias is shared. Loaned objects are shallow-copied as well (ownership is not transferred).
| target | Source Bytes to copy. |
|
noexcept |
Move constructor.
Transfers all ownership and data from target. After the move, target is empty.
| target | Source Bytes to move from. |
|
noexcept |
Constructs an owned Bytes from an initialiser list of byte values.
Equivalent to create(list.size()) followed by a memcpy of the list elements.
| list | Initialiser list of uint8_t values. |
|
explicitnoexcept |
|
noexcept |
|
inlinenodiscardnoexcept |
Iterator begin (const) – same as data() const.
|
inlinenodiscardnoexcept |
|
staticnoexcept |
Frees a buffer previously allocated by bytes_malloc().
| ptr | Pointer returned by bytes_malloc(). |
| size | Original size passed to bytes_malloc(). |
|
staticnodiscardnoexcept |
Allocates a raw byte buffer from the memory pool (or heap if pool is unavailable).
| size | Number of bytes to allocate. |
nullptr on failure.bytes_free() using the same size.
|
inlinenodiscardnoexcept |
Returns the allocated capacity of the backing buffer.
capacity() >= real_size() always holds. For SBO buffers, capacity() == kStackSize.
|
noexcept |
Releases the buffer and resets the object to the empty state.
If is_owner() is true, the backing memory is returned to the pool or heap. After this call, empty() returns true.
|
staticnodiscardnoexcept |
Compresses a raw byte buffer using the LZAV algorithm.
Wraps the compressed payload with a 4-byte header magic and a 4-byte footer magic so that is_compress_data() can recognise it. Buffers larger than kMaxCompressCacheSize (1 MiB) are rejected and an empty Bytes is returned.
| data | Pointer to the uncompressed data. |
| size | Number of bytes to compress. |
| high_ratio | If true, uses LZAV high-compression mode (slower but better ratio). Default: false (normal mode). |
Bytes with header/footer magic, or empty on failure.
|
staticnodiscardnoexcept |
Converts a raw byte array to an uppercase hex string with spaces.
Each byte is rendered as two uppercase hex digits followed by a space, e.g., {0x1A, 0xB2} -> "1A B2 ". Useful for logging binary frames.
| value | Pointer to the byte array. |
| size | Number of bytes to convert. |
|
staticnodiscardnoexcept |
Creates an owned Bytes buffer of the given size.
Memory is allocated from the pool or heap. If size <= kStackSize (96) the inline stack buffer is used instead (no heap allocation). The contents are not initialised.
| size | Number of usable bytes (i.e. size() after construction). |
| offset | Number of bytes to reserve at the start of the backing buffer as a prefix region (e.g., for protocol headers). data() = real_data() + offset. Default: 0. |
Bytes object.
|
inlinenodiscardnoexcept |
Returns a const pointer to the start of the user data region.
nullptr if not allocated.
|
inlinenodiscardnoexcept |
Returns a pointer to the start of the user data region (after the prefix offset).
Equivalent to real_data() + offset(). Returns nullptr if the buffer is empty.
nullptr if not allocated.
|
staticnodiscardnoexcept |
Replaces this object with a fully owned deep copy of bytes.
Releases the current buffer, allocates new memory, and copies all bytes from bytes. After this call is_owner() is true.
| bytes | Source to copy. |
*this.
|
staticnodiscardnoexcept |
Creates an owned deep copy of an external read-only buffer.
Same as the mutable overload but accepts a const source pointer.
| data | Pointer to the source read-only buffer. |
| size | Number of bytes to copy. |
| offset | Prefix-region size in the new buffer. Default: 0. |
Bytes containing a copy of the source data.
|
staticnodiscardnoexcept |
Creates an owned deep copy of an external mutable buffer.
Allocates new memory, copies size bytes from data, and returns a fully owned Bytes object. Safe even after the original buffer is freed.
| data | Pointer to the source buffer. |
| size | Number of bytes to copy. |
| offset | Prefix-region size in the new buffer. Default: 0. |
Bytes containing a copy of the source data.
|
noexcept |
Converts this object from a non-owning alias to a fully owned deep copy of its own data.
If the object is already an owner, this is a no-op. Otherwise, new memory is allocated, the current data is copied, and is_owner_ is set to true.
*this.
|
inlinenodiscardnoexcept |
Returns true if the buffer is empty (no data pointer and size == 0).
true if data_ == nullptr and size_ == 0.
|
staticnodiscardnoexcept |
Encodes a Bytes buffer as a standard Base-64 ASCII string.
| target | Buffer to encode. |
|
inlinenodiscardnoexcept |
Iterator end (const) – one past the last usable byte.
|
inlinenodiscardnoexcept |
Iterator end (mutable) – one past the last usable byte.
|
staticnodiscardnoexcept |
|
staticnodiscardnoexcept |
Parses a user-provided hex or binary string literal into a Bytes buffer.
Accepts formats such as "0x1A2B3C" (hex) or raw binary strings. Sets ok to false if parsing fails.
| str | Input string to parse. |
| ok | Optional pointer set to true on success, false on failure. |
Bytes, or an empty object on failure.
|
staticnodiscardnoexcept |
Computes the CRC-32 checksum of a Bytes buffer.
| target | Buffer to checksum. |
|
staticnoexcept |
Initialises the global thread-safe memory pool for Bytes allocations.
On Linux with __cpp_lib_memory_resource support this initialises a pmr::synchronized_pool_resource that reduces per-allocation overhead. Call this once at application start before any Bytes objects are created. Safe to call multiple times; subsequent calls are no-ops.
|
inlinestaticnodiscardconstexprnoexcept |
Returns true if the platform uses big-endian byte order.
true on big-endian platforms. Equivalent to !is_little_endian().
|
staticnodiscardnoexcept |
Checks whether a raw byte buffer contains LZAV-compressed VLink data.
Inspects the first 4 bytes for the header magic {0x17, 0x49, 0xB2, 0x6F} and the last 4 bytes for the footer magic {0xA7, 0x05, 0xED, 0x71}. Both must match for the function to return true.
true if header and footer magic are present.
|
inlinestaticnodiscardconstexprnoexcept |
Returns true if the platform uses little-endian byte order.
Determined at compile time from preprocessor macros (__BYTE_ORDER__, __LITTLE_ENDIAN__, Windows defaults).
true on little-endian platforms (x86, arm-le, etc.).
|
inlinenodiscardnoexcept |
Returns true if this object holds an iceoryx loaned chunk.
Loaned objects must not free the underlying memory; ownership belongs to RouDi.
true for objects created via loan_internal().
|
inlinenodiscardnoexcept |
Returns true if this object owns and is responsible for freeing its buffer.
true for objects created via create() or deep_copy().
|
inlinenodiscardnoexcept |
Returns true if the object holds only an opaque pointer (size == 0 and not owner).
An object created via shallow_copy_ptr() has size_ == 0, offset_ == 0 and is_owner_ == false. Use to_ptr<T>() to retrieve the wrapped pointer.
true if this is a pointer-only wrapper.
|
staticnodiscardnoexcept |
|
staticnodiscardnoexcept |
Creates a loaned (non-owning) alias for an iceoryx zero-copy payload (mutable).
Marks the object with is_loaned() == true. VLink will not free the memory because it is owned by the iceoryx RouDi daemon. This factory is used internally by the shm:// transport backend.
Bytes object.
|
inlinenodiscardnoexcept |
Returns the size of the prefix offset region in bytes.
data() == real_data() + offset().
|
nodiscardnoexcept |
Inequality comparison – compares byte content.
| target | Bytes to compare with. |
true if the objects differ in size or content.
|
nodiscardnoexcept |
Inequality comparison with a std::vector<uint8_t>.
| data | Vector to compare with. |
true if sizes or contents differ. Move assignment operator.
Releases the current buffer, then transfers all state from target.
| target | Source Bytes to move from. |
*this. Copy assignment operator.
Releases the current buffer, then copies target (deep if owner, shallow otherwise).
| target | Source Bytes to copy-assign from. |
*this.
|
noexcept |
Assignment from a std::vector<uint8_t> (deep copy).
| data | Vector whose contents are deep-copied into this buffer. |
*this.
|
nodiscardnoexcept |
Equality comparison – compares byte content.
| target | Bytes to compare with. |
true if both objects have identical size and content.
|
nodiscardnoexcept |
Equality comparison with a std::vector<uint8_t>.
| data | Vector to compare with. |
true if sizes and contents match.
|
inlinenodiscardnoexcept |
Read-only subscript operator.
| index | Logical index (0-based from the start of the user data region). |
index.
|
inlinenodiscardnoexcept |
Mutable subscript operator.
Details
Accesses the byte at logical index index (i.e., real_data()[offset + index]). No bounds checking is performed.
| index | Logical index (0-based from the start of the user data region). |
index.
|
inlinenodiscardnoexcept |
Iterator begin for the full backing buffer (const).
|
inlinenodiscardnoexcept |
Iterator begin for the full backing buffer (mutable) – same as real_data().
|
inlinenodiscardnoexcept |
Returns a const pointer to the very beginning of the backing buffer.
|
inlinenodiscardnoexcept |
Returns a pointer to the very beginning of the backing buffer (before the offset).
Use this to write protocol headers into the reserved prefix region. real_data() + offset() == data().
|
inlinenodiscardnoexcept |
Iterator end for the full backing buffer (const).
|
inlinenodiscardnoexcept |
Iterator end for the full backing buffer (mutable).
|
inlinenodiscardnoexcept |
Returns the total backing-buffer size including the prefix offset region.
real_size() == size() + offset().
|
staticnoexcept |
Releases the global memory pool and returns its memory to the OS.
After this call, bytes_malloc falls back to the system heap allocator. Call once at application shutdown after all Bytes objects that used the pool have been destroyed, to avoid a use-after-free.
|
noexcept |
Ensures the backing buffer can hold at least new_capacity bytes.
If the current capacity is already >= new_capacity this is a no-op. Otherwise a new buffer is allocated and the existing data is copied.
| new_capacity | Minimum required capacity in bytes. |
true on success, false if allocation failed.
|
nodiscardnoexcept |
Resizes the logical data region to size bytes.
If size <= current capacity, only size_ is updated. If size > current capacity, reserve(size) is called first. Newly added bytes are not initialised.
| size | New desired size in bytes. |
true on success, false if reallocation failed. Makes this object a non-owning alias of bytes (shallow copy in-place).
Releases the current buffer, then copies the pointer and metadata from bytes without copying the underlying data. After this call is_owner() is false.
| bytes | Source to alias. |
*this.
|
staticnodiscardnoexcept |
|
staticnodiscardnoexcept |
Creates a non-owning Bytes alias pointing to an external mutable buffer.
No memory is allocated or copied. The caller is responsible for ensuring the external buffer outlives the returned Bytes object. is_owner() returns false.
Bytes object wrapping data.
|
staticnodiscardnoexcept |
Wraps an opaque pointer without associating a byte size.
Sets size == 0 and offset == 0 so that is_ptr() returns true. Useful for passing opaque C handles or iceoryx chunk pointers through the Bytes transport. The caller owns the pointed-to memory; VLink will not free it.
| data | Opaque pointer to wrap. |
Bytes object carrying the pointer.
|
nodiscardnoexcept |
|
inlinenodiscardnoexcept |
Returns the number of usable bytes (excluding the prefix offset region).
|
inlinestaticnodiscardconstexprnoexcept |
Returns the size of the inline stack storage in bytes.
Buffers of this size or smaller use the embedded stack_data_ array and never incur a heap allocation.
kStackSize (96).
|
inlinenodiscardnoexcept |
Reinterprets the backing buffer as a pointer to T.
Calls reinterpret_cast<T*>(real_data()). Use with care; alignment must be compatible with T.
| T | Target type. Defaults to void. |
T, or nullptr if the buffer is empty.
|
nodiscardnoexcept |
|
nodiscardnoexcept |
|
nodiscardnoexcept |
Returns a zero-copy std::string_view over the usable byte region.
The returned view is valid only as long as this Bytes object is alive and unmodified.
string_view pointing into the buffer.
|
staticnodiscardnoexcept |
Decompresses a LZAV-compressed Bytes buffer.
Strips the 4-byte header and footer magic, then calls lzav_decompress(). If check_valid is true the magic bytes are validated first; an invalid magic returns an empty Bytes.
| data | Pointer to the compressed buffer (including header/footer). |
| size | Total size of the compressed buffer. |
| check_valid | If true, validate the header/footer magic before decompressing. Default: true. |
Bytes, or empty on failure.
|
friend |
Stream insertion operator – prints bytes as space-separated hex pairs.
| ostream | Output stream. |
| target | Bytes to print. |
ostream.