os.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright 2010-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include <bx/os.h>
  6. #include <bx/uint32_t.h>
  7. #include <bx/string.h>
  8. #include <stdio.h>
  9. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
  10. # include <windows.h>
  11. # include <psapi.h>
  12. #elif BX_PLATFORM_ANDROID \
  13. || BX_PLATFORM_EMSCRIPTEN \
  14. || BX_PLATFORM_BSD \
  15. || BX_PLATFORM_HURD \
  16. || BX_PLATFORM_IOS \
  17. || BX_PLATFORM_LINUX \
  18. || BX_PLATFORM_NACL \
  19. || BX_PLATFORM_OSX \
  20. || BX_PLATFORM_PS4 \
  21. || BX_PLATFORM_RPI \
  22. || BX_PLATFORM_STEAMLINK
  23. # include <sched.h> // sched_yield
  24. # if BX_PLATFORM_BSD \
  25. || BX_PLATFORM_IOS \
  26. || BX_PLATFORM_NACL \
  27. || BX_PLATFORM_OSX \
  28. || BX_PLATFORM_PS4 \
  29. || BX_PLATFORM_STEAMLINK
  30. # include <pthread.h> // mach_port_t
  31. # endif // BX_PLATFORM_*
  32. # include <time.h> // nanosleep
  33. # if !BX_PLATFORM_PS4 && !BX_PLATFORM_NACL
  34. # include <dlfcn.h> // dlopen, dlclose, dlsym
  35. # endif // !BX_PLATFORM_PS4 && !BX_PLATFORM_NACL
  36. # if BX_PLATFORM_ANDROID
  37. # include <malloc.h> // mallinfo
  38. # elif BX_PLATFORM_LINUX \
  39. || BX_PLATFORM_RPI \
  40. || BX_PLATFORM_STEAMLINK
  41. # include <unistd.h> // syscall
  42. # include <sys/syscall.h>
  43. # elif BX_PLATFORM_OSX
  44. # include <mach/mach.h> // mach_task_basic_info
  45. # elif BX_PLATFORM_HURD
  46. # include <pthread/pthread.h> // pthread_self
  47. # elif BX_PLATFORM_ANDROID
  48. # include "debug.h" // getTid is not implemented...
  49. # endif // BX_PLATFORM_ANDROID
  50. #endif // BX_PLATFORM_
  51. #if BX_CRT_MSVC
  52. # include <direct.h> // _getcwd
  53. #else
  54. # include <unistd.h> // getcwd
  55. #endif // BX_CRT_MSVC
  56. namespace bx
  57. {
  58. void sleep(uint32_t _ms)
  59. {
  60. #if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360
  61. ::Sleep(_ms);
  62. #elif BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  63. BX_UNUSED(_ms);
  64. debugOutput("sleep is not implemented"); debugBreak();
  65. #else
  66. timespec req = {(time_t)_ms/1000, (long)((_ms%1000)*1000000)};
  67. timespec rem = {0, 0};
  68. ::nanosleep(&req, &rem);
  69. #endif // BX_PLATFORM_
  70. }
  71. void yield()
  72. {
  73. #if BX_PLATFORM_WINDOWS
  74. ::SwitchToThread();
  75. #elif BX_PLATFORM_XBOX360
  76. ::Sleep(0);
  77. #elif BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
  78. debugOutput("yield is not implemented"); debugBreak();
  79. #else
  80. ::sched_yield();
  81. #endif // BX_PLATFORM_
  82. }
  83. uint32_t getTid()
  84. {
  85. #if BX_PLATFORM_WINDOWS
  86. return ::GetCurrentThreadId();
  87. #elif BX_PLATFORM_LINUX || BX_PLATFORM_RPI || BX_PLATFORM_STEAMLINK
  88. return (pid_t)::syscall(SYS_gettid);
  89. #elif BX_PLATFORM_IOS || BX_PLATFORM_OSX
  90. return (mach_port_t)::pthread_mach_thread_np(pthread_self() );
  91. #elif BX_PLATFORM_BSD || BX_PLATFORM_NACL
  92. // Casting __nc_basic_thread_data*... need better way to do this.
  93. return *(uint32_t*)::pthread_self();
  94. #elif BX_PLATFORM_HURD
  95. return (pthread_t)::pthread_self();
  96. #else
  97. //# pragma message "not implemented."
  98. debugOutput("getTid is not implemented"); debugBreak();
  99. return 0;
  100. #endif //
  101. }
  102. size_t getProcessMemoryUsed()
  103. {
  104. #if BX_PLATFORM_ANDROID
  105. struct mallinfo mi = mallinfo();
  106. return mi.uordblks;
  107. #elif BX_PLATFORM_LINUX || BX_PLATFORM_HURD
  108. FILE* file = fopen("/proc/self/statm", "r");
  109. if (NULL == file)
  110. {
  111. return 0;
  112. }
  113. long pages = 0;
  114. int items = fscanf(file, "%*s%ld", &pages);
  115. fclose(file);
  116. return 1 == items
  117. ? pages * sysconf(_SC_PAGESIZE)
  118. : 0
  119. ;
  120. #elif BX_PLATFORM_OSX
  121. # if defined(MACH_TASK_BASIC_INFO)
  122. mach_task_basic_info info;
  123. mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
  124. int const result = task_info(mach_task_self()
  125. , MACH_TASK_BASIC_INFO
  126. , (task_info_t)&info
  127. , &infoCount
  128. );
  129. # else // MACH_TASK_BASIC_INFO
  130. task_basic_info info;
  131. mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
  132. int const result = task_info(mach_task_self()
  133. , TASK_BASIC_INFO
  134. , (task_info_t)&info
  135. , &infoCount
  136. );
  137. # endif // MACH_TASK_BASIC_INFO
  138. if (KERN_SUCCESS != result)
  139. {
  140. return 0;
  141. }
  142. return info.resident_size;
  143. #elif BX_PLATFORM_WINDOWS
  144. PROCESS_MEMORY_COUNTERS pmc;
  145. GetProcessMemoryInfo(GetCurrentProcess()
  146. , &pmc
  147. , sizeof(pmc)
  148. );
  149. return pmc.WorkingSetSize;
  150. #else
  151. return 0;
  152. #endif // BX_PLATFORM_*
  153. }
  154. void* dlopen(const char* _filePath)
  155. {
  156. #if BX_PLATFORM_WINDOWS
  157. return (void*)::LoadLibraryA(_filePath);
  158. #elif BX_PLATFORM_EMSCRIPTEN \
  159. || BX_PLATFORM_NACL \
  160. || BX_PLATFORM_PS4 \
  161. || BX_PLATFORM_XBOXONE \
  162. || BX_PLATFORM_WINRT
  163. BX_UNUSED(_filePath);
  164. return NULL;
  165. #else
  166. return ::dlopen(_filePath, RTLD_LOCAL|RTLD_LAZY);
  167. #endif // BX_PLATFORM_
  168. }
  169. void dlclose(void* _handle)
  170. {
  171. #if BX_PLATFORM_WINDOWS
  172. ::FreeLibrary( (HMODULE)_handle);
  173. #elif BX_PLATFORM_EMSCRIPTEN \
  174. || BX_PLATFORM_NACL \
  175. || BX_PLATFORM_PS4 \
  176. || BX_PLATFORM_XBOXONE \
  177. || BX_PLATFORM_WINRT
  178. BX_UNUSED(_handle);
  179. #else
  180. ::dlclose(_handle);
  181. #endif // BX_PLATFORM_
  182. }
  183. void* dlsym(void* _handle, const char* _symbol)
  184. {
  185. #if BX_PLATFORM_WINDOWS
  186. return (void*)::GetProcAddress( (HMODULE)_handle, _symbol);
  187. #elif BX_PLATFORM_EMSCRIPTEN \
  188. || BX_PLATFORM_NACL \
  189. || BX_PLATFORM_PS4 \
  190. || BX_PLATFORM_XBOXONE \
  191. || BX_PLATFORM_WINRT
  192. BX_UNUSED(_handle, _symbol);
  193. return NULL;
  194. #else
  195. return ::dlsym(_handle, _symbol);
  196. #endif // BX_PLATFORM_
  197. }
  198. bool getenv(const char* _name, char* _out, uint32_t* _inOutSize)
  199. {
  200. #if BX_PLATFORM_WINDOWS
  201. DWORD len = ::GetEnvironmentVariableA(_name, _out, *_inOutSize);
  202. bool result = len != 0 && len < *_inOutSize;
  203. *_inOutSize = len;
  204. return result;
  205. #elif BX_PLATFORM_PS4 \
  206. || BX_PLATFORM_XBOXONE \
  207. || BX_PLATFORM_WINRT
  208. BX_UNUSED(_name, _out, _inOutSize);
  209. return false;
  210. #else
  211. const char* ptr = ::getenv(_name);
  212. uint32_t len = 0;
  213. bool result = false;
  214. if (NULL != ptr)
  215. {
  216. len = (uint32_t)strnlen(ptr);
  217. result = len != 0 && len < *_inOutSize;
  218. if (len < *_inOutSize)
  219. {
  220. strlncpy(_out, len, ptr);
  221. }
  222. }
  223. *_inOutSize = len;
  224. return result;
  225. #endif // BX_PLATFORM_
  226. }
  227. void setenv(const char* _name, const char* _value)
  228. {
  229. #if BX_PLATFORM_WINDOWS
  230. ::SetEnvironmentVariableA(_name, _value);
  231. #elif BX_PLATFORM_PS4 \
  232. || BX_PLATFORM_XBOXONE \
  233. || BX_PLATFORM_WINRT
  234. BX_UNUSED(_name, _value);
  235. #else
  236. ::setenv(_name, _value, 1);
  237. #endif // BX_PLATFORM_
  238. }
  239. void unsetenv(const char* _name)
  240. {
  241. #if BX_PLATFORM_WINDOWS
  242. ::SetEnvironmentVariableA(_name, NULL);
  243. #elif BX_PLATFORM_PS4 \
  244. || BX_PLATFORM_XBOXONE \
  245. || BX_PLATFORM_WINRT
  246. BX_UNUSED(_name);
  247. #else
  248. ::unsetenv(_name);
  249. #endif // BX_PLATFORM_
  250. }
  251. int chdir(const char* _path)
  252. {
  253. #if BX_PLATFORM_PS4 \
  254. || BX_PLATFORM_XBOXONE \
  255. || BX_PLATFORM_WINRT
  256. BX_UNUSED(_path);
  257. return -1;
  258. #elif BX_CRT_MSVC
  259. return ::_chdir(_path);
  260. #else
  261. return ::chdir(_path);
  262. #endif // BX_COMPILER_
  263. }
  264. char* pwd(char* _buffer, uint32_t _size)
  265. {
  266. #if BX_PLATFORM_PS4 \
  267. || BX_PLATFORM_XBOXONE \
  268. || BX_PLATFORM_WINRT
  269. BX_UNUSED(_buffer, _size);
  270. return NULL;
  271. #elif BX_CRT_MSVC
  272. return ::_getcwd(_buffer, (int)_size);
  273. #else
  274. return ::getcwd(_buffer, _size);
  275. #endif // BX_COMPILER_
  276. }
  277. bool getTempPath(char* _out, uint32_t* _inOutSize)
  278. {
  279. #if BX_PLATFORM_WINDOWS
  280. uint32_t len = ::GetTempPathA(*_inOutSize, _out);
  281. bool result = len != 0 && len < *_inOutSize;
  282. *_inOutSize = len;
  283. return result;
  284. #else
  285. static const char* s_tmp[] =
  286. {
  287. "TMPDIR",
  288. "TMP",
  289. "TEMP",
  290. "TEMPDIR",
  291. NULL
  292. };
  293. for (const char** tmp = s_tmp; *tmp != NULL; ++tmp)
  294. {
  295. uint32_t len = *_inOutSize;
  296. *_out = '\0';
  297. bool result = getenv(*tmp, _out, &len);
  298. if (result
  299. && len != 0
  300. && len < *_inOutSize)
  301. {
  302. *_inOutSize = len;
  303. return result;
  304. }
  305. }
  306. FileInfo fi;
  307. if (stat("/tmp", fi)
  308. && FileInfo::Directory == fi.m_type)
  309. {
  310. strlncpy(_out, *_inOutSize, "/tmp");
  311. *_inOutSize = 4;
  312. return true;
  313. }
  314. return false;
  315. #endif // BX_PLATFORM_*
  316. }
  317. bool stat(const char* _filePath, FileInfo& _fileInfo)
  318. {
  319. _fileInfo.m_size = 0;
  320. _fileInfo.m_type = FileInfo::Count;
  321. #if BX_COMPILER_MSVC
  322. struct ::_stat64 st;
  323. int32_t result = ::_stat64(_filePath, &st);
  324. if (0 != result)
  325. {
  326. return false;
  327. }
  328. if (0 != (st.st_mode & _S_IFREG) )
  329. {
  330. _fileInfo.m_type = FileInfo::Regular;
  331. }
  332. else if (0 != (st.st_mode & _S_IFDIR) )
  333. {
  334. _fileInfo.m_type = FileInfo::Directory;
  335. }
  336. #else
  337. struct ::stat st;
  338. int32_t result = ::stat(_filePath, &st);
  339. if (0 != result)
  340. {
  341. return false;
  342. }
  343. if (0 != (st.st_mode & S_IFREG) )
  344. {
  345. _fileInfo.m_type = FileInfo::Regular;
  346. }
  347. else if (0 != (st.st_mode & S_IFDIR) )
  348. {
  349. _fileInfo.m_type = FileInfo::Directory;
  350. }
  351. #endif // BX_COMPILER_MSVC
  352. _fileInfo.m_size = st.st_size;
  353. return true;
  354. }
  355. void* exec(const char* const* _argv)
  356. {
  357. #if BX_PLATFORM_LINUX || BX_PLATFORM_HURD
  358. pid_t pid = fork();
  359. if (0 == pid)
  360. {
  361. int result = execvp(_argv[0], const_cast<char *const*>(&_argv[1]) );
  362. BX_UNUSED(result);
  363. return NULL;
  364. }
  365. return (void*)uintptr_t(pid);
  366. #elif BX_PLATFORM_WINDOWS
  367. STARTUPINFO si;
  368. memSet(&si, 0, sizeof(STARTUPINFO) );
  369. si.cb = sizeof(STARTUPINFO);
  370. PROCESS_INFORMATION pi;
  371. memSet(&pi, 0, sizeof(PROCESS_INFORMATION) );
  372. int32_t total = 0;
  373. for (uint32_t ii = 0; NULL != _argv[ii]; ++ii)
  374. {
  375. total += (int32_t)strnlen(_argv[ii]) + 1;
  376. }
  377. char* temp = (char*)alloca(total);
  378. int32_t len = 0;
  379. for(uint32_t ii = 0; NULL != _argv[ii]; ++ii)
  380. {
  381. len += snprintf(&temp[len], bx::uint32_imax(0, total-len)
  382. , "%s "
  383. , _argv[ii]
  384. );
  385. }
  386. bool ok = !!CreateProcessA(_argv[0]
  387. , temp
  388. , NULL
  389. , NULL
  390. , false
  391. , 0
  392. , NULL
  393. , NULL
  394. , &si
  395. , &pi
  396. );
  397. if (ok)
  398. {
  399. return pi.hProcess;
  400. }
  401. return NULL;
  402. #else
  403. BX_UNUSED(_argv);
  404. return NULL;
  405. #endif // BX_PLATFORM_LINUX || BX_PLATFORM_HURD
  406. }
  407. } // namespace bx