3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Random portability stuff
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
31 #define _WIN32_WINNT 0x0501 // We need to do this before any other headers
32 // because those might include sdkddkver.h which defines _WIN32_WINNT if not already set
37 #include "irrlichttypes.h" // u32
38 #include "irrlichttypes_extrabloated.h"
40 #include "constants.h"
45 #define SWPRINTF_CHARSTRING L"%S"
47 #define SWPRINTF_CHARSTRING L"%s"
50 //currently not needed
51 //template<typename T> struct alignment_trick { char c; T member; };
52 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
57 #define sleep_ms(x) Sleep(x)
60 #include <stdint.h> //for uintptr_t
62 #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
69 #include <pthread_np.h>
70 typedef cpuset_t cpu_set_t;
71 #elif defined(__sun) || defined(sun)
72 #include <sys/types.h>
73 #include <sys/processor.h>
75 #include <sys/processor.h>
77 #include <mach/mach_init.h>
78 #include <mach/thread_policy.h>
81 #define sleep_ms(x) usleep(x*1000)
83 #define THREAD_PRIORITY_LOWEST 0
84 #define THREAD_PRIORITY_BELOW_NORMAL 1
85 #define THREAD_PRIORITY_NORMAL 2
86 #define THREAD_PRIORITY_ABOVE_NORMAL 3
87 #define THREAD_PRIORITY_HIGHEST 4
91 #define ALIGNOF(x) __alignof(x)
92 #define strtok_r(x, y, z) strtok_s(x, y, z)
93 #define strtof(x, y) (float)strtod(x, y)
94 #define strtoll(x, y, z) _strtoi64(x, y, z)
95 #define strtoull(x, y, z) _strtoui64(x, y, z)
96 #define strcasecmp(x, y) stricmp(x, y)
97 #define strncasecmp(x, y, n) strnicmp(x, y, n)
99 #define ALIGNOF(x) __alignof__(x)
103 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
106 // strlcpy is missing from glibc. thanks a lot, drepper.
107 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
108 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
109 // default raises an assertion error and aborts the program if the buffer is
111 #if defined(__FreeBSD__) || defined(__NetBSD__) || \
112 defined(__OpenBSD__) || defined(__DragonFly__) || \
113 defined(__APPLE__) || \
114 defined(__sun) || defined(sun) || \
115 defined(__QNX__) || defined(__QNXNTO__)
119 // So we need to define our own.
121 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
124 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
126 #if defined(__APPLE__)
127 #include <mach-o/dyld.h>
128 #include <CoreFoundation/CoreFoundation.h>
135 Signal handler (grabs Ctrl-C on POSIX systems)
138 void signal_handler_init(void);
139 // Returns a pointer to a bool.
140 // When the bool is true, program should quit.
141 bool * signal_handler_killstatus(void);
144 Path of static data directory.
146 extern std::string path_share;
149 Directory for storing user data. Examples:
150 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
151 Linux: "~/.<PROJECT_NAME>"
152 Mac: "~/Library/Application Support/<PROJECT_NAME>"
154 extern std::string path_user;
157 Get full path of stuff in data directory.
158 Example: "stone.png" -> "../data/stone.png"
160 std::string getDataPath(const char *subpath);
163 Initialize path_share and path_user.
165 void initializePaths();
168 Get number of online processors in the system.
170 int getNumberOfProcessors();
173 Set a thread's affinity to a particular processor.
175 bool threadBindToProcessor(threadid_t tid, int pnumber);
178 Set a thread's priority.
180 bool threadSetPriority(threadid_t tid, int prio);
183 Return system information
184 e.g. "Linux/3.12.7 x86_64"
186 std::string get_sysinfo();
188 void initIrrlicht(irr::IrrlichtDevice * );
191 Resolution is 10-20ms.
192 Remember to check for overflows.
193 Overflow can occur at any value higher than 10000000.
195 #ifdef _WIN32 // Windows
197 #define _WIN32_WINNT 0x0501
201 inline u32 getTimeS()
203 return GetTickCount() / 1000;
206 inline u32 getTimeMs()
208 return GetTickCount();
211 inline u32 getTimeUs()
213 LARGE_INTEGER freq, t;
214 QueryPerformanceFrequency(&freq);
215 QueryPerformanceCounter(&t);
216 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
219 inline u32 getTimeNs()
221 LARGE_INTEGER freq, t;
222 QueryPerformanceFrequency(&freq);
223 QueryPerformanceCounter(&t);
224 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
228 #include <sys/time.h>
231 #include <mach/clock.h>
232 #include <mach/mach.h>
235 inline u32 getTimeS()
238 gettimeofday(&tv, NULL);
242 inline u32 getTimeMs()
245 gettimeofday(&tv, NULL);
246 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
249 inline u32 getTimeUs()
252 gettimeofday(&tv, NULL);
253 return tv.tv_sec * 1000000 + tv.tv_usec;
256 inline u32 getTimeNs()
259 // from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
260 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
263 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
264 clock_get_time(cclock, &mts);
265 mach_port_deallocate(mach_task_self(), cclock);
266 ts.tv_sec = mts.tv_sec;
267 ts.tv_nsec = mts.tv_nsec;
269 clock_gettime(CLOCK_REALTIME, &ts);
271 return ts.tv_sec * 1000000000 + ts.tv_nsec;
274 /*#include <sys/timeb.h>
275 inline u32 getTimeMs()
279 return tb.time * 1000 + tb.millitm;
283 inline u32 getTime(TimePrecision prec)
286 case PRECISION_SECONDS:
288 case PRECISION_MILLI:
290 case PRECISION_MICRO:
299 * Delta calculation function taking two 32bit arguments.
300 * @param old_time_ms old time for delta calculation (order is relevant!)
301 * @param new_time_ms new time for delta calculation (order is relevant!)
302 * @return positive 32bit delta value
304 inline u32 getDeltaMs(u32 old_time_ms, u32 new_time_ms)
306 if (new_time_ms >= old_time_ms) {
307 return (new_time_ms - old_time_ms);
309 return (old_time_ms - new_time_ms);
313 #if defined(linux) || defined(__linux)
314 #include <sys/prctl.h>
316 inline void setThreadName(const char *name) {
317 /* It would be cleaner to do this with pthread_setname_np,
318 * which was added to glibc in version 2.12, but some major
319 * distributions are still runing 2.11 and previous versions.
321 prctl(PR_SET_NAME, name);
323 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
325 #include <pthread_np.h>
327 inline void setThreadName(const char *name) {
328 pthread_set_name_np(pthread_self(), name);
330 #elif defined(__NetBSD__)
333 inline void setThreadName(const char *name) {
334 pthread_setname_np(pthread_self(), name);
336 #elif defined(_MSC_VER)
337 typedef struct tagTHREADNAME_INFO {
338 DWORD dwType; // must be 0x1000
339 LPCSTR szName; // pointer to name (in user addr space)
340 DWORD dwThreadID; // thread ID (-1=caller thread)
341 DWORD dwFlags; // reserved for future use, must be zero
344 inline void setThreadName(const char *name) {
345 THREADNAME_INFO info;
346 info.dwType = 0x1000;
348 info.dwThreadID = -1;
351 RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR *) &info);
352 } __except (EXCEPTION_CONTINUE_EXECUTION) {}
354 #elif defined(__APPLE__)
357 inline void setThreadName(const char *name) {
358 pthread_setname_np(name);
360 #elif defined(_WIN32)
361 inline void setThreadName(const char* name) {}
363 #warning "Unrecognized platform, thread names will not be available."
364 inline void setThreadName(const char* name) {}
368 float getDisplayDensity();
370 v2u32 getDisplaySize();
371 v2u32 getWindowSize();
374 } // namespace porting
377 #include "porting_android.h"
380 #endif // PORTING_HEADER