From patchwork Thu Jun 10 07:50:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 12534 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 97A07BD78E for ; Thu, 10 Jun 2021 07:50:39 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6991568936; Thu, 10 Jun 2021 09:50:38 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="IpnhTORn"; dkim-atps=neutral Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A57C6602C5 for ; Thu, 10 Jun 2021 09:50:36 +0200 (CEST) Received: by mail-pf1-x429.google.com with SMTP id u126so904995pfu.13 for ; Thu, 10 Jun 2021 00:50:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VK/FliDppCrFRUYd6LB558bOEJnuwUUhkrgBuRhSYhg=; b=IpnhTORnWQ7xNYJdxDWxfErZyKzUIklfLh+NBVL4saeC+8WlWLtojYYw5Pg5dL9oqI EvzJ9JnCZh2zqIKHux65bL8sdrfWGBAEooaKigcYRCyhiDjFaWpweyuOpC52mIB+R9Kx EoARLxmMJyA6J7NApH3BOqgqw1/dNgv/g7oiE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VK/FliDppCrFRUYd6LB558bOEJnuwUUhkrgBuRhSYhg=; b=cKCYSJeJbro++cE/6ZB1BzDK44NGtt1Z23k0MpXs4Rra+vcPbw23xepkRiCRjwdO8c x8hOs5bORfdi6RWTiErszNLquqi7/ldi0PJwahElEls/KtvmV2fq1iaLp9xFdcSuFoHG qlwcddaHRcEytvTQjUIbCd8TGel6l3M9OFNlygBzQ0AicqaMshP05H7CNXBdHz6f6L40 QInk4fqZVAZSD9monYqus40x03kI1JYJCf5sgy+0mzsTHohm5pfep2tYXYcZ63bKPaAW pxFK3KcfIzusxe38y4vFsTKB7obf1PsbiCoXSlwJ1kzvlylzmG/DJPyNoxvLHsHMuKrA 4dGA== X-Gm-Message-State: AOAM5317xGBsJTGVIAJOWWqjpndshG24WrVjK/e0EmcacTePMvCI7wW8 Q5R/rGXK8ngHF7gut0jKEkH8ialEnP4iqA== X-Google-Smtp-Source: ABdhPJyBF34RPS7l34siTXSNTbWMAvRSu489JUW1rSoRinLZ0ennB9tQeyEzVCRy4Dxczlrmsm9xhg== X-Received: by 2002:a63:ee11:: with SMTP id e17mr3711858pgi.323.1623311434901; Thu, 10 Jun 2021 00:50:34 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:98e0:b356:1c8a:25d4]) by smtp.gmail.com with ESMTPSA id d66sm1565161pfa.32.2021.06.10.00.50.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 00:50:34 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Thu, 10 Jun 2021 16:50:18 +0900 Message-Id: <20210610075027.523672-2-hiroh@chromium.org> X-Mailer: git-send-email 2.32.0.rc1.229.g3e70b5a671-goog In-Reply-To: <20210610075027.523672-1-hiroh@chromium.org> References: <20210610075027.523672-1-hiroh@chromium.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 01/10] libcamera: ScopedFD: Introduce ScopedFD 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" This introduces ScopedFD. It acts like unique_ptr to a file descriptor. Signed-off-by: Hirokazu Honda --- include/libcamera/file_descriptor.h | 3 + include/libcamera/meson.build | 1 + include/libcamera/scoped_fd.h | 36 ++++++++ src/libcamera/file_descriptor.cpp | 20 +++++ src/libcamera/meson.build | 1 + src/libcamera/scoped_fd.cpp | 129 ++++++++++++++++++++++++++++ 6 files changed, 190 insertions(+) create mode 100644 include/libcamera/scoped_fd.h create mode 100644 src/libcamera/scoped_fd.cpp diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h index d514aac7..1d71d58c 100644 --- a/include/libcamera/file_descriptor.h +++ b/include/libcamera/file_descriptor.h @@ -11,11 +11,14 @@ namespace libcamera { +class ScopedFD; + class FileDescriptor final { public: explicit FileDescriptor(const int &fd = -1); explicit FileDescriptor(int &&fd); + explicit FileDescriptor(ScopedFD &&fd); FileDescriptor(const FileDescriptor &other); FileDescriptor(FileDescriptor &&other); ~FileDescriptor(); diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 086c958b..e1c8b253 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -15,6 +15,7 @@ libcamera_public_headers = files([ 'object.h', 'pixel_format.h', 'request.h', + 'scoped_fd.h', 'signal.h', 'span.h', 'stream.h', diff --git a/include/libcamera/scoped_fd.h b/include/libcamera/scoped_fd.h new file mode 100644 index 00000000..d91b53b4 --- /dev/null +++ b/include/libcamera/scoped_fd.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * scoped_fd.h - File descriptor wrapper that owns a file descriptor. + */ +#ifndef __LIBCAMERA_SCOPED_FD_H__ +#define __LIBCAMERA_SCOPED_FD_H__ + +#include +#include + +namespace libcamera { + +class ScopedFD final +{ +public: + explicit ScopedFD(const int fd = -1); + ~ScopedFD(); + ScopedFD(ScopedFD &&other); + ScopedFD &operator=(ScopedFD &&other); + + bool isValid() const { return fd_ == -1; } + int get() const { return fd_; } + void reset(int fd = -1); + __nodiscard int release(); + +private: + int fd_; + + LIBCAMERA_DISABLE_COPY(ScopedFD) +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_SCOPED_FD_H__ */ diff --git a/src/libcamera/file_descriptor.cpp b/src/libcamera/file_descriptor.cpp index 8b505ed3..4d30b757 100644 --- a/src/libcamera/file_descriptor.cpp +++ b/src/libcamera/file_descriptor.cpp @@ -11,6 +11,8 @@ #include #include +#include + #include "libcamera/internal/log.h" /** @@ -108,6 +110,24 @@ FileDescriptor::FileDescriptor(int &&fd) fd = -1; } +/** + * \brief Create a FileDescriptor taking ownership of a given ScopedFD \a fd + * \param[in] fd ScopedFD + * + * Construct a FileDescriptor from ScopedFD by taking ownership of the \a fd. + * The original \a fd becomes invalid. In particular, the caller shall not close + * the original \a fd manually. The duplicated file descriptor will be closed + * automatically when all FileDescriptor instances that reference it are + * destroyed. + * + * If the \a fd is negative, the FileDescriptor is constructed as invalid and + * the fd() method will return -1. + */ +FileDescriptor::FileDescriptor(ScopedFD &&fd) + : FileDescriptor(fd.release()) +{ +} + /** * \brief Copy constructor, create a FileDescriptor from a copy of \a other * \param[in] other The other FileDescriptor diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 7e19a177..ed311acf 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -44,6 +44,7 @@ libcamera_sources = files([ 'process.cpp', 'pub_key.cpp', 'request.cpp', + 'scoped_fd.cpp', 'semaphore.cpp', 'signal.cpp', 'stream.cpp', diff --git a/src/libcamera/scoped_fd.cpp b/src/libcamera/scoped_fd.cpp new file mode 100644 index 00000000..106386dd --- /dev/null +++ b/src/libcamera/scoped_fd.cpp @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2021, Google Inc. + * + * scoped_fd.cpp - File descriptor wrapper that owns a file descriptor. + */ + +#include + +#include + +#include "libcamera/internal/log.h" + +/** + * \file scoped_fd.h + * \brief File descriptor wrapper that owns a file descriptor + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(ScopedFD) + +/** + * \class ScopedFD + * \brief unique_ptr like wrapper for a file descriptor + * + * The ScopedFD provides RAII-style lifetime management of a file descriptor. + * It doesn't allow the shared ownership unlike FileDescriptor. It is + * constructed from a numerical file descriptor and takes over the ownership of + * the file descriptor. When the ScopedFD is destroyed, the managed file + * descriptor is closed. + */ + +/** + * \brief Create a ScopedFD taking over a given \a fd + * \param[in] fd a numerical file descriptor + * + * Construct a ScopedFD from a numerical file descriptor and take ownership of + * the file descriptor. The given file descriptor is automatically closed when + * the ScopedFD is destructed. + */ +ScopedFD::ScopedFD(const int fd) + : fd_(fd >= 0 ? fd : -1) +{ +} + +/** + * \brief Destroy the ScopedFD instance + * + * The owned file descriptor is automatically closed if it is valid. + */ +ScopedFD::~ScopedFD() +{ + reset(); +} + +/** + * \brief Move constructor, create a ScopedFD by taking over \a other + * \param[in] other The other ScopedFD + * + * Create a ScopedFD that takes the ownership of the file descriptor owned by \a + * other. Upon return, the \a other ScopedFD is invalid. + */ +ScopedFD::ScopedFD(ScopedFD &&other) + : fd_(other.release()) +{ +} + +/** + * \brief Move assignment operator, replace a ScopedFD by taking over \a other + * \param[in] other The other ScopedFD + * + * If this ScopedFD has a valid file descriptor, the file descriptor is closed + * first. The file descriptor is then replaced by the one of \a other. Upon + * return, \a other is invalid. + * + * \return A reference to this ScopedFD + */ +ScopedFD &ScopedFD::operator=(ScopedFD &&other) +{ + reset(other.release()); + + return *this; +} + +/** + * \fn ScopedFD::isValid() + * \brief Check if the ScopedFD has a valid file descriptor + * \return True if the ScopedFD has a valid file descriptor, false otherwise + */ + +/** + * \fn ScopedFD::get() + * \brief Retrieve the numerical file descriptor + * \return The numerical file descriptor + */ + +/** + * \fn ScopedFD::reset() + * \brief Swap the owned file descriptor with \a fd. The originally owned file + * descriptor is closed. + * \param[in] fd a numerical file descriptor + */ +void ScopedFD::reset(int fd) +{ + ASSERT(!isValid() || fd != fd_); + if (isValid()) + close(fd_); + fd_ = fd; +} + +/** + * \fn ScopedFD::release() + * \brief Release ownership of the file descriptor without closing it + * + * This function releases and returns the owned file descriptor without closing + * it. The caller owns the returned value and must take care of handling its + * life time to avoid file descriptor leakages. Upon return the ScopedFD is + * invalid. + * + * \return The numerical file descriptor + */ +int ScopedFD::release() +{ + int fd = fd_; + fd_ = -1; + return fd; +} +} /* namespace libcamera */