14 #if __GNUC__ && __GNUC__ < 8
20 #if defined(__SYCL_RT_OS_LINUX)
32 #include <linux/limits.h>
34 #include <sys/sysinfo.h>
36 #elif defined(__SYCL_RT_OS_WINDOWS)
44 #elif defined(__SYCL_RT_OS_DARWIN)
47 #include <sys/sysctl.h>
48 #include <sys/types.h>
53 inline namespace _V1 {
56 #if defined(__SYCL_RT_OS_LINUX)
57 bool procMapsAddressInRange(std::istream &Stream, uintptr_t Addr) {
58 uintptr_t Start = 0, End = 0;
60 assert(!Stream.fail() && Stream.peek() ==
'-' &&
61 "Couldn't read /proc/self/maps correctly");
65 assert(!Stream.fail() && Stream.peek() ==
' ' &&
66 "Couldn't read /proc/self/maps correctly");
69 return Addr >= Start && Addr < End;
95 std::ifstream Stream(
"/proc/self/maps");
97 while (!Stream.eof()) {
98 if (!procMapsAddressInRange(Stream, CurrentFunc)) {
105 Stream.readsome(Perm,
sizeof(Perm));
106 assert(Perm[0] ==
'r' && Perm[2] ==
'x' &&
107 "Invalid flags in /proc/self/maps");
108 assert(Stream.peek() ==
' ');
127 while (Stream.peek() ==
' ') {
131 Stream.getline(Path, PATH_MAX - 1);
132 Path[PATH_MAX - 1] =
'\0';
135 assert(
false &&
"Unable to find the current function in /proc/self/maps");
140 std::string Tmp(Path);
143 size_t TruncatedSize = strlen(dirname(
const_cast<char *
>(Tmp.c_str())));
144 Tmp.resize(TruncatedSize);
148 #elif defined(__SYCL_RT_OS_WINDOWS)
156 Path[
sizeof(Path) - 1] =
'\0';
158 DWORD Ret = GetModuleFileNameA(
160 reinterpret_cast<LPSTR
>(&Path),
sizeof(Path));
161 assert(Ret <
sizeof(Path) &&
"Path is longer than PATH_MAX?");
162 assert(Ret > 0 &&
"GetModuleFileNameA failed");
165 BOOL RetCode = PathRemoveFileSpecA(
reinterpret_cast<LPSTR
>(&Path));
166 assert(RetCode &&
"PathRemoveFileSpecA failed");
173 std::string Tmp(Path);
175 Tmp.erase(Tmp.find_last_not_of(
"/\\") + 1, std::string::npos);
177 size_t pos = Tmp.find_last_of(
"/\\");
178 if (pos != std::string::npos)
179 return Tmp.substr(0, pos);
185 #elif defined(__SYCL_RT_OS_DARWIN)
189 int RetCode = dladdr(CurrentFunc, &Info);
195 auto Path = std::string(Info.dli_fname);
196 auto LastSlashPos = Path.find_last_of(
'/');
198 return Path.substr(0, LastSlashPos);
204 #if defined(__SYCL_RT_OS_LINUX)
205 struct sysinfo MemInfo;
207 return static_cast<size_t>(MemInfo.totalram * MemInfo.mem_unit);
208 #elif defined(__SYCL_RT_OS_WINDOWS)
209 MEMORYSTATUSEX MemInfo;
210 MemInfo.dwLength =
sizeof(MemInfo);
211 GlobalMemoryStatusEx(&MemInfo);
212 return static_cast<size_t>(MemInfo.ullTotalPhys);
213 #elif defined(__SYCL_RT_OS_DARWIN)
215 sysctlbyname(
"hw.memsize", &Size,
nullptr,
nullptr, 0);
216 return static_cast<size_t>(Size);
221 #if defined(__SYCL_RT_OS_LINUX) && (defined(_GLIBCXX_HAVE_ALIGNED_ALLOC) || \
222 defined(_LIBCPP_HAS_C11_FEATURES))
224 #elif defined(__SYCL_RT_OS_POSIX_SUPPORT)
225 void *Addr =
nullptr;
226 int ReturnCode = posix_memalign(&Addr,
Alignment, NumBytes);
227 return (ReturnCode == 0) ? Addr :
nullptr;
228 #elif defined(__SYCL_RT_OS_WINDOWS)
229 return _aligned_malloc(NumBytes,
Alignment);
234 #if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_POSIX_SUPPORT)
236 #elif defined(__SYCL_RT_OS_WINDOWS)
243 assert((Dir !=
nullptr) &&
"Passed null-pointer as directory name.");
248 #if __GNUC__ && __GNUC__ < 8
249 std::string Path{Dir}, CurPath;
253 pos = Path.find_first_of(
"/\\", ++pos);
254 CurPath = Path.substr(0, pos);
255 #if defined(__SYCL_RT_OS_POSIX_SUPPORT)
256 auto Res = mkdir(CurPath.c_str(), 0777);
258 auto Res = _mkdir(CurPath.c_str());
260 if (Res && errno != EEXIST)
261 throw std::runtime_error(
"Failed to mkdir: " + CurPath +
" (" +
262 std::strerror(errno) +
")");
264 }
while (pos != std::string::npos);
267 std::filesystem::path path(Dir);
268 std::filesystem::create_directories(path.make_preferred());
static void * alignedAlloc(size_t Alignment, size_t NumBytes)
Allocates NumBytes bytes of uninitialized storage whose alignment is specified by Alignment.
static int makeDir(const char *Dir)
Make all directories on the path, throws on error.
static void alignedFree(void *Ptr)
Deallocates the memory referenced by Ptr.
static bool isPathPresent(const std::string &Path)
Checks if specified path is present.
static size_t getOSMemSize()
Returns the amount of RAM available for the operating system.
static std::string getDirName(const char *Path)
Returns a directory component of a path.
static std::string getCurrentDSODir()
Returns an absolute path to a directory where the object was found.
conditional< sizeof(long)==8, long, long long >::type int64_t
void * aligned_alloc(size_t alignment, size_t size, const device &dev, const context &ctxt, usm::alloc kind, const detail::code_location &CodeLoc=detail::code_location::current())
void free(void *ptr, const context &ctxt, const detail::code_location &CodeLoc=detail::code_location::current())
OSModuleHandle getOSModuleHandle(const void *VirtAddr)
constexpr OSModuleHandle ExeModuleHandle