summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-02-29 01:42:01 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2020-03-06 18:10:44 +0200
commit7f2da874cdacf2870c7a55cd1bdf34a01b01787d (patch)
treeee497d48be18cfee4a9cf9d4d9c6d4a8d7cfb67f
parent8a1f0321dc031a34e5fd19bfeefd8e95f9a6b683 (diff)
libcamera: byte_stream_buffer: Add zero-copy read() variant
Add a read() function to ByteStreamBuffer that returns a pointer to the data instead of copying it. Overflow check is still handled by the class, but the caller must check the returned pointer explicitly. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--src/libcamera/byte_stream_buffer.cpp39
-rw-r--r--src/libcamera/include/byte_stream_buffer.h9
2 files changed, 48 insertions, 0 deletions
diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp
index 17e710c4..20d6a655 100644
--- a/src/libcamera/byte_stream_buffer.cpp
+++ b/src/libcamera/byte_stream_buffer.cpp
@@ -242,6 +242,19 @@ int ByteStreamBuffer::skip(size_t size)
*/
/**
+ * \fn template<typename T> const T *ByteStreamBuffer::read(size_t count)
+ * \brief Read data from the managed memory buffer without performing a copy
+ * \param[in] count Number of data items to read
+ *
+ * This function reads \a count elements of type \a 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.
+ *
+ * \return A pointer to the data on success, or nullptr otherwise
+ */
+
+/**
* \fn template<typename T> int ByteStreamBuffer::write(const T *t)
* \brief Write \a t to the managed memory buffer
* \param[in] t The data to write to memory
@@ -259,6 +272,32 @@ int ByteStreamBuffer::skip(size_t size)
* \retval -ENOSPC no more space is available in the managed memory buffer
*/
+const uint8_t *ByteStreamBuffer::read(size_t size, size_t count)
+{
+ if (!read_)
+ return nullptr;
+
+ if (overflow_)
+ return nullptr;
+
+ size_t bytes;
+ if (__builtin_mul_overflow(size, count, &bytes)) {
+ setOverflow();
+ return nullptr;
+ }
+
+ if (read_ + bytes > base_ + size_) {
+ LOG(Serialization, Error)
+ << "Unable to read " << bytes << " bytes: out of bounds";
+ setOverflow();
+ return nullptr;
+ }
+
+ const uint8_t *data = read_;
+ read_ += bytes;
+ return data;
+}
+
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 17cb0146..b3aaa8b9 100644
--- a/src/libcamera/include/byte_stream_buffer.h
+++ b/src/libcamera/include/byte_stream_buffer.h
@@ -9,6 +9,7 @@
#include <stddef.h>
#include <stdint.h>
+#include <type_traits>
#include <libcamera/span.h>
@@ -44,6 +45,13 @@ public:
}
template<typename T>
+ const std::remove_reference_t<T> *read(size_t count = 1)
+ {
+ using return_type = const std::remove_reference_t<T> *;
+ return reinterpret_cast<return_type>(read(sizeof(T), count));
+ }
+
+ template<typename T>
int write(const T *t)
{
return write(reinterpret_cast<const uint8_t *>(t), sizeof(*t));
@@ -63,6 +71,7 @@ private:
void setOverflow();
int read(uint8_t *data, size_t size);
+ const uint8_t *read(size_t size, size_t count);
int write(const uint8_t *data, size_t size);
ByteStreamBuffer *parent_;
class="hl opt">(ret || wbuf.offset() != 4 || wbuf.overflow() || *reinterpret_cast<uint32_t *>(data.data()) != 0x12345678) { cerr << "Write failed on write buffer" << endl; return TestFail; } /* Test write carve out. */ ByteStreamBuffer wco = wbuf.carveOut(10); if (wco.base() != wbuf.base() + 4 || wco.size() != 10 || wco.offset() != 0 || wco.overflow() || wbuf.offset() != 14 || wbuf.overflow()) { cerr << "Carving out write buffer failed" << endl; return TestFail; } /* Test write on the carved out buffer. */ value = 0x87654321; ret = wco.write(&value); if (ret || wco.offset() != 4 || wco.overflow() || *reinterpret_cast<uint32_t *>(data.data() + 4) != 0x87654321) { cerr << "Write failed on carve out buffer" << endl; return TestFail; } if (wbuf.offset() != 14 || wbuf.overflow()) { cerr << "Write on carve out buffer modified write buffer" << endl; return TestFail; } /* Test read, this should fail. */ ret = wbuf.read(&value); if (!ret || wbuf.overflow()) { cerr << "Read should fail on write buffer" << endl; return TestFail; } /* Test overflow on carved out buffer. */ for (i = 0; i < 2; ++i) { ret = wco.write(&value); if (ret < 0) break; } if (i != 1 || !wco.overflow() || !wbuf.overflow()) { cerr << "Write on carve out buffer failed to overflow" << endl; return TestFail; } /* Test reinitialization of the buffer. */ wbuf = ByteStreamBuffer(data.data(), data.size()); if (wbuf.overflow() || wbuf.base() != data.data() || wbuf.offset() != 0) { cerr << "Write buffer reinitialization failed" << endl; return TestFail; } /* * Read mode. */ ByteStreamBuffer rbuf(const_cast<const uint8_t *>(data.data()), data.size()); if (rbuf.base() != data.data() || rbuf.size() != data.size() || rbuf.offset() != 0 || rbuf.overflow()) { cerr << "Read buffer incorrectly constructed" << endl; return TestFail; } /* Test read. */ value = 0; ret = rbuf.read(&value); if (ret || rbuf.offset() != 4 || rbuf.overflow() || value != 0x12345678) { cerr << "Write failed on write buffer" << endl; return TestFail; } /* Test read carve out. */ ByteStreamBuffer rco = rbuf.carveOut(10); if (rco.base() != rbuf.base() + 4 || rco.size() != 10 || rco.offset() != 0 || rco.overflow() || rbuf.offset() != 14 || rbuf.overflow()) { cerr << "Carving out read buffer failed" << endl; return TestFail; } /* Test read on the carved out buffer. */ value = 0; ret = rco.read(&value); if (ret || rco.offset() != 4 || rco.overflow() || value != 0x87654321) { cerr << "Read failed on carve out buffer" << endl; return TestFail; } if (rbuf.offset() != 14 || rbuf.overflow()) { cerr << "Read on carve out buffer modified read buffer" << endl; return TestFail; }