Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
porting.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5/*
6 Random portability stuff
7*/
8
9#pragma once
10
11#if (defined(__linux__) || defined(__GNU__)) && !defined(_GNU_SOURCE)
12 #define _GNU_SOURCE
13#endif
14
15// Be mindful of what you include here!
16#include <string>
17#include "config.h"
18#include "irrlichttypes.h" // u64
19#include "debug.h"
20#include "constants.h"
21#include "util/timetaker.h" // TimePrecision
22
23#ifdef _MSC_VER
24 #define SWPRINTF_CHARSTRING L"%S"
25#else
26 #define SWPRINTF_CHARSTRING L"%s"
27#endif
28
29#ifdef _WIN32
30 #include <windows.h>
31
32 #define sleep_ms(x) Sleep(x)
33 #define sleep_us(x) Sleep((x)/1000)
34
35 #define SLEEP_ACCURACY_US 2000
36
37 #define setenv(n,v,o) _putenv_s(n,v)
38 #define unsetenv(n) _putenv_s(n,"")
39#else
40 #include <unistd.h>
41 #include <cstdlib> // setenv
42
43 #define SLEEP_ACCURACY_US 200
44
45 #define sleep_ms(x) usleep((x)*1000)
46 #define sleep_us(x) usleep(x)
47#endif
48
49#ifdef _MSC_VER
50 #define strtok_r(x, y, z) strtok_s(x, y, z)
51 #define strtof(x, y) (float)strtod(x, y)
52 #define strtoll(x, y, z) _strtoi64(x, y, z)
53 #define strtoull(x, y, z) _strtoui64(x, y, z)
54 #define strcasecmp(x, y) stricmp(x, y)
55 #define strncasecmp(x, y, n) strnicmp(x, y, n)
56#endif
57
58#ifdef __MINGW32__
59 // was broken in 2013, unclear if still needed
60 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
61#endif
62
63#if !HAVE_STRLCPY
64 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
65#endif
66
67#ifndef _WIN32 // POSIX
68 #include <sys/time.h>
69 #include <ctime>
70 #if defined(__MACH__) && defined(__APPLE__)
71 #include <TargetConditionals.h>
72 #endif
73#endif
74
75namespace porting
76{
77
78/*
79 Signal handler (grabs Ctrl-C on POSIX systems)
80*/
81
83// Returns a pointer to a bool.
84// When the bool is true, program should quit.
86
87/*
88 Path of static data directory.
89*/
90extern std::string path_share;
91
92/*
93 Directory for storing user data. Examples:
94 Windows: "C:\Documents and Settings\user\Application Data<PROJECT_NAME>"
95 Linux: "~/.<PROJECT_NAME>"
96 Mac: "~/Library/Application Support/<PROJECT_NAME>"
97*/
98extern std::string path_user;
99
100/*
101 Path to gettext locale files
102*/
103extern std::string path_locale;
104
105/*
106 Path to directory for storing caches.
107*/
108extern std::string path_cache;
109
110/*
111 Gets the path of our executable.
112*/
113bool getCurrentExecPath(char *buf, size_t len);
114
115/*
116 Concatenate subpath to path_share.
117*/
118std::string getDataPath(const char *subpath);
119
120/*
121 Initialize path_*.
122*/
123void initializePaths();
124
125/*
126 Return system information
127 e.g. "Linux/3.12.7 x86_64"
128*/
129const std::string &get_sysinfo();
130
131
132// Monotonic timer
133
134#ifdef _WIN32 // Windows
135
136extern double perf_freq;
137
138inline u64 os_get_time(double mult)
139{
140 LARGE_INTEGER t;
141 QueryPerformanceCounter(&t);
142 return static_cast<double>(t.QuadPart) / (perf_freq / mult);
143}
144
145// Resolution is <1us.
146inline u64 getTimeS() { return os_get_time(1); }
147inline u64 getTimeMs() { return os_get_time(1000); }
148inline u64 getTimeUs() { return os_get_time(1000*1000); }
149inline u64 getTimeNs() { return os_get_time(1000*1000*1000); }
150
151#else // POSIX
152
153inline void os_get_clock(struct timespec *ts)
154{
155#if defined(CLOCK_MONOTONIC_RAW)
156 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
157#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0
158 clock_gettime(CLOCK_MONOTONIC, ts);
159#else
160# if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK == 0
161 // zero means it might be supported at runtime
162 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
163 return;
164# endif
165 struct timeval tv;
166 gettimeofday(&tv, NULL);
167 TIMEVAL_TO_TIMESPEC(&tv, ts);
168#endif
169}
170
171inline u64 getTimeS()
172{
173 struct timespec ts;
174 os_get_clock(&ts);
175 return ts.tv_sec;
176}
177
178inline u64 getTimeMs()
179{
180 struct timespec ts;
181 os_get_clock(&ts);
182 return ((u64) ts.tv_sec) * 1000LL + ((u64) ts.tv_nsec) / 1000000LL;
183}
184
185inline u64 getTimeUs()
186{
187 struct timespec ts;
188 os_get_clock(&ts);
189 return ((u64) ts.tv_sec) * 1000000LL + ((u64) ts.tv_nsec) / 1000LL;
190}
191
192inline u64 getTimeNs()
193{
194 struct timespec ts;
195 os_get_clock(&ts);
196 return ((u64) ts.tv_sec) * 1000000000LL + ((u64) ts.tv_nsec);
197}
198
199#endif
200
201inline u64 getTime(TimePrecision prec)
202{
203 switch (prec) {
204 case PRECISION_SECONDS: return getTimeS();
205 case PRECISION_MILLI: return getTimeMs();
206 case PRECISION_MICRO: return getTimeUs();
207 case PRECISION_NANO: return getTimeNs();
208 }
209 FATAL_ERROR("Called getTime with invalid time precision");
210}
211
218inline u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
219{
220 if (new_time_ms >= old_time_ms) {
221 return (new_time_ms - old_time_ms);
222 }
223
224 return (old_time_ms - new_time_ms);
225}
226
227inline void preciseSleepUs(u64 sleep_time)
228{
229 if (sleep_time > 0)
230 {
231 u64 target_time = porting::getTimeUs() + sleep_time;
232 if (sleep_time > SLEEP_ACCURACY_US)
233 sleep_us(sleep_time - SLEEP_ACCURACY_US);
234
235 // Busy-wait the remaining time to adjust for sleep inaccuracies
236 // The target - now > 0 construct will handle overflow gracefully (even though it should
237 // never happen)
238 while ((s64)(target_time - porting::getTimeUs()) > 0) {}
239 }
240}
241
242inline const char *getPlatformName()
243{
244 return
245#if defined(ANDROID)
246 "Android"
247#elif defined(__linux__)
248 "Linux"
249#elif defined(_WIN32) || defined(_WIN64)
250 "Windows"
251#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
252 defined(__NetBSD__) || defined(__OpenBSD__)
253 "BSD"
254#elif defined(__APPLE__) && defined(__MACH__)
255 #if TARGET_OS_MAC
256 "OSX"
257 #elif TARGET_OS_IPHONE
258 "iOS"
259 #else
260 "Apple"
261 #endif
262#elif defined(_AIX)
263 "AIX"
264#elif defined(__hpux)
265 "HP-UX"
266#elif defined(__sun) || defined(sun)
267 #if defined(__SVR4)
268 "Solaris"
269 #else
270 "SunOS"
271 #endif
272#elif defined(__HAIKU__)
273 "Haiku"
274#elif defined(__CYGWIN__)
275 "Cygwin"
276#elif defined(__unix__) || defined(__unix)
277 #if defined(_POSIX_VERSION)
278 "POSIX"
279 #else
280 "Unix"
281 #endif
282#else
283 "?"
284#endif
285 ;
286}
287
288bool secure_rand_fill_buf(void *buf, size_t len);
289
290// Call once near beginning of main function.
291void osSpecificInit();
292
293// This attaches to the parents process console, or creates a new one if it doesnt exist.
295
296#if HAVE_MALLOC_TRIM
302void TrackFreedMemory(size_t amount);
303
308void TriggerMemoryTrim();
309#else
310static inline void TrackFreedMemory(size_t amount) { (void)amount; }
311static inline void TriggerMemoryTrim() { (void)0; }
312#endif
313
314#ifdef _WIN32
315// Quotes an argument for use in a CreateProcess() commandline (not cmd.exe!!)
316std::string QuoteArgv(const std::string &arg);
317
318// Convert an error code (e.g. from GetLastError()) into a string.
319std::string ConvertError(DWORD error_code);
320#endif
321
322// snprintf wrapper
323int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
324
333bool open_url(const std::string &url);
334
343bool open_directory(const std::string &path);
344
345} // namespace porting
346
347#ifdef __ANDROID__
348#include "porting_android.h"
349#endif
#define FATAL_ERROR(msg)
Definition debug.h:32
Definition porting.cpp:78
u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
Delta calculation function arguments.
Definition porting.h:218
std::string path_locale
Definition porting.cpp:161
bool secure_rand_fill_buf(void *buf, size_t len)
Definition porting.cpp:749
std::string path_share
Definition porting.cpp:159
int mt_snprintf(char *buf, const size_t buf_size, const char *fmt,...)
Definition porting.cpp:845
void os_get_clock(struct timespec *ts)
Definition porting.h:153
void signal_handler_init(void)
Definition porting.cpp:115
void osSpecificInit()
Definition porting.cpp:770
static void TrackFreedMemory(size_t amount)
Definition porting.h:310
bool open_directory(const std::string &path)
Opens a directory in the default file manager.
Definition porting.cpp:904
u64 getTimeS()
Definition porting.h:171
std::string path_cache
Definition porting.cpp:162
std::string path_user
Definition porting.cpp:160
u64 getTime(TimePrecision prec)
Definition porting.h:201
bool * signal_handler_killstatus()
Definition porting.cpp:86
u64 getTimeMs()
Definition porting.h:178
bool open_url(const std::string &url)
Opens URL in default web browser.
Definition porting.cpp:894
u64 getTimeNs()
Definition porting.h:192
const char * getPlatformName()
Definition porting.h:242
bool getCurrentExecPath(char *buf, size_t len)
Definition porting.cpp:405
std::string getDataPath(const char *subpath)
Definition porting.cpp:165
void initializePaths()
Definition porting.cpp:614
void attachOrCreateConsole()
Definition porting.cpp:783
void preciseSleepUs(u64 sleep_time)
Definition porting.h:227
static void TriggerMemoryTrim()
Definition porting.h:311
u64 getTimeUs()
Definition porting.h:185
const std::string & get_sysinfo()
Definition porting.cpp:266
#define sleep_us(x)
Definition porting.h:46
#define SLEEP_ACCURACY_US
Definition porting.h:43
TimePrecision
Definition timetaker.h:11
@ PRECISION_SECONDS
Definition timetaker.h:12
@ PRECISION_NANO
Definition timetaker.h:15
@ PRECISION_MICRO
Definition timetaker.h:14
@ PRECISION_MILLI
Definition timetaker.h:13