123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #ifndef BASE_WIN_SCOPED_HANDLE_H_
- #define BASE_WIN_SCOPED_HANDLE_H_
- #include "base/win/windows_types.h"
- #include "base/base_export.h"
- #include "base/check_op.h"
- #include "base/compiler_specific.h"
- #include "base/dcheck_is_on.h"
- #include "base/gtest_prod_util.h"
- #include "base/location.h"
- #include "base/macros.h"
- #include "build/build_config.h"
- #if defined(COMPILER_MSVC)
- #include <intrin.h>
- #define BASE_WIN_GET_CALLER _ReturnAddress()
- #elif defined(COMPILER_GCC)
- #define BASE_WIN_GET_CALLER \
- __builtin_extract_return_addr(__builtin_return_address(0))
- #endif
- namespace base {
- namespace win {
- template <class Traits, class Verifier>
- class GenericScopedHandle {
- public:
- using Handle = typename Traits::Handle;
- GenericScopedHandle() : handle_(Traits::NullHandle()) {}
- explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) {
- Set(handle);
- }
- GenericScopedHandle(GenericScopedHandle&& other)
- : handle_(Traits::NullHandle()) {
- Set(other.Take());
- }
- ~GenericScopedHandle() { Close(); }
- bool IsValid() const { return Traits::IsHandleValid(handle_); }
- GenericScopedHandle& operator=(GenericScopedHandle&& other) {
- DCHECK_NE(this, &other);
- Set(other.Take());
- return *this;
- }
- void Set(Handle handle) {
- if (handle_ != handle) {
-
- auto last_error = ::GetLastError();
- Close();
- if (Traits::IsHandleValid(handle)) {
- handle_ = handle;
- Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER,
- GetProgramCounter());
- }
- ::SetLastError(last_error);
- }
- }
- Handle Get() const { return handle_; }
-
- Handle Take() WARN_UNUSED_RESULT {
- Handle temp = handle_;
- handle_ = Traits::NullHandle();
- if (Traits::IsHandleValid(temp)) {
- Verifier::StopTracking(temp, this, BASE_WIN_GET_CALLER,
- GetProgramCounter());
- }
- return temp;
- }
-
- void Close() {
- if (Traits::IsHandleValid(handle_)) {
- Verifier::StopTracking(handle_, this, BASE_WIN_GET_CALLER,
- GetProgramCounter());
- Traits::CloseHandle(handle_);
- handle_ = Traits::NullHandle();
- }
- }
- private:
- FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, HandleVerifierWrongOwner);
- FRIEND_TEST_ALL_PREFIXES(ScopedHandleTest, HandleVerifierUntrackedHandle);
- Handle handle_;
- DISALLOW_COPY_AND_ASSIGN(GenericScopedHandle);
- };
- #undef BASE_WIN_GET_CALLER
- class HandleTraits {
- public:
- using Handle = HANDLE;
-
- static bool BASE_EXPORT CloseHandle(HANDLE handle);
-
- static bool IsHandleValid(HANDLE handle) {
- return handle != nullptr && handle != INVALID_HANDLE_VALUE;
- }
-
- static HANDLE NullHandle() { return nullptr; }
- private:
- DISALLOW_IMPLICIT_CONSTRUCTORS(HandleTraits);
- };
- class DummyVerifierTraits {
- public:
- using Handle = HANDLE;
- static void StartTracking(HANDLE handle,
- const void* owner,
- const void* pc1,
- const void* pc2) {}
- static void StopTracking(HANDLE handle,
- const void* owner,
- const void* pc1,
- const void* pc2) {}
- private:
- DISALLOW_IMPLICIT_CONSTRUCTORS(DummyVerifierTraits);
- };
- class BASE_EXPORT VerifierTraits {
- public:
- using Handle = HANDLE;
- static void StartTracking(HANDLE handle,
- const void* owner,
- const void* pc1,
- const void* pc2);
- static void StopTracking(HANDLE handle,
- const void* owner,
- const void* pc1,
- const void* pc2);
- private:
- DISALLOW_IMPLICIT_CONSTRUCTORS(VerifierTraits);
- };
- using UncheckedScopedHandle =
- GenericScopedHandle<HandleTraits, DummyVerifierTraits>;
- using CheckedScopedHandle = GenericScopedHandle<HandleTraits, VerifierTraits>;
- #if DCHECK_IS_ON() && !defined(ARCH_CPU_64_BITS)
- using ScopedHandle = CheckedScopedHandle;
- #else
- using ScopedHandle = UncheckedScopedHandle;
- #endif
- BASE_EXPORT void DisableHandleVerifier();
- BASE_EXPORT void OnHandleBeingClosed(HANDLE handle);
- }
- }
- #endif
|