Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1 | /* |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 2 | * |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 3 | * Copyright (c) 2015-2016 The Khronos Group Inc. |
| 4 | * Copyright (c) 2015-2016 Valve Corporation |
| 5 | * Copyright (c) 2015-2016 LunarG, Inc. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 6 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 12 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 18 | * |
| 19 | * Author: Ian Elliot <ian@lunarg.com> |
| 20 | * Author: Jon Ashburn <jon@lunarg.com> |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 21 | * |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 22 | */ |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 23 | #pragma once |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 24 | |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 25 | #if defined(_WIN32) |
| 26 | // WinSock2.h must be included *BEFORE* windows.h |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 27 | #include <WinSock2.h> |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 28 | #endif // _WIN32 |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 29 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 30 | #include "vulkan/vk_platform.h" |
| 31 | #include "vulkan/vk_sdk_platform.h" |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 32 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 33 | #if defined(__linux__) |
| 34 | /* Linux-specific common code: */ |
| 35 | |
| 36 | // Headers: |
| 37 | //#define _GNU_SOURCE 1 |
| 38 | // TBD: Are the contents of the following file used? |
| 39 | #include <unistd.h> |
| 40 | // Note: The following file is for dynamic loading: |
| 41 | #include <dlfcn.h> |
| 42 | #include <pthread.h> |
| 43 | #include <assert.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 44 | #include <string.h> |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 45 | #include <stdbool.h> |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 46 | #include <stdlib.h> |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 47 | #include <libgen.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 48 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 49 | // VK Library Filenames, Paths, etc.: |
Frank Henigman | 5717310 | 2016-11-24 22:15:20 -0500 | [diff] [blame] | 50 | #define PATH_SEPARATOR ':' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 51 | #define DIRECTORY_SYMBOL '/' |
| 52 | |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 53 | #define VULKAN_DIR "/vulkan/" |
| 54 | #define VULKAN_ICDCONF_DIR "icd.d" |
| 55 | #define VULKAN_ICD_DIR "icd" |
John Drinkwater | 9ac3f4f | 2016-08-01 17:00:00 +0100 | [diff] [blame] | 56 | #define VULKAN_ELAYERCONF_DIR "explicit_layer.d" |
| 57 | #define VULKAN_ILAYERCONF_DIR "implicit_layer.d" |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 58 | #define VULKAN_LAYER_DIR "layer" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 59 | |
Benjamin Saunders | 31a4801 | 2017-01-29 14:49:54 -0800 | [diff] [blame] | 60 | #define DEFAULT_VK_DRIVERS_INFO "" |
| 61 | #define DEFAULT_VK_ELAYERS_INFO "" |
| 62 | #define DEFAULT_VK_ILAYERS_INFO "" |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 63 | |
| 64 | #define DEFAULT_VK_DRIVERS_PATH "" |
Frank Henigman | 5ecb4f6 | 2016-11-24 17:47:26 -0500 | [diff] [blame] | 65 | #if !defined(DEFAULT_VK_LAYERS_PATH) |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 66 | #define DEFAULT_VK_LAYERS_PATH "" |
Frank Henigman | 5ecb4f6 | 2016-11-24 17:47:26 -0500 | [diff] [blame] | 67 | #endif |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 68 | |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 69 | #if !defined(LAYERS_SOURCE_PATH) |
| 70 | #define LAYERS_SOURCE_PATH NULL |
| 71 | #endif |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 72 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Dor Askayo | a120bb3 | 2016-10-22 22:20:29 +0300 | [diff] [blame] | 73 | |
Benjamin Saunders | 31a4801 | 2017-01-29 14:49:54 -0800 | [diff] [blame] | 74 | #define RELATIVE_VK_DRIVERS_INFO VULKAN_DIR VULKAN_ICDCONF_DIR |
| 75 | #define RELATIVE_VK_ELAYERS_INFO VULKAN_DIR VULKAN_ELAYERCONF_DIR |
| 76 | #define RELATIVE_VK_ILAYERS_INFO VULKAN_DIR VULKAN_ILAYERCONF_DIR |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 77 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 78 | // C99: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 79 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 80 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 81 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 82 | static inline bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 83 | if (access(path, F_OK)) |
| 84 | return false; |
| 85 | else |
| 86 | return true; |
| 87 | } |
| 88 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 89 | static inline bool loader_platform_is_path_absolute(const char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 90 | if (path[0] == '/') |
| 91 | return true; |
| 92 | else |
| 93 | return false; |
| 94 | } |
| 95 | |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 96 | static inline char *loader_platform_dirname(char *path) { return dirname(path); } |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 97 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 98 | // Dynamic Loading of libraries: |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 99 | typedef void *loader_platform_dl_handle; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 100 | static inline loader_platform_dl_handle loader_platform_open_library(const char *libPath) { |
Lenny Komow | 21fd92c | 2017-05-18 14:02:26 -0600 | [diff] [blame] | 101 | // When loading the library, we use RTLD_LAZY so that not all symbols have to be |
| 102 | // resolved at this time (which improves performance). Note that if not all symbols |
| 103 | // can be resolved, this could cause crashes later. Use the LD_BIND_NOW environment |
| 104 | // variable to force all symbols to be resolved here. |
| 105 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 106 | } |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 107 | static inline const char *loader_platform_open_library_error(const char *libPath) { return dlerror(); } |
| 108 | static inline void loader_platform_close_library(loader_platform_dl_handle library) { dlclose(library); } |
| 109 | static inline void *loader_platform_get_proc_address(loader_platform_dl_handle library, const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 110 | assert(library); |
| 111 | assert(name); |
| 112 | return dlsym(library, name); |
| 113 | } |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 114 | static inline const char *loader_platform_get_proc_address_error(const char *name) { return dlerror(); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 115 | |
| 116 | // Threads: |
| 117 | typedef pthread_t loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 118 | #define THREAD_LOCAL_DECL __thread |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 119 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 120 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) pthread_once_t var; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 121 | static inline void loader_platform_thread_once(pthread_once_t *ctl, void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 122 | assert(func != NULL); |
| 123 | assert(ctl != NULL); |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 124 | pthread_once(ctl, func); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Thread IDs: |
| 128 | typedef pthread_t loader_platform_thread_id; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 129 | static inline loader_platform_thread_id loader_platform_get_thread_id() { return pthread_self(); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 130 | |
| 131 | // Thread mutex: |
| 132 | typedef pthread_mutex_t loader_platform_thread_mutex; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 133 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); } |
| 134 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); } |
| 135 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); } |
| 136 | static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); } |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 137 | typedef pthread_cond_t loader_platform_thread_cond; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 138 | static inline void loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); } |
| 139 | static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 140 | pthread_cond_wait(pCond, pMutex); |
| 141 | } |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 142 | static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 143 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 144 | #define loader_stack_alloc(size) alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 145 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 146 | #elif defined(_WIN32) // defined(__linux__) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 147 | /* Windows-specific common code: */ |
Tobin Ehlis | 890adf1 | 2015-11-02 15:45:19 -0700 | [diff] [blame] | 148 | // WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent |
| 149 | // undefine them to avoid conflicts with VkLayerDispatchTable struct members. |
| 150 | #ifdef CreateSemaphore |
| 151 | #undef CreateSemaphore |
| 152 | #endif |
| 153 | #ifdef CreateEvent |
| 154 | #undef CreateEvent |
| 155 | #endif |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 156 | #include <assert.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 157 | #include <stdio.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 158 | #include <string.h> |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 159 | #include <io.h> |
| 160 | #include <stdbool.h> |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 161 | #include <shlwapi.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 162 | #ifdef __cplusplus |
| 163 | #include <iostream> |
| 164 | #include <string> |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 165 | #endif // __cplusplus |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 166 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 167 | // VK Library Filenames, Paths, etc.: |
Frank Henigman | 5717310 | 2016-11-24 22:15:20 -0500 | [diff] [blame] | 168 | #define PATH_SEPARATOR ';' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 169 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 170 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
Lenny Komow | da849dc | 2017-03-01 17:22:29 -0700 | [diff] [blame] | 171 | #define DEFAULT_VK_REGISTRY_HIVE_STR "HKEY_LOCAL_MACHINE" |
| 172 | #define SECONDARY_VK_REGISTRY_HIVE HKEY_CURRENT_USER |
| 173 | #define SECONDARY_VK_REGISTRY_HIVE_STR "HKEY_CURRENT_USER" |
Lenny Komow | 367c4ea | 2016-11-07 11:40:00 -0700 | [diff] [blame] | 174 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\Drivers" |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 175 | #define DEFAULT_VK_DRIVERS_PATH "" |
Lenny Komow | 367c4ea | 2016-11-07 11:40:00 -0700 | [diff] [blame] | 176 | #define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ExplicitLayers" |
| 177 | #define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\" API_NAME "\\ImplicitLayers" |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 178 | #if !defined(DEFAULT_VK_LAYERS_PATH) |
Jamie Madill | 2fcbd15 | 2016-04-27 16:33:23 -0400 | [diff] [blame] | 179 | #define DEFAULT_VK_LAYERS_PATH "" |
Jamie Madill | 00c3c91 | 2016-04-06 18:26:46 -0400 | [diff] [blame] | 180 | #endif |
| 181 | #if !defined(LAYERS_SOURCE_PATH) |
| 182 | #define LAYERS_SOURCE_PATH NULL |
| 183 | #endif |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 184 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Benjamin Saunders | 31a4801 | 2017-01-29 14:49:54 -0800 | [diff] [blame] | 185 | #define RELATIVE_VK_DRIVERS_INFO "" |
| 186 | #define RELATIVE_VK_ELAYERS_INFO "" |
| 187 | #define RELATIVE_VK_ILAYERS_INFO "" |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 188 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
Courtney Goeltzenleuchter | 22d8d79 | 2015-10-07 17:03:42 -0600 | [diff] [blame] | 189 | |
Lenny Komow | 2089cdb | 2017-08-31 10:44:17 -0600 | [diff] [blame] | 190 | #if defined(_WIN32) |
| 191 | // Get the key for the plug n play driver registry |
| 192 | // The string returned by this function should NOT be freed |
| 193 | static inline const char *LoaderPnpDriverRegistry() { |
| 194 | BOOL is_wow; |
| 195 | IsWow64Process(GetCurrentProcess(), &is_wow); |
| 196 | return is_wow ? (API_NAME "DriverNameWow") : (API_NAME "DriverName"); |
| 197 | } |
Lenny Komow | f7c0938 | 2017-08-31 16:35:08 -0600 | [diff] [blame] | 198 | |
| 199 | // Get the key for the plug 'n play explicit layer registry |
| 200 | // The string returned by this function should NOT be freed |
| 201 | static inline const char *LoaderPnpELayerRegistry() { |
| 202 | BOOL is_wow; |
| 203 | IsWow64Process(GetCurrentProcess(), &is_wow); |
| 204 | return is_wow ? (API_NAME "ExplicitLayersWow") : (API_NAME "ExplicitLayers"); |
| 205 | } |
| 206 | // Get the key for the plug 'n play implicit layer registry |
| 207 | // The string returned by this function should NOT be freed |
| 208 | |
| 209 | static inline const char *LoaderPnpILayerRegistry() { |
| 210 | BOOL is_wow; |
| 211 | IsWow64Process(GetCurrentProcess(), &is_wow); |
| 212 | return is_wow ? (API_NAME "ImplicitLayersWow") : (API_NAME "ImplicitLayers"); |
| 213 | } |
Lenny Komow | 2089cdb | 2017-08-31 10:44:17 -0600 | [diff] [blame] | 214 | #endif |
| 215 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 216 | // File IO |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 217 | static bool loader_platform_file_exists(const char *path) { |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 218 | if ((_access(path, 0)) == -1) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 219 | return false; |
| 220 | else |
| 221 | return true; |
| 222 | } |
| 223 | |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 224 | static bool loader_platform_is_path_absolute(const char *path) { return !PathIsRelative(path); } |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 225 | |
| 226 | // WIN32 runtime doesn't have dirname(). |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 227 | static inline char *loader_platform_dirname(char *path) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 228 | char *current, *next; |
| 229 | |
| 230 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 231 | |
| 232 | for (current = path; *current != '\0'; current = next) { |
| 233 | next = strchr(current, DIRECTORY_SYMBOL); |
| 234 | if (next == NULL) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 235 | if (current != path) *(current - 1) = '\0'; |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 236 | return path; |
| 237 | } else { |
| 238 | // Point one character past the DIRECTORY_SYMBOL: |
| 239 | next++; |
| 240 | } |
| 241 | } |
| 242 | return path; |
| 243 | } |
| 244 | |
| 245 | // WIN32 runtime doesn't have basename(). |
| 246 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 247 | // so this is just a temporary solution in order to get us compiling, so that we |
| 248 | // can test some scenarios, and develop the correct solution for Windows. |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 249 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 250 | // temporary code: |
| 251 | static char *loader_platform_basename(char *pathname) { |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 252 | char *current, *next; |
| 253 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 254 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 255 | |
| 256 | for (current = pathname; *current != '\0'; current = next) { |
| 257 | next = strchr(current, DIRECTORY_SYMBOL); |
| 258 | if (next == NULL) { |
| 259 | // No more DIRECTORY_SYMBOL's so return p: |
| 260 | return current; |
| 261 | } else { |
| 262 | // Point one character past the DIRECTORY_SYMBOL: |
| 263 | next++; |
| 264 | } |
| 265 | } |
| 266 | // We shouldn't get to here, but this makes the compiler happy: |
| 267 | return current; |
| 268 | } |
| 269 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 270 | // Dynamic Loading: |
| 271 | typedef HMODULE loader_platform_dl_handle; |
Mark Young | 5305c1d | 2017-05-16 07:03:56 -0600 | [diff] [blame] | 272 | static loader_platform_dl_handle loader_platform_open_library(const char *lib_path) { |
| 273 | // Try loading the library the original way first. |
| 274 | loader_platform_dl_handle lib_handle = LoadLibrary(lib_path); |
| 275 | if (lib_handle == NULL && GetLastError() == ERROR_MOD_NOT_FOUND && PathFileExists(lib_path)) { |
Mark Young | b986b5f | 2017-05-16 09:48:55 -0600 | [diff] [blame] | 276 | // If that failed, then try loading it with broader search folders. |
Mark Young | 5305c1d | 2017-05-16 07:03:56 -0600 | [diff] [blame] | 277 | lib_handle = LoadLibraryEx(lib_path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR); |
| 278 | } |
| 279 | return lib_handle; |
| 280 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 281 | static char *loader_platform_open_library_error(const char *libPath) { |
Tobin Ehlis | b2dfb55 | 2016-06-07 06:07:13 -0600 | [diff] [blame] | 282 | static char errorMsg[164]; |
Mark Young | b986b5f | 2017-05-16 09:48:55 -0600 | [diff] [blame] | 283 | (void)snprintf(errorMsg, 163, "Failed to open dynamic library \"%s\" with error %d", libPath, GetLastError()); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 284 | return errorMsg; |
| 285 | } |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 286 | static void loader_platform_close_library(loader_platform_dl_handle library) { FreeLibrary(library); } |
| 287 | static void *loader_platform_get_proc_address(loader_platform_dl_handle library, const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 288 | assert(library); |
| 289 | assert(name); |
| 290 | return GetProcAddress(library, name); |
| 291 | } |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 292 | static char *loader_platform_get_proc_address_error(const char *name) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 293 | static char errorMsg[120]; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 294 | (void)snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 295 | return errorMsg; |
| 296 | } |
| 297 | |
| 298 | // Threads: |
| 299 | typedef HANDLE loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 300 | #define THREAD_LOCAL_DECL __declspec(thread) |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 301 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 302 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) INIT_ONCE var; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 303 | static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) { |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 304 | void (*func)(void) = (void (*)(void))Parameter; |
| 305 | func(); |
| 306 | return TRUE; |
| 307 | } |
| 308 | |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 309 | static void loader_platform_thread_once(void *ctl, void (*func)(void)) { |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 310 | assert(func != NULL); |
| 311 | assert(ctl != NULL); |
Jon Ashburn | 23d36b1 | 2016-02-02 17:47:28 -0700 | [diff] [blame] | 312 | InitOnceExecuteOnce((PINIT_ONCE)ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Thread IDs: |
| 316 | typedef DWORD loader_platform_thread_id; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 317 | static loader_platform_thread_id loader_platform_get_thread_id() { return GetCurrentThreadId(); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 318 | |
| 319 | // Thread mutex: |
| 320 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 321 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); } |
| 322 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); } |
| 323 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); } |
| 324 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); } |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 325 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 326 | static void loader_platform_thread_init_cond(loader_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); } |
| 327 | static void loader_platform_thread_cond_wait(loader_platform_thread_cond *pCond, loader_platform_thread_mutex *pMutex) { |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 328 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 329 | } |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 330 | static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 331 | |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 332 | #define loader_stack_alloc(size) _alloca(size) |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 333 | #else // defined(_WIN32) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 334 | |
| 335 | #error The "loader_platform.h" file must be modified for this OS. |
| 336 | |
| 337 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 338 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 339 | // contents of this file must be created. |
| 340 | |
| 341 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 342 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 343 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 344 | #endif // defined(_WIN32) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 345 | |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 346 | // returns true if the given string appears to be a relative or absolute |
| 347 | // path, as opposed to a bare filename. |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 348 | static inline bool loader_platform_is_path(const char *path) { return strchr(path, DIRECTORY_SYMBOL) != NULL; } |