[libcamera-devel,v4,20/22] libcamera: base: shared_fd: Add comparison operators
diff mbox series

Message ID 20211130033820.18235-21-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: Introduce UniqueFD
Related show

Commit Message

Laurent Pinchart Nov. 30, 2021, 3:38 a.m. UTC
Add == and != comparison operators between two SharedFD instances, and
use them to replace manuel get() calls.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/base/shared_fd.h | 10 ++++++++++
 src/libcamera/base/shared_fd.cpp   | 26 ++++++++++++++++++++++++++
 src/libcamera/framebuffer.cpp      |  2 +-
 3 files changed, 37 insertions(+), 1 deletion(-)

Comments

Hirokazu Honda Nov. 30, 2021, 5:27 a.m. UTC | #1
Hi Laurent, thank you for the patch.

On Tue, Nov 30, 2021 at 12:39 PM Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> Add == and != comparison operators between two SharedFD instances, and
> use them to replace manuel get() calls.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  include/libcamera/base/shared_fd.h | 10 ++++++++++
>  src/libcamera/base/shared_fd.cpp   | 26 ++++++++++++++++++++++++++
>  src/libcamera/framebuffer.cpp      |  2 +-
>  3 files changed, 37 insertions(+), 1 deletion(-)
>
> diff --git a/include/libcamera/base/shared_fd.h b/include/libcamera/base/shared_fd.h
> index a786885ceb32..1dd51a7f046b 100644
> --- a/include/libcamera/base/shared_fd.h
> +++ b/include/libcamera/base/shared_fd.h
> @@ -46,4 +46,14 @@ private:
>         std::shared_ptr<Descriptor> fd_;
>  };
>
> +static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
> +{
> +       return lhs.get() == rhs.get();
> +}
> +
> +static inline bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
> +{
> +       return !(lhs == rhs);
> +}
> +
>  } /* namespace libcamera */
> diff --git a/src/libcamera/base/shared_fd.cpp b/src/libcamera/base/shared_fd.cpp
> index 05b6892f7e19..56dc579258c7 100644
> --- a/src/libcamera/base/shared_fd.cpp
> +++ b/src/libcamera/base/shared_fd.cpp
> @@ -214,6 +214,32 @@ SharedFD &SharedFD::operator=(SharedFD &&other)
>   * instance is invalid
>   */
>
> +/**
> + * \fn bool operator==(const SharedFD &lhs, const SharedFD &rhs)
> + * \brief Compare the owned file descriptors of two SharedFD for equality
> + * \param[in] lhs The first SharedFD
> + * \param[in] lhs The second SharedFD

rhs

> + *
> + * Two file descriptors are considered equal if they have the same numerical
> + * value. File descriptors with different values that both reference the same
> + * file (for instance obtained using dup()) are considered not equal.
> + *
> + * \return True if the two file descriptors are equal, false otherwise
> + */
> +
> +/**
> + * \fn bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
> + * \brief Compare the owned file descriptors of two SharedFD for equality
> + * \param[in] lhs The first SharedFD
> + * \param[in] lhs The second SharedFD

rhs.

Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
> + *
> + * Two file descriptors are considered equal if they have the same numerical
> + * value. File descriptors with different values that both reference the same
> + * file (for instance obtained using dup()) are considered not equal.
> + *
> + * \return True if the two file descriptors are not equal, false otherwise
> + */
> +
>  /**
>   * \brief Duplicate a SharedFD
>   *
> diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp
> index f035e16859f8..f313566c5561 100644
> --- a/src/libcamera/framebuffer.cpp
> +++ b/src/libcamera/framebuffer.cpp
> @@ -258,7 +258,7 @@ FrameBuffer::FrameBuffer(const std::vector<Plane> &planes, unsigned int cookie)
>                  * Two different dmabuf file descriptors may still refer to the
>                  * same dmabuf instance. Check this using inodes.
>                  */
> -               if (plane.fd.fd() != planes_[0].fd.fd()) {
> +               if (plane.fd != planes_[0].fd) {
>                         if (!inode)
>                                 inode = fileDescriptorInode(planes_[0].fd);
>                         if (fileDescriptorInode(plane.fd) != inode) {
> --
> Regards,
>
> Laurent Pinchart
>
Jacopo Mondi Nov. 30, 2021, 8:09 a.m. UTC | #2
Hi Laurent

On Tue, Nov 30, 2021 at 05:38:18AM +0200, Laurent Pinchart wrote:
> Add == and != comparison operators between two SharedFD instances, and
> use them to replace manuel get() calls.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

This should prbably go after s/SharedFD::fd()/SharedFD::get()/

> ---
>  include/libcamera/base/shared_fd.h | 10 ++++++++++
>  src/libcamera/base/shared_fd.cpp   | 26 ++++++++++++++++++++++++++
>  src/libcamera/framebuffer.cpp      |  2 +-
>  3 files changed, 37 insertions(+), 1 deletion(-)
>
> diff --git a/include/libcamera/base/shared_fd.h b/include/libcamera/base/shared_fd.h
> index a786885ceb32..1dd51a7f046b 100644
> --- a/include/libcamera/base/shared_fd.h
> +++ b/include/libcamera/base/shared_fd.h
> @@ -46,4 +46,14 @@ private:
>  	std::shared_ptr<Descriptor> fd_;
>  };
>
> +static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
> +{
> +	return lhs.get() == rhs.get();
> +}
> +
> +static inline bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
> +{
> +	return !(lhs == rhs);
> +}
> +
>  } /* namespace libcamera */
> diff --git a/src/libcamera/base/shared_fd.cpp b/src/libcamera/base/shared_fd.cpp
> index 05b6892f7e19..56dc579258c7 100644
> --- a/src/libcamera/base/shared_fd.cpp
> +++ b/src/libcamera/base/shared_fd.cpp
> @@ -214,6 +214,32 @@ SharedFD &SharedFD::operator=(SharedFD &&other)
>   * instance is invalid
>   */
>
> +/**
> + * \fn bool operator==(const SharedFD &lhs, const SharedFD &rhs)
> + * \brief Compare the owned file descriptors of two SharedFD for equality
> + * \param[in] lhs The first SharedFD
> + * \param[in] lhs The second SharedFD

rhs

> + *
> + * Two file descriptors are considered equal if they have the same numerical
> + * value. File descriptors with different values that both reference the same
> + * file (for instance obtained using dup()) are considered not equal.
> + *
> + * \return True if the two file descriptors are equal, false otherwise
> + */
> +
> +/**
> + * \fn bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
> + * \brief Compare the owned file descriptors of two SharedFD for equality
> + * \param[in] lhs The first SharedFD
> + * \param[in] lhs The second SharedFD

here too

> + *
> + * Two file descriptors are considered equal if they have the same numerical
> + * value. File descriptors with different values that both reference the same
> + * file (for instance obtained using dup()) are considered not equal.
> + *
> + * \return True if the two file descriptors are not equal, false otherwise
> + */
> +
>  /**
>   * \brief Duplicate a SharedFD
>   *
> diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp
> index f035e16859f8..f313566c5561 100644
> --- a/src/libcamera/framebuffer.cpp
> +++ b/src/libcamera/framebuffer.cpp
> @@ -258,7 +258,7 @@ FrameBuffer::FrameBuffer(const std::vector<Plane> &planes, unsigned int cookie)
>  		 * Two different dmabuf file descriptors may still refer to the
>  		 * same dmabuf instance. Check this using inodes.
>  		 */
> -		if (plane.fd.fd() != planes_[0].fd.fd()) {
> +		if (plane.fd != planes_[0].fd) {
>  			if (!inode)
>  				inode = fileDescriptorInode(planes_[0].fd);
>  			if (fileDescriptorInode(plane.fd) != inode) {
> --
> Regards,
>
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/include/libcamera/base/shared_fd.h b/include/libcamera/base/shared_fd.h
index a786885ceb32..1dd51a7f046b 100644
--- a/include/libcamera/base/shared_fd.h
+++ b/include/libcamera/base/shared_fd.h
@@ -46,4 +46,14 @@  private:
 	std::shared_ptr<Descriptor> fd_;
 };
 
+static inline bool operator==(const SharedFD &lhs, const SharedFD &rhs)
+{
+	return lhs.get() == rhs.get();
+}
+
+static inline bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
+{
+	return !(lhs == rhs);
+}
+
 } /* namespace libcamera */
diff --git a/src/libcamera/base/shared_fd.cpp b/src/libcamera/base/shared_fd.cpp
index 05b6892f7e19..56dc579258c7 100644
--- a/src/libcamera/base/shared_fd.cpp
+++ b/src/libcamera/base/shared_fd.cpp
@@ -214,6 +214,32 @@  SharedFD &SharedFD::operator=(SharedFD &&other)
  * instance is invalid
  */
 
+/**
+ * \fn bool operator==(const SharedFD &lhs, const SharedFD &rhs)
+ * \brief Compare the owned file descriptors of two SharedFD for equality
+ * \param[in] lhs The first SharedFD
+ * \param[in] lhs The second SharedFD
+ *
+ * Two file descriptors are considered equal if they have the same numerical
+ * value. File descriptors with different values that both reference the same
+ * file (for instance obtained using dup()) are considered not equal.
+ *
+ * \return True if the two file descriptors are equal, false otherwise
+ */
+
+/**
+ * \fn bool operator!=(const SharedFD &lhs, const SharedFD &rhs)
+ * \brief Compare the owned file descriptors of two SharedFD for equality
+ * \param[in] lhs The first SharedFD
+ * \param[in] lhs The second SharedFD
+ *
+ * Two file descriptors are considered equal if they have the same numerical
+ * value. File descriptors with different values that both reference the same
+ * file (for instance obtained using dup()) are considered not equal.
+ *
+ * \return True if the two file descriptors are not equal, false otherwise
+ */
+
 /**
  * \brief Duplicate a SharedFD
  *
diff --git a/src/libcamera/framebuffer.cpp b/src/libcamera/framebuffer.cpp
index f035e16859f8..f313566c5561 100644
--- a/src/libcamera/framebuffer.cpp
+++ b/src/libcamera/framebuffer.cpp
@@ -258,7 +258,7 @@  FrameBuffer::FrameBuffer(const std::vector<Plane> &planes, unsigned int cookie)
 		 * Two different dmabuf file descriptors may still refer to the
 		 * same dmabuf instance. Check this using inodes.
 		 */
-		if (plane.fd.fd() != planes_[0].fd.fd()) {
+		if (plane.fd != planes_[0].fd) {
 			if (!inode)
 				inode = fileDescriptorInode(planes_[0].fd);
 			if (fileDescriptorInode(plane.fd) != inode) {