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 setenv(n,v,o) _putenv_s(n,v)
36 #define unsetenv(n) _putenv_s(n,"")
37#else
38 #include <unistd.h>
39 #include <cstdlib> // setenv
40
41 #define sleep_ms(x) usleep((x)*1000)
42 #define sleep_us(x) usleep(x)
43#endif
44
45#ifdef _MSC_VER
46 #define strtok_r(x, y, z) strtok_s(x, y, z)
47 #define strtof(x, y) (float)strtod(x, y)
48 #define strtoll(x, y, z) _strtoi64(x, y, z)
49 #define strtoull(x, y, z) _strtoui64(x, y, z)
50 #define strcasecmp(x, y) stricmp(x, y)
51 #define strncasecmp(x, y, n) strnicmp(x, y, n)
52#endif
53
54#ifdef __MINGW32__
55 // was broken in 2013, unclear if still needed
56 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
57#endif
58
59#if !HAVE_STRLCPY
60 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
61#endif
62
63#ifndef _WIN32 // POSIX
64 #include <sys/time.h>
65 #include <ctime>
66 #if defined(__MACH__) && defined(__APPLE__)
67 #include <TargetConditionals.h>
68 #endif
69#endif
70
71namespace porting
72{
73
74/*
75 Signal handler (grabs Ctrl-C on POSIX systems)
76*/
77
79// Returns a pointer to a bool.
80// When the bool is true, program should quit.
82
83/*
84 Path of static data directory.
85*/
86extern std::string path_share;
87
88/*
89 Directory for storing user data. Examples:
90 Windows: "C:\Documents and Settings\user\Application Data<PROJECT_NAME>"
91 Linux: "~/.<PROJECT_NAME>"
92 Mac: "~/Library/Application Support/<PROJECT_NAME>"
93*/
94extern std::string path_user;
95
96/*
97 Path to gettext locale files
98*/
99extern std::string path_locale;
100
101/*
102 Path to directory for storing caches.
103*/
104extern std::string path_cache;
105
106/*
107 Gets the path of our executable.
108*/
109bool getCurrentExecPath(char *buf, size_t len);
110
111/*
112 Concatenate subpath to path_share.
113*/
114std::string getDataPath(const char *subpath);
115
116/*
117 Initialize path_*.
118*/
119void initializePaths();
120
121/*
122 Return system information
123 e.g. "Linux/3.12.7 x86_64"
124*/
125const std::string &get_sysinfo();
126
127
128// Monotonic timer
129
130#ifdef _WIN32 // Windows
131
132extern double perf_freq;
133
134inline u64 os_get_time(double mult)
135{
136 LARGE_INTEGER t;
137 QueryPerformanceCounter(&t);
138 return static_cast<double>(t.QuadPart) / (perf_freq / mult);
139}
140
141// Resolution is <1us.
142inline u64 getTimeS() { return os_get_time(1); }
143inline u64 getTimeMs() { return os_get_time(1000); }
144inline u64 getTimeUs() { return os_get_time(1000*1000); }
145inline u64 getTimeNs() { return os_get_time(1000*1000*1000); }
146
147#else // POSIX
148
149inline void os_get_clock(struct timespec *ts)
150{
151#if defined(CLOCK_MONOTONIC_RAW)
152 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
153#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0
154 clock_gettime(CLOCK_MONOTONIC, ts);
155#else
156# if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK == 0
157 // zero means it might be supported at runtime
158 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
159 return;
160# endif
161 struct timeval tv;
162 gettimeofday(&tv, NULL);
163 TIMEVAL_TO_TIMESPEC(&tv, ts);
164#endif
165}
166
167inline u64 getTimeS()
168{
169 struct timespec ts;
170 os_get_clock(&ts);
171 return ts.tv_sec;
172}
173
174inline u64 getTimeMs()
175{
176 struct timespec ts;
177 os_get_clock(&ts);
178 return ((u64) ts.tv_sec) * 1000LL + ((u64) ts.tv_nsec) / 1000000LL;
179}
180
181inline u64 getTimeUs()
182{
183 struct timespec ts;
184 os_get_clock(&ts);
185 return ((u64) ts.tv_sec) * 1000000LL + ((u64) ts.tv_nsec) / 1000LL;
186}
187
188inline u64 getTimeNs()
189{
190 struct timespec ts;
191 os_get_clock(&ts);
192 return ((u64) ts.tv_sec) * 1000000000LL + ((u64) ts.tv_nsec);
193}
194
195#endif
196
197inline u64 getTime(TimePrecision prec)
198{
199 switch (prec) {
200 case PRECISION_SECONDS: return getTimeS();
201 case PRECISION_MILLI: return getTimeMs();
202 case PRECISION_MICRO: return getTimeUs();
203 case PRECISION_NANO: return getTimeNs();
204 }
205 FATAL_ERROR("Called getTime with invalid time precision");
206}
207
214inline u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
215{
216 if (new_time_ms >= old_time_ms) {
217 return (new_time_ms - old_time_ms);
218 }
219
220 return (old_time_ms - new_time_ms);
221}
222
223inline const char *getPlatformName()
224{
225 return
226#if defined(ANDROID)
227 "Android"
228#elif defined(__linux__)
229 "Linux"
230#elif defined(_WIN32) || defined(_WIN64)
231 "Windows"
232#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
233 defined(__NetBSD__) || defined(__OpenBSD__)
234 "BSD"
235#elif defined(__APPLE__) && defined(__MACH__)
236 #if TARGET_OS_MAC
237 "OSX"
238 #elif TARGET_OS_IPHONE
239 "iOS"
240 #else
241 "Apple"
242 #endif
243#elif defined(_AIX)
244 "AIX"
245#elif defined(__hpux)
246 "HP-UX"
247#elif defined(__sun) || defined(sun)
248 #if defined(__SVR4)
249 "Solaris"
250 #else
251 "SunOS"
252 #endif
253#elif defined(__HAIKU__)
254 "Haiku"
255#elif defined(__CYGWIN__)
256 "Cygwin"
257#elif defined(__unix__) || defined(__unix)
258 #if defined(_POSIX_VERSION)
259 "POSIX"
260 #else
261 "Unix"
262 #endif
263#else
264 "?"
265#endif
266 ;
267}
268
269bool secure_rand_fill_buf(void *buf, size_t len);
270
271// Call once near beginning of main function.
272void osSpecificInit();
273
274// This attaches to the parents process console, or creates a new one if it doesnt exist.
276
277#if HAVE_MALLOC_TRIM
283void TrackFreedMemory(size_t amount);
284
289void TriggerMemoryTrim();
290#else
291static inline void TrackFreedMemory(size_t amount) { (void)amount; }
292static inline void TriggerMemoryTrim() { (void)0; }
293#endif
294
295#ifdef _WIN32
296// Quotes an argument for use in a CreateProcess() commandline (not cmd.exe!!)
297std::string QuoteArgv(const std::string &arg);
298
299// Convert an error code (e.g. from GetLastError()) into a string.
300std::string ConvertError(DWORD error_code);
301#endif
302
303// snprintf wrapper
304int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
305
314bool open_url(const std::string &url);
315
324bool open_directory(const std::string &path);
325
326} // namespace porting
327
328#ifdef __ANDROID__
329#include "porting_android.h"
330#endif
#define FATAL_ERROR(msg)
Definition debug.h:33
Definition porting.cpp:78
u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
Delta calculation function arguments.
Definition porting.h:214
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:149
void signal_handler_init(void)
Definition porting.cpp:115
void osSpecificInit()
Definition porting.cpp:770
static void TrackFreedMemory(size_t amount)
Definition porting.h:291
bool open_directory(const std::string &path)
Opens a directory in the default file manager.
Definition porting.cpp:904
u64 getTimeS()
Definition porting.h:167
std::string path_cache
Definition porting.cpp:162
std::string path_user
Definition porting.cpp:160
u64 getTime(TimePrecision prec)
Definition porting.h:197
bool * signal_handler_killstatus()
Definition porting.cpp:86
u64 getTimeMs()
Definition porting.h:174
bool open_url(const std::string &url)
Opens URL in default web browser.
Definition porting.cpp:894
u64 getTimeNs()
Definition porting.h:188
const char * getPlatformName()
Definition porting.h:223
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
static void TriggerMemoryTrim()
Definition porting.h:292
u64 getTimeUs()
Definition porting.h:181
const std::string & get_sysinfo()
Definition porting.cpp:266
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