Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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 #else
91         #define ALIGNOF(x) __alignof__(x)
92 #endif
93
94 #ifdef __MINGW32__
95         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
96 #endif
97
98 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
99
100 namespace porting
101 {
102
103 /*
104         Signal handler (grabs Ctrl-C on POSIX systems)
105 */
106
107 void signal_handler_init(void);
108 // Returns a pointer to a bool.
109 // When the bool is true, program should quit.
110 bool * signal_handler_killstatus(void);
111
112 /*
113         Path of static data directory.
114 */
115 extern std::string path_share;
116
117 /*
118         Directory for storing user data. Examples:
119         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
120         Linux: "~/.<PROJECT_NAME>"
121         Mac: "~/Library/Application Support/<PROJECT_NAME>"
122 */
123 extern std::string path_user;
124
125 /*
126         Get full path of stuff in data directory.
127         Example: "stone.png" -> "../data/stone.png"
128 */
129 std::string getDataPath(const char *subpath);
130
131 /*
132         Initialize path_share and path_user.
133 */
134 void initializePaths();
135
136 /*
137         Get number of online processors in the system.
138 */
139 int getNumberOfProcessors();
140
141 /*
142         Set a thread's affinity to a particular processor.
143 */
144 bool threadBindToProcessor(threadid_t tid, int pnumber);
145
146 /*
147         Set a thread's priority.
148 */
149 bool threadSetPriority(threadid_t tid, int prio);
150
151 /*
152         Resolution is 10-20ms.
153         Remember to check for overflows.
154         Overflow can occur at any value higher than 10000000.
155 */
156 #ifdef _WIN32 // Windows
157 #ifndef _WIN32_WINNT
158         #define _WIN32_WINNT 0x0501
159 #endif
160         #include <windows.h>
161         
162         inline u32 getTimeS()
163         {
164                 return GetTickCount() / 1000;
165         }
166         
167         inline u32 getTimeMs()
168         {
169                 return GetTickCount();
170         }
171         
172         inline u32 getTimeUs()
173         {
174                 LARGE_INTEGER freq, t;
175                 QueryPerformanceFrequency(&freq);
176                 QueryPerformanceCounter(&t);
177                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
178         }
179         
180         inline u32 getTimeNs()
181         {
182                 LARGE_INTEGER freq, t;
183                 QueryPerformanceFrequency(&freq);
184                 QueryPerformanceCounter(&t);
185                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
186         }
187         
188 #else // Posix
189         #include <sys/time.h>
190         #include <time.h>
191         
192         inline u32 getTimeS()
193         {
194                 struct timeval tv;
195                 gettimeofday(&tv, NULL);
196                 return tv.tv_sec;
197         }
198         
199         inline u32 getTimeMs()
200         {
201                 struct timeval tv;
202                 gettimeofday(&tv, NULL);
203                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
204         }
205         
206         inline u32 getTimeUs()
207         {
208                 struct timeval tv;
209                 gettimeofday(&tv, NULL);
210                 return tv.tv_sec * 1000000 + tv.tv_usec;
211         }
212         
213         inline u32 getTimeNs()
214         {
215                 struct timespec ts;
216                 clock_gettime(CLOCK_REALTIME, &ts);
217                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
218         }
219         
220         /*#include <sys/timeb.h>
221         inline u32 getTimeMs()
222         {
223                 struct timeb tb;
224                 ftime(&tb);
225                 return tb.time * 1000 + tb.millitm;
226         }*/
227 #endif
228
229 inline u32 getTime(TimePrecision prec)
230 {
231         switch (prec) {
232                 case PRECISION_SECONDS:
233                         return getTimeS();
234                 case PRECISION_MILLI:
235                         return getTimeMs();
236                 case PRECISION_MICRO:
237                         return getTimeUs();
238                 case PRECISION_NANO:
239                         return getTimeNs();
240         }
241         return 0;
242 }
243
244
245 } // namespace porting
246
247 #endif // PORTING_HEADER
248