1 /*
2     Copyright (c) 2014-2016 Intel Corporation.  All Rights Reserved.
3 
4     Redistribution and use in source and binary forms, with or without
5     modification, are permitted provided that the following conditions
6     are met:
7 
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.
16 
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.
28 */
29 
30 
31 #ifndef OFFLOAD_UTIL_H_INCLUDED
32 #define OFFLOAD_UTIL_H_INCLUDED
33 
34 #include <stdlib.h>
35 
36 #ifdef TARGET_WINNT
37     #define DLL_LOCAL
38 #else
39     #define DLL_LOCAL  __attribute__((visibility("hidden")))
40 #endif
41 
42 #ifdef TARGET_WINNT
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;
47 #include <windows.h>
48 #include <process.h>
49 #else // TARGET_WINNT
50 #include <stdint.h>
51 #include <dlfcn.h>
52 #include <pthread.h>
53 #endif // TARGET_WINNT
54 
55 #ifdef TARGET_WINNT
56 typedef unsigned pthread_key_t;
57 typedef int pid_t;
58 
59 #define __func__ __FUNCTION__
60 #define strtok_r(s,d,p) strtok_s(s,d,p)
61 #define strcasecmp(a,b) stricmp(a,b)
62 
63 #define thread_key_create(key, destructor) \
64     (((*key = TlsAlloc()) > 0) ? 0 : GetLastError())
65 #define thread_key_delete(key) TlsFree(key)
66 
67 #ifndef S_ISREG
68 #define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
69 #endif
70 
71 void*   thread_getspecific(pthread_key_t key);
72 int     thread_setspecific(pthread_key_t key, const void *value);
73 #else
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
80 
81 // Mutex implementation
82 struct mutex_t {
mutex_tmutex_t83     mutex_t() {
84 #ifdef TARGET_WINNT
85         InitializeCriticalSection(&m_lock);
86 #else // TARGET_WINNT
87         pthread_mutex_init(&m_lock, 0);
88 #endif // TARGET_WINNT
89     }
90 
~mutex_tmutex_t91     ~mutex_t() {
92 #ifdef TARGET_WINNT
93         DeleteCriticalSection(&m_lock);
94 #else // TARGET_WINNT
95         pthread_mutex_destroy(&m_lock);
96 #endif // TARGET_WINNT
97     }
98 
lockmutex_t99     void lock() {
100 #ifdef TARGET_WINNT
101         EnterCriticalSection(&m_lock);
102 #else // TARGET_WINNT
103         pthread_mutex_lock(&m_lock);
104 #endif // TARGET_WINNT
105     }
106 
unlockmutex_t107     void unlock() {
108 #ifdef TARGET_WINNT
109         LeaveCriticalSection(&m_lock);
110 #else // TARGET_WINNT
111         pthread_mutex_unlock(&m_lock);
112 #endif // TARGET_WINNT
113     }
114 
115 private:
116 #ifdef TARGET_WINNT
117     CRITICAL_SECTION    m_lock;
118 #else
119     pthread_mutex_t     m_lock;
120 #endif
121 };
122 
123 struct mutex_locker_t {
mutex_locker_tmutex_locker_t124     mutex_locker_t(mutex_t &mutex) : m_mutex(mutex) {
125         m_mutex.lock();
126     }
127 
~mutex_locker_tmutex_locker_t128     ~mutex_locker_t() {
129         m_mutex.unlock();
130     }
131 
132 private:
133     mutex_t &m_mutex;
134 };
135 
136 // Dynamic loader interface
137 #ifdef TARGET_WINNT
138 struct Dl_info
139 {
140     char        dli_fname[MAX_PATH];
141     void       *dli_fbase;
142     char        dli_sname[MAX_PATH];
143     const void *dli_saddr;
144 };
145 
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);
149 #else
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
154 
155 DLL_LOCAL extern void* DL_sym(void *handle, const char *name, const char *version);
156 
157 // One-time initialization API
158 #ifdef TARGET_WINNT
159 typedef INIT_ONCE                   OffloadOnceControl;
160 #define OFFLOAD_ONCE_CONTROL_INIT   INIT_ONCE_STATIC_INIT
161 
162 extern void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void));
163 #else
164 typedef pthread_once_t              OffloadOnceControl;
165 #define OFFLOAD_ONCE_CONTROL_INIT   PTHREAD_ONCE_INIT
166 
167 #define __offload_run_once(ctrl, func) pthread_once(ctrl, func)
168 #endif // TARGET_WINNT
169 
170 // Parses size specification string.
171 DLL_LOCAL extern bool __offload_parse_size_string(const char *str, uint64_t &new_size);
172 
173 // Parses string with integer value
174 DLL_LOCAL extern bool __offload_parse_int_string(const char *str, int64_t &value);
175 
176 // get value by its base, offset and size
177 DLL_LOCAL int64_t get_el_value(
178     char   *base,
179     int64_t offset,
180     int64_t size
181 );
182 #endif // OFFLOAD_UTIL_H_INCLUDED
183