mirror of
https://git.libcamera.org/libcamera/libcamera.git
synced 2025-07-23 08:35:07 +03:00
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 353fc4c223
("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>
This commit is contained in:
parent
a237dbeb7e
commit
206fada99d
2 changed files with 66 additions and 22 deletions
|
@ -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_; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue