simple_span.h#

namespace ONNX_LIGHT_NAMESPACE
namespace utils
class ByteSpan : public ONNX_LIGHT_NAMESPACE::utils::Span#
#include <simple_span.h>

A byte buffer that can either own its data or borrow a non-owning view into an external buffer. Inherits the non-owning-view interface from Span and overrides all read accessors so they always return correct data in both modes.

The borrowed mode is used for zero-copy parsing: when ParseOptions::no_copy is true and the stream supports it, tensor raw data is not copied — instead assign_borrowed() sets the base-class ptr_/size_ to point directly into the source bytes buffer. The caller MUST keep that buffer alive for as long as the ByteSpan is in borrowed mode.

The aligned-owned mode is used when ParseOptions::alignment > 0: the buffer is over-allocated by (alignment - 1) bytes and ptr_/size_ are set to the aligned region within owned_. In this mode mutable data() returns the aligned pointer and size() returns the logical (not allocated) size.

An explicit borrowed_ flag is used to unambiguously track the storage mode, including degenerate edge cases such as a zero-length borrowed span.

In plain owned mode the class behaves like a growable byte buffer; all read accessors delegate to owned_.data() / owned_.size() so there is no risk of stale cached pointers when owned_ reallocates.

Public Functions

ByteSpan() = default#

Constructs an empty buffer (owned mode, no allocation).

inline ByteSpan(const std::vector<uint8_t> &v)#

Constructs an owned buffer by copying from a std::vector<uint8_t>.

inline ByteSpan(const ByteSpan &other)#

Copy constructor: handles aligned-owned mode by recomputing the internal pointer.

inline ByteSpan &operator=(const ByteSpan &other)#

Copy assignment: handles aligned-owned mode by recomputing the internal pointer.

inline ByteSpan(ByteSpan &&other) noexcept#

Move constructor: for aligned-owned mode the pointer remains valid after the move.

inline ByteSpan &operator=(ByteSpan &&other) noexcept#

Move assignment: for aligned-owned mode the pointer remains valid after the move.

inline ByteSpan &operator=(const std::vector<uint8_t> &v)#

Assigns owned data by copying from a std::vector<uint8_t>; clears all special modes.

inline bool is_borrowed() const#

Returns true when the data is borrowed (non-owning).

inline bool is_aligned_owned() const#

Returns true when the data is in aligned-owned mode.

inline size_t size() const#

Returns the number of bytes in either storage mode.

inline bool empty() const#

Returns true when no data is stored (zero bytes in either mode).

inline const uint8_t *data() const#

Returns a const pointer to the byte data in either storage mode.

inline const uint8_t &operator[](size_t i) const#

Returns a const reference to the byte at index i (no bounds check).

inline bool operator==(const ByteSpan &other) const#

Returns true when both ByteSpans have the same size and byte content.

inline bool operator!=(const ByteSpan &other) const#

Returns true when the ByteSpans differ in size or content.

inline uint8_t *data()#

Returns a mutable pointer to the owned data. Calling this in borrowed mode raises an error at runtime.

inline uint8_t &operator[](size_t i)#

Returns a mutable reference to the byte at index i; valid only in owned mode. No bounds check is performed; behaviour is undefined for i >= size().

inline void resize(size_t n)#

Resizes the owned buffer to n bytes and switches to plain owned mode.

inline void resize_aligned(size_t n, size_t align)#

Resizes the owned buffer ensuring data() is aligned to align bytes. Over-allocates by (align - 1) bytes so std::align can find a suitable start. If align <= 1 or n == 0, falls back to plain resize(n). The caller may read/write the data via data() as usual; size() returns n.

inline void assign_borrowed(const uint8_t *ptr, size_t sz, std::shared_ptr<void> owner = {})#

Sets borrowed mode: stores ptr/size in the base-class Span fields without any copy. The pointed-to buffer MUST outlive this ByteSpan.

inline void clear()#

Clears all data and resets to the empty owned state.

inline void push_back(uint8_t v)#

Appends a single byte; switches to owned mode (copying any borrowed/aligned data first).

Private Members

OwnedByteBuffer owned_#
bool borrowed_ = false#
bool aligned_owned_ = false#
size_t align_ = 0#

Stored alignment for aligned-owned mode; used to re-align on copy.

std::shared_ptr<void> owner_#

Keeps borrowed backing storage alive when the model owns the shared buffer.

class OwnedByteBuffer#
#include <simple_span.h>

Owns a contiguous byte buffer without value-initializing newly allocated bytes. It preserves the existing prefix on resize and grows geometrically for append-heavy use.

Public Functions

OwnedByteBuffer() = default#

Initializes an empty buffer.

inline OwnedByteBuffer(const OwnedByteBuffer &other)#

Creates a deep copy of the stored bytes.

inline OwnedByteBuffer &operator=(const OwnedByteBuffer &other)#

Replaces the buffer with a deep copy of the stored bytes.

inline OwnedByteBuffer(OwnedByteBuffer &&other) noexcept#

Transfers ownership of the stored bytes and empties the source buffer.

inline OwnedByteBuffer &operator=(OwnedByteBuffer &&other) noexcept#

Transfers ownership of the stored bytes and empties the source buffer.

inline OwnedByteBuffer(const std::vector<uint8_t> &v)#

Creates a buffer by copying bytes from a vector.

inline OwnedByteBuffer &operator=(const std::vector<uint8_t> &v)#

Replaces the buffer content by copying bytes from a vector.

inline size_t size() const#

Returns the number of stored bytes.

inline uint8_t *data()#

Returns a mutable pointer to the stored bytes.

inline const uint8_t *data() const#

Returns a const pointer to the stored bytes.

inline void clear()#

Clears the logical content while preserving capacity.

inline void resize(size_t n)#

Resizes the buffer without value-initializing newly exposed bytes.

inline void assign(const uint8_t *src, size_t n)#

Replaces the content by copying raw bytes.

inline void assign(const uint8_t *begin, const uint8_t *end)#

Replaces the content by copying a byte range.

inline void push_back(uint8_t v)#

Appends one byte, growing geometrically when needed.

Private Functions

inline void reserve(size_t n)#

Ensures the buffer can store at least n bytes.

Private Members

std::unique_ptr<uint8_t[]> storage_#
size_t size_ = 0#
size_t capacity_ = 0#
class Span#
#include <simple_span.h>

Non-owning, read-only view of a contiguous byte sequence. It references existing memory and never allocates or frees storage. Analogous to RefString but for raw bytes.

Subclassed by ONNX_LIGHT_NAMESPACE::utils::ByteSpan

Public Functions

inline Span() = default#

Initializes an empty span.

inline Span(const uint8_t *ptr, size_t size)#

Initializes a span from a pointer and a size.

inline size_t size() const#

Returns the number of bytes in the span.

inline const uint8_t *data() const#

Returns a const pointer to the byte data.

inline bool empty() const#

Returns true when the span covers zero bytes.

inline const uint8_t &operator[](size_t i) const#

Returns a const reference to the byte at index i (no bounds check).

inline bool operator==(const Span &other) const#

Returns true when both spans have the same size and byte content.

inline bool operator!=(const Span &other) const#

Returns true when the spans differ in size or content.

Protected Attributes

const uint8_t *ptr_ = nullptr#
size_t size_ = 0#