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