[libcamera-devel] [PATCH 04/30] libcamera: buffer: Add FileDecriptor to help deal with file descriptors
Jacopo Mondi
jacopo at jmondi.org
Wed Nov 27 11:52:04 CET 2019
Hi Niklas,
On Wed, Nov 27, 2019 at 12:35:54AM +0100, Niklas Söderlund wrote:
> Add a helper to make it easier to pass file descriptors around. The
> helper class duplicates the fd which decouples it from the original fd
> which could be closed by its owner while the new FileDescriptor remains
> valid.
>
> Signed-off-by: Niklas Söderlund <niklas.soderlund at ragnatech.se>
> ---
> include/libcamera/buffer.h | 12 ++++++++++++
> src/libcamera/buffer.cpp | 40 ++++++++++++++++++++++++++++++++++++++
> 2 files changed, 52 insertions(+)
>
> diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h
> index 7302b9f32ce8c50d..33793b4ccf881eda 100644
> --- a/include/libcamera/buffer.h
> +++ b/include/libcamera/buffer.h
> @@ -46,6 +46,18 @@ private:
> std::vector<Plane> planes_;
> };
>
> +class FileDescriptor final
> +{
> +public:
> + FileDescriptor(int fd);
> + ~FileDescriptor();
> +
> + int fd() const { return fd_; }
> +
> +private:
> + int fd_;
> +};
> +
> class Plane final
> {
> public:
> diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp
> index 4acff216cafba484..0676586ae3be2a61 100644
> --- a/src/libcamera/buffer.cpp
> +++ b/src/libcamera/buffer.cpp
> @@ -115,6 +115,46 @@ void BufferInfo::update(Status status, unsigned int sequence,
> * \return A array holding all plane information for the buffer
> */
>
> +/**
> + * \class FileDescriptor
> + * \brief Life time management of a file descriptor
> + *
> + * The managed file descriptor (fd) is duplicated from the original one and
> + * is opened for the life time of the FileDescriptor object disregarding
s/is opened/stays opened/ ?
> + * if the original one is closed.
> + */
> +
> +/**
> + * \brief Create a life time managed file descriptor
> + * \param[in] fd File descriptor
> + */
> +FileDescriptor::FileDescriptor(int fd)
> + : fd_(-1)
> +{
> + if (fd < 0)
> + return;
If one has gone so fare as providing an invalid fd to the constructor,
probably has missed some error check, I would LOG() an error here (or
even Fatal ? )
> +
> + /* Failing to dup() a fd should not happen and is fatal. */
> + fd_ = dup(fd);
> + if (fd_ == -1) {
> + int ret = -errno;
> + LOG(Buffer, Fatal)
> + << "Failed to dup() fd: " << strerror(-ret);
> + }
> +}
> +
> +FileDescriptor::~FileDescriptor()
> +{
> + if (fd_ != -1)
> + close(fd_);
> +}
> +
> +/**
> + * \fn FileDescriptor::fd()
> + * \brief Retrieve the file descriptor or
or.. ?
> + * \return A valid file descriptor or a negative error code
Not a negative error code, but -1 if the instance has been created
with an invalid file descriptor. How to put it nicely here, well...
not sure :)
Wording apart:
Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>
Thanks
j
> + */
> +
> /**
> * \class Plane
> * \brief A memory region to store a single plane of a frame
> --
> 2.24.0
>
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel at lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.libcamera.org/pipermail/libcamera-devel/attachments/20191127/33565e23/attachment.sig>
More information about the libcamera-devel
mailing list