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"
33 #define SWPRINTF_CHARSTRING L"%S"
35 #define SWPRINTF_CHARSTRING L"%s"
38 //currently not needed
39 //template<typename T> struct alignment_trick { char c; T member; };
40 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
45 #define sleep_ms(x) Sleep(x)
48 #include <stdint.h> //for uintptr_t
50 #if defined(linux) || defined(__linux)
57 #include <pthread_np.h>
58 typedef cpuset_t cpu_set_t;
59 #elif defined(__sun) || defined(sun)
60 #include <sys/types.h>
61 #include <sys/processor.h>
63 #include <sys/processor.h>
65 #include <mach/mach_init.h>
66 #include <mach/thread_policy.h>
69 #define sleep_ms(x) usleep(x*1000)
71 #define THREAD_PRIORITY_LOWEST 0
72 #define THREAD_PRIORITY_BELOW_NORMAL 1
73 #define THREAD_PRIORITY_NORMAL 2
74 #define THREAD_PRIORITY_ABOVE_NORMAL 3
75 #define THREAD_PRIORITY_HIGHEST 4
79 #define ALIGNOF(x) __alignof(x)
80 #define strtok_r(x, y, z) strtok_s(x, y, z)
81 #define strtof(x, y) (float)strtod(x, y)
82 #define strtoll(x, y, z) _strtoi64(x, y, z)
83 #define strtoull(x, y, z) _strtoui64(x, y, z)
84 #define strcasecmp(x, y) stricmp(x, y)
86 #define ALIGNOF(x) __alignof__(x)
90 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
93 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
99 Signal handler (grabs Ctrl-C on POSIX systems)
102 void signal_handler_init(void);
103 // Returns a pointer to a bool.
104 // When the bool is true, program should quit.
105 bool * signal_handler_killstatus(void);
108 Path of static data directory.
110 extern std::string path_share;
113 Directory for storing user data. Examples:
114 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
115 Linux: "~/.<PROJECT_NAME>"
116 Mac: "~/Library/Application Support/<PROJECT_NAME>"
118 extern std::string path_user;
121 Get full path of stuff in data directory.
122 Example: "stone.png" -> "../data/stone.png"
124 std::string getDataPath(const char *subpath);
127 Initialize path_share and path_user.
129 void initializePaths();
132 Get number of online processors in the system.
134 int getNumberOfProcessors();
137 Set a thread's affinity to a particular processor.
139 bool threadBindToProcessor(threadid_t tid, int pnumber);
142 Set a thread's priority.
144 bool threadSetPriority(threadid_t tid, int prio);
147 Resolution is 10-20ms.
148 Remember to check for overflows.
149 Overflow can occur at any value higher than 10000000.
151 #ifdef _WIN32 // Windows
153 inline u32 getTimeMs()
155 return GetTickCount();
158 #include <sys/time.h>
159 inline u32 getTimeMs()
162 gettimeofday(&tv, NULL);
163 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
165 /*#include <sys/timeb.h>
166 inline u32 getTimeMs()
170 return tb.time * 1000 + tb.millitm;
174 } // namespace porting
176 #endif // PORTING_HEADER