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
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 #ifndef OFFLOAD_UTIL_H_INCLUDED
32 #define OFFLOAD_UTIL_H_INCLUDED
39 #define DLL_LOCAL __attribute__((visibility("hidden")))
43 // Don't use <stdint.h> as compiling with VS2010 makes ofldbegin.obj
44 // incompatible with STL library of versions older than VS2010.
45 typedef unsigned long long int uint64_t;
46 typedef signed long long int int64_t;
53 #endif // TARGET_WINNT
56 typedef unsigned pthread_key_t
;
59 #define __func__ __FUNCTION__
60 #define strtok_r(s,d,p) strtok_s(s,d,p)
61 #define strcasecmp(a,b) stricmp(a,b)
63 #define thread_key_create(key, destructor) \
64 (((*key = TlsAlloc()) > 0) ? 0 : GetLastError())
65 #define thread_key_delete(key) TlsFree(key)
68 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
71 void* thread_getspecific(pthread_key_t key
);
72 int thread_setspecific(pthread_key_t key
, const void *value
);
74 #define thread_key_create(key, destructor) \
75 pthread_key_create((key), (destructor))
76 #define thread_key_delete(key) pthread_key_delete(key)
77 #define thread_getspecific(key) pthread_getspecific(key)
78 #define thread_setspecific(key, value) pthread_setspecific(key, value)
79 #endif // TARGET_WINNT
81 // Mutex implementation
85 InitializeCriticalSection(&m_lock
);
87 pthread_mutex_init(&m_lock
, 0);
88 #endif // TARGET_WINNT
93 DeleteCriticalSection(&m_lock
);
95 pthread_mutex_destroy(&m_lock
);
96 #endif // TARGET_WINNT
101 EnterCriticalSection(&m_lock
);
102 #else // TARGET_WINNT
103 pthread_mutex_lock(&m_lock
);
104 #endif // TARGET_WINNT
109 LeaveCriticalSection(&m_lock
);
110 #else // TARGET_WINNT
111 pthread_mutex_unlock(&m_lock
);
112 #endif // TARGET_WINNT
117 CRITICAL_SECTION m_lock
;
119 pthread_mutex_t m_lock
;
123 struct mutex_locker_t
{
124 mutex_locker_t(mutex_t
&mutex
) : m_mutex(mutex
) {
136 // Dynamic loader interface
140 char dli_fname
[MAX_PATH
];
142 char dli_sname
[MAX_PATH
];
143 const void *dli_saddr
;
146 void* DL_open(const char *path
);
147 #define DL_close(handle) FreeLibrary((HMODULE) (handle))
148 int DL_addr(const void *addr
, Dl_info
*info
);
150 #define DL_open(path) dlopen((path), RTLD_NOW)
151 #define DL_close(handle) dlclose(handle)
152 #define DL_addr(addr, info) dladdr((addr), (info))
153 #endif // TARGET_WINNT
155 DLL_LOCAL
extern void* DL_sym(void *handle
, const char *name
, const char *version
);
157 // One-time initialization API
159 typedef INIT_ONCE OffloadOnceControl
;
160 #define OFFLOAD_ONCE_CONTROL_INIT INIT_ONCE_STATIC_INIT
162 extern void __offload_run_once(OffloadOnceControl
*ctrl
, void (*func
)(void));
164 typedef pthread_once_t OffloadOnceControl
;
165 #define OFFLOAD_ONCE_CONTROL_INIT PTHREAD_ONCE_INIT
167 #define __offload_run_once(ctrl, func) pthread_once(ctrl, func)
168 #endif // TARGET_WINNT
170 // Parses size specification string.
171 DLL_LOCAL
extern bool __offload_parse_size_string(const char *str
, uint64_t &new_size
);
173 // Parses string with integer value
174 DLL_LOCAL
extern bool __offload_parse_int_string(const char *str
, int64_t &value
);
176 // get value by its base, offset and size
177 DLL_LOCAL
int64_t get_el_value(
182 #endif // OFFLOAD_UTIL_H_INCLUDED