[libcamera-devel] [PATCH 21/23] libcamera: byte_stream_buffer: Support span<>

Jacopo Mondi jacopo at jmondi.org
Mon Jan 13 17:42:43 CET 2020


Add support to write and read span<> of data to the ByteStreamBuffer
class.

Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
---
 src/libcamera/byte_stream_buffer.cpp       | 31 ++++++++++++++++++++--
 src/libcamera/include/byte_stream_buffer.h | 14 ++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp
index 23d624dd0a06..9fb14508722f 100644
--- a/src/libcamera/byte_stream_buffer.cpp
+++ b/src/libcamera/byte_stream_buffer.cpp
@@ -225,22 +225,49 @@ int ByteStreamBuffer::skip(size_t size)
 
 /**
  * \fn template<typename T> int ByteStreamBuffer::read(T *t)
- * \brief Read \a size \a data from the managed memory buffer
+ * \brief Read data from the managed memory buffer into \a t
  * \param[out] t Pointer to the memory containing the read data
  * \return 0 on success, a negative error code otherwise
  * \retval -EACCES attempting to read from a write buffer
  * \retval -ENOSPC no more space is available in the managed memory buffer
  */
 
+/**
+ * \fn template<typename T> int ByteStreamBuffer::read(Span<T> *t)
+ * \brief Read a sequence of data from the managed memory buffer into span \a t
+ * \param[out] t Pointer to an intialized span where to read data
+ *
+ * This function reads data from managed memory and writes it to an initialized
+ * memory location represented by the span \a t.
+ *
+ * The provided span \a t should be initialized with a memory area large
+ * enough to contain the read data. This function does not perform any
+ * memory allocation or initialization in the span \a t. It is responsibility of
+ * the caller to do so.
+ *
+ * \return 0 on success, a negative error code otherwise
+ * \retval -EACCES attempting to read from a write buffer
+ * \retval -ENOSPC no more space is available in the managed memory buffer
+ */
+
 /**
  * \fn template<typename T> int ByteStreamBuffer::write(const T *t)
- * \brief Write \a data of \a size to the managed memory buffer
+ * \brief Write \a t to the managed memory buffer
  * \param[in] t The data to write to memory
  * \return 0 on success, a negative error code otherwise
  * \retval -EACCES attempting to write to a read buffer
  * \retval -ENOSPC no more space is available in the managed memory buffer
  */
 
+/**
+ * \fn template<typename T> int ByteStreamBuffer::write(const Span<T> &t)
+ * \brief Write a span of data \a t to the managed memory buffer
+ * \param[in] t The span of data to write to memory
+ * \return 0 on success, a negative error code otherwise
+ * \retval -EACCES attempting to write to a read buffer
+ * \retval -ENOSPC no more space is available in the managed memory buffer
+ */
+
 int ByteStreamBuffer::read(uint8_t *data, size_t size)
 {
 	if (!read_)
diff --git a/src/libcamera/include/byte_stream_buffer.h b/src/libcamera/include/byte_stream_buffer.h
index b5274c62b85e..2d5267ac06c0 100644
--- a/src/libcamera/include/byte_stream_buffer.h
+++ b/src/libcamera/include/byte_stream_buffer.h
@@ -10,6 +10,8 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include <libcamera/span.h>
+
 namespace libcamera {
 
 class ByteStreamBuffer
@@ -34,10 +36,22 @@ public:
 		return read(reinterpret_cast<uint8_t *>(t), sizeof(*t));
 	}
 	template<typename T>
+	int read(Span<T> *t)
+	{
+		return read(reinterpret_cast<uint8_t *>(t->data()),
+			    sizeof(t->data()[0]) * t->size());
+	}
+	template<typename T>
 	int write(const T *t)
 	{
 		return write(reinterpret_cast<const uint8_t *>(t), sizeof(*t));
 	}
+	template<typename T>
+	int write(const Span<T> &t)
+	{
+		return write(reinterpret_cast<const uint8_t *>(t.data()),
+			     sizeof(t.data()[0]) * t.size());
+	}
 
 private:
 	ByteStreamBuffer(const ByteStreamBuffer &other) = delete;
-- 
2.24.0



More information about the libcamera-devel mailing list