1 #include "HalideRuntime.h"
2
3 extern "C" {
4
5 #ifdef BITS_64
6 #define WIN32API
7 #else
8 #define WIN32API __stdcall
9 #endif
10
11 typedef unsigned short WCHAR;
12
13 int WIN32API MultiByteToWideChar(
14 unsigned int CodePage,
15 unsigned long dwFlags,
16 const char *lpMultiByteStr,
17 int cbMultiByte,
18 WCHAR *lpWideCharStr,
19 int cchWideChar);
20 WIN32API void *LoadLibraryW(const WCHAR *);
21 WIN32API void *GetProcAddress(void *, const char *);
22 WIN32API unsigned SetErrorMode(unsigned);
23 #define SEM_FAILCRITICALERRORS 0x0001
24 #define SEM_NOOPENFILEERRORBOX 0x8000
25 #define CP_UTF8 65001
26
halide_default_get_symbol(const char * name)27 WEAK void *halide_default_get_symbol(const char *name) {
28 return GetProcAddress(NULL, name);
29 }
30
halide_default_load_library(const char * name)31 WEAK void *halide_default_load_library(const char *name) {
32 // Suppress dialog windows during library open.
33 unsigned old_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
34 void *lib = NULL;
35 int wide_len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
36 if (wide_len > 0) {
37 WCHAR *wide_lib = (WCHAR *)malloc(wide_len * sizeof(*wide_lib));
38 wide_len = MultiByteToWideChar(CP_UTF8, 0, name, -1, wide_lib, wide_len);
39 if (wide_len > 0) {
40 lib = LoadLibraryW(wide_lib);
41 }
42 free(wide_lib);
43 }
44 SetErrorMode(old_mode);
45 return lib;
46 }
47
halide_default_get_library_symbol(void * lib,const char * name)48 WEAK void *halide_default_get_library_symbol(void *lib, const char *name) {
49 return GetProcAddress(lib, name);
50 }
51
52 } // extern "C"
53
54 namespace Halide {
55 namespace Runtime {
56 namespace Internal {
57
58 WEAK halide_get_symbol_t custom_get_symbol = halide_default_get_symbol;
59 WEAK halide_load_library_t custom_load_library = halide_default_load_library;
60 WEAK halide_get_library_symbol_t custom_get_library_symbol = halide_default_get_library_symbol;
61
62 } // namespace Internal
63 } // namespace Runtime
64 } // namespace Halide
65
66 extern "C" {
67
halide_set_custom_get_symbol(halide_get_symbol_t f)68 WEAK halide_get_symbol_t halide_set_custom_get_symbol(halide_get_symbol_t f) {
69 halide_get_symbol_t result = custom_get_symbol;
70 custom_get_symbol = f;
71 return result;
72 }
73
halide_set_custom_load_library(halide_load_library_t f)74 WEAK halide_load_library_t halide_set_custom_load_library(halide_load_library_t f) {
75 halide_load_library_t result = custom_load_library;
76 custom_load_library = f;
77 return result;
78 }
79
halide_set_custom_get_library_symbol(halide_get_library_symbol_t f)80 WEAK halide_get_library_symbol_t halide_set_custom_get_library_symbol(halide_get_library_symbol_t f) {
81 halide_get_library_symbol_t result = custom_get_library_symbol;
82 custom_get_library_symbol = f;
83 return result;
84 }
85
halide_get_symbol(const char * name)86 WEAK void *halide_get_symbol(const char *name) {
87 return custom_get_symbol(name);
88 }
89
halide_load_library(const char * name)90 WEAK void *halide_load_library(const char *name) {
91 return custom_load_library(name);
92 }
93
halide_get_library_symbol(void * lib,const char * name)94 WEAK void *halide_get_library_symbol(void *lib, const char *name) {
95 return custom_get_library_symbol(lib, name);
96 }
97
98 } // extern "C"
99