Version
menu_open
link
Wwise SDK 2019.2.15
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 #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
34 #include <cpuid.h>
35 #endif
36 #include <mach/task.h>
37 #include <mach/semaphore.h>
38 #include <CoreFoundation/CFString.h>
39 #include <libkern/OSAtomic.h>
40 #include <mach/task.h>
41 #include <mach/mach_init.h>
42 #include <mach/mach_time.h>
43 #include <wchar.h>
44 
45 namespace AKPLATFORM
46 {
47  extern inline size_t AkUtf16StrLen( const AkUtf16* in_pStr );
48  // Simple automatic event API
49  // ------------------------------------------------------------------
50 
51  /// Platform Independent Helper
52  inline void AkClearEvent( AkEvent & out_event )
53  {
54  out_event = 0;
55  }
56 
57  /// Platform Independent Helper
58  inline AKRESULT AkCreateEvent( AkEvent & out_event )
59  {
60  kern_return_t ret = semaphore_create(
61  mach_task_self(),
62  &out_event,
63  SYNC_POLICY_FIFO,
64  0 );
65 
66  return ( ret == noErr ) ? AK_Success : AK_Fail;
67  }
68 
69  inline AKRESULT AkCreateSemaphore( AkSemaphore & out_semaphore, AkUInt32 in_initialCount )
70  {
71  kern_return_t ret = semaphore_create(
72  mach_task_self(),
73  &out_semaphore,
74  SYNC_POLICY_FIFO,
75  in_initialCount );
76 
77  return ( ret == noErr ) ? AK_Success : AK_Fail;
78  }
79 
80  /// Platform Independent Helper
81  inline void AkDestroyEvent( AkEvent & io_event )
82  {
83  if( io_event != 0 )
84  {
85  AKVERIFY( semaphore_destroy( mach_task_self(), io_event ) == noErr);
86  }
87  io_event = 0;
88  }
89 
90  /// Platform Independent Helper
91  inline void AkDestroySemaphore( AkSemaphore & io_semaphore )
92  {
93  if( io_semaphore != 0 )
94  {
95  AKVERIFY( semaphore_destroy( mach_task_self(), io_semaphore ) == noErr);
96  }
97  io_semaphore = 0;
98  }
99 
100  /// Platform Independent Helper
101  inline void AkWaitForEvent( AkEvent & in_event )
102  {
103  AKVERIFY( semaphore_wait( in_event ) == noErr );
104  }
105 
106  inline void AkWaitForSemaphore( AkSemaphore & in_semaphore )
107  {
108  AKVERIFY( semaphore_wait( in_semaphore ) == noErr );
109  }
110 
111  /// Platform Independent Helper
112  inline void AkSignalEvent( const AkEvent & in_event )
113  {
114  AKVERIFY( semaphore_signal( in_event ) == noErr );
115  }
116 
117  inline void AkReleaseSemaphore( const AkSemaphore & in_event )
118  {
119  AKVERIFY( semaphore_signal( in_event ) == noErr );
120  }
121 
122  // Time functions
123  // ------------------------------------------------------------------
124 
125  /// Platform Independent Helper
126  inline void PerformanceCounter( AkInt64 * out_piLastTime )
127  {
128  *out_piLastTime = mach_absolute_time();
129  }
130 
131  /// Platform Independent Helper
132  inline void PerformanceFrequency( AkInt64 * out_piFreq )
133  {
134  static mach_timebase_info_data_t sTimebaseInfo;
135  mach_timebase_info(&sTimebaseInfo);
136  if ( sTimebaseInfo.numer !=0 )
137  {
138  *out_piFreq = AkInt64((1E9 * sTimebaseInfo.denom) / sTimebaseInfo.numer );
139  }
140  else
141  {
142  *out_piFreq = 0;
143  }
144  }
145 
146 
147  template<class destType, class srcType>
148  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 *) )
149  {
150  CFStringBuiltInEncodings dstEncoding;
151  CFStringBuiltInEncodings srcEncoding;
152  switch(sizeof(destType))
153  {
154  case 1:
155  dstEncoding = kCFStringEncodingUTF8;
156  break;
157  case 2:
158  dstEncoding = kCFStringEncodingUTF16LE;
159  break;
160  case 4:
161  dstEncoding = kCFStringEncodingUTF32LE;
162  break;
163  default:
164  AKASSERT(!"Invalid Char size");
165  }
166 
167  switch(sizeof(srcType))
168  {
169  case 1:
170  srcEncoding = kCFStringEncodingUTF8;
171  break;
172  case 2:
173  srcEncoding = kCFStringEncodingUTF16LE;
174  break;
175  case 4:
176  srcEncoding = kCFStringEncodingUTF32LE;
177  break;
178  default:
179  AKASSERT(!"Invalid Char size");
180  }
181 
182  CFStringRef strRef;
183  strRef = CFStringCreateWithBytes( nil,
184  (UInt8 *) in_pSrc,
185  (srcStrLen( in_pSrc ) + 1) * sizeof(srcType),
186  srcEncoding,
187  false );
188  CFRange rangeToProcess = CFRangeMake(0, CFStringGetLength(strRef));
189  CFIndex sizeConverted = CFStringGetBytes(strRef, rangeToProcess, dstEncoding, '?', false, (UInt8 *)in_pdDest , in_MaxSize * sizeof(destType), NULL);
190 
191  //WG-28497 Memory leak when converting strings on Mac & iOS
192  CFRelease(strRef);
193 
194  return sizeConverted;
195  }
196 
197  #define CONVERT_UTF16_TO_WCHAR( _astring_, _wcharstring_ ) \
198  _wcharstring_ = (wchar_t*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(wchar_t) ); \
199  AK_UTF16_TO_WCHAR( _wcharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
200 
201  #define CONVERT_WCHAR_TO_UTF16( _astring_, _utf16string_ ) \
202  _utf16string_ = (AkUtf16*)AkAlloca( (1 + wcslen(_astring_)) * sizeof(AkUtf16) ); \
203  AK_WCHAR_TO_UTF16( _utf16string_, (const wchar_t*)_astring_, wcslen(_astring_)+1 )
204 
205  #define CONVERT_OSCHAR_TO_UTF16( _astring_, _utf16string_ ) \
206  _utf16string_ = (AkUtf16*)AkAlloca( (1 + strlen(_astring_)) * sizeof(AkUtf16) ); \
207  AK_OSCHAR_TO_UTF16( _utf16string_, (const AkOSChar*)_astring_, strlen(_astring_)+1 )
208 
209  #define CONVERT_UTF16_TO_OSCHAR( _astring_, _oscharstring_ ) \
210  _oscharstring_ = (AkOSChar*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(AkOSChar) ); \
211  AK_UTF16_TO_OSCHAR( _oscharstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
212 
213  #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)
214  #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 )
215  #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkOSChar, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
216  #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<char, AkUtf16>( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
217  #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, char>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
218  #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkMacConvertString<AkUtf16, AkOSChar>( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
219 
220  /// Stack allocations.
221  #define AkAlloca( _size_ ) alloca( _size_ )
222 
223 #if __BIGGEST_ALIGNMENT__ < AK_SIMD_ALIGNMENT
224  #define AkAllocaSIMD( _size_ ) __builtin_alloca_with_align( _size_, AK_SIMD_ALIGNMENT*8 )
225 #endif
226 
227  #define AK_LIBRARY_PREFIX ("")
228  #define AK_DYNAMIC_LIBRARY_EXTENSION (".dylib")
229 
230 #if (defined(AK_CPU_X86_64) || defined(AK_CPU_X86))
231  /// Support to fetch the CPUID for the platform. Only valid for X86 targets
232  /// \remark Note that IAkProcessorFeatures should be preferred to fetch this data
233  /// as it will have already translated the feature bits into AK-relevant enums
234  inline void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
235  {
236  __get_cpuid_count( in_uLeafOpcode, in_uSubLeafOpcode,
237  &out_uCPUFeatures[0],
238  &out_uCPUFeatures[1],
239  &out_uCPUFeatures[2],
240  &out_uCPUFeatures[3]);
241  }
242 #endif
243 }
AKRESULT AkCreateSemaphore(AkSemaphore &out_semaphore, AkUInt32 in_initialCount)
Definition: AkPlatformFuncs.h:69
size_t AkUtf16StrLen(const AkUtf16 *in_pStr)
Definition: AkPlatformFuncs.h:496
@ AK_Fail
The operation failed.
Definition: AkTypes.h:125
semaphore_t AkEvent
Definition: AkTypes.h:77
void AkClearEvent(AkEvent &out_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:52
void AkWaitForEvent(AkEvent &in_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:101
wchar_t AkUtf16
Definition: AkTypes.h:113
AKRESULT
Standard function call result.
Definition: AkTypes.h:122
void CPUID(AkUInt32 in_uLeafOpcode, AkUInt32 in_uSubLeafOpcode, unsigned int out_uCPUFeatures[4])
Definition: AkPlatformFuncs.h:234
#define NULL
Definition: AkTypes.h:49
@ AK_Success
The operation was successful.
Definition: AkTypes.h:124
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:82
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:148
void AkDestroySemaphore(AkSemaphore &io_semaphore)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:91
void AkDestroyEvent(AkEvent &io_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:81
int64_t AkInt64
Signed 64-bit integer.
Definition: AkTypes.h:99
#define AKASSERT(Condition)
Definition: AkAssert.h:76
#define AKVERIFY(x)
Definition: AkAssert.h:78
void AkReleaseSemaphore(const AkSemaphore &in_event)
Definition: AkPlatformFuncs.h:117
AKRESULT AkCreateEvent(AkEvent &out_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:58
void AkWaitForSemaphore(AkSemaphore &in_semaphore)
Definition: AkPlatformFuncs.h:106
void PerformanceFrequency(AkInt64 *out_piFreq)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:46
void AkSignalEvent(const AkEvent &in_event)
Platform Independent Helper.
Definition: AkPlatformFuncs.h:112
uint32_t AkUInt32
Unsigned 32-bit integer.
Definition: AkTypes.h:85
semaphore_t AkSemaphore
Definition: AkTypes.h:78

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