2 Copyright (c) 2014 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
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"
33 #include "liboffload_error_codes.h"
36 void *thread_getspecific(pthread_key_t key
)
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
)
59 val
= strtoul(str
, &suffix
, 10);
61 val
= strtoull(str
, &suffix
, 10);
62 #endif // TARGET_WINNT
63 if (errno
!= 0 || suffix
== str
) {
67 if (suffix
[0] == '\0') {
68 // default is Kilobytes
69 new_size
= val
* 1024;
72 else if (suffix
[1] == '\0') {
73 // Optional suffixes: B (bytes), K (Kilobytes), M (Megabytes),
74 // G (Gigabytes), or T (Terabytes) specify the units.
83 new_size
= val
* 1024;
88 new_size
= val
* 1024 * 1024;
93 new_size
= val
* 1024 * 1024 * 1024;
98 new_size
= val
* 1024 * 1024 * 1024 * 1024;
110 bool __offload_parse_int_string(const char *str
, int64_t &value
)
117 val
= strtol(str
, &suffix
, 0);
119 val
= strtoll(str
, &suffix
, 0);
121 if (errno
== 0 && suffix
!= str
&& *suffix
== '\0') {
129 extern void* DL_open(const char *path
)
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
);
149 extern int DL_addr(const void *addr
, Dl_info
*dl_info
)
151 MEMORY_BASIC_INFORMATION mem_info
;
152 char mod_name
[MAX_PATH
];
155 /* Fill MEMORY_BASIC_INFORMATION struct */
156 if (!VirtualQuery(addr
, &mem_info
, sizeof(mem_info
))) {
159 mod_handle
= (HMODULE
)mem_info
.AllocationBase
;
161 /* ANSI file name for module */
162 if (!GetModuleFileNameA(mod_handle
, (char*) mod_name
, sizeof(mod_name
))) {
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
);
173 static BOOL CALLBACK
__offload_run_once_wrapper(
179 void (*init_routine
)(void) = (void(*)(void)) parameter
;
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
)
194 return GetProcAddress((HMODULE
) handle
, name
);
195 #else // TARGET_WINNT
197 return dlsym(handle
, name
);
200 return dlvsym(handle
, name
, version
);
202 #endif // TARGET_WINNT
205 int64_t get_el_value(
213 val
= static_cast<int64_t>(*((char *)(base
+ offset
)));
216 val
= static_cast<int64_t>(*((short *)(base
+ offset
)));
219 val
= static_cast<int64_t>(*((int *)(base
+ offset
)));
222 val
= *((int64_t *)(base
+ offset
));