mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-12 02:33:33 +00:00
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
This commit is contained in:
parent
93e5ec2cba
commit
7eec50bf4f
@ -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);
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user