summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Windows
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-12-06 04:28:13 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-12-06 04:28:13 +0000
commit39c4621f42de732014413de1c982ca279429ac30 (patch)
treec2fd956f9ef11b2b2d5ba67b1c90bc24a1410aa8 /llvm/lib/Support/Windows
parent459a6f150ac3e93c8a48f395882c77e35498f0c4 (diff)
downloadbcm5719-llvm-39c4621f42de732014413de1c982ca279429ac30.tar.gz
bcm5719-llvm-39c4621f42de732014413de1c982ca279429ac30.zip
Support/Windows: Add ScopedHandle and move some clients over to it.
llvm-svn: 120987
Diffstat (limited to 'llvm/lib/Support/Windows')
-rw-r--r--llvm/lib/Support/Windows/PathV2.inc20
-rw-r--r--llvm/lib/Support/Windows/Windows.h40
2 files changed, 50 insertions, 10 deletions
diff --git a/llvm/lib/Support/Windows/PathV2.inc b/llvm/lib/Support/Windows/PathV2.inc
index f6f53714b06..6bd541e49cc 100644
--- a/llvm/lib/Support/Windows/PathV2.inc
+++ b/llvm/lib/Support/Windows/PathV2.inc
@@ -113,15 +113,14 @@ namespace {
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 @@ error_code unique_file(const Twine &model, int &result_fd,
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/llvm/lib/Support/Windows/Windows.h b/llvm/lib/Support/Windows/Windows.h
index 00a7e75fc2a..0233a420512 100644
--- a/llvm/lib/Support/Windows/Windows.h
+++ b/llvm/lib/Support/Windows/Windows.h
@@ -58,3 +58,43 @@ public:
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;
+};
OpenPOWER on IntegriCloud