Patch Detail
Show a patch.
GET /api/patches/2310/?format=api
{ "id": 2310, "url": "https://patchwork.libcamera.org/api/patches/2310/?format=api", "web_url": "https://patchwork.libcamera.org/patch/2310/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20191108205409.18845-15-laurent.pinchart@ideasonboard.com>", "date": "2019-11-08T20:53:59", "name": "[libcamera-devel,v2,14/24] libcamera: Add ByteStreamBuffer", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "00626b63543d85ba220a36b935173ee6e2f23f17", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/2310/mbox/", "series": [ { "id": 568, "url": "https://patchwork.libcamera.org/api/series/568/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=568", "date": "2019-11-08T20:53:45", "name": "Control serialization and IPA C API", "version": 2, "mbox": "https://patchwork.libcamera.org/series/568/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/2310/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/2310/checks/", "tags": {}, "headers": { "Return-Path": "<laurent.pinchart@ideasonboard.com>", "Received": [ "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8749E6153B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 8 Nov 2019 21:54:27 +0100 (CET)", "from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2338FA2A;\n\tFri, 8 Nov 2019 21:54:27 +0100 (CET)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1573246467;\n\tbh=82aMwBs2We3GVW0iOAbGs0JATI6AI1mxiO9lrnljP9w=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=AGIt2ic9/3bPsM1cW+9IQQHmpcqjpKzzxd/dXupc9r26dOkF+iUp8vUzVscuukxx1\n\tA4GY5gwwkehxEE+chr+upFoYnjyC+CzLuUT6H7dMLRTd6GonrxBpbZtxjJbJdT6TCy\n\t0IUPGfAfRNbTDz04PSR5GHrBmrTsC2kuJZ7ezb2A=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Fri, 8 Nov 2019 22:53:59 +0200", "Message-Id": "<20191108205409.18845-15-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.23.0", "In-Reply-To": "<20191108205409.18845-1-laurent.pinchart@ideasonboard.com>", "References": "<20191108205409.18845-1-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v2 14/24] libcamera: Add ByteStreamBuffer", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "X-List-Received-Date": "Fri, 08 Nov 2019 20:54:28 -0000" }, "content": "From: Jacopo Mondi <jacopo@jmondi.org>\n\nThe ByteStreamBuffer class wraps a memory area, expected to be allocated\nby the user of the class and provides operations to perform sequential\naccess in read and write modes.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/libcamera/byte_stream_buffer.cpp | 286 +++++++++++++++++++++\n src/libcamera/include/byte_stream_buffer.h | 63 +++++\n src/libcamera/include/meson.build | 1 +\n src/libcamera/meson.build | 1 +\n 4 files changed, 351 insertions(+)\n create mode 100644 src/libcamera/byte_stream_buffer.cpp\n create mode 100644 src/libcamera/include/byte_stream_buffer.h", "diff": "diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp\nnew file mode 100644\nindex 000000000000..23d624dd0a06\n--- /dev/null\n+++ b/src/libcamera/byte_stream_buffer.cpp\n@@ -0,0 +1,286 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * byte_stream_buffer.cpp - Byte stream buffer\n+ */\n+\n+#include \"byte_stream_buffer.h\"\n+\n+#include <stdint.h>\n+#include <string.h>\n+\n+#include \"log.h\"\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Serialization);\n+\n+/**\n+ * \\file byte_stream_buffer.h\n+ * \\brief Managed memory container for serialized data\n+ */\n+\n+/**\n+ * \\class ByteStreamBuffer\n+ * \\brief Wrap a memory buffer and provide sequential data read and write\n+ *\n+ * The ByteStreamBuffer class wraps a memory buffer and exposes sequential read\n+ * and write operation with integrated boundary checks. Access beyond the end\n+ * of the buffer are blocked and logged, allowing error checks to take place at\n+ * the of of access operations instead of at each access. This simplifies\n+ * serialization and deserialization of data.\n+ *\n+ * A byte stream buffer is created with a base memory pointer and a size. If the\n+ * memory pointer is const, the buffer operates in read-only mode, and write\n+ * operations are denied. Otherwise the buffer operates in write-only mode, and\n+ * read operations are denied.\n+ *\n+ * Once a buffer is created, data is read or written with read() and write()\n+ * respectively. Access is strictly sequential, the buffer keeps track of the\n+ * current access location and advances it automatically. Reading or writing\n+ * the same location multiple times is thus not possible. Bytes may also be\n+ * skipped with the skip() method.\n+ *\n+ * The ByteStreamBuffer also supports carving out pieces of memory into other\n+ * ByteStreamBuffer instances. Like a read or write operation, a carveOut()\n+ * advances the internal access location, but allows the carved out memory to\n+ * be accessed at a later time.\n+ *\n+ * All accesses beyond the end of the buffer (read, write, skip or carve out)\n+ * are blocked. The first of such accesses causes a message to be logged, and\n+ * the buffer being marked as having overflown. If the buffer has been carved\n+ * out from a parent buffer, the parent buffer is also marked as having\n+ * overflown. Any later access on an overflown buffer is blocked. The buffer\n+ * overflow status can be checked with the overflow() method.\n+ */\n+\n+/**\n+ * \\brief Construct a read ByteStreamBuffer from the memory area \\a base\n+ * of \\a size\n+ * \\param[in] base The address of the memory area to wrap\n+ * \\param[in] size The size of the memory area to wrap\n+ */\n+ByteStreamBuffer::ByteStreamBuffer(const uint8_t *base, size_t size)\n+\t: parent_(nullptr), base_(base), size_(size), overflow_(false),\n+\t read_(base), write_(nullptr)\n+{\n+}\n+\n+/**\n+ * \\brief Construct a write ByteStreamBuffer from the memory area \\a base\n+ * of \\a size\n+ * \\param[in] base The address of the memory area to wrap\n+ * \\param[in] size The size of the memory area to wrap\n+ */\n+ByteStreamBuffer::ByteStreamBuffer(uint8_t *base, size_t size)\n+\t: parent_(nullptr), base_(base), size_(size), overflow_(false),\n+\t read_(nullptr), write_(base)\n+{\n+}\n+\n+/**\n+ * \\brief Construct a ByteStreamBuffer from the contents of \\a other using move\n+ * semantics\n+ * \\param[in] other The other buffer\n+ *\n+ * After the move construction the \\a other buffer is invalidated. Any attempt\n+ * to access its contents will be considered as an overflow.\n+ */\n+ByteStreamBuffer::ByteStreamBuffer(ByteStreamBuffer &&other)\n+{\n+\t*this = std::move(other);\n+}\n+\n+/**\n+ * \\brief Replace the contents of the buffer with those of \\a other using move\n+ * semantics\n+ * \\param[in] other The other buffer\n+ *\n+ * After the assignment the \\a other buffer is invalidated. Any attempt to\n+ * access its contents will be considered as an overflow.\n+ */\n+ByteStreamBuffer &ByteStreamBuffer::operator=(ByteStreamBuffer &&other)\n+{\n+\tparent_ = other.parent_;\n+\tbase_ = other.base_;\n+\tsize_ = other.size_;\n+\toverflow_ = other.overflow_;\n+\tread_ = other.read_;\n+\twrite_ = other.write_;\n+\n+\tother.parent_ = nullptr;\n+\tother.base_ = nullptr;\n+\tother.size_ = 0;\n+\tother.overflow_ = false;\n+\tother.read_ = nullptr;\n+\tother.write_ = nullptr;\n+\n+\treturn *this;\n+}\n+\n+/**\n+ * \\fn ByteStreamBuffer::base()\n+ * \\brief Retrieve a pointer to the start location of the managed memory buffer\n+ * \\return A pointer to the managed memory buffer\n+ */\n+\n+/**\n+ * \\fn ByteStreamBuffer::offset()\n+ * \\brief Retrieve the offset of the current access location from the base\n+ * \\return The offset in bytes\n+ */\n+\n+/**\n+ * \\fn ByteStreamBuffer::size()\n+ * \\brief Retrieve the size of the managed memory buffer\n+ * \\return The size of managed memory buffer\n+ */\n+\n+/**\n+ * \\fn ByteStreamBuffer::overflow()\n+ * \\brief Check if the buffer has overflown\n+ * \\return True if the buffer has overflow, false otherwise\n+ */\n+\n+void ByteStreamBuffer::setOverflow()\n+{\n+\tif (parent_)\n+\t\tparent_->setOverflow();\n+\n+\toverflow_ = true;\n+}\n+\n+/**\n+ * \\brief Carve out an area of \\a size bytes into a new ByteStreamBuffer\n+ * \\param[in] size The size of the newly created memory buffer\n+ *\n+ * This method carves out an area of \\a size bytes from the buffer into a new\n+ * ByteStreamBuffer, and returns the new buffer. It operates identically to a\n+ * read or write access from the point of view of the current buffer, but allows\n+ * the new buffer to be read or written at a later time after other read or\n+ * write accesses on the current buffer.\n+ *\n+ * \\return A newly created ByteStreamBuffer of \\a size\n+ */\n+ByteStreamBuffer ByteStreamBuffer::carveOut(size_t size)\n+{\n+\tif (!size_ || overflow_)\n+\t\treturn ByteStreamBuffer(static_cast<const uint8_t *>(nullptr), 0);\n+\n+\tconst uint8_t *curr = read_ ? read_ : write_;\n+\tif (curr + size > base_ + size_) {\n+\t\tLOG(Serialization, Error)\n+\t\t\t<< \"Unable to reserve \" << size << \" bytes\";\n+\t\tsetOverflow();\n+\n+\t\treturn ByteStreamBuffer(static_cast<const uint8_t *>(nullptr), 0);\n+\t}\n+\n+\tif (read_) {\n+\t\tByteStreamBuffer b(read_, size);\n+\t\tb.parent_ = this;\n+\t\tread_ += size;\n+\t\treturn b;\n+\t} else {\n+\t\tByteStreamBuffer b(write_, size);\n+\t\tb.parent_ = this;\n+\t\twrite_ += size;\n+\t\treturn b;\n+\t}\n+}\n+\n+/**\n+ * \\brief Skip \\a size bytes from the buffer\n+ * \\param[in] size The number of bytes to skip\n+ *\n+ * This method skips the next \\a size bytes from the buffer.\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ * \\retval -ENOSPC no more space is available in the managed memory buffer\n+ */\n+int ByteStreamBuffer::skip(size_t size)\n+{\n+\tif (overflow_)\n+\t\treturn -ENOSPC;\n+\n+\tconst uint8_t *curr = read_ ? read_ : write_;\n+\tif (curr + size > base_ + size_) {\n+\t\tLOG(Serialization, Error)\n+\t\t\t<< \"Unable to skip \" << size << \" bytes\";\n+\t\tsetOverflow();\n+\n+\t\treturn -ENOSPC;\n+\t}\n+\n+\tif (read_) {\n+\t\tread_ += size;\n+\t} else {\n+\t\tmemset(write_, 0, size);\n+\t\twrite_ += size;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\fn template<typename T> int ByteStreamBuffer::read(T *t)\n+ * \\brief Read \\a size \\a data from the managed memory buffer\n+ * \\param[out] t Pointer to the memory containing the read data\n+ * \\return 0 on success, a negative error code otherwise\n+ * \\retval -EACCES attempting to read from a write buffer\n+ * \\retval -ENOSPC no more space is available in the managed memory buffer\n+ */\n+\n+/**\n+ * \\fn template<typename T> int ByteStreamBuffer::write(const T *t)\n+ * \\brief Write \\a data of \\a size to the managed memory buffer\n+ * \\param[in] t The data to write to memory\n+ * \\return 0 on success, a negative error code otherwise\n+ * \\retval -EACCES attempting to write to a read buffer\n+ * \\retval -ENOSPC no more space is available in the managed memory buffer\n+ */\n+\n+int ByteStreamBuffer::read(uint8_t *data, size_t size)\n+{\n+\tif (!read_)\n+\t\treturn -EACCES;\n+\n+\tif (overflow_)\n+\t\treturn -ENOSPC;\n+\n+\tif (read_ + size > base_ + size_) {\n+\t\tLOG(Serialization, Error)\n+\t\t\t<< \"Unable to read \" << size << \" bytes: out of bounds\";\n+\t\tsetOverflow();\n+\t\treturn -ENOSPC;\n+\t}\n+\n+\tmemcpy(data, read_, size);\n+\tread_ += size;\n+\n+\treturn 0;\n+}\n+\n+int ByteStreamBuffer::write(const uint8_t *data, size_t size)\n+{\n+\tif (!write_)\n+\t\treturn -EACCES;\n+\n+\tif (overflow_)\n+\t\treturn -ENOSPC;\n+\n+\tif (write_ + size > base_ + size_) {\n+\t\tLOG(Serialization, Error)\n+\t\t\t<< \"Unable to write \" << size << \" bytes: no space left\";\n+\t\tsetOverflow();\n+\t\treturn -ENOSPC;\n+\t}\n+\n+\tmemcpy(write_, data, size);\n+\twrite_ += size;\n+\n+\treturn 0;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/include/byte_stream_buffer.h b/src/libcamera/include/byte_stream_buffer.h\nnew file mode 100644\nindex 000000000000..b5274c62b85e\n--- /dev/null\n+++ b/src/libcamera/include/byte_stream_buffer.h\n@@ -0,0 +1,63 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * byte_stream_buffer.h - Byte stream buffer\n+ */\n+#ifndef __LIBCAMERA_BYTE_STREAM_BUFFER_H__\n+#define __LIBCAMERA_BYTE_STREAM_BUFFER_H__\n+\n+#include <stddef.h>\n+#include <stdint.h>\n+\n+namespace libcamera {\n+\n+class ByteStreamBuffer\n+{\n+public:\n+\tByteStreamBuffer(const uint8_t *base, size_t size);\n+\tByteStreamBuffer(uint8_t *base, size_t size);\n+\tByteStreamBuffer(ByteStreamBuffer &&other);\n+\tByteStreamBuffer &operator=(ByteStreamBuffer &&other);\n+\n+\tconst uint8_t *base() const { return base_; }\n+\tuint32_t offset() const { return (write_ ? write_ : read_) - base_; }\n+\tsize_t size() const { return size_; }\n+\tbool overflow() const { return overflow_; }\n+\n+\tByteStreamBuffer carveOut(size_t size);\n+\tint skip(size_t size);\n+\n+\ttemplate<typename T>\n+\tint read(T *t)\n+\t{\n+\t\treturn read(reinterpret_cast<uint8_t *>(t), sizeof(*t));\n+\t}\n+\ttemplate<typename T>\n+\tint write(const T *t)\n+\t{\n+\t\treturn write(reinterpret_cast<const uint8_t *>(t), sizeof(*t));\n+\t}\n+\n+private:\n+\tByteStreamBuffer(const ByteStreamBuffer &other) = delete;\n+\tByteStreamBuffer &operator=(const ByteStreamBuffer &other) = delete;\n+\n+\tvoid setOverflow();\n+\n+\tint read(uint8_t *data, size_t size);\n+\tint write(const uint8_t *data, size_t size);\n+\n+\tByteStreamBuffer *parent_;\n+\n+\tconst uint8_t *base_;\n+\tsize_t size_;\n+\tbool overflow_;\n+\n+\tconst uint8_t *read_;\n+\tuint8_t *write_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_BYTE_STREAM_BUFFER_H__ */\ndiff --git a/src/libcamera/include/meson.build b/src/libcamera/include/meson.build\nindex 64c2155f90cf..1ff0198662cc 100644\n--- a/src/libcamera/include/meson.build\n+++ b/src/libcamera/include/meson.build\n@@ -1,4 +1,5 @@\n libcamera_headers = files([\n+ 'byte_stream_buffer.h',\n 'camera_controls.h',\n 'camera_sensor.h',\n 'control_validator.h',\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex be0cd53f6f10..dab2d8ad2649 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -1,6 +1,7 @@\n libcamera_sources = files([\n 'bound_method.cpp',\n 'buffer.cpp',\n+ 'byte_stream_buffer.cpp',\n 'camera.cpp',\n 'camera_controls.cpp',\n 'camera_manager.cpp',\n", "prefixes": [ "libcamera-devel", "v2", "14/24" ] }