libcamera
v0.3.2+116-83c5ad0f
Supporting cameras in Linux since 2019
|
Wrap a memory buffer and provide sequential data read and write. More...
Public Member Functions | |
ByteStreamBuffer (const uint8_t *base, size_t size) | |
Construct a read ByteStreamBuffer from the memory area base of size. More... | |
ByteStreamBuffer (uint8_t *base, size_t size) | |
Construct a write ByteStreamBuffer from the memory area base of size. More... | |
ByteStreamBuffer (ByteStreamBuffer &&other) | |
Construct a ByteStreamBuffer from the contents of other using move semantics. More... | |
ByteStreamBuffer & | operator= (ByteStreamBuffer &&other) |
Replace the contents of the buffer with those of other using move semantics. More... | |
const uint8_t * | base () const |
Retrieve a pointer to the start location of the managed memory buffer. More... | |
uint32_t | offset () const |
Retrieve the offset of the current access location from the base. More... | |
size_t | size () const |
Retrieve the size of the managed memory buffer. More... | |
bool | overflow () const |
Check if the buffer has overflown. More... | |
ByteStreamBuffer | carveOut (size_t size) |
Carve out an area of size bytes into a new ByteStreamBuffer. More... | |
int | skip (size_t size) |
Skip size bytes from the buffer. More... | |
template<typename T > | |
int | read (T *t) |
Read data from the managed memory buffer into t. More... | |
template<typename T > | |
int | read (const Span< T > &data) |
Read data from the managed memory buffer into Span data. More... | |
template<typename T > | |
const std::remove_reference_t< T > * | read (size_t count=1) |
Read data from the managed memory buffer without performing a copy. More... | |
template<typename T > | |
int | write (const T *t) |
Write t to the managed memory buffer. More... | |
template<typename T > | |
int | write (const Span< T > &data) |
Write data to the managed memory buffer. More... | |
Wrap a memory buffer and provide sequential data read and write.
The ByteStreamBuffer class wraps a memory buffer and exposes sequential read and write operation with integrated boundary checks. Access beyond the end of the buffer are blocked and logged, allowing error checks to take place at the of of access operations instead of at each access. This simplifies serialization and deserialization of data.
A byte stream buffer is created with a base memory pointer and a size. If the memory pointer is const, the buffer operates in read-only mode, and write operations are denied. Otherwise the buffer operates in write-only mode, and read operations are denied.
Once a buffer is created, data is read or written with read() and write() respectively. Access is strictly sequential, the buffer keeps track of the current access location and advances it automatically. Reading or writing the same location multiple times is thus not possible. Bytes may also be skipped with the skip() function.
The ByteStreamBuffer also supports carving out pieces of memory into other ByteStreamBuffer instances. Like a read or write operation, a carveOut() advances the internal access location, but allows the carved out memory to be accessed at a later time.
All accesses beyond the end of the buffer (read, write, skip or carve out) are blocked. The first of such accesses causes a message to be logged, and the buffer being marked as having overflown. If the buffer has been carved out from a parent buffer, the parent buffer is also marked as having overflown. Any later access on an overflown buffer is blocked. The buffer overflow status can be checked with the overflow() function.
libcamera::ByteStreamBuffer::ByteStreamBuffer | ( | const uint8_t * | base, |
size_t | size | ||
) |
Construct a read ByteStreamBuffer from the memory area base of size.
[in] | base | The address of the memory area to wrap |
[in] | size | The size of the memory area to wrap |
libcamera::ByteStreamBuffer::ByteStreamBuffer | ( | uint8_t * | base, |
size_t | size | ||
) |
Construct a write ByteStreamBuffer from the memory area base of size.
[in] | base | The address of the memory area to wrap |
[in] | size | The size of the memory area to wrap |
libcamera::ByteStreamBuffer::ByteStreamBuffer | ( | ByteStreamBuffer && | other | ) |
Construct a ByteStreamBuffer from the contents of other using move semantics.
[in] | other | The other buffer |
After the move construction the other buffer is invalidated. Any attempt to access its contents will be considered as an overflow.
|
inline |
Retrieve a pointer to the start location of the managed memory buffer.
ByteStreamBuffer libcamera::ByteStreamBuffer::carveOut | ( | size_t | size | ) |
Carve out an area of size bytes into a new ByteStreamBuffer.
[in] | size | The size of the newly created memory buffer |
This function carves out an area of size bytes from the buffer into a new ByteStreamBuffer, and returns the new buffer. It operates identically to a read or write access from the point of view of the current buffer, but allows the new buffer to be read or written at a later time after other read or write accesses on the current buffer.
|
inline |
Retrieve the offset of the current access location from the base.
ByteStreamBuffer & libcamera::ByteStreamBuffer::operator= | ( | ByteStreamBuffer && | other | ) |
Replace the contents of the buffer with those of other using move semantics.
[in] | other | The other buffer |
After the assignment the other buffer is invalidated. Any attempt to access its contents will be considered as an overflow.
|
inline |
Check if the buffer has overflown.
|
inline |
Read data from the managed memory buffer into t.
[out] | t | Pointer to the memory containing the read data |
-EACCES | attempting to read from a write buffer |
-ENOSPC | no more space is available in the managed memory buffer |
|
inline |
Read data from the managed memory buffer into Span data.
[out] | data | Span representing the destination memory |
-EACCES | attempting to read from a write buffer |
-ENOSPC | no more space is available in the managed memory buffer |
|
inline |
Read data from the managed memory buffer without performing a copy.
[in] | count | Number of data items to read |
This function reads count elements of type T from the buffer. Unlike the other read variants, it doesn't copy the data but returns a pointer to the first element. If data can't be read for any reason (usually due to reading more data than available), the function returns nullptr.
|
inline |
Retrieve the size of the managed memory buffer.
int libcamera::ByteStreamBuffer::skip | ( | size_t | size | ) |
Skip size bytes from the buffer.
[in] | size | The number of bytes to skip |
This function skips the next size bytes from the buffer.
-ENOSPC | no more space is available in the managed memory buffer |
|
inline |
Write t to the managed memory buffer.
[in] | t | The data to write to memory |
-EACCES | attempting to write to a read buffer |
-ENOSPC | no more space is available in the managed memory buffer |
|
inline |
Write data to the managed memory buffer.
[in] | data | The data to write to memory |
-EACCES | attempting to write to a read buffer |
-ENOSPC | no more space is available in the managed memory buffer |