Support/Windows: Add ScopedHandle and move some clients over to it.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120987 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc
index f6f5371..6bd541e 100644
--- a/lib/Support/Windows/PathV2.inc
+++ b/lib/Support/Windows/PathV2.inc
@@ -113,15 +113,14 @@
return success;
}
- struct AutoCryptoProvider {
- HCRYPTPROV CryptoProvider;
+ // Forwarder for ScopedHandle.
+ BOOL WINAPI CryptReleaseContext(HCRYPTPROV Provider) {
+ return ::CryptReleaseContext(Provider, 0);
+ }
- ~AutoCryptoProvider() {
- ::CryptReleaseContext(CryptoProvider, 0);
- }
-
- operator HCRYPTPROV() const {return CryptoProvider;}
- };
+ typedef ScopedHandle<HCRYPTPROV, HCRYPTPROV(INVALID_HANDLE_VALUE),
+ BOOL (WINAPI*)(HCRYPTPROV), CryptReleaseContext>
+ ScopedCryptContext;
}
namespace llvm {
@@ -503,13 +502,14 @@
SmallVector<wchar_t, 128> random_path_utf16;
// Get a Crypto Provider for CryptGenRandom.
- AutoCryptoProvider CryptoProvider;
- if (!::CryptAcquireContextW(&CryptoProvider.CryptoProvider,
+ HCRYPTPROV HCPC;
+ if (!::CryptAcquireContextW(&HCPC,
NULL,
NULL,
PROV_RSA_FULL,
0))
return windows_error(::GetLastError());
+ ScopedCryptContext CryptoProvider(HCPC);
retry_random_path:
random_path_utf16.set_size(0);
diff --git a/lib/Support/Windows/Windows.h b/lib/Support/Windows/Windows.h
index 00a7e75..0233a42 100644
--- a/lib/Support/Windows/Windows.h
+++ b/lib/Support/Windows/Windows.h
@@ -58,3 +58,43 @@
return *this;
}
};
+
+template <class HandleType, HandleType InvalidHandle,
+ class DeleterType, DeleterType D>
+class ScopedHandle {
+ HandleType Handle;
+
+public:
+ ScopedHandle() : Handle(InvalidHandle) {}
+ ScopedHandle(HandleType handle) : Handle(handle) {}
+
+ ~ScopedHandle() {
+ if (Handle != InvalidHandle)
+ D(Handle);
+ }
+
+ HandleType take() {
+ HandleType temp = Handle;
+ Handle = InvalidHandle;
+ return temp;
+ }
+
+ operator HandleType() const { return Handle; }
+
+ ScopedHandle &operator=(HandleType handle) {
+ Handle = handle;
+ return *this;
+ }
+
+ typedef void (*unspecified_bool_type)();
+ static void unspecified_bool_true() {}
+
+ // True if Handle is valid.
+ operator unspecified_bool_type() const {
+ return Handle == InvalidHandle ? 0 : unspecified_bool_true;
+ }
+
+ typedef ScopedHandle<HANDLE, INVALID_HANDLE_VALUE,
+ BOOL (WINAPI*)(HANDLE), ::FindClose>
+ ScopedFindHandle;
+};