From patchwork Fri Mar 6 15:59:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2997 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1F9CF628B7 for ; Fri, 6 Mar 2020 17:00:26 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B08A824B for ; Fri, 6 Mar 2020 17:00:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1583510425; bh=IB2SdpK9t3+O0qL+nuKVMs4Yb2mP1wIBYwMpFTN+us8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=YEbbRE5aG1gkR9YKCgGp2Xspz1rCcktGU2WcsTv6UTifs+q9ItauegMR+SOmKXRRk zvExdlaiQ0BDWCutQ94xMa+NQhcyh3F9IdoD7e5Ejedl/VtLC7n2kMpGv+EmFD2bPQ x/eokJv8XRDVsKvMUVKPs+7LzVjScJ764l3HgkBQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Fri, 6 Mar 2020 17:59:55 +0200 Message-Id: <20200306160002.30549-26-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200306160002.30549-1-laurent.pinchart@ideasonboard.com> References: <20200306160002.30549-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 25/32] libcamera: byte_stream_buffer: Add zero-copy read() variant X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Mar 2020 16:00:27 -0000 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 Reviewed-by: Kieran Bingham --- src/libcamera/byte_stream_buffer.cpp | 39 ++++++++++++++++++++++ src/libcamera/include/byte_stream_buffer.h | 9 +++++ 2 files changed, 48 insertions(+) diff --git a/src/libcamera/byte_stream_buffer.cpp b/src/libcamera/byte_stream_buffer.cpp index 17e710c4887e..20d6a6552c5f 100644 --- a/src/libcamera/byte_stream_buffer.cpp +++ b/src/libcamera/byte_stream_buffer.cpp @@ -241,6 +241,19 @@ int ByteStreamBuffer::skip(size_t size) * \retval -ENOSPC no more space is available in the managed memory buffer */ +/** + * \fn template 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 int ByteStreamBuffer::write(const T *t) * \brief Write \a t to the managed memory buffer @@ -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 17cb0146061e..b3aaa8b9fb28 100644 --- a/src/libcamera/include/byte_stream_buffer.h +++ b/src/libcamera/include/byte_stream_buffer.h @@ -9,6 +9,7 @@ #include #include +#include #include @@ -43,6 +44,13 @@ public: data.size_bytes()); } + template + const std::remove_reference_t *read(size_t count = 1) + { + using return_type = const std::remove_reference_t *; + return reinterpret_cast(read(sizeof(T), count)); + } + template int write(const T *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_;