Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. C/C++
  4. Memory & storage
  5. pw_buf
Loading...
Searching...
No Matches

Overview

Contiguous buffer views with optional memory ownership.

Classes

class  pw::Buf
 
class  pw::ConstBuf
 

Functions

Buf pw::Slice (Buf &&buf, size_t offset, size_t length)
 Slices a mutable Buf by shifting its start address and setting its size.
 
Buf pw::Slice (Buf &&buf, size_t offset)
 
Buf pw::Truncate (Buf &&buf, size_t length)
 Truncates a mutable Buf to a smaller size.
 
Buf pw::Reclaim (Buf &&buf, size_t prefix_count, size_t suffix_count)
 
Buf pw::ReclaimPrefix (Buf &&buf, size_t count)
 
Buf pw::ReclaimSuffix (Buf &&buf, size_t count)
 
UniquePtr< std::byte[]> pw::TakeAllocation (Buf &&buf)
 
ConstBuf pw::Slice (ConstBuf &&const_buf, size_t offset, size_t length)
 
ConstBuf pw::Slice (ConstBuf &&const_buf, size_t offset)
 
ConstBuf pw::Truncate (ConstBuf &&const_buf, size_t length)
 Truncates a ConstBuf to a smaller size.
 
 pw::Buf::Buf (UniquePtr< std::byte[]> &&buffer, size_t offset)
 
 pw::Buf::Buf (UniquePtr< std::byte[]> &&buffer, size_t offset, size_t size)
 Constructs an owned Buf from a UniquePtr, offset, and size.
 
static Buf pw::Buf::Unowned (ByteSpan span, size_t offset, size_t size)
 Creates an unowned Buf from a ByteSpan, offset, and size.
 
static Buf pw::Buf::Unowned (ByteSpan span, size_t offset=0)
 

Function Documentation

◆ Buf()

pw::Buf::Buf ( UniquePtr< std::byte[]> &&  buffer,
size_t  offset 
)
inlineexplicit

Constructs an owned Buf using the remaining length starting from offset.

◆ Reclaim()

Buf pw::Reclaim ( Buf &&  buf,
size_t  prefix_count,
size_t  suffix_count 
)

Reclaims prefix_count bytes at the beginning and suffix_count bytes at the end of the buffer.

Precondition
prefix_count cannot exceed buf.prefix_size(), and suffix_count cannot exceed buf.suffix_size().

◆ ReclaimPrefix()

Buf pw::ReclaimPrefix ( Buf &&  buf,
size_t  count 
)
inline

Reclaims count prefix bytes at the beginning of the buffer.

Precondition
count cannot exceed buf.prefix_size().

◆ ReclaimSuffix()

Buf pw::ReclaimSuffix ( Buf &&  buf,
size_t  count 
)
inline

Reclaims count suffix bytes at the end of the buffer.

Precondition
count cannot exceed buf.suffix_size().

◆ Slice() [1/3]

Buf pw::Slice ( Buf &&  buf,
size_t  offset 
)
inline

Slices a mutable Buf by shifting its start address to the end.

Precondition
offset cannot exceed buf.size().

◆ Slice() [2/3]

ConstBuf pw::Slice ( ConstBuf &&  const_buf,
size_t  offset 
)
inline

Slices a ConstBuf by shifting its start address to the end.

Precondition
offset cannot exceed const_buf.size().

◆ Slice() [3/3]

ConstBuf pw::Slice ( ConstBuf &&  const_buf,
size_t  offset,
size_t  length 
)
inline

Slices a read-only ConstBuf by shifting its start address and setting its size.

◆ TakeAllocation()

UniquePtr< std::byte[]> pw::TakeAllocation ( Buf &&  buf)

Transfers ownership of a Buf's underlying memory allocation to a UniquePtr. The Buf is null after this call.

The returned UniquePtr spans the entire base allocation (base() and base_size()), not only the bytes in the current view. To preserve the view, record prefix_size() and size() before calling TakeAllocation(), then pass them to Buf(UniquePtr<std::byte[]>&&, size_t, size_t).

To take sole responsibility for the memory, call Release() on the returned UniquePtr after saving its deallocator() and size().

If the Buf is null, returns a null UniquePtr.

Precondition
buf must be owned (buf.owned()) or null. An unowned Buf has no allocation to take.

◆ Unowned()

Buf pw::Buf::Unowned ( ByteSpan  span,
size_t  offset = 0 
)
inlinestatic

Creates an unowned Buf using the remaining length starting from offset.