Version
menu_open
link
Wwise SDK 2018.1.11
AkPlatformFuncs.h
Go to the documentation of this file.
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 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Version: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
26 *******************************************************************************/
27 
28 #pragma once
29 
32 
33 #include <mach/task.h>
34 #include <mach/semaphore.h>
35 #include <CoreFoundation/CFString.h>
36 #include <libkern/OSAtomic.h>
37 #include <mach/task.h>
38 #include <mach/mach_init.h>
39 #include <mach/mach_time.h>
40 #include <wchar.h>
41 
42 namespace AKPLATFORM
43 {
44  extern inline size_t AkUtf16StrLen( const AkUtf16* in_pStr );
45  // Simple automatic event API
46  // ------------------------------------------------------------------
47 
48  /// Platform Independent Helper
49  inline void AkClearEvent( AkEvent & out_event )
50  {
51  out_event = 0;
52  }
53 
54  /// Platform Independent Helper
55  inline AKRESULT AkCreateEvent( AkEvent & out_event )
56  {
57  kern_return_t ret = semaphore_create(
58  mach_task_self(),
59  &out_event,
60  SYNC_POLICY_FIFO,
61  0 );
62 
63  return ( ret == noErr ) ? AK_Success : AK_Fail;
64  }
65 
66  inline AKRESULT AkCreateSemaphore( AkSemaphore & out_semaphore, AkUInt32 in_initialCount )
67  {
68  kern_return_t ret = semaphore_create(
69  mach_task_self(),
70  &out_semaphore,
71  SYNC_POLICY_FIFO,
72  in_initialCount );
73 
74  return ( ret == noErr ) ? AK_Success : AK_Fail;
75  }
76 
77  /// Platform Independent Helper
78  inline void AkDestroyEvent( AkEvent & io_event )
79  {
80  if( io_event != 0 )
81  {
82  AKVERIFY( semaphore_destroy( mach_task_self(), io_event ) == noErr);
83  }
84  io_event = 0;
85  }
86 
87  /// Platform Independent Helper
88  inline void AkDestroySemaphore( AkSemaphore & io_semaphore )
89  {
90  if( io_semaphore != 0 )
91  {
92  AKVERIFY( semaphore_destroy( mach_task_self(), io_semaphore ) == noErr);
93  }
94  io_semaphore = 0;
95  }
96 
97  /// Platform Independent Helper
98  inline void AkWaitForEvent( AkEvent & in_event )
99  {
100  AKVERIFY( semaphore_wait( in_event ) == noErr );
101  }
102 
103  inline void AkWaitForSemaphore( AkSemaphore & in_semaphore )
104  {
105  AKVERIFY( semaphore_wait( in_semaphore ) == noErr );
106  }
107 
108  /// Platform Independent Helper
109  inline void AkSignalEvent( const AkEvent & in_event )
110  {
111  AKVERIFY( semaphore_signal( in_event ) == noErr );
112  }
113 
114  inline void AkReleaseSemaphore( const AkSemaphore & in_event )
115  {
116  AKVERIFY( semaphore_signal( in_event ) == noErr );
117  }
118 
119  // Atomic Operations
120  // ------------------------------------------------------------------
121 
122  /// Platform Independent Helper
123  inline AkInt32 AkInterlockedIncrement(AkAtomic32 * pValue)
124  {
125  return __sync_add_and_fetch(pValue,1);
126  }
127 
128  /// Platform Independent Helper
129  inline AkInt32 AkInterlockedDecrement(AkAtomic32 * pValue)
130  {
131  return __sync_sub_and_fetch(pValue,1);
132  }
133 
134  AkForceInline bool AkInterlockedCompareExchange(volatile AkAtomic32* io_pDest, AkInt32 in_newValue, AkInt32 in_expectedOldVal)
135  {
136  return __sync_bool_compare_and_swap(io_pDest, in_expectedOldVal, in_newValue);
137  }
138 
139  AkForceInline bool AkInterlockedCompareExchange(volatile AkAtomic64* io_pDest, AkInt64 in_newValue, AkInt64 in_expectedOldVal)
140  {
141  return __sync_bool_compare_and_swap(io_pDest, in_expectedOldVal, in_newValue);
142  }
143 
144  AkForceInline bool AkInterlockedCompareExchange(volatile AkAtomicPtr* io_pDest, AkIntPtr in_newValue, AkIntPtr in_expectedOldVal)
145  {
146  return __sync_bool_compare_and_swap(io_pDest, in_expectedOldVal, in_newValue);
147  }
148 
149  inline void AkMemoryBarrier()
150  {
151  __sync_synchronize();
152  }
153 
154  // Time functions
155  // ------------------------------------------------------------------
156 
157  /// Platform Independent Helper
158  inline void PerformanceCounter( AkInt64 * out_piLastTime )
159  {
160  *out_piLastTime = mach_absolute_time();
161  }
162 
163  /// Platform Independent Helper
164  inline void PerformanceFrequency( AkInt64 * out_piFreq )
165  {
166  static mach_timebase_info_data_t sTimebaseInfo;
167  mach_timebase_info(&sTimebaseInfo);
168  if ( sTimebaseInfo.numer !=0 )
169  {
170  *out_piFreq = AkInt64((1E9 * sTimebaseInfo.denom) / sTimebaseInfo.numer );
171  }
172  else
173  {
174  *out_piFreq = 0;
175  }
176  }
177 
178 
179  template<class destType, class srcType>
180  inline size_t AkMacConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *) )
181  {
182  CFStringBuiltInEncodings dstEncoding;
183  CFStringBuiltInEncodings srcEncoding;
184  switch(sizeof(destType))
185  {
186  case 1:
187  dstEncoding = kCFStringEncodingUTF8;
188  break;
189  case 2:
190  dstEncoding = kCFStringEncodingUTF16LE;
191  break;
192  case 4:
193  dstEncoding = kCFStringEncodingUTF32LE;
194  break;
195  default:
196  AKASSERT(!"Invalid Char size");
197  }
198 
199  switch(sizeof(srcType))
200  {
201  case 1:
202  srcEncoding = kCFStringEncodingUTF8;
203  break;
204  case 2:
205  srcEncoding = kCFStringEncodingUTF16LE;
206  break;
207  case 4:
208  srcEncoding = kCFStringEncodingUTF32LE;
209  break;
210  default:
211  AKASSERT(!"Invalid Char size");
212  }
213 
214  CFStringRef strRef;
215  strRef = CFStringCreateWithBytes( nil,
216  (UInt8 *) in_pSrc,
217  (srcStrLen( in_pSrc ) + 1) * sizeof(srcType),
218  srcEncoding,
219  false );
220  CFRange rangeToProcess = CFRangeMake(0, CFStringGetLength(strRef));
221  CFIndex sizeConverted = CFStringGetBytes(strRef, rangeToProcess, dstEncoding, '?', false, (UInt8 *)in_pdDest , in_MaxSize * sizeof(destType), NULL);
222 
223  //WG-28497 Memory leak when converting strings on Mac & iOS
224  CFRelease(strRef);
225 
226  return sizeConverted;
227  }
228 
229  #define CONVERT_UTF16_TO_WCHAR( _astring_, _wcharstring_ ) \
230  _wcharstring_ = (wchar_t*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(wchar_t) ); \
231  AK_UTF16_TO_WCHAR( _wcharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
232 
233  #define CONVERT_WCHAR_TO_UTF16( _astring_, _utf16string_ ) \
234  _utf16string_ = (AkUtf16*)AkAlloca( (1 + wcslen(_astring_)) * sizeof(AkUtf16) ); \
235  AK_WCHAR_TO_UTF16( _utf16string_, (const wchar_t*)_astring_, wcslen(_astring_)+1 )
236 
237  #define CONVERT_OSCHAR_TO_UTF16( _astring_, _utf16string_ ) \
238  _utf16string_ = (AkUtf16*)AkAlloca( (1 + strlen(_astring_)) * sizeof(AkUtf16) ); \
239  AK_OSCHAR_TO_UTF16( _utf16string_, (const AkOSChar*)_astring_, strlen(_astring_)+1 )
240 
241  #define CONVERT_UTF16_TO_OSCHAR( _astring_, _oscharstring_ ) \
242  _oscharstring_ = (AkOSChar*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(AkOSChar) ); \
243  AK_UTF16_TO_OSCHAR( _oscharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
244 
245  #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<wchar_t, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, &wcslen , &AKPLATFORM::AkUtf16StrLen)
246  #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, wchar_t>( in_pdDest, in_pSrc, in_MaxSize, &AKPLATFORM::AkUtf16StrLen, &wcslen )
247  #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkOSChar, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
248  #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<char, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
249  #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, char>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
250  #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, AkOSChar>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
251 
252  /// Stack allocations.
253  #define AkAlloca( _size_ ) alloca( _size_ )
254 
255 #if __BIGGEST_ALIGNMENT__ < AK_SIMD_ALIGNMENT
256  #define AkAllocaSIMD( _size_ ) __builtin_alloca_with_align( _size_, AK_SIMD_ALIGNMENT*8 )
257 #endif
258 
259  #define AK_LIBRARY_PREFIX ("")
260  #define AK_DYNAMIC_LIBRARY_EXTENSION (".dylib")
261 }
void AkSignalEvent(const AkEvent &in_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:109
semaphore_t AkSemaphore
Definition: AkTypes.h:81
int64_t AkIntPtr
Definition: AkTypes.h:83
The operation was successful.
Definition: AkTypes.h:129
void AkMemoryBarrier()
Definition: AkPlatformFuncs.h:91
AKRESULT
Standard function call result.
Definition: AkTypes.h:126
void AkDestroyEvent(AkEvent &io_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:78
AkInt32 AkInterlockedDecrement(AkAtomic32 *pValue)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:76
wchar_t AkUtf16
Definition: AkTypes.h:107
AkIntPtr AkAtomicPtr
Signed platform sized integer - Atomic Declaration.
Definition: AkTypes.h:60
void AkClearEvent(AkEvent &out_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:49
The operation failed.
Definition: AkTypes.h:130
void AkWaitForEvent(AkEvent &in_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:98
#define AKASSERT(Condition)
Definition: AkAssert.h:69
size_t AkMacConvertString(destType *in_pdDest, const srcType *in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *))
Definition: AkPlatformFuncs.h:180
#define AkForceInline
Force inlining.
Definition: AkTypes.h:63
void AkReleaseSemaphore(const AkSemaphore &in_event)
Definition: AkPlatformFuncs.h:114
int32_t AkInt32
Signed 32-bit integer.
Definition: AkTypes.h:92
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:106
void AkDestroySemaphore(AkSemaphore &io_semaphore)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:88
AKRESULT AkCreateEvent(AkEvent &out_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:55
void AkWaitForSemaphore(AkSemaphore &in_semaphore)
Definition: AkPlatformFuncs.h:103
AkInt32 AkAtomic32
Signed 32-bit integer - Atomic Declaration.
Definition: AkTypes.h:57
#define NULL
Definition: AkTypes.h:49
int64_t AkInt64
Signed 64-bit integer.
Definition: AkTypes.h:93
#define AKVERIFY(x)
Definition: AkAssert.h:71
void PerformanceFrequency(AkInt64 *out_piFreq)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:43
bool AkInterlockedCompareExchange(volatile AkAtomic32 *io_pDest, AkInt32 in_newValue, AkInt32 in_expectedOldVal)
Definition: AkPlatformFuncs.h:81
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:79
semaphore_t AkEvent
Definition: AkTypes.h:80
size_t AkUtf16StrLen(const AkUtf16 *in_pStr)
Definition: AkPlatformFuncs.h:493
AkInt64 AkAtomic64
Signed 64-bit integer - Atomic Declaration.
Definition: AkTypes.h:58
AkInt32 AkInterlockedIncrement(AkAtomic32 *pValue)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:70
AKRESULT AkCreateSemaphore(AkSemaphore &out_semaphore, AkUInt32 in_initialCount)
Definition: AkPlatformFuncs.h:66

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise