PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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