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
36 #include "irrlichttypes.h" // u32
38 #include "constants.h"
43 #define SWPRINTF_CHARSTRING L"%S"
45 #define SWPRINTF_CHARSTRING L"%s"
48 //currently not needed
49 //template<typename T> struct alignment_trick { char c; T member; };
50 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
55 #define sleep_ms(x) Sleep(x)
58 #include <stdint.h> //for uintptr_t
60 #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
67 #include <pthread_np.h>
68 typedef cpuset_t cpu_set_t;
69 #elif defined(__sun) || defined(sun)
70 #include <sys/types.h>
71 #include <sys/processor.h>
73 #include <sys/processor.h>
75 #include <mach/mach_init.h>
76 #include <mach/thread_policy.h>
79 #define sleep_ms(x) usleep(x*1000)
81 #define THREAD_PRIORITY_LOWEST 0
82 #define THREAD_PRIORITY_BELOW_NORMAL 1
83 #define THREAD_PRIORITY_NORMAL 2
84 #define THREAD_PRIORITY_ABOVE_NORMAL 3
85 #define THREAD_PRIORITY_HIGHEST 4
89 #define ALIGNOF(x) __alignof(x)
90 #define strtok_r(x, y, z) strtok_s(x, y, z)
91 #define strtof(x, y) (float)strtod(x, y)
92 #define strtoll(x, y, z) _strtoi64(x, y, z)
93 #define strtoull(x, y, z) _strtoui64(x, y, z)
94 #define strcasecmp(x, y) stricmp(x, y)
95 #define strncasecmp(x, y, n) strnicmp(x, y, n)
97 #define ALIGNOF(x) __alignof__(x)
101 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
104 // strlcpy is missing from glibc. thanks a lot, drepper.
105 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
106 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
107 // default raises an assertion error and aborts the program if the buffer is
109 #if defined(__FreeBSD__) || defined(__NetBSD__) || \
110 defined(__OpenBSD__) || defined(__DragonFly__) || \
111 defined(__APPLE__) || \
112 defined(__sun) || defined(sun) || \
113 defined(__QNX__) || defined(__QNXNTO__)
117 // So we need to define our own.
119 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
122 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
128 Signal handler (grabs Ctrl-C on POSIX systems)
131 void signal_handler_init(void);
132 // Returns a pointer to a bool.
133 // When the bool is true, program should quit.
134 bool * signal_handler_killstatus(void);
137 Path of static data directory.
139 extern std::string path_share;
142 Directory for storing user data. Examples:
143 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
144 Linux: "~/.<PROJECT_NAME>"
145 Mac: "~/Library/Application Support/<PROJECT_NAME>"
147 extern std::string path_user;
150 Get full path of stuff in data directory.
151 Example: "stone.png" -> "../data/stone.png"
153 std::string getDataPath(const char *subpath);
156 Initialize path_share and path_user.
158 void initializePaths();
161 Get number of online processors in the system.
163 int getNumberOfProcessors();
166 Set a thread's affinity to a particular processor.
168 bool threadBindToProcessor(threadid_t tid, int pnumber);
171 Set a thread's priority.
173 bool threadSetPriority(threadid_t tid, int prio);
176 Return system information
177 e.g. "Linux/3.12.7 x86_64"
179 std::string get_sysinfo();
182 Resolution is 10-20ms.
183 Remember to check for overflows.
184 Overflow can occur at any value higher than 10000000.
186 #ifdef _WIN32 // Windows
188 #define _WIN32_WINNT 0x0501
192 inline u32 getTimeS()
194 return GetTickCount() / 1000;
197 inline u32 getTimeMs()
199 return GetTickCount();
202 inline u32 getTimeUs()
204 LARGE_INTEGER freq, t;
205 QueryPerformanceFrequency(&freq);
206 QueryPerformanceCounter(&t);
207 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
210 inline u32 getTimeNs()
212 LARGE_INTEGER freq, t;
213 QueryPerformanceFrequency(&freq);
214 QueryPerformanceCounter(&t);
215 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
219 #include <sys/time.h>
222 inline u32 getTimeS()
225 gettimeofday(&tv, NULL);
229 inline u32 getTimeMs()
232 gettimeofday(&tv, NULL);
233 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
236 inline u32 getTimeUs()
239 gettimeofday(&tv, NULL);
240 return tv.tv_sec * 1000000 + tv.tv_usec;
243 inline u32 getTimeNs()
246 clock_gettime(CLOCK_REALTIME, &ts);
247 return ts.tv_sec * 1000000000 + ts.tv_nsec;
250 /*#include <sys/timeb.h>
251 inline u32 getTimeMs()
255 return tb.time * 1000 + tb.millitm;
259 inline u32 getTime(TimePrecision prec)
262 case PRECISION_SECONDS:
264 case PRECISION_MILLI:
266 case PRECISION_MICRO:
274 #if defined(linux) || defined(__linux)
275 #include <sys/prctl.h>
277 inline void setThreadName(const char *name) {
278 /* It would be cleaner to do this with pthread_setname_np,
279 * which was added to glibc in version 2.12, but some major
280 * distributions are still runing 2.11 and previous versions.
282 prctl(PR_SET_NAME, name);
284 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
287 inline void setThreadName(const char *name) {
288 pthread_set_name_np(pthread_self(), name);
290 #elif defined(_MSC_VER)
291 typedef struct tagTHREADNAME_INFO {
292 DWORD dwType; // must be 0x1000
293 LPCSTR szName; // pointer to name (in user addr space)
294 DWORD dwThreadID; // thread ID (-1=caller thread)
295 DWORD dwFlags; // reserved for future use, must be zero
298 inline void setThreadName(const char *name) {
299 THREADNAME_INFO info;
300 info.dwType = 0x1000;
302 info.dwThreadID = -1;
305 RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (DWORD *) &info);
306 } __except (EXCEPTION_CONTINUE_EXECUTION) {}
308 #elif defined(__APPLE__)
311 inline void setThreadName(const char *name) {
312 pthread_setname_np(name);
314 #elif defined(_WIN32)
315 inline void setThreadName(const char* name) {}
317 #warning "Unrecognized platform, thread names will not be available."
318 inline void setThreadName(const char* name) {}
321 } // namespace porting
323 #endif // PORTING_HEADER