|
| 1 | +//===--- Win32.h - Win32 utility functions ----------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Utility functions that are specific to the Windows port. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_RUNTIME_WIN32_H |
| 18 | +#define SWIFT_RUNTIME_WIN32_H |
| 19 | + |
| 20 | +#ifdef _WIN32 |
| 21 | + |
| 22 | +#include "swift/shims/Visibility.h" |
| 23 | + |
| 24 | +#include <functional> |
| 25 | +#include <type_traits> |
| 26 | + |
| 27 | +// For HANDLE |
| 28 | +#define WIN32_LEAN_AND_MEAN |
| 29 | +#define NOMINMAX |
| 30 | +#include <windows.h> |
| 31 | + |
| 32 | +#include <wchar.h> |
| 33 | + |
| 34 | +/// Convert a wide string to UTF-8. |
| 35 | +/// |
| 36 | +/// @param str The string to convert. |
| 37 | +/// |
| 38 | +/// @returns The string, converted to UTF-8. The caller is responsible |
| 39 | +/// for freeing this string with @c free() when done with it. |
| 40 | +/// |
| 41 | +/// If @a str cannot be converted to UTF-8, @c nullptr is returned. |
| 42 | +SWIFT_RUNTIME_STDLIB_SPI |
| 43 | +char *_swift_win32_copyUTF8FromWide(const wchar_t *str); |
| 44 | + |
| 45 | +/// Convert a UTF-8 string to a wide string. |
| 46 | +/// |
| 47 | +/// @param str The string to convert. |
| 48 | +/// |
| 49 | +/// @returns The string, converted to UTF-16. The caller is responsible |
| 50 | +/// for freeing this string with @c free() when done with it. |
| 51 | +/// |
| 52 | +/// If @a str cannot be converted to UTF-16, @c nullptr is returned. |
| 53 | +SWIFT_RUNTIME_STDLIB_SPI |
| 54 | +wchar_t *_swift_win32_copyWideFromUTF8(const char *str); |
| 55 | + |
| 56 | +/// Configure the environment to allow calling into the Debug Help library. |
| 57 | +/// |
| 58 | +/// \param body A function to invoke. This function attempts to first initialize |
| 59 | +/// the Debug Help library. If it did so successfully, the handle used during |
| 60 | +/// initialization is passed to this function and should be used with |
| 61 | +/// subsequent calls to the Debug Help library. Do not close this handle. |
| 62 | +/// \param context A caller-supplied value to pass to \a body. |
| 63 | +/// |
| 64 | +/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All |
| 65 | +/// calls into it from the Swift runtime and stdlib should route through this |
| 66 | +/// function. |
| 67 | +/// |
| 68 | +/// This function sets the Debug Help library's options by calling |
| 69 | +/// \c SymSetOptions() before \a body is invoked, and then resets them back to |
| 70 | +/// their old value before returning. \a body can also call \c SymSetOptions() |
| 71 | +/// if needed. |
| 72 | +SWIFT_RUNTIME_STDLIB_SPI |
| 73 | +void _swift_win32_withDbgHelpLibrary( |
| 74 | + void (* body)(HANDLE hProcess, void *context), void *context); |
| 75 | + |
| 76 | +/// Configure the environment to allow calling into the Debug Help library. |
| 77 | +/// |
| 78 | +/// \param body A function to invoke. This function attempts to first initialize |
| 79 | +/// the Debug Help library. If it did so successfully, the handle used during |
| 80 | +/// initialization is passed to this function and should be used with |
| 81 | +/// subsequent calls to the Debug Help library. Do not close this handle. |
| 82 | +/// |
| 83 | +/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All |
| 84 | +/// calls into it from the Swift runtime and stdlib should route through this |
| 85 | +/// function. |
| 86 | +/// |
| 87 | +/// This function sets the Debug Help library's options by calling |
| 88 | +/// \c SymSetOptions() before \a body is invoked, and then resets them back to |
| 89 | +/// their old value before returning. \a body can also call \c SymSetOptions() |
| 90 | +/// if needed. |
| 91 | +static inline void _swift_win32_withDbgHelpLibrary( |
| 92 | + const std::function<void(HANDLE /*hProcess*/)> &body) { |
| 93 | + _swift_win32_withDbgHelpLibrary([](HANDLE hProcess, void *context) { |
| 94 | + auto bodyp = reinterpret_cast<std::function<void(HANDLE)> *>(context); |
| 95 | + (* bodyp)(hProcess); |
| 96 | + }, const_cast<void *>(reinterpret_cast<const void *>(&body))); |
| 97 | +} |
| 98 | + |
| 99 | +/// Configure the environment to allow calling into the Debug Help library. |
| 100 | +/// |
| 101 | +/// \param body A function to invoke. This function attempts to first initialize |
| 102 | +/// the Debug Help library. If it did so successfully, the handle used during |
| 103 | +/// initialization is passed to this function and should be used with |
| 104 | +/// subsequent calls to the Debug Help library. Do not close this handle. |
| 105 | +/// |
| 106 | +/// \returns Whatever is returned from \a body. |
| 107 | +/// |
| 108 | +/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All |
| 109 | +/// calls into it from the Swift runtime and stdlib should route through this |
| 110 | +/// function. |
| 111 | +/// |
| 112 | +/// This function sets the Debug Help library's options by calling |
| 113 | +/// \c SymSetOptions() before \a body is invoked, and then resets them back to |
| 114 | +/// their old value before returning. \a body can also call \c SymSetOptions() |
| 115 | +/// if needed. |
| 116 | +template < |
| 117 | + typename F, |
| 118 | + typename R = typename std::result_of_t<F&(HANDLE /*hProcess*/)>, |
| 119 | + typename = typename std::enable_if_t<!std::is_same<void, R>::value> |
| 120 | +> |
| 121 | +static inline R _swift_win32_withDbgHelpLibrary(const F& body) { |
| 122 | + R result; |
| 123 | + |
| 124 | + _swift_win32_withDbgHelpLibrary([&body, &result] (HANDLE hProcess) { |
| 125 | + result = body(hProcess); |
| 126 | + }); |
| 127 | + |
| 128 | + return result; |
| 129 | +} |
| 130 | + |
| 131 | +#endif // defined(_WIN32) |
| 132 | + |
| 133 | +#endif // SWIFT_RUNTIME_WIN32_H |
0 commit comments