Don't dereference the end iterator
[openal-soft.git] / alc / helpers.cpp
blob25b2e4a2f39b27dc7382dcdb229c3719fefcc366
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #ifdef _WIN32
22 #ifdef __MINGW32__
23 #define _WIN32_IE 0x501
24 #else
25 #define _WIN32_IE 0x400
26 #endif
27 #endif
29 #include "config.h"
31 #include <algorithm>
32 #include <cerrno>
33 #include <cstdarg>
34 #include <cstdlib>
35 #include <cstdio>
36 #include <cstring>
37 #include <mutex>
38 #include <string>
40 #ifdef HAVE_DIRENT_H
41 #include <dirent.h>
42 #endif
43 #ifdef HAVE_INTRIN_H
44 #include <intrin.h>
45 #endif
46 #ifdef HAVE_CPUID_H
47 #include <cpuid.h>
48 #endif
49 #ifdef HAVE_SSE_INTRINSICS
50 #include <xmmintrin.h>
51 #endif
52 #ifdef HAVE_SYS_SYSCONF_H
53 #include <sys/sysconf.h>
54 #endif
56 #ifdef HAVE_PROC_PIDPATH
57 #include <libproc.h>
58 #endif
60 #ifdef __FreeBSD__
61 #include <sys/types.h>
62 #include <sys/sysctl.h>
63 #endif
65 #ifndef _WIN32
66 #include <unistd.h>
67 #elif defined(_WIN32_IE)
68 #include <shlobj.h>
69 #endif
71 #include "alcmain.h"
72 #include "almalloc.h"
73 #include "alfstream.h"
74 #include "alspan.h"
75 #include "alstring.h"
76 #include "compat.h"
77 #include "cpu_caps.h"
78 #include "fpu_modes.h"
79 #include "logging.h"
80 #include "strutils.h"
81 #include "vector.h"
84 #if defined(HAVE_GCC_GET_CPUID) && (defined(__i386__) || defined(__x86_64__) || \
85 defined(_M_IX86) || defined(_M_X64))
86 using reg_type = unsigned int;
87 static inline void get_cpuid(unsigned int f, reg_type *regs)
88 { __get_cpuid(f, &regs[0], &regs[1], &regs[2], &regs[3]); }
89 #define CAN_GET_CPUID
90 #elif defined(HAVE_CPUID_INTRINSIC) && (defined(__i386__) || defined(__x86_64__) || \
91 defined(_M_IX86) || defined(_M_X64))
92 using reg_type = int;
93 static inline void get_cpuid(unsigned int f, reg_type *regs)
94 { (__cpuid)(regs, f); }
95 #define CAN_GET_CPUID
96 #endif
98 int CPUCapFlags = 0;
100 void FillCPUCaps(int capfilter)
102 int caps = 0;
104 /* FIXME: We really should get this for all available CPUs in case different
105 * CPUs have different caps (is that possible on one machine?). */
106 #ifdef CAN_GET_CPUID
107 union {
108 reg_type regs[4];
109 char str[sizeof(reg_type[4])];
110 } cpuinf[3]{};
112 get_cpuid(0, cpuinf[0].regs);
113 if(cpuinf[0].regs[0] == 0)
114 ERR("Failed to get CPUID\n");
115 else
117 unsigned int maxfunc = cpuinf[0].regs[0];
118 unsigned int maxextfunc;
120 get_cpuid(0x80000000, cpuinf[0].regs);
121 maxextfunc = cpuinf[0].regs[0];
123 TRACE("Detected max CPUID function: 0x%x (ext. 0x%x)\n", maxfunc, maxextfunc);
125 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
126 if(maxextfunc >= 0x80000004)
128 get_cpuid(0x80000002, cpuinf[0].regs);
129 get_cpuid(0x80000003, cpuinf[1].regs);
130 get_cpuid(0x80000004, cpuinf[2].regs);
131 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
134 if(maxfunc >= 1)
136 get_cpuid(1, cpuinf[0].regs);
137 if((cpuinf[0].regs[3]&(1<<25)))
138 caps |= CPU_CAP_SSE;
139 if((caps&CPU_CAP_SSE) && (cpuinf[0].regs[3]&(1<<26)))
140 caps |= CPU_CAP_SSE2;
141 if((caps&CPU_CAP_SSE2) && (cpuinf[0].regs[2]&(1<<0)))
142 caps |= CPU_CAP_SSE3;
143 if((caps&CPU_CAP_SSE3) && (cpuinf[0].regs[2]&(1<<19)))
144 caps |= CPU_CAP_SSE4_1;
147 #else
148 /* Assume support for whatever's supported if we can't check for it */
149 #if defined(HAVE_SSE4_1)
150 #warning "Assuming SSE 4.1 run-time support!"
151 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3 | CPU_CAP_SSE4_1;
152 #elif defined(HAVE_SSE3)
153 #warning "Assuming SSE 3 run-time support!"
154 caps |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE3;
155 #elif defined(HAVE_SSE2)
156 #warning "Assuming SSE 2 run-time support!"
157 caps |= CPU_CAP_SSE | CPU_CAP_SSE2;
158 #elif defined(HAVE_SSE)
159 #warning "Assuming SSE run-time support!"
160 caps |= CPU_CAP_SSE;
161 #endif
162 #endif
163 #ifdef HAVE_NEON
164 al::ifstream file{"/proc/cpuinfo"};
165 if(!file.is_open())
166 ERR("Failed to open /proc/cpuinfo, cannot check for NEON support\n");
167 else
169 std::string features;
171 auto getline = [](std::istream &f, std::string &output) -> bool
173 while(f.good() && f.peek() == '\n')
174 f.ignore();
175 return std::getline(f, output) && !output.empty();
178 while(getline(file, features))
180 if(features.compare(0, 10, "Features\t:", 10) == 0)
181 break;
183 file.close();
185 size_t extpos{9};
186 while((extpos=features.find("neon", extpos+1)) != std::string::npos)
188 if((extpos == 0 || std::isspace(features[extpos-1])) &&
189 (extpos+4 == features.length() || std::isspace(features[extpos+4])))
191 caps |= CPU_CAP_NEON;
192 break;
196 #endif
198 TRACE("Extensions:%s%s%s%s%s%s\n",
199 ((capfilter&CPU_CAP_SSE) ? ((caps&CPU_CAP_SSE) ? " +SSE" : " -SSE") : ""),
200 ((capfilter&CPU_CAP_SSE2) ? ((caps&CPU_CAP_SSE2) ? " +SSE2" : " -SSE2") : ""),
201 ((capfilter&CPU_CAP_SSE3) ? ((caps&CPU_CAP_SSE3) ? " +SSE3" : " -SSE3") : ""),
202 ((capfilter&CPU_CAP_SSE4_1) ? ((caps&CPU_CAP_SSE4_1) ? " +SSE4.1" : " -SSE4.1") : ""),
203 ((capfilter&CPU_CAP_NEON) ? ((caps&CPU_CAP_NEON) ? " +NEON" : " -NEON") : ""),
204 ((!capfilter) ? " -none-" : "")
206 CPUCapFlags = caps & capfilter;
210 FPUCtl::FPUCtl()
212 #if defined(HAVE_SSE_INTRINSICS)
213 this->sse_state = _mm_getcsr();
214 unsigned int sseState = this->sse_state;
215 sseState |= 0x8000; /* set flush-to-zero */
216 sseState |= 0x0040; /* set denormals-are-zero */
217 _mm_setcsr(sseState);
219 #elif defined(__GNUC__) && defined(HAVE_SSE)
221 if((CPUCapFlags&CPU_CAP_SSE))
223 __asm__ __volatile__("stmxcsr %0" : "=m" (*&this->sse_state));
224 unsigned int sseState = this->sse_state;
225 sseState |= 0x8000; /* set flush-to-zero */
226 if((CPUCapFlags&CPU_CAP_SSE2))
227 sseState |= 0x0040; /* set denormals-are-zero */
228 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&sseState));
230 #endif
232 this->in_mode = true;
235 void FPUCtl::leave()
237 if(!this->in_mode) return;
239 #if defined(HAVE_SSE_INTRINSICS)
240 _mm_setcsr(this->sse_state);
242 #elif defined(__GNUC__) && defined(HAVE_SSE)
244 if((CPUCapFlags&CPU_CAP_SSE))
245 __asm__ __volatile__("ldmxcsr %0" : : "m" (*&this->sse_state));
246 #endif
247 this->in_mode = false;
251 #ifdef _WIN32
253 const PathNamePair &GetProcBinary()
255 static PathNamePair ret;
256 if(!ret.fname.empty() || !ret.path.empty())
257 return ret;
259 al::vector<WCHAR> fullpath(256);
260 DWORD len;
261 while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))) == fullpath.size())
262 fullpath.resize(fullpath.size() << 1);
263 if(len == 0)
265 ERR("Failed to get process name: error %lu\n", GetLastError());
266 return ret;
269 fullpath.resize(len);
270 if(fullpath.back() != 0)
271 fullpath.push_back(0);
273 auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
274 sep = std::find(fullpath.rbegin()+1, sep, '/');
275 if(sep != fullpath.rend())
277 *sep = 0;
278 ret.fname = wstr_to_utf8(&*sep + 1);
279 ret.path = wstr_to_utf8(fullpath.data());
281 else
282 ret.fname = wstr_to_utf8(fullpath.data());
284 TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
285 return ret;
289 void al_print(FILE *logfile, const char *fmt, ...)
291 al::vector<char> dynmsg;
292 char stcmsg[256];
293 char *str{stcmsg};
295 va_list args, args2;
296 va_start(args, fmt);
297 va_copy(args2, args);
298 int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
299 if UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg))
301 dynmsg.resize(static_cast<size_t>(msglen) + 1u);
302 str = dynmsg.data();
303 msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
305 va_end(args2);
306 va_end(args);
308 std::wstring wstr{utf8_to_wstr(str)};
309 fputws(wstr.c_str(), logfile);
310 fflush(logfile);
314 static inline int is_slash(int c)
315 { return (c == '\\' || c == '/'); }
317 static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
319 std::string pathstr{path};
320 pathstr += "\\*";
321 pathstr += ext;
322 TRACE("Searching %s\n", pathstr.c_str());
324 std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
325 WIN32_FIND_DATAW fdata;
326 HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
327 if(hdl == INVALID_HANDLE_VALUE) return;
329 const auto base = results->size();
331 do {
332 results->emplace_back();
333 std::string &str = results->back();
334 str = path;
335 str += '\\';
336 str += wstr_to_utf8(fdata.cFileName);
337 } while(FindNextFileW(hdl, &fdata));
338 FindClose(hdl);
340 const al::span<std::string> newlist{results->data()+base, results->size()-base};
341 std::sort(newlist.begin(), newlist.end());
342 for(const auto &name : newlist)
343 TRACE(" got %s\n", name.c_str());
346 al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
348 static std::mutex search_lock;
349 std::lock_guard<std::mutex> _{search_lock};
351 /* If the path is absolute, use it directly. */
352 al::vector<std::string> results;
353 if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
355 std::string path{subdir};
356 std::replace(path.begin(), path.end(), '/', '\\');
357 DirectorySearch(path.c_str(), ext, &results);
358 return results;
360 if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
362 DirectorySearch(subdir, ext, &results);
363 return results;
366 std::string path;
368 /* Search the app-local directory. */
369 if(auto localpath = al::getenv(L"ALSOFT_LOCAL_PATH"))
371 path = wstr_to_utf8(localpath->c_str());
372 if(is_slash(path.back()))
373 path.pop_back();
375 else if(WCHAR *cwdbuf{_wgetcwd(nullptr, 0)})
377 path = wstr_to_utf8(cwdbuf);
378 if(is_slash(path.back()))
379 path.pop_back();
380 free(cwdbuf);
382 else
383 path = ".";
384 std::replace(path.begin(), path.end(), '/', '\\');
385 DirectorySearch(path.c_str(), ext, &results);
387 /* Search the local and global data dirs. */
388 static constexpr int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
389 for(int id : ids)
391 WCHAR buffer[MAX_PATH];
392 if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
393 continue;
395 path = wstr_to_utf8(buffer);
396 if(!is_slash(path.back()))
397 path += '\\';
398 path += subdir;
399 std::replace(path.begin(), path.end(), '/', '\\');
401 DirectorySearch(path.c_str(), ext, &results);
404 return results;
407 void SetRTPriority(void)
409 bool failed = false;
410 if(RTPrioLevel > 0)
411 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
412 if(failed) ERR("Failed to set priority level for thread\n");
415 #else
417 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
418 #include <pthread.h>
419 #include <sched.h>
420 #endif
422 const PathNamePair &GetProcBinary()
424 static PathNamePair ret;
425 if(!ret.fname.empty() || !ret.path.empty())
426 return ret;
428 al::vector<char> pathname;
429 #ifdef __FreeBSD__
430 size_t pathlen;
431 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
432 if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
433 WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
434 else
436 pathname.resize(pathlen + 1);
437 sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
438 pathname.resize(pathlen);
440 #endif
441 #ifdef HAVE_PROC_PIDPATH
442 if(pathname.empty())
444 char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
445 const pid_t pid{getpid()};
446 if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
447 ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
448 else
449 pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
451 #endif
452 if(pathname.empty())
454 pathname.resize(256);
456 const char *selfname{"/proc/self/exe"};
457 ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
458 if(len == -1 && errno == ENOENT)
460 selfname = "/proc/self/file";
461 len = readlink(selfname, pathname.data(), pathname.size());
463 if(len == -1 && errno == ENOENT)
465 selfname = "/proc/curproc/exe";
466 len = readlink(selfname, pathname.data(), pathname.size());
468 if(len == -1 && errno == ENOENT)
470 selfname = "/proc/curproc/file";
471 len = readlink(selfname, pathname.data(), pathname.size());
474 while(len > 0 && static_cast<size_t>(len) == pathname.size())
476 pathname.resize(pathname.size() << 1);
477 len = readlink(selfname, pathname.data(), pathname.size());
479 if(len <= 0)
481 WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
482 return ret;
485 pathname.resize(static_cast<size_t>(len));
487 while(!pathname.empty() && pathname.back() == 0)
488 pathname.pop_back();
490 auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
491 if(sep != pathname.crend())
493 ret.path = std::string(pathname.cbegin(), sep.base()-1);
494 ret.fname = std::string(sep.base(), pathname.cend());
496 else
497 ret.fname = std::string(pathname.cbegin(), pathname.cend());
499 TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
500 return ret;
504 void al_print(FILE *logfile, const char *fmt, ...)
506 va_list ap;
508 va_start(ap, fmt);
509 vfprintf(logfile, fmt, ap);
510 va_end(ap);
512 fflush(logfile);
516 static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
518 TRACE("Searching %s for *%s\n", path, ext);
519 DIR *dir{opendir(path)};
520 if(!dir) return;
522 const auto base = results->size();
523 const size_t extlen{strlen(ext)};
525 struct dirent *dirent;
526 while((dirent=readdir(dir)) != nullptr)
528 if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
529 continue;
531 const size_t len{strlen(dirent->d_name)};
532 if(len <= extlen) continue;
533 if(al::strcasecmp(dirent->d_name+len-extlen, ext) != 0)
534 continue;
536 results->emplace_back();
537 std::string &str = results->back();
538 str = path;
539 if(str.back() != '/')
540 str.push_back('/');
541 str += dirent->d_name;
543 closedir(dir);
545 const al::span<std::string> newlist{results->data()+base, results->size()-base};
546 std::sort(newlist.begin(), newlist.end());
547 for(const auto &name : newlist)
548 TRACE(" got %s\n", name.c_str());
551 al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
553 static std::mutex search_lock;
554 std::lock_guard<std::mutex> _{search_lock};
556 al::vector<std::string> results;
557 if(subdir[0] == '/')
559 DirectorySearch(subdir, ext, &results);
560 return results;
563 /* Search the app-local directory. */
564 if(auto localpath = al::getenv("ALSOFT_LOCAL_PATH"))
565 DirectorySearch(localpath->c_str(), ext, &results);
566 else
568 al::vector<char> cwdbuf(256);
569 while(!getcwd(cwdbuf.data(), cwdbuf.size()))
571 if(errno != ERANGE)
573 cwdbuf.clear();
574 break;
576 cwdbuf.resize(cwdbuf.size() << 1);
578 if(cwdbuf.empty())
579 DirectorySearch(".", ext, &results);
580 else
582 DirectorySearch(cwdbuf.data(), ext, &results);
583 cwdbuf.clear();
587 // Search local data dir
588 if(auto datapath = al::getenv("XDG_DATA_HOME"))
590 std::string &path = *datapath;
591 if(path.back() != '/')
592 path += '/';
593 path += subdir;
594 DirectorySearch(path.c_str(), ext, &results);
596 else if(auto homepath = al::getenv("HOME"))
598 std::string &path = *homepath;
599 if(path.back() == '/')
600 path.pop_back();
601 path += "/.local/share/";
602 path += subdir;
603 DirectorySearch(path.c_str(), ext, &results);
606 // Search global data dirs
607 std::string datadirs{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")};
609 size_t curpos{0u};
610 while(curpos < datadirs.size())
612 size_t nextpos{datadirs.find(':', curpos)};
614 std::string path{(nextpos != std::string::npos) ?
615 datadirs.substr(curpos, nextpos++ - curpos) : datadirs.substr(curpos)};
616 curpos = nextpos;
618 if(path.empty()) continue;
619 if(path.back() != '/')
620 path += '/';
621 path += subdir;
623 DirectorySearch(path.c_str(), ext, &results);
626 return results;
629 void SetRTPriority()
631 bool failed = false;
632 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
633 if(RTPrioLevel > 0)
635 struct sched_param param;
636 /* Use the minimum real-time priority possible for now (on Linux this
637 * should be 1 for SCHED_RR) */
638 param.sched_priority = sched_get_priority_min(SCHED_RR);
639 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
641 #else
642 /* Real-time priority not available */
643 failed = (RTPrioLevel>0);
644 #endif
645 if(failed)
646 ERR("Failed to set priority level for thread\n");
649 #endif