Fix broken win32+bsd build
[oweals/minetest.git] / src / porting.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21         Random portability stuff
22 */
23
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
26
27 #include <string>
28 #include "irrlichttypes.h" // u32
29 #include "debug.h"
30 #include "constants.h"
31 #include "gettime.h"
32 #include "threads.h"
33
34 #ifdef _MSC_VER
35         #define SWPRINTF_CHARSTRING L"%S"
36 #else
37         #define SWPRINTF_CHARSTRING L"%s"
38 #endif
39
40 //currently not needed
41 //template<typename T> struct alignment_trick { char c; T member; };
42 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
43
44 #ifdef _WIN32
45         #ifndef _WIN32_WINNT
46                 #define _WIN32_WINNT 0x0501
47         #endif
48         #include <windows.h>
49         
50         #define sleep_ms(x) Sleep(x)
51 #else
52         #include <unistd.h>
53         #include <stdint.h> //for uintptr_t
54         
55         #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
56                 #define _GNU_SOURCE
57         #endif
58
59         #include <sched.h>
60
61         #ifdef __FreeBSD__
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>
67         #elif defined(_AIX)
68                 #include <sys/processor.h>
69         #elif __APPLE__
70                 #include <mach/mach_init.h>
71                 #include <mach/thread_policy.h>
72         #endif
73
74         #define sleep_ms(x) usleep(x*1000)
75         
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
81 #endif
82
83 #ifdef _MSC_VER
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)
91 #else
92         #define ALIGNOF(x) __alignof__(x)
93 #endif
94
95 #ifdef __MINGW32__
96         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
97 #endif
98
99 // strlcpy is missing from glibc.  thanks a lot, drepper.
100 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
101 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
102 // default raises an assertion error and aborts the program if the buffer is
103 // too small.
104 #if defined(__FreeBSD__) || defined(__NetBSD__)    || \
105         defined(__OpenBSD__) || defined(__DragonFly__) || \
106         defined(__APPLE__)   ||                           \
107         defined(__sun)       || defined(sun)           || \
108         defined(__QNX__)     || defined(__QNXNTO__)
109         #define HAVE_STRLCPY
110 #endif
111
112 // So we need to define our own.
113 #ifndef HAVE_STRLCPY
114         #define strlcpy(d, s, n) mystrlcpy(d, s, n)
115 #endif
116
117 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
118
119 namespace porting
120 {
121
122 /*
123         Signal handler (grabs Ctrl-C on POSIX systems)
124 */
125
126 void signal_handler_init(void);
127 // Returns a pointer to a bool.
128 // When the bool is true, program should quit.
129 bool * signal_handler_killstatus(void);
130
131 /*
132         Path of static data directory.
133 */
134 extern std::string path_share;
135
136 /*
137         Directory for storing user data. Examples:
138         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
139         Linux: "~/.<PROJECT_NAME>"
140         Mac: "~/Library/Application Support/<PROJECT_NAME>"
141 */
142 extern std::string path_user;
143
144 /*
145         Get full path of stuff in data directory.
146         Example: "stone.png" -> "../data/stone.png"
147 */
148 std::string getDataPath(const char *subpath);
149
150 /*
151         Initialize path_share and path_user.
152 */
153 void initializePaths();
154
155 /*
156         Get number of online processors in the system.
157 */
158 int getNumberOfProcessors();
159
160 /*
161         Set a thread's affinity to a particular processor.
162 */
163 bool threadBindToProcessor(threadid_t tid, int pnumber);
164
165 /*
166         Set a thread's priority.
167 */
168 bool threadSetPriority(threadid_t tid, int prio);
169
170 /*
171         Return system information
172         e.g. "Linux/3.12.7 x86_64"
173 */
174 std::string get_sysinfo();
175
176 /*
177         Resolution is 10-20ms.
178         Remember to check for overflows.
179         Overflow can occur at any value higher than 10000000.
180 */
181 #ifdef _WIN32 // Windows
182 #ifndef _WIN32_WINNT
183         #define _WIN32_WINNT 0x0501
184 #endif
185         #include <windows.h>
186         
187         inline u32 getTimeS()
188         {
189                 return GetTickCount() / 1000;
190         }
191         
192         inline u32 getTimeMs()
193         {
194                 return GetTickCount();
195         }
196         
197         inline u32 getTimeUs()
198         {
199                 LARGE_INTEGER freq, t;
200                 QueryPerformanceFrequency(&freq);
201                 QueryPerformanceCounter(&t);
202                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
203         }
204         
205         inline u32 getTimeNs()
206         {
207                 LARGE_INTEGER freq, t;
208                 QueryPerformanceFrequency(&freq);
209                 QueryPerformanceCounter(&t);
210                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
211         }
212         
213 #else // Posix
214         #include <sys/time.h>
215         #include <time.h>
216         
217         inline u32 getTimeS()
218         {
219                 struct timeval tv;
220                 gettimeofday(&tv, NULL);
221                 return tv.tv_sec;
222         }
223         
224         inline u32 getTimeMs()
225         {
226                 struct timeval tv;
227                 gettimeofday(&tv, NULL);
228                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
229         }
230         
231         inline u32 getTimeUs()
232         {
233                 struct timeval tv;
234                 gettimeofday(&tv, NULL);
235                 return tv.tv_sec * 1000000 + tv.tv_usec;
236         }
237         
238         inline u32 getTimeNs()
239         {
240                 struct timespec ts;
241                 clock_gettime(CLOCK_REALTIME, &ts);
242                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
243         }
244         
245         /*#include <sys/timeb.h>
246         inline u32 getTimeMs()
247         {
248                 struct timeb tb;
249                 ftime(&tb);
250                 return tb.time * 1000 + tb.millitm;
251         }*/
252 #endif
253
254 inline u32 getTime(TimePrecision prec)
255 {
256         switch (prec) {
257                 case PRECISION_SECONDS:
258                         return getTimeS();
259                 case PRECISION_MILLI:
260                         return getTimeMs();
261                 case PRECISION_MICRO:
262                         return getTimeUs();
263                 case PRECISION_NANO:
264                         return getTimeNs();
265         }
266         return 0;
267 }
268
269 #if (defined(linux) || defined(__linux))
270
271 #include <sys/prctl.h>
272
273 inline void setThreadName(const char* name) {
274         prctl(PR_SET_NAME,name);
275 }
276 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
277 /* BSD doesn't seem to support thread names. If you know about a way 
278  * to add this feature please create a pull request.
279  * "setproctitle" doesn't work for threadnames.
280  */
281 inline void setThreadName(const char* name) {}
282 #elif defined(_WIN32)
283 // threadnames are not supported on windows
284 inline void setThreadName(const char* name) {}
285 #else
286 #warning "Unknown platform for setThreadName support, you wont have threadname support."
287 inline void setThreadName(const char* name) {}
288 #endif
289
290 } // namespace porting
291
292 #endif // PORTING_HEADER
293