]> git.lizzy.rs Git - dragonfireclient.git/blob - src/porting.h
d7d1073406e69a558d1452aa29b1e42e5178e374
[dragonfireclient.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
32 #ifdef _MSC_VER
33         #define SWPRINTF_CHARSTRING L"%S"
34 #else
35         #define SWPRINTF_CHARSTRING L"%s"
36 #endif
37
38 //currently not needed
39 //template<typename T> struct alignment_trick { char c; T member; };
40 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
41
42 #ifdef _WIN32
43         #ifndef _WIN32_WINNT
44                 #define _WIN32_WINNT 0x0500
45         #endif
46         #include <windows.h>
47         
48         #define sleep_ms(x) Sleep(x)
49 #else
50         #include <unistd.h>
51         #include <stdint.h> //for uintptr_t
52         
53         #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
54                 #define _GNU_SOURCE
55         #endif
56
57         #include <sched.h>
58
59         #ifdef __FreeBSD__
60                 #include <pthread_np.h>
61                 typedef cpuset_t cpu_set_t;
62         #elif defined(__sun) || defined(sun)
63                 #include <sys/types.h>
64                 #include <sys/processor.h>
65         #elif defined(_AIX)
66                 #include <sys/processor.h>
67         #elif __APPLE__
68                 #include <mach/mach_init.h>
69                 #include <mach/thread_policy.h>
70         #endif
71
72         #define sleep_ms(x) usleep(x*1000)
73         
74         #define THREAD_PRIORITY_LOWEST       0
75         #define THREAD_PRIORITY_BELOW_NORMAL 1
76         #define THREAD_PRIORITY_NORMAL       2
77         #define THREAD_PRIORITY_ABOVE_NORMAL 3
78         #define THREAD_PRIORITY_HIGHEST      4
79 #endif
80
81 #ifdef _MSC_VER
82         #define ALIGNOF(x) __alignof(x)
83         #define strtok_r(x, y, z) strtok_s(x, y, z)
84         #define strtof(x, y) (float)strtod(x, y)
85         #define strtoll(x, y, z) _strtoi64(x, y, z)
86         #define strtoull(x, y, z) _strtoui64(x, y, z)
87         #define strcasecmp(x, y) stricmp(x, y)
88 #else
89         #define ALIGNOF(x) __alignof__(x)
90 #endif
91
92 #ifdef __MINGW32__
93         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
94 #endif
95
96 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
97
98 namespace porting
99 {
100
101 /*
102         Signal handler (grabs Ctrl-C on POSIX systems)
103 */
104
105 void signal_handler_init(void);
106 // Returns a pointer to a bool.
107 // When the bool is true, program should quit.
108 bool * signal_handler_killstatus(void);
109
110 /*
111         Path of static data directory.
112 */
113 extern std::string path_share;
114
115 /*
116         Directory for storing user data. Examples:
117         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
118         Linux: "~/.<PROJECT_NAME>"
119         Mac: "~/Library/Application Support/<PROJECT_NAME>"
120 */
121 extern std::string path_user;
122
123 /*
124         Get full path of stuff in data directory.
125         Example: "stone.png" -> "../data/stone.png"
126 */
127 std::string getDataPath(const char *subpath);
128
129 /*
130         Initialize path_share and path_user.
131 */
132 void initializePaths();
133
134 /*
135         Get number of online processors in the system.
136 */
137 int getNumberOfProcessors();
138
139 /*
140         Set a thread's affinity to a particular processor.
141 */
142 bool threadBindToProcessor(threadid_t tid, int pnumber);
143
144 /*
145         Set a thread's priority.
146 */
147 bool threadSetPriority(threadid_t tid, int prio);
148
149 /*
150         Resolution is 10-20ms.
151         Remember to check for overflows.
152         Overflow can occur at any value higher than 10000000.
153 */
154 #ifdef _WIN32 // Windows
155         #include <windows.h>
156         inline u32 getTimeMs()
157         {
158                 return GetTickCount();
159         }
160 #else // Posix
161         #include <sys/time.h>
162         inline u32 getTimeMs()
163         {
164                 struct timeval tv;
165                 gettimeofday(&tv, NULL);
166                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
167         }
168         /*#include <sys/timeb.h>
169         inline u32 getTimeMs()
170         {
171                 struct timeb tb;
172                 ftime(&tb);
173                 return tb.time * 1000 + tb.millitm;
174         }*/
175 #endif
176
177 } // namespace porting
178
179 #endif // PORTING_HEADER
180