diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2020-05-18 16:08:15 +0300 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2020-05-19 18:07:49 +0300 |
commit | 206fada99d8774fb7a9b4f1924f6caeccafdb9a1 (patch) | |
tree | ce4abd315ff3e6c6770476ade88382fdb50456cd /include | |
parent | a237dbeb7e2238beea7222bafe75f8a0d912c4af (diff) |
libcamera: file_descriptor: Implement move semantics for constructor
The FileDescriptor class, when constructed from a numerical file
descriptor, duplicates the file descriptor and takes ownership of the
copy. The caller has to close the original file descriptor manually if
needed. This is inefficient as the dup() and close() calls could be
avoided, but can also lead to resource leakage, as recently shown by
commit 353fc4c22322 ("libcamera: v4l2_videodevice: Fix dangling file
descriptor").
In an attempt to solve this problem, implement move semantics for the
FileDescriptor constructor. The constructor taking a numerical file
descriptor is split in two variants:
- A "fd copy" constructor that takes a const lvalue reference to a
numerical file descriptor and duplicates it (corresponding to the
current behaviour).
- A "fd move" constructor that takes a rvalue reference to a numerical
file descriptor and takes ownership of it.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Naushir Patuck <naush@raspberrypi.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/libcamera/file_descriptor.h | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/include/libcamera/file_descriptor.h b/include/libcamera/file_descriptor.h index 8612f865..d514aac7 100644 --- a/include/libcamera/file_descriptor.h +++ b/include/libcamera/file_descriptor.h @@ -14,7 +14,8 @@ namespace libcamera { class FileDescriptor final { public: - explicit FileDescriptor(int fd = -1); + explicit FileDescriptor(const int &fd = -1); + explicit FileDescriptor(int &&fd); FileDescriptor(const FileDescriptor &other); FileDescriptor(FileDescriptor &&other); ~FileDescriptor(); @@ -30,7 +31,7 @@ private: class Descriptor { public: - Descriptor(int fd); + Descriptor(int fd, bool duplicate); ~Descriptor(); int fd() const { return fd_; } |