[libcamera-devel,23/31] libcamera: byte_stream_buffer: Add Span<> support

Message ID 20200229164254.23604-24-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Add support for array controls
Related show

Commit Message

Laurent Pinchart Feb. 29, 2020, 4:42 p.m. UTC
From: Jacopo Mondi <jacopo@jmondi.org>

Add support to read and write a Span<> from and to the ByteStreamBuffer
class.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/byte_stream_buffer.cpp       | 18 ++++++++++++++++++
 src/libcamera/include/byte_stream_buffer.h | 17 +++++++++++++++++
 2 files changed, 35 insertions(+)

Comments

Kieran Bingham March 5, 2020, 3:38 p.m. UTC | #1
On 29/02/2020 16:42, Laurent Pinchart wrote:
> From: Jacopo Mondi <jacopo@jmondi.org>
> 
> Add support to read and write a Span<> from and to the ByteStreamBuffer

s/from and to/to and from/

(Yes, I know that matches the 'order' of read and write, but we would
not really say "from and to" in English)

> class.
> 
> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>



> ---
>  src/libcamera/byte_stream_buffer.cpp       | 18 ++++++++++++++++++
>  src/libcamera/include/byte_stream_buffer.h | 17 +++++++++++++++++
>  2 files changed, 35 insertions(+)
> 
> diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp
> index cd1d8a36d464..40380bf0434a 100644
> --- a/src/libcamera/byte_stream_buffer.cpp
> +++ b/src/libcamera/byte_stream_buffer.cpp
> @@ -232,6 +232,15 @@ int ByteStreamBuffer::skip(size_t size)
>   * \retval -ENOSPC no more space is available in the managed memory buffer
>   */
>  
> +/**
> + * \fn template<typename T> int ByteStreamBuffer::read(const Span<T> &data)
> + * \brief Read data from the managed memory buffer into span \a data

s/span/Span/ ?

> + * \param[out] data Span representing the destination memory
> + * \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 t to the managed memory buffer
> @@ -241,6 +250,15 @@ int ByteStreamBuffer::skip(size_t size)
>   * \retval -ENOSPC no more space is available in the managed memory buffer
>   */
>  
> +/**
> + * \fn template<typename T> int ByteStreamBuffer::write(const Span<T> &data)
> + * \brief Write \a data to the managed memory buffer
> + * \param[in] data 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
> + */
> +
>  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..17cb0146061e 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
> @@ -33,12 +35,27 @@ public:
>  	{
>  		return read(reinterpret_cast<uint8_t *>(t), sizeof(*t));
>  	}
> +
> +	template<typename T>
> +	int read(const Span<T> &data)
> +	{
> +		return read(reinterpret_cast<uint8_t *>(data.data()),
> +			    data.size_bytes());
> +	}
> +
>  	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> &data)
> +	{
> +		return write(reinterpret_cast<const uint8_t *>(data.data()),
> +			     data.size_bytes());
> +	}
> +
>  private:
>  	ByteStreamBuffer(const ByteStreamBuffer &other) = delete;
>  	ByteStreamBuffer &operator=(const ByteStreamBuffer &other) = delete;
>
Laurent Pinchart March 6, 2020, 2:50 p.m. UTC | #2
Hi Kieran,

On Thu, Mar 05, 2020 at 03:38:26PM +0000, Kieran Bingham wrote:
> On 29/02/2020 16:42, Laurent Pinchart wrote:
> > From: Jacopo Mondi <jacopo@jmondi.org>
> > 
> > Add support to read and write a Span<> from and to the ByteStreamBuffer
> 
> s/from and to/to and from/
> 
> (Yes, I know that matches the 'order' of read and write, but we would
> not really say "from and to" in English)

I've worked around the issue with "write and read ... to and from" :-)

> > class.
> > 
> > Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> > ---
> >  src/libcamera/byte_stream_buffer.cpp       | 18 ++++++++++++++++++
> >  src/libcamera/include/byte_stream_buffer.h | 17 +++++++++++++++++
> >  2 files changed, 35 insertions(+)
> > 
> > diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp
> > index cd1d8a36d464..40380bf0434a 100644
> > --- a/src/libcamera/byte_stream_buffer.cpp
> > +++ b/src/libcamera/byte_stream_buffer.cpp
> > @@ -232,6 +232,15 @@ int ByteStreamBuffer::skip(size_t size)
> >   * \retval -ENOSPC no more space is available in the managed memory buffer
> >   */
> >  
> > +/**
> > + * \fn template<typename T> int ByteStreamBuffer::read(const Span<T> &data)
> > + * \brief Read data from the managed memory buffer into span \a data
> 
> s/span/Span/ ?

Fixed.

> > + * \param[out] data Span representing the destination memory
> > + * \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 t to the managed memory buffer
> > @@ -241,6 +250,15 @@ int ByteStreamBuffer::skip(size_t size)
> >   * \retval -ENOSPC no more space is available in the managed memory buffer
> >   */
> >  
> > +/**
> > + * \fn template<typename T> int ByteStreamBuffer::write(const Span<T> &data)
> > + * \brief Write \a data to the managed memory buffer
> > + * \param[in] data 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
> > + */
> > +
> >  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..17cb0146061e 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
> > @@ -33,12 +35,27 @@ public:
> >  	{
> >  		return read(reinterpret_cast<uint8_t *>(t), sizeof(*t));
> >  	}
> > +
> > +	template<typename T>
> > +	int read(const Span<T> &data)
> > +	{
> > +		return read(reinterpret_cast<uint8_t *>(data.data()),
> > +			    data.size_bytes());
> > +	}
> > +
> >  	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> &data)
> > +	{
> > +		return write(reinterpret_cast<const uint8_t *>(data.data()),
> > +			     data.size_bytes());
> > +	}
> > +
> >  private:
> >  	ByteStreamBuffer(const ByteStreamBuffer &other) = delete;
> >  	ByteStreamBuffer &operator=(const ByteStreamBuffer &other) = delete;
> >

Patch

diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp
index cd1d8a36d464..40380bf0434a 100644
--- a/src/libcamera/byte_stream_buffer.cpp
+++ b/src/libcamera/byte_stream_buffer.cpp
@@ -232,6 +232,15 @@  int ByteStreamBuffer::skip(size_t size)
  * \retval -ENOSPC no more space is available in the managed memory buffer
  */
 
+/**
+ * \fn template<typename T> int ByteStreamBuffer::read(const Span<T> &data)
+ * \brief Read data from the managed memory buffer into span \a data
+ * \param[out] data Span representing the destination memory
+ * \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 t to the managed memory buffer
@@ -241,6 +250,15 @@  int ByteStreamBuffer::skip(size_t size)
  * \retval -ENOSPC no more space is available in the managed memory buffer
  */
 
+/**
+ * \fn template<typename T> int ByteStreamBuffer::write(const Span<T> &data)
+ * \brief Write \a data to the managed memory buffer
+ * \param[in] data 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
+ */
+
 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..17cb0146061e 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
@@ -33,12 +35,27 @@  public:
 	{
 		return read(reinterpret_cast<uint8_t *>(t), sizeof(*t));
 	}
+
+	template<typename T>
+	int read(const Span<T> &data)
+	{
+		return read(reinterpret_cast<uint8_t *>(data.data()),
+			    data.size_bytes());
+	}
+
 	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> &data)
+	{
+		return write(reinterpret_cast<const uint8_t *>(data.data()),
+			     data.size_bytes());
+	}
+
 private:
 	ByteStreamBuffer(const ByteStreamBuffer &other) = delete;
 	ByteStreamBuffer &operator=(const ByteStreamBuffer &other) = delete;