18 #include <QStringList> 25 #include <sys/statvfs.h> 31 #include <sys/param.h> 32 #include <sys/sysctl.h> 43 rpl_realloc (
void *p,
size_t n)
60 void usleep(
unsigned long usec)
66 int gettimeofday(
struct timeval *tv,
void * )
73 ::GetSystemTimeAsFileTime(&now.ft);
74 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
75 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
87 HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
95 if (kill(getpid(), 0) == 0) {
96 if (kill(pid, 0) == 0) {
114 DWORDLONG ullTotalPhys;
115 DWORDLONG ullAvailPhys;
116 DWORDLONG ullTotalPageFile;
117 DWORDLONG ullAvailPageFile;
118 DWORDLONG ullTotalVirtual;
119 DWORDLONG ullAvailVirtual;
120 DWORDLONG ullAvailExtendedVirtual;
122 typedef BOOL (WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
133 static bool checked =
false;
134 static bool exFound =
false;
139 HMODULE h = GetModuleHandleA(
"kernel32.dll");
142 if ((ex = (PFN_MS_EX)GetProcAddress(h,
"GlobalMemoryStatusEx"))) {
150 DWORDLONG wavail = 0;
151 DWORDLONG wtotal = 0;
156 lms.dwLength =
sizeof(lms);
158 cerr <<
"WARNING: GlobalMemoryStatusEx failed: error code " 159 << GetLastError() << endl;
162 wavail = lms.ullAvailPhys;
163 wtotal = lms.ullTotalPhys;
171 GlobalMemoryStatus(&ms);
172 wavail = ms.dwAvailPhys;
173 wtotal = ms.dwTotalPhys;
176 DWORDLONG size = wavail / 1048576;
177 if (size > INT_MAX) size = INT_MAX;
178 available = ssize_t(size);
180 size = wtotal / 1048576;
181 if (size > INT_MAX) size = INT_MAX;
182 total = ssize_t(size);
197 size_sys =
sizeof(val64);
198 sysctl(mib, 2, &val64, &size_sys, NULL, 0);
199 if (val64) total = val64 / 1048576;
202 size_sys =
sizeof(val32);
203 sysctl(mib, 2, &val32, &size_sys, NULL, 0);
204 if (val32) available = val32 / 1048576;
213 if (available < total/4) {
221 FILE *meminfo = fopen(
"/proc/meminfo",
"r");
222 if (!meminfo)
return;
225 while (!feof(meminfo)) {
226 if (!fgets(buf, 256, meminfo)) {
230 bool isMemFree = (strncmp(buf,
"MemFree:", 8) == 0);
231 bool isMemTotal = (!isMemFree && (strncmp(buf,
"MemTotal:", 9) == 0));
232 if (isMemFree || isMemTotal) {
233 QString line = QString(buf).trimmed();
234 QStringList elements = line.split(
' ', QString::SkipEmptyParts);
236 if (elements.size() > 2) unit = elements[2];
237 int size = elements[1].toInt();
240 if (unit.toLower() ==
"gb") size = size * 1024;
241 else if (unit.toLower() ==
"mb") size = size;
242 else if (unit.toLower() ==
"kb") size = size / 1024;
243 else size = size / 1048576;
245 if (isMemFree) available = size;
248 if (available != -1 && total != -1) {
265 ULARGE_INTEGER available, total, totalFree;
266 if (GetDiskFreeSpaceExA(path, &available, &total, &totalFree)) {
267 __int64 a = available.QuadPart;
269 if (a > INT_MAX) a = INT_MAX;
272 cerr <<
"WARNING: GetDiskFreeSpaceEx failed: error code " 273 << GetLastError() << endl;
278 if (!statvfs(path, &buf)) {
282 uint64_t available = ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
283 if (available > INT_MAX) available = INT_MAX;
284 return ssize_t(available);
286 perror(
"statvfs failed");
299 __asm__ __volatile__(
"xchgl %%eax,%0 " 304 #if !defined(__APPLE__) && defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)) 308 pthread_mutex_t dummy = PTHREAD_MUTEX_INITIALIZER;
309 pthread_mutex_lock(&dummy);
310 pthread_mutex_unlock(&dummy);
321 char *loc = setlocale(LC_ALL,
nullptr);
323 if (startupLocale) free(startupLocale);
324 startupLocale = strdup(loc);
330 if (!startupLocale) {
331 setlocale(LC_ALL,
"");
333 setlocale(LC_ALL, startupLocale);
337 double mod(
double x,
double y) {
return x - (y * floor(x / y)); }
338 float modf(
float x,
float y) {
return x - (y * floorf(x / y)); }
349 int wvarlen = MultiByteToWideChar(CP_UTF8, 0,
350 variable.c_str(), int(variable.length()),
353 SVCERR <<
"WARNING: Unable to convert environment variable name " 354 << variable <<
" to wide characters" << endl;
358 wchar_t *wvarbuf =
new wchar_t[wvarlen + 1];
359 (void)MultiByteToWideChar(CP_UTF8, 0,
360 variable.c_str(), int(variable.length()),
362 wvarbuf[wvarlen] = L
'\0';
364 wchar_t *wvalue = _wgetenv(wvarbuf);
372 int wvallen = int(wcslen(wvalue));
373 int vallen = WideCharToMultiByte(CP_UTF8, 0,
377 SVCERR <<
"WARNING: Unable to convert environment value to UTF-8" 382 char *val =
new char[vallen + 1];
383 (void)WideCharToMultiByte(CP_UTF8, 0,
395 char *val = getenv(variable.c_str());
410 std::string entry = variable +
"=" + value;
412 int wentlen = MultiByteToWideChar(CP_UTF8, 0,
413 entry.c_str(), int(entry.length()),
416 SVCERR <<
"WARNING: Unable to convert environment entry to " 417 <<
"wide characters" << endl;
421 wchar_t *wentbuf =
new wchar_t[wentlen + 1];
422 (void)MultiByteToWideChar(CP_UTF8, 0,
423 entry.c_str(), int(entry.length()),
425 wentbuf[wentlen] = L
'\0';
427 int rv = _wputenv(wentbuf);
432 SVCERR <<
"WARNING: Failed to set environment entry" << endl;
439 int rv = setenv(variable.c_str(), value.c_str(), 1);
441 SVCERR <<
"WARNING: Failed to set environment entry" << endl;
bool putEnvUtf8(std::string variable, std::string value)
Set the value of the given environment variable.
double mod(double x, double y)
void GetRealMemoryMBAvailable(ssize_t &available, ssize_t &total)
ssize_t GetDiscSpaceMBAvailable(const char *path)
bool getEnvUtf8(std::string variable, std::string &value)
Return the value of the given environment variable by reference.
static char * startupLocale
ProcessStatus GetProcessStatus(int pid)
void SystemMemoryBarrier()
double princarg(double a)
void RestoreStartupLocale()
float modf(float x, float y)
void StoreStartupLocale()