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))
130 Signal handler (grabs Ctrl-C on POSIX systems)
133 void signal_handler_init(void);
134 // Returns a pointer to a bool.
135 // When the bool is true, program should quit.
136 bool * signal_handler_killstatus(void);
139 Path of static data directory.
141 extern std::string path_share;
144 Directory for storing user data. Examples:
145 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
146 Linux: "~/.<PROJECT_NAME>"
147 Mac: "~/Library/Application Support/<PROJECT_NAME>"
149 extern std::string path_user;
152 Get full path of stuff in data directory.
153 Example: "stone.png" -> "../data/stone.png"
155 std::string getDataPath(const char *subpath);
158 Initialize path_share and path_user.
160 void initializePaths();
163 Get number of online processors in the system.
165 int getNumberOfProcessors();
168 Set a thread's affinity to a particular processor.
170 bool threadBindToProcessor(threadid_t tid, int pnumber);
173 Set a thread's priority.
175 bool threadSetPriority(threadid_t tid, int prio);
178 Return system information
179 e.g. "Linux/3.12.7 x86_64"
181 std::string get_sysinfo();
183 void initIrrlicht(irr::IrrlichtDevice * );
186 Resolution is 10-20ms.
187 Remember to check for overflows.
188 Overflow can occur at any value higher than 10000000.
190 #ifdef _WIN32 // Windows
192 #define _WIN32_WINNT 0x0501
196 inline u32 getTimeS()
198 return GetTickCount() / 1000;
201 inline u32 getTimeMs()
203 return GetTickCount();
206 inline u32 getTimeUs()
208 LARGE_INTEGER freq, t;
209 QueryPerformanceFrequency(&freq);
210 QueryPerformanceCounter(&t);
211 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
214 inline u32 getTimeNs()
216 LARGE_INTEGER freq, t;
217 QueryPerformanceFrequency(&freq);
218 QueryPerformanceCounter(&t);
219 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
223 #include <sys/time.h>
226 inline u32 getTimeS()
229 gettimeofday(&tv, NULL);
233 inline u32 getTimeMs()
236 gettimeofday(&tv, NULL);
237 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
240 inline u32 getTimeUs()
243 gettimeofday(&tv, NULL);
244 return tv.tv_sec * 1000000 + tv.tv_usec;
247 inline u32 getTimeNs()
250 clock_gettime(CLOCK_REALTIME, &ts);
251 return ts.tv_sec * 1000000000 + ts.tv_nsec;
254 /*#include <sys/timeb.h>
255 inline u32 getTimeMs()
259 return tb.time * 1000 + tb.millitm;
263 inline u32 getTime(TimePrecision prec)
266 case PRECISION_SECONDS:
268 case PRECISION_MILLI:
270 case PRECISION_MICRO:
278 #if defined(linux) || defined(__linux)
279 #include <sys/prctl.h>
281 inline void setThreadName(const char *name) {
282 /* It would be cleaner to do this with pthread_setname_np,
283 * which was added to glibc in version 2.12, but some major
284 * distributions are still runing 2.11 and previous versions.
286 prctl(PR_SET_NAME, name);
288 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
290 #include <pthread_np.h>
292 inline void setThreadName(const char *name) {
293 pthread_set_name_np(pthread_self(), name);
295 #elif defined(__NetBSD__)
298 inline void setThreadName(const char *name) {
299 pthread_setname_np(pthread_self(), name);
301 #elif defined(_MSC_VER)
302 typedef struct tagTHREADNAME_INFO {
303 DWORD dwType; // must be 0x1000
304 LPCSTR szName; // pointer to name (in user addr space)
305 DWORD dwThreadID; // thread ID (-1=caller thread)
306 DWORD dwFlags; // reserved for future use, must be zero
309 inline void setThreadName(const char *name) {
310 THREADNAME_INFO info;
311 info.dwType = 0x1000;
313 info.dwThreadID = -1;
316 RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR *) &info);
317 } __except (EXCEPTION_CONTINUE_EXECUTION) {}
319 #elif defined(__APPLE__)
322 inline void setThreadName(const char *name) {
323 pthread_setname_np(name);
325 #elif defined(_WIN32)
326 inline void setThreadName(const char* name) {}
328 #warning "Unrecognized platform, thread names will not be available."
329 inline void setThreadName(const char* name) {}
333 float getDisplayDensity();
335 v2u32 getDisplaySize();
336 v2u32 getWindowSize();
344 } // namespace porting
346 #endif // PORTING_HEADER