Loading base/include/android-base/unique_fd.h +19 −15 Original line number Diff line number Diff line Loading @@ -21,18 +21,18 @@ #include <android-base/macros.h> /* Container for a file descriptor that automatically closes the descriptor as * it goes out of scope. * * unique_fd ufd(open("/some/path", "r")); * * if (ufd.get() < 0) // invalid descriptor * return error; * * // Do something useful * * return 0; // descriptor is closed here */ // Container for a file descriptor that automatically closes the descriptor as // it goes out of scope. // // unique_fd ufd(open("/some/path", "r")); // if (ufd.get() == -1) return error; // // // Do something useful, possibly including 'return'. // // return 0; // Descriptor is closed for you. // // unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help // you find this class if you're searching for one of those names. namespace android { namespace base { Loading @@ -50,8 +50,12 @@ class unique_fd final { } void reset(int new_value) { if (value_ >= 0) if (value_ != -1) { // Even if close(2) fails with EINTR, the fd will have been closed. // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html close(value_); } value_ = new_value; } Loading @@ -61,7 +65,7 @@ class unique_fd final { int get() const { return value_; } int release() { int release() __attribute__((warn_unused_result)) { int ret = value_; value_ = -1; return ret; Loading Loading
base/include/android-base/unique_fd.h +19 −15 Original line number Diff line number Diff line Loading @@ -21,18 +21,18 @@ #include <android-base/macros.h> /* Container for a file descriptor that automatically closes the descriptor as * it goes out of scope. * * unique_fd ufd(open("/some/path", "r")); * * if (ufd.get() < 0) // invalid descriptor * return error; * * // Do something useful * * return 0; // descriptor is closed here */ // Container for a file descriptor that automatically closes the descriptor as // it goes out of scope. // // unique_fd ufd(open("/some/path", "r")); // if (ufd.get() == -1) return error; // // // Do something useful, possibly including 'return'. // // return 0; // Descriptor is closed for you. // // unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help // you find this class if you're searching for one of those names. namespace android { namespace base { Loading @@ -50,8 +50,12 @@ class unique_fd final { } void reset(int new_value) { if (value_ >= 0) if (value_ != -1) { // Even if close(2) fails with EINTR, the fd will have been closed. // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html close(value_); } value_ = new_value; } Loading @@ -61,7 +65,7 @@ class unique_fd final { int get() const { return value_; } int release() { int release() __attribute__((warn_unused_result)) { int ret = value_; value_ = -1; return ret; Loading