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
28 #include "irrlichttypes.h" // u32
30 #include "constants.h"
35 #define SWPRINTF_CHARSTRING L"%S"
37 #define SWPRINTF_CHARSTRING L"%s"
40 //currently not needed
41 //template<typename T> struct alignment_trick { char c; T member; };
42 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
46 #define _WIN32_WINNT 0x0501
50 #define sleep_ms(x) Sleep(x)
53 #include <stdint.h> //for uintptr_t
55 #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
62 #include <pthread_np.h>
63 typedef cpuset_t cpu_set_t;
64 #elif defined(__sun) || defined(sun)
65 #include <sys/types.h>
66 #include <sys/processor.h>
68 #include <sys/processor.h>
70 #include <mach/mach_init.h>
71 #include <mach/thread_policy.h>
74 #define sleep_ms(x) usleep(x*1000)
76 #define THREAD_PRIORITY_LOWEST 0
77 #define THREAD_PRIORITY_BELOW_NORMAL 1
78 #define THREAD_PRIORITY_NORMAL 2
79 #define THREAD_PRIORITY_ABOVE_NORMAL 3
80 #define THREAD_PRIORITY_HIGHEST 4
84 #define ALIGNOF(x) __alignof(x)
85 #define strtok_r(x, y, z) strtok_s(x, y, z)
86 #define strtof(x, y) (float)strtod(x, y)
87 #define strtoll(x, y, z) _strtoi64(x, y, z)
88 #define strtoull(x, y, z) _strtoui64(x, y, z)
89 #define strcasecmp(x, y) stricmp(x, y)
90 #define strncasecmp(x, y, n) strnicmp(x, y, n)
92 #define ALIGNOF(x) __alignof__(x)
96 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
99 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
105 Signal handler (grabs Ctrl-C on POSIX systems)
108 void signal_handler_init(void);
109 // Returns a pointer to a bool.
110 // When the bool is true, program should quit.
111 bool * signal_handler_killstatus(void);
114 Path of static data directory.
116 extern std::string path_share;
119 Directory for storing user data. Examples:
120 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
121 Linux: "~/.<PROJECT_NAME>"
122 Mac: "~/Library/Application Support/<PROJECT_NAME>"
124 extern std::string path_user;
127 Get full path of stuff in data directory.
128 Example: "stone.png" -> "../data/stone.png"
130 std::string getDataPath(const char *subpath);
133 Initialize path_share and path_user.
135 void initializePaths();
138 Get number of online processors in the system.
140 int getNumberOfProcessors();
143 Set a thread's affinity to a particular processor.
145 bool threadBindToProcessor(threadid_t tid, int pnumber);
148 Set a thread's priority.
150 bool threadSetPriority(threadid_t tid, int prio);
153 Return system information
154 e.g. "Linux/3.12.7 x86_64"
156 std::string get_sysinfo();
159 Resolution is 10-20ms.
160 Remember to check for overflows.
161 Overflow can occur at any value higher than 10000000.
163 #ifdef _WIN32 // Windows
165 #define _WIN32_WINNT 0x0501
169 inline u32 getTimeS()
171 return GetTickCount() / 1000;
174 inline u32 getTimeMs()
176 return GetTickCount();
179 inline u32 getTimeUs()
181 LARGE_INTEGER freq, t;
182 QueryPerformanceFrequency(&freq);
183 QueryPerformanceCounter(&t);
184 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
187 inline u32 getTimeNs()
189 LARGE_INTEGER freq, t;
190 QueryPerformanceFrequency(&freq);
191 QueryPerformanceCounter(&t);
192 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
196 #include <sys/time.h>
199 inline u32 getTimeS()
202 gettimeofday(&tv, NULL);
206 inline u32 getTimeMs()
209 gettimeofday(&tv, NULL);
210 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
213 inline u32 getTimeUs()
216 gettimeofday(&tv, NULL);
217 return tv.tv_sec * 1000000 + tv.tv_usec;
220 inline u32 getTimeNs()
223 clock_gettime(CLOCK_REALTIME, &ts);
224 return ts.tv_sec * 1000000000 + ts.tv_nsec;
227 /*#include <sys/timeb.h>
228 inline u32 getTimeMs()
232 return tb.time * 1000 + tb.millitm;
236 inline u32 getTime(TimePrecision prec)
239 case PRECISION_SECONDS:
241 case PRECISION_MILLI:
243 case PRECISION_MICRO:
252 } // namespace porting
254 #endif // PORTING_HEADER