[llvm-commits] [llvm] r120987 - in /llvm/trunk/lib/Support/Windows: PathV2.inc Windows.h
Michael J. Spencer
bigcheesegs at gmail.com
Sun Dec 5 20:28:13 PST 2010
Author: mspencer
Date: Sun Dec 5 22:28:13 2010
New Revision: 120987
URL: http://llvm.org/viewvc/llvm-project?rev=120987&view=rev
Log:
Support/Windows: Add ScopedHandle and move some clients over to it.
Modified:
llvm/trunk/lib/Support/Windows/PathV2.inc
llvm/trunk/lib/Support/Windows/Windows.h
Modified: llvm/trunk/lib/Support/Windows/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/PathV2.inc?rev=120987&r1=120986&r2=120987&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Windows/PathV2.inc Sun Dec 5 22:28:13 2010
@@ -113,15 +113,14 @@
return success;
}
- struct AutoCryptoProvider {
- HCRYPTPROV CryptoProvider;
-
- ~AutoCryptoProvider() {
- ::CryptReleaseContext(CryptoProvider, 0);
- }
+ // Forwarder for ScopedHandle.
+ BOOL WINAPI CryptReleaseContext(HCRYPTPROV Provider) {
+ return ::CryptReleaseContext(Provider, 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);
Modified: llvm/trunk/lib/Support/Windows/Windows.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Windows.h?rev=120987&r1=120986&r2=120987&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Windows.h (original)
+++ llvm/trunk/lib/Support/Windows/Windows.h Sun Dec 5 22:28:13 2010
@@ -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;
+};
More information about the llvm-commits
mailing list