aix.h (TARGET_IEEEQUAD_DEFAULT): Set long double default to IBM.
[official-gcc.git] / liboffloadmic / runtime / offload_util.cpp
blobbb434871f6025b737c5d98a2e6e8daecf1429663
1 /*
2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "offload_util.h"
32 #include <errno.h>
33 #include "liboffload_error_codes.h"
35 #ifdef TARGET_WINNT
36 void *thread_getspecific(pthread_key_t key)
38 if (key == 0) {
39 return NULL;
41 else {
42 return TlsGetValue(key);
46 int thread_setspecific(pthread_key_t key, const void *value)
48 return (TlsSetValue(key, (LPVOID)value)) ? 0 : GetLastError();
50 #endif // TARGET_WINNT
52 bool __offload_parse_size_string(const char *str, uint64_t &new_size)
54 uint64_t val;
55 char *suffix;
57 errno = 0;
58 #ifdef TARGET_WINNT
59 val = strtoul(str, &suffix, 10);
60 #else // TARGET_WINNT
61 val = strtoull(str, &suffix, 10);
62 #endif // TARGET_WINNT
63 if (errno != 0 || suffix == str) {
64 return false;
67 if (suffix[0] == '\0') {
68 // default is Kilobytes
69 new_size = val * 1024;
70 return true;
72 else if (suffix[1] == '\0') {
73 // Optional suffixes: B (bytes), K (Kilobytes), M (Megabytes),
74 // G (Gigabytes), or T (Terabytes) specify the units.
75 switch (suffix[0]) {
76 case 'b':
77 case 'B':
78 new_size = val;
79 break;
81 case 'k':
82 case 'K':
83 new_size = val * 1024;
84 break;
86 case 'm':
87 case 'M':
88 new_size = val * 1024 * 1024;
89 break;
91 case 'g':
92 case 'G':
93 new_size = val * 1024 * 1024 * 1024;
94 break;
96 case 't':
97 case 'T':
98 new_size = val * 1024 * 1024 * 1024 * 1024;
99 break;
101 default:
102 return false;
104 return true;
107 return false;
110 bool __offload_parse_int_string(const char *str, int64_t &value)
112 int64_t val;
113 char *suffix;
115 errno = 0;
116 #ifdef TARGET_WINNT
117 val = strtol(str, &suffix, 0);
118 #else
119 val = strtoll(str, &suffix, 0);
120 #endif
121 if (errno == 0 && suffix != str && *suffix == '\0') {
122 value = val;
123 return true;
125 return false;
128 #ifdef TARGET_WINNT
129 extern void* DL_open(const char *path)
131 void *handle;
132 int error_mode;
135 * do not display message box with error if it the call below fails to
136 * load dynamic library.
138 error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
140 /* load dynamic library */
141 handle = (void*) LoadLibrary(path);
143 /* restore error mode */
144 SetErrorMode(error_mode);
146 return handle;
149 extern int DL_addr(const void *addr, Dl_info *dl_info)
151 MEMORY_BASIC_INFORMATION mem_info;
152 char mod_name[MAX_PATH];
153 HMODULE mod_handle;
155 /* Fill MEMORY_BASIC_INFORMATION struct */
156 if (!VirtualQuery(addr, &mem_info, sizeof(mem_info))) {
157 return 0;
159 mod_handle = (HMODULE)mem_info.AllocationBase;
161 /* ANSI file name for module */
162 if (!GetModuleFileNameA(mod_handle, (char*) mod_name, sizeof(mod_name))) {
163 return 0;
165 strcpy(dl_info->dli_fname, mod_name);
166 dl_info->dli_fbase = mem_info.BaseAddress;
167 dl_info->dli_saddr = addr;
168 strcpy(dl_info->dli_sname, mod_name);
169 return 1;
172 // Run once
173 static BOOL CALLBACK __offload_run_once_wrapper(
174 PINIT_ONCE initOnce,
175 PVOID parameter,
176 PVOID *context
179 void (*init_routine)(void) = (void(*)(void)) parameter;
180 init_routine();
181 return true;
184 void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void))
186 InitOnceExecuteOnce(ctrl, __offload_run_once_wrapper, (void*) func, 0);
188 #endif // TARGET_WINNT
190 /* ARGSUSED */ // version is not used on windows
191 void* DL_sym(void *handle, const char *name, const char *version)
193 #ifdef TARGET_WINNT
194 return GetProcAddress((HMODULE) handle, name);
195 #else // TARGET_WINNT
196 if (version == 0) {
197 return dlsym(handle, name);
199 else {
200 return dlvsym(handle, name, version);
202 #endif // TARGET_WINNT
205 int64_t get_el_value(
206 char *base,
207 int64_t offset,
208 int64_t size)
210 int64_t val = 0;
211 switch (size) {
212 case 1:
213 val = static_cast<int64_t>(*((char *)(base + offset)));
214 break;
215 case 2:
216 val = static_cast<int64_t>(*((short *)(base + offset)));
217 break;
218 case 4:
219 val = static_cast<int64_t>(*((int *)(base + offset)));
220 break;
221 default:
222 val = *((int64_t *)(base + offset));
223 break;
225 return val;