Table of Contents

Target Platform(s):

include/AK/Tools/Linux/AkPlatformFuncs.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
00003 released in source code form as part of the SDK installer package.
00004 
00005 Commercial License Usage
00006 
00007 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
00008 may use this file in accordance with the end user license agreement provided 
00009 with the software or, alternatively, in accordance with the terms contained in a
00010 written agreement between you and Audiokinetic Inc.
00011 
00012 Apache License Usage
00013 
00014 Alternatively, this file may be used under the Apache License, Version 2.0 (the 
00015 "Apache License"); you may not use this file except in compliance with the 
00016 Apache License. You may obtain a copy of the Apache License at 
00017 http://www.apache.org/licenses/LICENSE-2.0.
00018 
00019 Unless required by applicable law or agreed to in writing, software distributed
00020 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
00021 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
00022 the specific language governing permissions and limitations under the License.
00023 
00024   Version: <VERSION>  Build: <BUILDNUMBER>
00025   Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
00026 *******************************************************************************/
00027 
00028 #pragma once
00029 
00030 #include <AK/Tools/Common/AkAssert.h>
00031 #include <AK/SoundEngine/Common/AkTypes.h>
00032 
00033 //#include <sys/atomics.h>
00034 #include <time.h>
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 
00038 namespace AKPLATFORM
00039 {
00040     // Atomic Operations
00041     // ------------------------------------------------------------------
00042 
00043     /// Platform Independent Helper
00044     inline AkInt32 AkInterlockedIncrement(AkAtomic32 * pValue)
00045     {
00046         return __sync_add_and_fetch(pValue,1);
00047     }
00048 
00049     /// Platform Independent Helper
00050     inline AkInt32 AkInterlockedDecrement(AkAtomic32 * pValue)
00051     {
00052         return __sync_sub_and_fetch(pValue,1);
00053     }
00054 
00055     AkForceInline bool AkInterlockedCompareExchange(volatile AkAtomic32* io_pDest, AkInt32 in_newValue, AkInt32 in_expectedOldVal)
00056     {
00057         return __sync_bool_compare_and_swap(io_pDest, in_expectedOldVal, in_newValue);
00058     }
00059 
00060     AkForceInline bool AkInterlockedCompareExchange(volatile AkAtomic64* io_pDest, AkInt64 in_newValue, AkInt64 in_expectedOldVal)
00061     {
00062         return __sync_bool_compare_and_swap(io_pDest, in_expectedOldVal, in_newValue);
00063     }
00064 
00065     inline void AkMemoryBarrier()
00066     {
00067         __sync_synchronize();
00068     }
00069 
00070     // Time functions
00071     // ------------------------------------------------------------------
00072 
00073     /// Platform Independent Helper
00074     inline void PerformanceCounter( AkInt64 * out_piLastTime )
00075     {
00076         struct timespec clockNow;
00077         clock_gettime(CLOCK_MONOTONIC, &clockNow);
00078         *out_piLastTime = ((clockNow.tv_sec + clockNow.tv_nsec/ 1000000000.0) * CLOCKS_PER_SEC);
00079     }
00080 
00081     /// Platform Independent Helper
00082     inline void PerformanceFrequency( AkInt64 * out_piFreq )
00083     {
00084         // TO DO ANDROID ... is there something better
00085         *out_piFreq = CLOCKS_PER_SEC;
00086     }
00087 
00088     template<class destType, class srcType>
00089     inline size_t AkSimpleConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *),  size_t srcStrLen(const srcType *) )
00090     { 
00091         size_t i;
00092         size_t lenToCopy = srcStrLen(in_pSrc);
00093         
00094         lenToCopy = (lenToCopy > in_MaxSize-1) ? in_MaxSize-1 : lenToCopy;
00095         for(i = 0; i < lenToCopy; i++)
00096         {
00097             in_pdDest[i] = (destType) in_pSrc[i];
00098         }
00099         in_pdDest[lenToCopy] = (destType)0;
00100         
00101         return lenToCopy;
00102     }
00103 
00104     #define CONVERT_UTF16_TO_CHAR( _astring_, _charstring_ ) \
00105         _charstring_ = (char*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(char) ); \
00106         AK_UTF16_TO_CHAR( _charstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 ) 
00107 
00108     #define AK_UTF16_TO_CHAR(   in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
00109     #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
00110     #define AK_UTF16_TO_WCHAR(  in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, wcslen, AKPLATFORM::AkUtf16StrLen )
00111     #define AK_CHAR_TO_UTF16(   in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
00112     #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen )
00113     #define AK_WCHAR_TO_UTF16(  in_pdDest, in_pSrc, in_MaxSize )    AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, wcslen )
00114 
00115     /// Stack allocations.
00116     #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
00117 }