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 | * |
Courtney Goeltzenleuchter | fcbe16f | 2015-10-29 13:50:34 -0600 | [diff] [blame] | 3 | * Copyright (C) 2015 Valve Corporation |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 4 | * Copyright 2014 Valve Software |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 5 | * Copyright (C) 2015 Google Inc. |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the "Software"), |
| 10 | * to deal in the Software without restriction, including without limitation |
| 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | * and/or sell copies of the Software, and to permit persons to whom the |
| 13 | * Software is furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included |
| 16 | * in all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | * DEALINGS IN THE SOFTWARE. |
| 25 | * |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 26 | * Author: James Jones <jajones@nvidia.com> |
| 27 | * Author: Tobin Ehlis <tobin@lunarg.com> |
| 28 | * |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 29 | */ |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 30 | #pragma once |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 31 | |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 32 | #if defined(_WIN32) |
| 33 | // WinSock2.h must be included *BEFORE* windows.h |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 34 | #include <WinSock2.h> |
| 35 | #endif // _WIN32 |
| 36 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 37 | #include "vulkan/vk_platform.h" |
| 38 | #include "vulkan/vk_sdk_platform.h" |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 39 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 40 | #if defined(__linux__) |
| 41 | /* Linux-specific common code: */ |
| 42 | |
| 43 | // Headers: |
| 44 | //#define _GNU_SOURCE 1 |
| 45 | // TBD: Are the contents of the following file used? |
| 46 | #include <unistd.h> |
| 47 | // Note: The following file is for dynamic loading: |
| 48 | #include <dlfcn.h> |
| 49 | #include <pthread.h> |
| 50 | #include <assert.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 51 | #include <string.h> |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 52 | #include <stdbool.h> |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 53 | #include <stdlib.h> |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 54 | #include <libgen.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 55 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 56 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 57 | #define PATH_SEPERATOR ':' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 58 | #define DIRECTORY_SYMBOL '/' |
| 59 | |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 60 | #define VULKAN_ICDCONF_DIR "/" "vulkan" "/" "icd.d" |
| 61 | #define VULKAN_ICD_DIR "/" "vulkan" "/" "icd" |
James Jones | 34c500b | 2015-07-24 11:35:16 -0700 | [diff] [blame] | 62 | #define VULKAN_ELAYERCONF_DIR "/" "vulkan" "/" "explicit_layer.d" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 63 | #define VULKAN_ILAYERCONF_DIR "/" "vulkan" "/" "implicit_layer.d" |
| 64 | #define VULKAN_LAYER_DIR "/" "vulkan" "/" "layer" |
| 65 | |
| 66 | #if defined(LOCALPREFIX) |
| 67 | #define LOCAL_DRIVERS_INFO \ |
| 68 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \ |
| 69 | LOCALPREFIX "/" DATADIR VULKAN_ICDCONF_DIR ":" |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 70 | #define LOCAL_ELAYERS_INFO \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 71 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 72 | LOCALPREFIX "/" DATADIR VULKAN_ELAYERCONF_DIR ":" |
| 73 | #define LOCAL_ILAYERS_INFO \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 74 | LOCALPREFIX "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 75 | LOCALPREFIX "/" DATADIR VULKAN_ILAYERCONF_DIR ":" |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 76 | #else |
| 77 | #define LOCAL_DRIVERS_INFO |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 78 | #define LOCAL_ELAYERS_INFO |
| 79 | #define LOCAL_ILAYERS_INFO |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 80 | #endif |
| 81 | |
Adam Jackson | 3869f41 | 2015-07-23 16:29:26 -0400 | [diff] [blame] | 82 | #define DEFAULT_VK_DRIVERS_INFO \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 83 | LOCAL_DRIVERS_INFO \ |
James Jones | 09a436f | 2015-07-24 09:47:37 -0700 | [diff] [blame] | 84 | "/" SYSCONFDIR VULKAN_ICDCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 85 | "/usr/" DATADIR VULKAN_ICDCONF_DIR |
Daniel Dadap | 6e62684 | 2015-09-30 12:09:49 -0500 | [diff] [blame] | 86 | #define DEFAULT_VK_DRIVERS_PATH "" |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 87 | #define DEFAULT_VK_ELAYERS_INFO \ |
| 88 | LOCAL_ELAYERS_INFO \ |
James Jones | 09a436f | 2015-07-24 09:47:37 -0700 | [diff] [blame] | 89 | "/" SYSCONFDIR VULKAN_ELAYERCONF_DIR ":" \ |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 90 | "/usr/" DATADIR VULKAN_ELAYERCONF_DIR ":" |
| 91 | #define DEFAULT_VK_ILAYERS_INFO \ |
| 92 | LOCAL_ILAYERS_INFO \ |
James Jones | 09a436f | 2015-07-24 09:47:37 -0700 | [diff] [blame] | 93 | "/" SYSCONFDIR VULKAN_ILAYERCONF_DIR ":" \ |
James Jones | c28b0a1 | 2015-07-23 17:39:37 -0700 | [diff] [blame] | 94 | "/usr/" DATADIR VULKAN_ILAYERCONF_DIR |
Daniel Dadap | 6e62684 | 2015-09-30 12:09:49 -0500 | [diff] [blame] | 95 | #define DEFAULT_VK_LAYERS_PATH "" |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 96 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 97 | |
Ian Elliott | e536946 | 2015-02-12 16:44:56 -0700 | [diff] [blame] | 98 | // C99: |
Tobin Ehlis | b049785 | 2015-02-11 14:24:02 -0700 | [diff] [blame] | 99 | #define PRINTF_SIZE_T_SPECIFIER "%zu" |
| 100 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 101 | // File IO |
| 102 | static inline bool loader_platform_file_exists(const char *path) |
| 103 | { |
| 104 | if (access(path, F_OK)) |
| 105 | return false; |
| 106 | else |
| 107 | return true; |
| 108 | } |
| 109 | |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 110 | static inline bool loader_platform_is_path_absolute(const char *path) |
| 111 | { |
| 112 | if (path[0] == '/') |
| 113 | return true; |
| 114 | else |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | static inline char *loader_platform_dirname(char *path) |
| 119 | { |
| 120 | return dirname(path); |
| 121 | } |
| 122 | |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 123 | // Environment variables |
| 124 | |
| 125 | static inline char *loader_getenv(const char *name) |
| 126 | { |
| 127 | return getenv(name); |
| 128 | } |
| 129 | |
| 130 | static inline void loader_free_getenv(const char *val) |
| 131 | { |
| 132 | } |
| 133 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 134 | // Dynamic Loading of libraries: |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 135 | typedef void * loader_platform_dl_handle; |
| 136 | static inline loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 137 | { |
Chia-I Wu | 6a21834 | 2015-02-18 14:39:54 -0700 | [diff] [blame] | 138 | return dlopen(libPath, RTLD_LAZY | RTLD_LOCAL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 139 | } |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 140 | static inline const char * loader_platform_open_library_error(const char* libPath) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 141 | { |
| 142 | return dlerror(); |
| 143 | } |
| 144 | static inline void loader_platform_close_library(loader_platform_dl_handle library) |
| 145 | { |
| 146 | dlclose(library); |
| 147 | } |
| 148 | static inline void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 149 | const char *name) |
| 150 | { |
| 151 | assert(library); |
| 152 | assert(name); |
| 153 | return dlsym(library, name); |
| 154 | } |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 155 | static inline const char * loader_platform_get_proc_address_error(const char *name) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 156 | { |
| 157 | return dlerror(); |
| 158 | } |
| 159 | |
| 160 | // Threads: |
| 161 | typedef pthread_t loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 162 | #define THREAD_LOCAL_DECL __thread |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 163 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 164 | pthread_once_t var = PTHREAD_ONCE_INIT; |
Jon Ashburn | 27cd584 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 165 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 166 | pthread_once_t var; |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 167 | 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] | 168 | { |
| 169 | assert(func != NULL); |
| 170 | assert(ctl != NULL); |
Michael Lentine | 695f2c2 | 2015-09-09 12:39:13 -0700 | [diff] [blame] | 171 | pthread_once(ctl, func); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Thread IDs: |
| 175 | typedef pthread_t loader_platform_thread_id; |
| 176 | static inline loader_platform_thread_id loader_platform_get_thread_id() |
| 177 | { |
| 178 | return pthread_self(); |
| 179 | } |
| 180 | |
| 181 | // Thread mutex: |
| 182 | typedef pthread_mutex_t loader_platform_thread_mutex; |
| 183 | static inline void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 184 | { |
| 185 | pthread_mutex_init(pMutex, NULL); |
| 186 | } |
| 187 | static inline void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 188 | { |
| 189 | pthread_mutex_lock(pMutex); |
| 190 | } |
| 191 | static inline void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 192 | { |
| 193 | pthread_mutex_unlock(pMutex); |
| 194 | } |
| 195 | static inline void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 196 | { |
| 197 | pthread_mutex_destroy(pMutex); |
| 198 | } |
Mike Stroyan | ed238bb | 2015-05-15 08:50:57 -0600 | [diff] [blame] | 199 | typedef pthread_cond_t loader_platform_thread_cond; |
| 200 | static inline void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 201 | { |
| 202 | pthread_cond_init(pCond, NULL); |
| 203 | } |
| 204 | static inline void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 205 | { |
| 206 | pthread_cond_wait(pCond, pMutex); |
| 207 | } |
| 208 | static inline void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 209 | { |
| 210 | pthread_cond_broadcast(pCond); |
| 211 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 212 | |
Courtney Goeltzenleuchter | 7f5aafc | 2015-07-05 11:28:29 -0600 | [diff] [blame] | 213 | #define loader_stack_alloc(size) alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 214 | |
| 215 | #elif defined(_WIN32) // defined(__linux__) |
| 216 | /* Windows-specific common code: */ |
Tobin Ehlis | 890adf1 | 2015-11-02 15:45:19 -0700 | [diff] [blame] | 217 | // WinBase.h defines CreateSemaphore and synchapi.h defines CreateEvent |
| 218 | // undefine them to avoid conflicts with VkLayerDispatchTable struct members. |
| 219 | #ifdef CreateSemaphore |
| 220 | #undef CreateSemaphore |
| 221 | #endif |
| 222 | #ifdef CreateEvent |
| 223 | #undef CreateEvent |
| 224 | #endif |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 225 | #include <assert.h> |
Tony Barbour | 1d825c7 | 2015-06-18 16:29:32 -0600 | [diff] [blame] | 226 | #include <stdio.h> |
Jon Ashburn | fc1031e | 2015-11-17 15:31:02 -0700 | [diff] [blame] | 227 | #include <string.h> |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 228 | #include <io.h> |
| 229 | #include <stdbool.h> |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 230 | #include <shlwapi.h> |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 231 | #ifdef __cplusplus |
| 232 | #include <iostream> |
| 233 | #include <string> |
| 234 | using namespace std; |
| 235 | #endif // __cplusplus |
| 236 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 237 | // VK Library Filenames, Paths, etc.: |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 238 | #define PATH_SEPERATOR ';' |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 239 | #define DIRECTORY_SYMBOL '\\' |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 240 | #define DEFAULT_VK_REGISTRY_HIVE HKEY_LOCAL_MACHINE |
| 241 | #define DEFAULT_VK_DRIVERS_INFO "SOFTWARE\\Khronos\\Vulkan\\Drivers" |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 242 | // TODO: Are these the correct paths |
scygan | 713b1b0 | 2015-06-01 19:47:08 +0200 | [diff] [blame] | 243 | #define DEFAULT_VK_DRIVERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | 075ce43 | 2015-12-17 17:38:24 -0700 | [diff] [blame] | 244 | #define DEFAULT_VK_ELAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ExplicitLayers" |
| 245 | #define DEFAULT_VK_ILAYERS_INFO "SOFTWARE\\Khronos\\Vulkan\\ImplicitLayers" |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 246 | #define DEFAULT_VK_LAYERS_PATH "C:\\Windows\\System32;C:\\Windows\\SysWow64" |
Jon Ashburn | 1b95822 | 2015-08-06 11:22:33 -0600 | [diff] [blame] | 247 | #define LAYERS_PATH_ENV "VK_LAYER_PATH" |
Ian Elliott | 457810e | 2015-02-04 11:22:39 -0700 | [diff] [blame] | 248 | |
Courtney Goeltzenleuchter | 22d8d79 | 2015-10-07 17:03:42 -0600 | [diff] [blame] | 249 | #define PRINTF_SIZE_T_SPECIFIER "%Iu" |
| 250 | |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 251 | // File IO |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 252 | static bool loader_platform_file_exists(const char *path) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 253 | { |
Jon Ashburn | ffad94d | 2015-06-30 14:46:22 -0700 | [diff] [blame] | 254 | if ((_access(path, 0)) == -1) |
Jon Ashburn | 2077e38 | 2015-06-29 11:25:34 -0600 | [diff] [blame] | 255 | return false; |
| 256 | else |
| 257 | return true; |
| 258 | } |
| 259 | |
Cody Northrop | 33fa041 | 2015-07-08 16:48:37 -0600 | [diff] [blame] | 260 | static bool loader_platform_is_path_absolute(const char *path) |
Jon Ashburn | 1531517 | 2015-07-07 15:06:25 -0600 | [diff] [blame] | 261 | { |
| 262 | return !PathIsRelative(path); |
| 263 | } |
| 264 | |
| 265 | // WIN32 runtime doesn't have dirname(). |
| 266 | static inline char *loader_platform_dirname(char *path) |
| 267 | { |
| 268 | char *current, *next; |
| 269 | |
| 270 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 271 | |
| 272 | for (current = path; *current != '\0'; current = next) { |
| 273 | next = strchr(current, DIRECTORY_SYMBOL); |
| 274 | if (next == NULL) { |
| 275 | if (current != path) |
| 276 | *(current - 1) = '\0'; |
| 277 | return path; |
| 278 | } else { |
| 279 | // Point one character past the DIRECTORY_SYMBOL: |
| 280 | next++; |
| 281 | } |
| 282 | } |
| 283 | return path; |
| 284 | } |
| 285 | |
| 286 | // WIN32 runtime doesn't have basename(). |
| 287 | // Microsoft also doesn't have basename(). Paths are different on Windows, and |
| 288 | // so this is just a temporary solution in order to get us compiling, so that we |
| 289 | // can test some scenarios, and develop the correct solution for Windows. |
| 290 | // TODO: Develop a better, permanent solution for Windows, to replace this |
| 291 | // temporary code: |
| 292 | static char *loader_platform_basename(char *pathname) |
| 293 | { |
| 294 | char *current, *next; |
| 295 | |
| 296 | // TODO/TBD: Do we need to deal with the Windows's ":" character? |
| 297 | |
| 298 | for (current = pathname; *current != '\0'; current = next) { |
| 299 | next = strchr(current, DIRECTORY_SYMBOL); |
| 300 | if (next == NULL) { |
| 301 | // No more DIRECTORY_SYMBOL's so return p: |
| 302 | return current; |
| 303 | } else { |
| 304 | // Point one character past the DIRECTORY_SYMBOL: |
| 305 | next++; |
| 306 | } |
| 307 | } |
| 308 | // We shouldn't get to here, but this makes the compiler happy: |
| 309 | return current; |
| 310 | } |
| 311 | |
Jon Ashburn | 38a497f | 2016-01-04 14:01:38 -0700 | [diff] [blame] | 312 | // Environment variables |
| 313 | |
| 314 | static inline char *loader_getenv(const char *name) |
| 315 | { |
| 316 | char *retVal; |
| 317 | DWORD valSize; |
| 318 | |
| 319 | valSize = GetEnvironmentVariableA(name, NULL, 0); |
| 320 | |
| 321 | // valSize DOES include the null terminator, so for any set variable |
| 322 | // will always be at least 1. If it's 0, the variable wasn't set. |
| 323 | if (valSize == 0) |
| 324 | return NULL; |
| 325 | |
| 326 | //TODO; FIXME This should be using any app defined memory allocation |
| 327 | retVal = (char *)malloc(valSize); |
| 328 | |
| 329 | GetEnvironmentVariableA(name, retVal, valSize); |
| 330 | |
| 331 | return retVal; |
| 332 | } |
| 333 | |
| 334 | static inline void loader_free_getenv(const char *val) |
| 335 | { |
| 336 | free((void *)val); |
| 337 | } |
| 338 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 339 | // Dynamic Loading: |
| 340 | typedef HMODULE loader_platform_dl_handle; |
| 341 | static loader_platform_dl_handle loader_platform_open_library(const char* libPath) |
| 342 | { |
| 343 | return LoadLibrary(libPath); |
| 344 | } |
| 345 | static char * loader_platform_open_library_error(const char* libPath) |
| 346 | { |
| 347 | static char errorMsg[120]; |
| 348 | snprintf(errorMsg, 119, "Failed to open dynamic library \"%s\"", libPath); |
| 349 | return errorMsg; |
| 350 | } |
| 351 | static void loader_platform_close_library(loader_platform_dl_handle library) |
| 352 | { |
| 353 | FreeLibrary(library); |
| 354 | } |
| 355 | static void * loader_platform_get_proc_address(loader_platform_dl_handle library, |
| 356 | const char *name) |
| 357 | { |
| 358 | assert(library); |
| 359 | assert(name); |
| 360 | return GetProcAddress(library, name); |
| 361 | } |
| 362 | static char * loader_platform_get_proc_address_error(const char *name) |
| 363 | { |
| 364 | static char errorMsg[120]; |
| 365 | snprintf(errorMsg, 119, "Failed to find function \"%s\" in dynamic library", name); |
| 366 | return errorMsg; |
| 367 | } |
| 368 | |
| 369 | // Threads: |
| 370 | typedef HANDLE loader_platform_thread; |
Jon Ashburn | 87d6aa9 | 2015-08-28 15:19:27 -0600 | [diff] [blame] | 371 | #define THREAD_LOCAL_DECL __declspec(thread) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 372 | #define LOADER_PLATFORM_THREAD_ONCE_DECLARATION(var) \ |
| 373 | INIT_ONCE var = INIT_ONCE_STATIC_INIT; |
Jon Ashburn | 27cd584 | 2015-05-12 17:26:48 -0600 | [diff] [blame] | 374 | #define LOADER_PLATFORM_THREAD_ONCE_DEFINITION(var) \ |
| 375 | INIT_ONCE var; |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 376 | static BOOL CALLBACK InitFuncWrapper(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) |
| 377 | { |
| 378 | void (*func)(void) = (void (*)(void))Parameter; |
| 379 | func(); |
| 380 | return TRUE; |
| 381 | } |
| 382 | |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 383 | static void loader_platform_thread_once(void *ctl, void (* func) (void)) |
| 384 | { |
| 385 | assert(func != NULL); |
| 386 | assert(ctl != NULL); |
Piers Daniell | 4da523a | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 387 | InitOnceExecuteOnce((PINIT_ONCE) ctl, InitFuncWrapper, func, NULL); |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Thread IDs: |
| 391 | typedef DWORD loader_platform_thread_id; |
| 392 | static loader_platform_thread_id loader_platform_get_thread_id() |
| 393 | { |
| 394 | return GetCurrentThreadId(); |
| 395 | } |
| 396 | |
| 397 | // Thread mutex: |
| 398 | typedef CRITICAL_SECTION loader_platform_thread_mutex; |
| 399 | static void loader_platform_thread_create_mutex(loader_platform_thread_mutex* pMutex) |
| 400 | { |
| 401 | InitializeCriticalSection(pMutex); |
| 402 | } |
| 403 | static void loader_platform_thread_lock_mutex(loader_platform_thread_mutex* pMutex) |
| 404 | { |
| 405 | EnterCriticalSection(pMutex); |
| 406 | } |
| 407 | static void loader_platform_thread_unlock_mutex(loader_platform_thread_mutex* pMutex) |
| 408 | { |
| 409 | LeaveCriticalSection(pMutex); |
| 410 | } |
| 411 | static void loader_platform_thread_delete_mutex(loader_platform_thread_mutex* pMutex) |
| 412 | { |
| 413 | DeleteCriticalSection(pMutex); |
| 414 | } |
Mike Stroyan | c3d9833 | 2015-05-15 17:34:51 -0600 | [diff] [blame] | 415 | typedef CONDITION_VARIABLE loader_platform_thread_cond; |
| 416 | static void loader_platform_thread_init_cond(loader_platform_thread_cond* pCond) |
| 417 | { |
| 418 | InitializeConditionVariable(pCond); |
| 419 | } |
| 420 | static void loader_platform_thread_cond_wait(loader_platform_thread_cond* pCond, loader_platform_thread_mutex* pMutex) |
| 421 | { |
| 422 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 423 | } |
| 424 | static void loader_platform_thread_cond_broadcast(loader_platform_thread_cond* pCond) |
| 425 | { |
| 426 | WakeAllConditionVariable(pCond); |
| 427 | } |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 428 | |
Ian Elliott | 5aa4ea2 | 2015-03-31 15:32:41 -0600 | [diff] [blame] | 429 | // Windows Registry: |
| 430 | char *loader_get_registry_string(const HKEY hive, |
| 431 | const LPCTSTR sub_key, |
| 432 | const char *value); |
| 433 | |
Jon Ashburn | 3f8c300 | 2015-10-15 13:47:58 -0600 | [diff] [blame] | 434 | #define loader_stack_alloc(size) _alloca(size) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 435 | #else // defined(_WIN32) |
| 436 | |
| 437 | #error The "loader_platform.h" file must be modified for this OS. |
| 438 | |
| 439 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 440 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 441 | // contents of this file must be created. |
| 442 | |
| 443 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 444 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 445 | |
| 446 | #endif // defined(_WIN32) |
| 447 | |
jon | 547bbee | 2015-10-29 14:57:03 -0600 | [diff] [blame] | 448 | // returns true if the given string appears to be a relative or absolute |
| 449 | // path, as opposed to a bare filename. |
| 450 | static inline bool loader_platform_is_path(const char *path) |
| 451 | { |
| 452 | return strchr(path, DIRECTORY_SYMBOL) != NULL; |
| 453 | } |