/src/core/Platform.h

http://github.com/imageworks/OpenColorIO · C Header · 204 lines · 106 code · 23 blank · 75 comment · 3 complexity · 93ff6ec3a47c21a37f36d1a75eb31f50 MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef INCLUDED_OCIO_PLATFORM_H
  28. #define INCLUDED_OCIO_PLATFORM_H
  29. /*
  30. PTEX SOFTWARE
  31. Copyright 2009 Disney Enterprises, Inc. All rights reserved
  32. Redistribution and use in source and binary forms, with or without
  33. modification, are permitted provided that the following conditions are
  34. met:
  35. * Redistributions of source code must retain the above copyright
  36. notice, this list of conditions and the following disclaimer.
  37. * Redistributions in binary form must reproduce the above copyright
  38. notice, this list of conditions and the following disclaimer in
  39. the documentation and/or other materials provided with the
  40. distribution.
  41. * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
  42. Studios" or the names of its contributors may NOT be used to
  43. endorse or promote products derived from this software without
  44. specific prior written permission from Walt Disney Pictures.
  45. Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
  46. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  47. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  48. FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
  49. IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
  50. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  51. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  52. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  53. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
  54. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  55. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  56. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  57. */
  58. // platform-specific includes
  59. #if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS) || defined(_MSC_VER)
  60. #ifndef WINDOWS
  61. #define WINDOWS
  62. #endif
  63. #define _CRT_NONSTDC_NO_DEPRECATE 1
  64. #define _CRT_SECURE_NO_DEPRECATE 1
  65. #define NOMINMAX 1
  66. // windows - defined for both Win32 and Win64
  67. #include <Windows.h>
  68. #include <malloc.h>
  69. #include <io.h>
  70. #include <tchar.h>
  71. #include <process.h>
  72. #else
  73. // linux/unix/posix
  74. #include <stdlib.h>
  75. #if !defined(__FreeBSD__)
  76. #include <alloca.h>
  77. #endif
  78. #include <string.h>
  79. #include <pthread.h>
  80. // OS for spinlock
  81. #ifdef __APPLE__
  82. #include <libkern/OSAtomic.h>
  83. #include <sys/types.h>
  84. #endif
  85. #endif
  86. // general includes
  87. #include <stdio.h>
  88. #include <math.h>
  89. #include <assert.h>
  90. // missing functions on Windows
  91. #ifdef WINDOWS
  92. #define snprintf sprintf_s
  93. #define strtok_r strtok_s
  94. typedef __int64 FilePos;
  95. #define fseeko _fseeki64
  96. #define ftello _ftelli64
  97. inline double log2(double x) {
  98. return log(x) * 1.4426950408889634;
  99. }
  100. #else
  101. typedef off_t FilePos;
  102. #endif
  103. OCIO_NAMESPACE_ENTER
  104. {
  105. // TODO: Add proper endian detection using architecture / compiler mojo
  106. // In the meantime, hardcode to x86
  107. #define OCIO_LITTLE_ENDIAN 1 // This is correct on x86
  108. /*
  109. * Mutex/SpinLock classes
  110. */
  111. #ifdef WINDOWS
  112. class _Mutex {
  113. public:
  114. _Mutex() { _mutex = CreateMutex(NULL, FALSE, NULL); }
  115. ~_Mutex() { CloseHandle(_mutex); }
  116. void lock() { WaitForSingleObject(_mutex, INFINITE); }
  117. void unlock() { ReleaseMutex(_mutex); }
  118. private:
  119. HANDLE _mutex;
  120. };
  121. class _SpinLock {
  122. public:
  123. _SpinLock() { InitializeCriticalSection(&_spinlock); }
  124. ~_SpinLock() { DeleteCriticalSection(&_spinlock); }
  125. void lock() { EnterCriticalSection(&_spinlock); }
  126. void unlock() { LeaveCriticalSection(&_spinlock); }
  127. private:
  128. CRITICAL_SECTION _spinlock;
  129. };
  130. #else
  131. // assume linux/unix/posix
  132. class _Mutex {
  133. public:
  134. _Mutex() { pthread_mutex_init(&_mutex, 0); }
  135. ~_Mutex() { pthread_mutex_destroy(&_mutex); }
  136. void lock() { pthread_mutex_lock(&_mutex); }
  137. void unlock() { pthread_mutex_unlock(&_mutex); }
  138. private:
  139. pthread_mutex_t _mutex;
  140. };
  141. #if __APPLE__
  142. class _SpinLock {
  143. public:
  144. _SpinLock() { _spinlock = 0; }
  145. ~_SpinLock() { }
  146. void lock() { OSSpinLockLock(&_spinlock); }
  147. void unlock() { OSSpinLockUnlock(&_spinlock); }
  148. private:
  149. OSSpinLock _spinlock;
  150. };
  151. #elif ANDROID
  152. // we don't have access to pthread on andriod so we just make an empty
  153. // class that does nothing.
  154. class _SpinLock {
  155. public:
  156. _SpinLock() { }
  157. ~_SpinLock() { }
  158. void lock() { }
  159. void unlock() { }
  160. };
  161. #else
  162. class _SpinLock {
  163. public:
  164. _SpinLock() { pthread_spin_init(&_spinlock, PTHREAD_PROCESS_PRIVATE); }
  165. ~_SpinLock() { pthread_spin_destroy(&_spinlock); }
  166. void lock() { pthread_spin_lock(&_spinlock); }
  167. void unlock() { pthread_spin_unlock(&_spinlock); }
  168. private:
  169. pthread_spinlock_t _spinlock;
  170. };
  171. #endif // __APPLE__
  172. #endif // WINDOWS
  173. }
  174. OCIO_NAMESPACE_EXIT
  175. #endif // INCLUDED_OCIO_PLATFORM_H