comparison system/System.cpp @ 1527:710e6250a401 zoom

Merge from default branch
author Chris Cannam
date Mon, 17 Sep 2018 13:51:14 +0100
parents 5ac102155409
children 70e172e6cc59
comparison
equal deleted inserted replaced
1324:d4a28d1479a8 1527:710e6250a401
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam and QMUL. 7 This file copyright 2006-2018 Chris Cannam and QMUL.
8 8
9 This program is free software; you can redistribute it and/or 9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as 10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the 11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "System.h" 16 #include "System.h"
17 17
18 #include <QStringList> 18 #include <QStringList>
37 37
38 #include <iostream> 38 #include <iostream>
39 39
40 #ifdef __APPLE__ 40 #ifdef __APPLE__
41 extern "C" { 41 extern "C" {
42 void * 42 void *
43 rpl_realloc (void *p, size_t n) 43 rpl_realloc (void *p, size_t n)
44 {
45 p = realloc(p, n);
46 if (p == 0 && n == 0)
47 { 44 {
48 p = malloc(0); 45 p = realloc(p, n);
49 } 46 if (p == 0 && n == 0)
50 return p; 47 {
51 } 48 p = malloc(0);
49 }
50 return p;
51 }
52 } 52 }
53 #endif 53 #endif
54 54
55 #ifdef _WIN32 55 #ifdef _WIN32
56 56
57 extern "C" { 57 extern "C" {
58 58
59 #ifdef _MSC_VER 59 #ifdef _MSC_VER
60 void usleep(unsigned long usec) 60 void usleep(unsigned long usec)
61 { 61 {
62 ::Sleep(usec / 1000); 62 ::Sleep(usec / 1000);
63 } 63 }
64 #endif 64 #endif
65 65
66 int gettimeofday(struct timeval *tv, void *tz) 66 int gettimeofday(struct timeval *tv, void * /* tz */)
67 { 67 {
68 union { 68 union {
69 long long ns100; 69 long long ns100;
70 FILETIME ft; 70 FILETIME ft;
71 } now; 71 } now;
72 72
73 ::GetSystemTimeAsFileTime(&now.ft); 73 ::GetSystemTimeAsFileTime(&now.ft);
74 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL); 74 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
75 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL); 75 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
76 return 0; 76 return 0;
77 } 77 }
78 78
79 } 79 }
80 80
81 #endif 81 #endif
82 82
151 DWORDLONG wtotal = 0; 151 DWORDLONG wtotal = 0;
152 152
153 if (exFound) { 153 if (exFound) {
154 154
155 lMEMORYSTATUSEX lms; 155 lMEMORYSTATUSEX lms;
156 lms.dwLength = sizeof(lms); 156 lms.dwLength = sizeof(lms);
157 if (!ex(&lms)) { 157 if (!ex(&lms)) {
158 cerr << "WARNING: GlobalMemoryStatusEx failed: error code " 158 cerr << "WARNING: GlobalMemoryStatusEx failed: error code "
159 << GetLastError() << endl; 159 << GetLastError() << endl;
160 return; 160 return;
161 } 161 }
162 wavail = lms.ullAvailPhys; 162 wavail = lms.ullAvailPhys;
163 wtotal = lms.ullTotalPhys; 163 wtotal = lms.ullTotalPhys;
164 164
165 } else { 165 } else {
166 166
167 /* Fall back to GlobalMemoryStatus which is always available. 167 /* Fall back to GlobalMemoryStatus which is always available.
168 but returns wrong results for physical memory > 4GB */ 168 but returns wrong results for physical memory > 4GB */
169 169
170 MEMORYSTATUS ms; 170 MEMORYSTATUS ms;
171 GlobalMemoryStatus(&ms); 171 GlobalMemoryStatus(&ms);
172 wavail = ms.dwAvailPhys; 172 wavail = ms.dwAvailPhys;
173 wtotal = ms.dwTotalPhys; 173 wtotal = ms.dwTotalPhys;
174 } 174 }
175 175
176 DWORDLONG size = wavail / 1048576; 176 DWORDLONG size = wavail / 1048576;
177 if (size > INT_MAX) size = INT_MAX; 177 if (size > INT_MAX) size = INT_MAX;
209 FILE *meminfo = fopen("/proc/meminfo", "r"); 209 FILE *meminfo = fopen("/proc/meminfo", "r");
210 if (!meminfo) return; 210 if (!meminfo) return;
211 211
212 char buf[256]; 212 char buf[256];
213 while (!feof(meminfo)) { 213 while (!feof(meminfo)) {
214 fgets(buf, 256, meminfo); 214 if (!fgets(buf, 256, meminfo)) {
215 fclose(meminfo);
216 return;
217 }
215 bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0); 218 bool isMemFree = (strncmp(buf, "MemFree:", 8) == 0);
216 bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0)); 219 bool isMemTotal = (!isMemFree && (strncmp(buf, "MemTotal:", 9) == 0));
217 if (isMemFree || isMemTotal) { 220 if (isMemFree || isMemTotal) {
218 QString line = QString(buf).trimmed(); 221 QString line = QString(buf).trimmed();
219 QStringList elements = line.split(' ', QString::SkipEmptyParts); 222 QStringList elements = line.split(' ', QString::SkipEmptyParts);
247 GetDiscSpaceMBAvailable(const char *path) 250 GetDiscSpaceMBAvailable(const char *path)
248 { 251 {
249 #ifdef _WIN32 252 #ifdef _WIN32
250 ULARGE_INTEGER available, total, totalFree; 253 ULARGE_INTEGER available, total, totalFree;
251 if (GetDiskFreeSpaceExA(path, &available, &total, &totalFree)) { 254 if (GetDiskFreeSpaceExA(path, &available, &total, &totalFree)) {
252 __int64 a = available.QuadPart; 255 __int64 a = available.QuadPart;
253 a /= 1048576; 256 a /= 1048576;
254 if (a > INT_MAX) a = INT_MAX; 257 if (a > INT_MAX) a = INT_MAX;
255 return ssize_t(a); 258 return ssize_t(a);
256 } else { 259 } else {
257 cerr << "WARNING: GetDiskFreeSpaceEx failed: error code " 260 cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
258 << GetLastError() << endl; 261 << GetLastError() << endl;
259 return -1; 262 return -1;
260 } 263 }
261 #else 264 #else
262 struct statvfs buf; 265 struct statvfs buf;
263 if (!statvfs(path, &buf)) { 266 if (!statvfs(path, &buf)) {
284 __asm__ __volatile__("xchgl %%eax,%0 " 287 __asm__ __volatile__("xchgl %%eax,%0 "
285 : "=r" (Barrier)); 288 : "=r" (Barrier));
286 #endif 289 #endif
287 } 290 }
288 #else /* !_WIN32 */ 291 #else /* !_WIN32 */
289 #if !defined(__APPLE__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)) 292 #if !defined(__APPLE__) && defined(__GNUC__) && ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ == 0))
290 void 293 void
291 SystemMemoryBarrier() 294 SystemMemoryBarrier()
292 { 295 {
293 pthread_mutex_t dummy = PTHREAD_MUTEX_INITIALIZER; 296 pthread_mutex_t dummy = PTHREAD_MUTEX_INITIALIZER;
294 pthread_mutex_lock(&dummy); 297 pthread_mutex_lock(&dummy);
323 float modf(float x, float y) { return x - (y * floorf(x / y)); } 326 float modf(float x, float y) { return x - (y * floorf(x / y)); }
324 327
325 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; } 328 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
326 float princargf(float a) { return float(princarg(a)); } 329 float princargf(float a) { return float(princarg(a)); }
327 330
328 331 bool
332 getEnvUtf8(std::string variable, std::string &value)
333 {
334 value = "";
335
336 #ifdef _WIN32
337 int wvarlen = MultiByteToWideChar(CP_UTF8, 0,
338 variable.c_str(), int(variable.length()),
339 0, 0);
340 if (wvarlen < 0) {
341 SVCERR << "WARNING: Unable to convert environment variable name "
342 << variable << " to wide characters" << endl;
343 return false;
344 }
345
346 wchar_t *wvarbuf = new wchar_t[wvarlen + 1];
347 (void)MultiByteToWideChar(CP_UTF8, 0,
348 variable.c_str(), int(variable.length()),
349 wvarbuf, wvarlen);
350 wvarbuf[wvarlen] = L'\0';
351
352 wchar_t *wvalue = _wgetenv(wvarbuf);
353
354 delete[] wvarbuf;
355
356 if (!wvalue) {
357 return false;
358 }
359
360 int wvallen = int(wcslen(wvalue));
361 int vallen = WideCharToMultiByte(CP_UTF8, 0,
362 wvalue, wvallen,
363 0, 0, 0, 0);
364 if (vallen < 0) {
365 SVCERR << "WARNING: Unable to convert environment value to UTF-8"
366 << endl;
367 return false;
368 }
369
370 char *val = new char[vallen + 1];
371 (void)WideCharToMultiByte(CP_UTF8, 0,
372 wvalue, wvallen,
373 val, vallen, 0, 0);
374 val[vallen] = '\0';
375
376 value = val;
377
378 delete[] val;
379 return true;
380
381 #else
382
383 char *val = getenv(variable.c_str());
384 if (!val) {
385 return false;
386 }
387
388 value = val;
389 return true;
390
391 #endif
392 }
393
394 bool
395 putEnvUtf8(std::string variable, std::string value)
396 {
397 #ifdef _WIN32
398 std::string entry = variable + "=" + value;
399
400 int wentlen = MultiByteToWideChar(CP_UTF8, 0,
401 entry.c_str(), int(entry.length()),
402 0, 0);
403 if (wentlen < 0) {
404 SVCERR << "WARNING: Unable to convert environment entry to "
405 << "wide characters" << endl;
406 return false;
407 }
408
409 wchar_t *wentbuf = new wchar_t[wentlen + 1];
410 (void)MultiByteToWideChar(CP_UTF8, 0,
411 entry.c_str(), int(entry.length()),
412 wentbuf, wentlen);
413 wentbuf[wentlen] = L'\0';
414
415 int rv = _wputenv(wentbuf);
416
417 delete[] wentbuf;
418
419 if (rv != 0) {
420 SVCERR << "WARNING: Failed to set environment entry" << endl;
421 return false;
422 }
423 return true;
424
425 #else
426
427 int rv = setenv(variable.c_str(), value.c_str(), 1);
428 if (rv != 0) {
429 SVCERR << "WARNING: Failed to set environment entry" << endl;
430 return false;
431 }
432 return true;
433
434 #endif
435 }
436