offload_util.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef OFFLOAD_UTIL_H_INCLUDED
  27. #define OFFLOAD_UTIL_H_INCLUDED
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #ifdef TARGET_WINNT
  32. #include <windows.h>
  33. #include <process.h>
  34. #else // TARGET_WINNT
  35. #include <dlfcn.h>
  36. #include <pthread.h>
  37. #endif // TARGET_WINNT
  38. #ifdef TARGET_WINNT
  39. typedef unsigned pthread_key_t;
  40. typedef int pid_t;
  41. #define __func__ __FUNCTION__
  42. #define strtok_r(s,d,p) strtok_s(s,d,p)
  43. #define strcasecmp(a,b) stricmp(a,b)
  44. #define thread_key_create(key, destructor) \
  45. (((*key = TlsAlloc()) > 0) ? 0 : GetLastError())
  46. #define thread_key_delete(key) TlsFree(key)
  47. #ifndef S_ISREG
  48. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  49. #endif
  50. void* thread_getspecific(pthread_key_t key);
  51. int thread_setspecific(pthread_key_t key, const void *value);
  52. #else
  53. #define thread_key_create(key, destructor) \
  54. pthread_key_create((key), (destructor))
  55. #define thread_key_delete(key) pthread_key_delete(key)
  56. #define thread_getspecific(key) pthread_getspecific(key)
  57. #define thread_setspecific(key, value) pthread_setspecific(key, value)
  58. #endif // TARGET_WINNT
  59. // Mutex implementation
  60. struct mutex_t {
  61. mutex_t() {
  62. #ifdef TARGET_WINNT
  63. InitializeCriticalSection(&m_lock);
  64. #else // TARGET_WINNT
  65. pthread_mutex_init(&m_lock, 0);
  66. #endif // TARGET_WINNT
  67. }
  68. ~mutex_t() {
  69. #ifdef TARGET_WINNT
  70. DeleteCriticalSection(&m_lock);
  71. #else // TARGET_WINNT
  72. pthread_mutex_destroy(&m_lock);
  73. #endif // TARGET_WINNT
  74. }
  75. void lock() {
  76. #ifdef TARGET_WINNT
  77. EnterCriticalSection(&m_lock);
  78. #else // TARGET_WINNT
  79. pthread_mutex_lock(&m_lock);
  80. #endif // TARGET_WINNT
  81. }
  82. void unlock() {
  83. #ifdef TARGET_WINNT
  84. LeaveCriticalSection(&m_lock);
  85. #else // TARGET_WINNT
  86. pthread_mutex_unlock(&m_lock);
  87. #endif // TARGET_WINNT
  88. }
  89. private:
  90. #ifdef TARGET_WINNT
  91. CRITICAL_SECTION m_lock;
  92. #else
  93. pthread_mutex_t m_lock;
  94. #endif
  95. };
  96. struct mutex_locker_t {
  97. mutex_locker_t(mutex_t &mutex) : m_mutex(mutex) {
  98. m_mutex.lock();
  99. }
  100. ~mutex_locker_t() {
  101. m_mutex.unlock();
  102. }
  103. private:
  104. mutex_t &m_mutex;
  105. };
  106. // Dynamic loader interface
  107. #ifdef TARGET_WINNT
  108. struct Dl_info
  109. {
  110. char dli_fname[MAX_PATH];
  111. void *dli_fbase;
  112. char dli_sname[MAX_PATH];
  113. const void *dli_saddr;
  114. };
  115. void* DL_open(const char *path);
  116. #define DL_close(handle) FreeLibrary((HMODULE) (handle))
  117. int DL_addr(const void *addr, Dl_info *info);
  118. #else
  119. #define DL_open(path) dlopen((path), RTLD_NOW)
  120. #define DL_close(handle) dlclose(handle)
  121. #define DL_addr(addr, info) dladdr((addr), (info))
  122. #endif // TARGET_WINNT
  123. extern void* DL_sym(void *handle, const char *name, const char *version);
  124. // One-time initialization API
  125. #ifdef TARGET_WINNT
  126. typedef INIT_ONCE OffloadOnceControl;
  127. #define OFFLOAD_ONCE_CONTROL_INIT INIT_ONCE_STATIC_INIT
  128. extern void __offload_run_once(OffloadOnceControl *ctrl, void (*func)(void));
  129. #else
  130. typedef pthread_once_t OffloadOnceControl;
  131. #define OFFLOAD_ONCE_CONTROL_INIT PTHREAD_ONCE_INIT
  132. #define __offload_run_once(ctrl, func) pthread_once(ctrl, func)
  133. #endif // TARGET_WINNT
  134. // Parses size specification string.
  135. extern bool __offload_parse_size_string(const char *str, uint64_t &new_size);
  136. // Parses string with integer value
  137. extern bool __offload_parse_int_string(const char *str, int64_t &value);
  138. // get value by its base, offset and size
  139. int64_t get_el_value(
  140. char *base,
  141. int64_t offset,
  142. int64_t size
  143. );
  144. #endif // OFFLOAD_UTIL_H_INCLUDED