AkPlatformFuncs.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/Tools/Common/AkAssert.h>
  22. #include <AK/SoundEngine/Common/AkTypes.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <wchar.h>
  26. #include <unistd.h>
  27. #include <sys/mman.h>
  28. #ifndef AK_QNX
  29. #include <sys/syscall.h>
  30. #endif
  31. #include <stdlib.h>
  32. #if defined(AK_SUPPORT_THREADS)
  33. #include <pthread.h>
  34. #endif
  35. #define AK_POSIX_NO_ERR 0
  36. #define AK_POSIX
  37. //-----------------------------------------------------------------------------
  38. // Platform-specific thread properties definition.
  39. //-----------------------------------------------------------------------------
  40. struct AkThreadProperties
  41. {
  42. int nPriority; ///< Thread priority
  43. size_t uStackSize; ///< Thread stack size
  44. int uSchedPolicy; ///< Thread scheduling policy
  45. AkUInt32 dwAffinityMask; ///< Affinity mask
  46. };
  47. //-----------------------------------------------------------------------------
  48. // External variables.
  49. //-----------------------------------------------------------------------------
  50. // g_fFreqRatio is used by time helpers to return time values in milliseconds.
  51. // It is declared and updated by the sound engine.
  52. namespace AK
  53. {
  54. extern AkReal32 g_fFreqRatio;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Defines for POSIX (Mac, iOS, Android)
  58. //-----------------------------------------------------------------------------
  59. #define AK_DECLARE_THREAD_ROUTINE( FuncName ) void* FuncName(void* lpParameter)
  60. #define AK_THREAD_RETURN( _param_ ) return (_param_);
  61. #define AK_THREAD_ROUTINE_PARAMETER lpParameter
  62. #define AK_GET_THREAD_ROUTINE_PARAMETER_PTR(type) reinterpret_cast<type*>( AK_THREAD_ROUTINE_PARAMETER )
  63. #define AK_RETURN_THREAD_OK 0x00000000
  64. #define AK_RETURN_THREAD_ERROR 0x00000001
  65. #define AK_DEFAULT_STACK_SIZE (128*1024)
  66. #if defined(AK_SUPPORT_THREADS)
  67. #define AK_THREAD_DEFAULT_SCHED_POLICY SCHED_FIFO
  68. #define AK_THREAD_PRIORITY_NORMAL (((sched_get_priority_max( SCHED_FIFO ) - sched_get_priority_min( SCHED_FIFO )) / 2) + sched_get_priority_min( SCHED_FIFO ))
  69. #define AK_THREAD_PRIORITY_ABOVE_NORMAL sched_get_priority_max( SCHED_FIFO )
  70. #define AK_THREAD_PRIORITY_BELOW_NORMAL sched_get_priority_min( SCHED_FIFO )
  71. #else
  72. #define AK_THREAD_DEFAULT_SCHED_POLICY 1
  73. #define AK_THREAD_PRIORITY_NORMAL 50
  74. #define AK_THREAD_PRIORITY_ABOVE_NORMAL 99
  75. #define AK_THREAD_PRIORITY_BELOW_NORMAL 1
  76. #endif
  77. #define AK_THREAD_AFFINITY_DEFAULT 0xFFFF
  78. // NULL objects
  79. #define AK_NULL_THREAD 0
  80. #define AK_INFINITE (AK_UINT_MAX)
  81. #define AkMax(x1, x2) (((x1) > (x2))? (x1): (x2))
  82. #define AkMin(x1, x2) (((x1) < (x2))? (x1): (x2))
  83. #define AkClamp(x, min, max) ((x) < (min)) ? (min) : (((x) > (max) ? (max) : (x)))
  84. #pragma GCC visibility push(hidden)
  85. namespace AKPLATFORM
  86. {
  87. #define AkExitThread( _result ) return _result;
  88. // Simple automatic event API
  89. // ------------------------------------------------------------------
  90. #ifndef AK_APPLE
  91. /// Platform Independent Helper
  92. inline void AkClearEvent( AkEvent & out_event )
  93. {
  94. memset(&out_event,0,sizeof(AkEvent));
  95. }
  96. /// Platform Independent Helper
  97. inline AKRESULT AkCreateEvent( AkEvent & out_event )
  98. {
  99. int ret = sem_init(
  100. &out_event,
  101. 0,
  102. 0 );
  103. return ( ret == AK_POSIX_NO_ERR ) ? AK_Success : AK_Fail;
  104. }
  105. /// Platform Independent Helper
  106. inline void AkDestroyEvent( AkEvent & io_event )
  107. {
  108. AKVERIFY( sem_destroy( &io_event ) == AK_POSIX_NO_ERR);
  109. AkClearEvent(io_event);
  110. }
  111. /// Platform Independent Helper
  112. inline void AkWaitForEvent( AkEvent & in_event )
  113. {
  114. AKVERIFY( sem_wait( &in_event ) == AK_POSIX_NO_ERR );
  115. }
  116. /// Platform Independent Helper
  117. inline void AkSignalEvent( AkEvent & in_event )
  118. {
  119. AKVERIFY( sem_post( &in_event ) == AK_POSIX_NO_ERR );
  120. }
  121. /// Platform Independent Helper
  122. AkForceInline void AkClearSemaphore(AkSemaphore& io_semaphore)
  123. {
  124. memset(&io_semaphore, 0, sizeof(AkSemaphore));
  125. }
  126. /// Platform Independent Helper
  127. inline AKRESULT AkCreateSemaphore( AkSemaphore& out_semaphore, AkUInt32 in_initialCount )
  128. {
  129. int ret = sem_init(
  130. &out_semaphore,
  131. 0,
  132. in_initialCount );
  133. return ( ret == AK_POSIX_NO_ERR ) ? AK_Success : AK_Fail;
  134. }
  135. /// Platform Independent Helper
  136. inline void AkDestroySemaphore(AkSemaphore& io_semaphore)
  137. {
  138. AKVERIFY(sem_destroy(&io_semaphore) == AK_POSIX_NO_ERR);
  139. memset(&io_semaphore, 0, sizeof(AkSemaphore));
  140. }
  141. /// Platform Independent Helper - Semaphore wait, aka Operation P. Decrements value of semaphore, and, if the semaphore would be less than 0, waits for the semaphore to be released.
  142. inline void AkWaitForSemaphore(AkSemaphore& in_semaphore)
  143. {
  144. AKVERIFY(sem_wait(&in_semaphore) == AK_POSIX_NO_ERR);
  145. }
  146. /// Platform Independent Helper - Semaphore signal, aka Operation V. Increments value of semaphore by an arbitrary count.
  147. inline void AkReleaseSemaphore(AkSemaphore& in_semaphore, AkUInt32 in_count)
  148. {
  149. for (int i=0; i < in_count; i++)
  150. {
  151. AKVERIFY(sem_post(&in_semaphore) == AK_POSIX_NO_ERR);
  152. }
  153. }
  154. #endif
  155. #if defined(AK_SUPPORT_THREADS)
  156. // Threads
  157. // ------------------------------------------------------------------
  158. /// Platform Independent Helper
  159. inline bool AkIsValidThread( AkThread * in_pThread )
  160. {
  161. return (*in_pThread != AK_NULL_THREAD);
  162. }
  163. /// Platform Independent Helper
  164. inline void AkClearThread( AkThread * in_pThread )
  165. {
  166. *in_pThread = AK_NULL_THREAD;
  167. }
  168. /// Platform Independent Helper
  169. inline void AkCloseThread( AkThread * in_pThread )
  170. {
  171. AKASSERT( in_pThread );
  172. AKASSERT( *in_pThread );
  173. AkClearThread( in_pThread );
  174. }
  175. /// Platform Independent Helper
  176. inline void AkGetDefaultThreadProperties( AkThreadProperties & out_threadProperties )
  177. {
  178. out_threadProperties.uStackSize = AK_DEFAULT_STACK_SIZE;
  179. out_threadProperties.uSchedPolicy = AK_THREAD_DEFAULT_SCHED_POLICY;
  180. out_threadProperties.nPriority = AK_THREAD_PRIORITY_NORMAL;
  181. out_threadProperties.dwAffinityMask = AK_THREAD_AFFINITY_DEFAULT;
  182. }
  183. #if !defined(AK_ANDROID) && !defined(AK_LINUX_AOSP)
  184. /// Platform Independent Helper
  185. inline void AkCreateThread(
  186. AkThreadRoutine pStartRoutine, // Thread routine.
  187. void * pParams, // Routine params.
  188. const AkThreadProperties & in_threadProperties, // Properties. NULL for default.
  189. AkThread * out_pThread, // Returned thread handle.
  190. const char * /*in_szThreadName*/ ) // Opt thread name.
  191. {
  192. AKASSERT( out_pThread != NULL );
  193. pthread_attr_t attr;
  194. // Create the attr
  195. AKVERIFY(!pthread_attr_init(&attr));
  196. // Set the stack size
  197. #ifndef AK_EMSCRIPTEN
  198. // Apparently Emscripten doesn't like that we try to specify stack sizes.
  199. AKVERIFY(!pthread_attr_setstacksize(&attr,in_threadProperties.uStackSize));
  200. #endif //AK_EMSCRIPTEN
  201. AKVERIFY(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
  202. // Try to set the thread policy
  203. int sched_policy = in_threadProperties.uSchedPolicy;
  204. if( pthread_attr_setschedpolicy( &attr, sched_policy ) )
  205. {
  206. AKASSERT( !"AKCreateThread invalid sched policy, will automatically set it to FIFO scheduling" );
  207. sched_policy = AK_THREAD_DEFAULT_SCHED_POLICY;
  208. AKVERIFY( !pthread_attr_setschedpolicy( &attr, sched_policy ));
  209. }
  210. // Get the priority for the policy
  211. int minPriority, maxPriority;
  212. minPriority = sched_get_priority_min(sched_policy);
  213. maxPriority = sched_get_priority_max(sched_policy);
  214. // Set the thread priority if valid
  215. AKASSERT( in_threadProperties.nPriority >= minPriority && in_threadProperties.nPriority <= maxPriority );
  216. if( in_threadProperties.nPriority >= minPriority && in_threadProperties.nPriority <= maxPriority )
  217. {
  218. sched_param schedParam;
  219. AKVERIFY( !pthread_attr_getschedparam(&attr, &schedParam));
  220. schedParam.sched_priority = in_threadProperties.nPriority;
  221. AKVERIFY( !pthread_attr_setschedparam( &attr, &schedParam ));
  222. }
  223. #ifdef AK_APPLE
  224. int inherit;
  225. pthread_attr_getinheritsched(&attr, &inherit );
  226. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED );
  227. #endif
  228. // Create the tread
  229. int threadError = pthread_create( out_pThread, &attr, pStartRoutine, pParams);
  230. AKASSERT( threadError == 0 );
  231. AKVERIFY(!pthread_attr_destroy(&attr));
  232. if( threadError != 0 )
  233. {
  234. AkClearThread( out_pThread );
  235. return;
  236. }
  237. // ::CreateThread() return NULL if it fails.
  238. if ( !*out_pThread )
  239. {
  240. AkClearThread( out_pThread );
  241. return;
  242. }
  243. }
  244. #endif
  245. /// Platform Independent Helper
  246. inline void AkWaitForSingleThread( AkThread * in_pThread )
  247. {
  248. AKASSERT( in_pThread );
  249. AKASSERT( *in_pThread );
  250. AKVERIFY(!pthread_join( *in_pThread, NULL ));
  251. }
  252. /// Returns the calling thread's ID.
  253. inline AkThreadID CurrentThread()
  254. {
  255. return pthread_self();
  256. }
  257. #else // AK_SUPPORT_THREADS
  258. inline bool AkIsValidThread( AkThread * in_pThread )
  259. {
  260. return false;
  261. }
  262. inline void AkClearThread( AkThread * in_pThread )
  263. {
  264. }
  265. inline void AkCloseThread( AkThread * in_pThread )
  266. {
  267. }
  268. /// Platform Independent Helper
  269. inline void AkGetDefaultThreadProperties( AkThreadProperties & out_threadProperties )
  270. {
  271. out_threadProperties.uStackSize = AK_DEFAULT_STACK_SIZE;
  272. out_threadProperties.uSchedPolicy = AK_THREAD_DEFAULT_SCHED_POLICY;
  273. out_threadProperties.nPriority = AK_THREAD_PRIORITY_NORMAL;
  274. out_threadProperties.dwAffinityMask = AK_THREAD_AFFINITY_DEFAULT;
  275. }
  276. inline void AkCreateThread(
  277. AkThreadRoutine pStartRoutine, // Thread routine.
  278. void * pParams, // Routine params.
  279. const AkThreadProperties & in_threadProperties, // Properties. NULL for default.
  280. AkThread * out_pThread, // Returned thread handle.
  281. const char * /*in_szThreadName*/ ) // Opt thread name.
  282. {
  283. }
  284. /// Platform Independent Helper
  285. inline void AkWaitForSingleThread( AkThread * in_pThread )
  286. {
  287. }
  288. /// Returns the calling thread's ID.
  289. inline AkThreadID CurrentThread()
  290. {
  291. return 1;
  292. }
  293. #endif // AK_SUPPORT_THREADS
  294. /// Platform Independent Helper
  295. inline void AkSleep( AkUInt32 in_ulMilliseconds )
  296. {
  297. // usleep in micro second
  298. usleep( in_ulMilliseconds * 1000 );
  299. }
  300. // Optimized memory functions
  301. // --------------------------------------------------------------------
  302. /// Platform Independent Helper
  303. inline void AkMemCpy( void * pDest, const void * pSrc, AkUInt32 uSize )
  304. {
  305. memcpy( pDest, pSrc, uSize );
  306. }
  307. /// Platform Independent Helper
  308. inline void AkMemMove( void* pDest, const void* pSrc, AkUInt32 uSize )
  309. {
  310. memmove( pDest, pSrc, uSize );
  311. }
  312. /// Platform Independent Helper
  313. inline void AkMemSet( void * pDest, AkInt32 iVal, AkUInt32 uSize )
  314. {
  315. memset( pDest, iVal, uSize );
  316. }
  317. /// Platform Independent Helper
  318. inline void UpdatePerformanceFrequency()
  319. {
  320. AkInt64 iFreq;
  321. PerformanceFrequency( &iFreq );
  322. AK::g_fFreqRatio = (AkReal32)((AkReal64)iFreq / 1000);
  323. }
  324. /// Returns a time range in milliseconds, using the sound engine's updated count->milliseconds ratio.
  325. inline AkReal32 Elapsed( const AkInt64 & in_iNow, const AkInt64 & in_iStart )
  326. {
  327. return ( in_iNow - in_iStart ) / AK::g_fFreqRatio;
  328. }
  329. /// String conversion helper
  330. inline AkInt32 AkWideCharToChar( const wchar_t* in_pszUnicodeString,
  331. AkUInt32 in_uiOutBufferSize,
  332. char* io_pszAnsiString )
  333. {
  334. AKASSERT( io_pszAnsiString != NULL );
  335. io_pszAnsiString[0] = 0;
  336. AkInt32 ConvRet = (AkInt32) wcstombs(io_pszAnsiString, in_pszUnicodeString, in_uiOutBufferSize);
  337. return ConvRet;
  338. }
  339. #ifdef AK_SUPPORT_WCHAR
  340. /// String conversion helper
  341. inline AkInt32 AkCharToWideChar( const char* in_pszAnsiString,
  342. AkUInt32 in_uiOutBufferSize,
  343. wchar_t* io_pvUnicodeStringBuffer )
  344. {
  345. AKASSERT( io_pvUnicodeStringBuffer != NULL );
  346. io_pvUnicodeStringBuffer[0] = 0;
  347. AkInt32 ConvRet = (AkInt32) mbstowcs((wchar_t *)io_pvUnicodeStringBuffer, in_pszAnsiString, in_uiOutBufferSize );
  348. return ConvRet;
  349. }
  350. inline AkInt32 AkUtf8ToWideChar( const char* in_pszUtf8String,
  351. AkUInt32 in_uiOutBufferSize,
  352. void* io_pvUnicodeStringBuffer )
  353. {
  354. return AkCharToWideChar( in_pszUtf8String, in_uiOutBufferSize, (wchar_t*)io_pvUnicodeStringBuffer );
  355. }
  356. #endif
  357. /// Safe unicode string copy.
  358. inline void SafeStrCpy( wchar_t * in_pDest, const wchar_t* in_pSrc, size_t in_uDestMaxNumChars )
  359. {
  360. size_t iSizeCopy = AkMin( in_uDestMaxNumChars - 1, wcslen( in_pSrc ) + 1 );
  361. wcsncpy( in_pDest, in_pSrc, iSizeCopy );
  362. in_pDest[iSizeCopy] = '\0';
  363. }
  364. /// Safe ansi string copy.
  365. inline void SafeStrCpy( char * in_pDest, const char* in_pSrc, size_t in_uDestMaxNumChars )
  366. {
  367. size_t iSizeCopy = AkMin( in_uDestMaxNumChars - 1, strlen( in_pSrc ) + 1 );
  368. strncpy( in_pDest, in_pSrc, iSizeCopy );
  369. in_pDest[iSizeCopy] = '\0';
  370. }
  371. /// Safe unicode string concatenation.
  372. inline void SafeStrCat( wchar_t * in_pDest, const wchar_t* in_pSrc, size_t in_uDestMaxNumChars )
  373. {
  374. size_t iAvailableSize = in_uDestMaxNumChars - wcslen( in_pDest ) - 1;
  375. wcsncat( in_pDest, in_pSrc, AkMin( iAvailableSize, wcslen( in_pSrc ) ) );
  376. }
  377. /// Safe ansi string concatenation.
  378. inline void SafeStrCat( char * in_pDest, const char* in_pSrc, size_t in_uDestMaxNumChars )
  379. {
  380. size_t iAvailableSize = in_uDestMaxNumChars - strlen( in_pDest ) - 1;
  381. strncat( in_pDest, in_pSrc, AkMin( iAvailableSize, strlen( in_pSrc ) ) );
  382. }
  383. inline int SafeStrFormat(wchar_t * in_pDest, size_t in_uDestMaxNumChars, const wchar_t* in_pszFmt, ...)
  384. {
  385. va_list args;
  386. va_start(args, in_pszFmt);
  387. int r = vswprintf(in_pDest, in_uDestMaxNumChars, in_pszFmt, args);
  388. va_end(args);
  389. return r;
  390. }
  391. inline int SafeStrFormat(char * in_pDest, size_t in_uDestMaxNumChars, const char* in_pszFmt, ...)
  392. {
  393. va_list args;
  394. va_start(args, in_pszFmt);
  395. int r = vsnprintf(in_pDest, in_uDestMaxNumChars, in_pszFmt, args);
  396. va_end(args);
  397. return r;
  398. }
  399. /// Get the length, in characters, of a NULL-terminated AkUtf16 string
  400. /// \return The length, in characters, of the specified string (excluding terminating NULL)
  401. inline size_t AkUtf16StrLen( const AkUtf16* in_pStr )
  402. {
  403. size_t len = 0;
  404. while( *in_pStr != 0 )
  405. {
  406. in_pStr++;
  407. len++;
  408. }
  409. return len;
  410. }
  411. #if !defined(AK_ANDROID) && !defined(AK_LINUX_AOSP)
  412. #ifndef AK_OPTIMIZED
  413. /// Output a debug message on the console (Unicode string)
  414. inline void OutputDebugMsg( const wchar_t* in_pszMsg )
  415. {
  416. fputws( in_pszMsg, stderr );
  417. }
  418. /// Output a debug message on the console (Ansi string)
  419. inline void OutputDebugMsg( const char* in_pszMsg )
  420. {
  421. fputs( in_pszMsg, stderr );
  422. }
  423. /// Output a debug message on the console (Unicode string)
  424. template <int MaxSize = 0> // Unused
  425. inline void OutputDebugMsgV( const wchar_t* in_pszFmt, ... )
  426. {
  427. va_list args;
  428. va_start(args, in_pszFmt);
  429. vfwprintf(stderr, in_pszFmt, args);
  430. va_end(args);
  431. }
  432. /// Output a debug message on the console (Ansi string)
  433. template <int MaxSize = 0> // Unused
  434. inline void OutputDebugMsgV( const char* in_pszFmt, ... )
  435. {
  436. va_list args;
  437. va_start(args, in_pszFmt);
  438. vfprintf(stderr, in_pszFmt, args);
  439. va_end(args);
  440. }
  441. #else
  442. inline void OutputDebugMsg( const wchar_t* ){}
  443. inline void OutputDebugMsg( const char* ){}
  444. template <int MaxSize = 0> // Unused
  445. inline void OutputDebugMsgV( const wchar_t* in_pszFmt, ... ){}
  446. template <int MaxSize = 0> // Unused
  447. inline void OutputDebugMsgV( const char* in_pszFmt, ... ){}
  448. #endif
  449. #endif
  450. /// Converts a wchar_t string to an AkOSChar string.
  451. /// \remark On some platforms the AkOSChar string simply points to the same string,
  452. /// on others a new buffer is allocated on the stack using AkAlloca. This means
  453. /// you must make sure that:
  454. /// - The source string stays valid and unmodified for as long as you need the
  455. /// AkOSChar string (for cases where they point to the same string)
  456. /// - The AkOSChar string is used within this scope only -- for example, do NOT
  457. /// return that string from a function (for cases where it is allocated on the stack)
  458. #define CONVERT_WIDE_TO_OSCHAR( _astring_, _oscharstring_ ) \
  459. _oscharstring_ = (AkOSChar*)AkAlloca( (1 + wcslen( _astring_ )) * sizeof(AkOSChar)); \
  460. AKPLATFORM::AkWideCharToChar( _astring_, (AkUInt32)(1 + wcslen(_astring_ )), (AkOSChar*)( _oscharstring_ ) )
  461. /// Converts a char string to an AkOSChar string.
  462. /// \remark On some platforms the AkOSChar string simply points to the same string,
  463. /// on others a new buffer is allocated on the stack using AkAlloca. This means
  464. /// you must make sure that:
  465. /// - The source string stays valid and unmodified for as long as you need the
  466. /// AkOSChar string (for cases where they point to the same string)
  467. /// - The AkOSChar string is used within this scope only -- for example, do NOT
  468. /// return that string from a function (for cases where it is allocated on the stack)
  469. #define CONVERT_CHAR_TO_OSCHAR( _astring_, _oscharstring_ ) ( _oscharstring_ ) = (AkOSChar*)( _astring_ )
  470. /// Converts a AkOSChar string into wide char string.
  471. /// \remark On some platforms the AkOSChar string simply points to the same string,
  472. /// on others a new buffer is allocated on the stack using AkAlloca. This means
  473. /// you must make sure that:
  474. /// - The source string stays valid and unmodified for as long as you need the
  475. /// AkOSChar string (for cases where they point to the same string)
  476. /// - The AkOSChar string is used within this scope only -- for example, do NOT
  477. /// return that string from a function (for cases where it is allocated on the stack)
  478. #define CONVERT_OSCHAR_TO_WIDE( _osstring_, _wstring_ ) \
  479. _wstring_ = (wchar_t*)AkAlloca((1+strlen(_osstring_)) * sizeof(wchar_t)); \
  480. AKPLATFORM::AkCharToWideChar( _osstring_, (AkUInt32)(1 + strlen(_osstring_ )), _wstring_ )
  481. /// Converts a AkOSChar string into char string.
  482. /// \remark On some platforms the AkOSChar string simply points to the same string,
  483. /// on others a new buffer is allocated on the stack using AkAlloca. This means
  484. /// you must make sure that:
  485. /// - The source string stays valid and unmodified for as long as you need the
  486. /// AkOSChar string (for cases where they point to the same string)
  487. /// - The AkOSChar string is used within this scope only -- for example, do NOT
  488. /// return that string from a function (for cases where it is allocated on the stack)
  489. #define CONVERT_OSCHAR_TO_CHAR( _osstring_, _astring_ ) _astring_ = (char*)_osstring_
  490. inline size_t OsStrLen( const AkOSChar* in_pszString )
  491. {
  492. return ( strlen( in_pszString ) );
  493. }
  494. /// AkOSChar version of snprintf().
  495. #define AK_OSPRINTF snprintf
  496. inline int OsStrCmp( const AkOSChar* in_pszString1, const AkOSChar* in_pszString2 )
  497. {
  498. return ( strcmp( in_pszString1, in_pszString2 ) );
  499. }
  500. /// Compare two NULL-terminated AkOSChar strings up to the specified count of characters.
  501. /// \return
  502. /// - \< 0 if in_pszString1 \< in_pszString2
  503. /// - 0 if the two strings are identical
  504. /// - \> 0 if in_pszString1 \> in_pszString2
  505. /// \remark The comparison is case-sensitive
  506. inline int OsStrNCmp( const AkOSChar* in_pszString1, const AkOSChar* in_pszString2, size_t in_MaxCountSize )
  507. {
  508. return ( strncmp(in_pszString1, in_pszString2, in_MaxCountSize) );
  509. }
  510. /// Detects whether the string represents an absolute path to a file
  511. inline bool IsAbsolutePath(const AkOSChar* in_pszPath, size_t in_pathLen)
  512. {
  513. return in_pathLen >= 1 && in_pszPath[0] == '/';
  514. }
  515. // Use with AkOSChar.
  516. #ifndef AK_PATH_SEPARATOR
  517. #define AK_PATH_SEPARATOR "/"
  518. #endif
  519. #ifndef AK_LIBRARY_PREFIX
  520. #define AK_LIBRARY_PREFIX "lib"
  521. #endif
  522. #ifndef AK_DYNAMIC_LIBRARY_EXTENSION
  523. #define AK_DYNAMIC_LIBRARY_EXTENSION ".so"
  524. #endif
  525. #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
  526. #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
  527. }
  528. #pragma GCC visibility pop