OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ | 5 #ifndef BASE_WIN_SCOPED_HANDLE_H_ |
6 #define BASE_WIN_SCOPED_HANDLE_H_ | 6 #define BASE_WIN_SCOPED_HANDLE_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/move.h" | 14 #include "base/move.h" |
15 | 15 |
16 // TODO(rvargas): remove this with the rest of the verifier. | 16 // TODO(rvargas): remove this with the rest of the verifier. |
17 #if defined(COMPILER_MSVC) | 17 #if defined(COMPILER_MSVC) |
18 #include <intrin.h> | 18 #include <intrin.h> |
19 #define BASE_WIN_GET_CALLER _ReturnAddress() | 19 #define BASE_WIN_GET_CALLER _ReturnAddress() |
20 #elif defined(COMPILER_GCC) | 20 #elif defined(COMPILER_GCC) |
21 #define BASE_WIN_GET_CALLER __builtin_extract_return_addr(\\ | 21 #define BASE_WIN_GET_CALLER __builtin_extract_return_addr(\\ |
22 __builtin_return_address(0)) | 22 __builtin_return_address(0)) |
23 #endif | 23 #endif |
24 | 24 |
25 namespace base { | 25 namespace base { |
26 namespace win { | 26 namespace win { |
27 | 27 |
28 // Generic wrapper for raw handles that takes care of closing handles | 28 // Generic wrapper for raw handles that takes care of closing handles |
29 // automatically. The class interface follows the style of | 29 // automatically. The class interface follows the style of |
30 // the ScopedFILE class with one addition: | 30 // the ScopedFILE class with two additions: |
31 // - IsValid() method can tolerate multiple invalid handle values such as NULL | 31 // - IsValid() method can tolerate multiple invalid handle values such as NULL |
32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. | 32 // and INVALID_HANDLE_VALUE (-1) for Win32 handles. |
33 // - Set() (and the constructors and assignment operators that call it) | |
34 // preserve the Windows LastError code. This ensures that GetLastError() can | |
35 // be called after stashing a handle in a GenericScopedHandle object. Doing | |
36 // this explicitly is necessary because of bug 528394 and VC++ 2015. | |
33 template <class Traits, class Verifier> | 37 template <class Traits, class Verifier> |
34 class GenericScopedHandle { | 38 class GenericScopedHandle { |
35 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle, RValue) | 39 MOVE_ONLY_TYPE_FOR_CPP_03(GenericScopedHandle, RValue) |
36 | 40 |
37 public: | 41 public: |
38 typedef typename Traits::Handle Handle; | 42 typedef typename Traits::Handle Handle; |
39 | 43 |
40 GenericScopedHandle() : handle_(Traits::NullHandle()) {} | 44 GenericScopedHandle() : handle_(Traits::NullHandle()) {} |
41 | 45 |
42 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { | 46 explicit GenericScopedHandle(Handle handle) : handle_(Traits::NullHandle()) { |
(...skipping 16 matching lines...) Expand all Loading... | |
59 // Move operator= for C++03 move emulation of this type. | 63 // Move operator= for C++03 move emulation of this type. |
60 GenericScopedHandle& operator=(RValue other) { | 64 GenericScopedHandle& operator=(RValue other) { |
61 if (this != other.object) { | 65 if (this != other.object) { |
62 Set(other.object->Take()); | 66 Set(other.object->Take()); |
63 } | 67 } |
64 return *this; | 68 return *this; |
65 } | 69 } |
66 | 70 |
67 void Set(Handle handle) { | 71 void Set(Handle handle) { |
68 if (handle_ != handle) { | 72 if (handle_ != handle) { |
73 // Preserve old LastError to avoid bug 528394. | |
74 auto last_error = GetLastError(); | |
rvargas (doing something else)
2015/09/10 22:42:02
nit: use ::GetLastError()
brucedawson
2015/09/10 23:21:46
Done. Same thing for SetLastError().
| |
69 Close(); | 75 Close(); |
70 | 76 |
71 if (Traits::IsHandleValid(handle)) { | 77 if (Traits::IsHandleValid(handle)) { |
72 handle_ = handle; | 78 handle_ = handle; |
73 Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER, | 79 Verifier::StartTracking(handle, this, BASE_WIN_GET_CALLER, |
74 tracked_objects::GetProgramCounter()); | 80 tracked_objects::GetProgramCounter()); |
75 } | 81 } |
82 SetLastError(last_error); | |
76 } | 83 } |
77 } | 84 } |
78 | 85 |
79 Handle Get() const { | 86 Handle Get() const { |
80 return handle_; | 87 return handle_; |
81 } | 88 } |
82 | 89 |
83 // Transfers ownership away from this object. | 90 // Transfers ownership away from this object. |
84 Handle Take() { | 91 Handle Take() { |
85 Handle temp = handle_; | 92 Handle temp = handle_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 // This should be called whenever the OS is closing a handle, if extended | 175 // This should be called whenever the OS is closing a handle, if extended |
169 // verification of improper handle closing is desired. If |handle| is being | 176 // verification of improper handle closing is desired. If |handle| is being |
170 // tracked by the handle verifier and ScopedHandle is not the one closing it, | 177 // tracked by the handle verifier and ScopedHandle is not the one closing it, |
171 // a CHECK is generated. | 178 // a CHECK is generated. |
172 void BASE_EXPORT OnHandleBeingClosed(HANDLE handle); | 179 void BASE_EXPORT OnHandleBeingClosed(HANDLE handle); |
173 | 180 |
174 } // namespace win | 181 } // namespace win |
175 } // namespace base | 182 } // namespace base |
176 | 183 |
177 #endif // BASE_WIN_SCOPED_HANDLE_H_ | 184 #endif // BASE_WIN_SCOPED_HANDLE_H_ |
OLD | NEW |