Home

Resume

Blog

Teikitu


/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/*  »Project«   Teikitu Gaming System (TgS) (∂)
    »File«      TgS Common - Base - Define - ANSI API.h
    »Author«    Andrew Aye (EMail: mailto:andrew.aye@gmail.com, Web: http://www.andrewaye.com)
    »Version«   4.51 / »GUID« A9981407-3EC9-42AF-8B6F-8BE6DD919615                                                                                                        */
/*   -------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/*  Copyright: © 2002-2017, Andrew Aye.  All Rights Reserved.
    This software is free for non-commercial use.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
      following conditions are met:
        Redistribution of source code must retain this copyright notice, this list of conditions and the following disclaimers.
        Redistribution in binary form must reproduce this copyright notice, this list of conditions and the following disclaimers in the documentation and other materials
          provided with the distribution.
    The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
    The intellectual property rights of the algorithms used reside with Andrew Aye.
    You may not use this software, in whole or in part, in support of any commercial product without the express written consent of the author.
    There is no warranty or other guarantee of fitness of this software for any purpose. It is provided solely "as is".                                                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#if !defined(TGS_COMMON_BASE_DEFINE_ANSI_API_H)
#define TGS_COMMON_BASE_DEFINE_ANSI_API_H
#pragma once


/* == Common ============================================================================================================================================================ */

/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */
/*  Function Replacements                                                                                                                                                 */
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */

#if defined(TgCOMPILE_SUPPORT_MSFT_SYSTEM_INCLUDE)

    #define TgGETTIME(...)                      timespec_get( __VA_ARGS__, TIME_UTC )

    #define TgFOPEN(A,...)                      fopen_s( &(A), __VA_ARGS__ )

    #define TgMEMCPY(A,B,...)                   memcpy_s( (A), (B), __VA_ARGS__ )
    #define TgMEMCPY_NOCACHE(A,B,...)           memcpy_s( (A), (B), __VA_ARGS__)
    #define TgMEMMOVE(A,B,...)                  memmove_s( (A), (B), __VA_ARGS__ )

    #define TgSTRUPR(A,B)                       _strupr_s( (A), (B) )
    #define TgSTRLWR(A,B)                       _strlwr_s( (A), (B) )
    #define TgSTRNCPY(A,B,...)                  strncpy_s( (A), (B), __VA_ARGS__ )
    #define TgSTRNCAT(A,B,...)                  strncat_s( (A), (B), __VA_ARGS__ )
    #define TgSNPRINTF(A,B,...)                 _snprintf_s( (A), (B), _TRUNCATE, __VA_ARGS__ )
    #define TgVSNPRINTF(A,B,...)                _vsnprintf_s( (A), (B), _TRUNCATE, __VA_ARGS__ )

    #define TgWCSUPR(A,B)                       _wcsupr_s( (A), (B) )
    #define TgWCSLWR(A,B)                       _wcslwr_s( (A), (B) )
    #define TgWCSNCPY(A,B,...)                  wcsncpy_s( (A), (B), __VA_ARGS__ )
    #define TgWCSNCAT(A,B,...)                  wcsncat_s( (A), (B), __VA_ARGS__ )
    #define TgSNWPRINTF(A,B,...)                _snwprintf_s( (A), (B), _TRUNCATE, __VA_ARGS__ )
    #define TgVSNWPRINTF(A,B,...)               _vsnwprintf_s( (A), (B), _TRUNCATE, __VA_ARGS__ )

    #define TgFREEA( VAR )                      _freea( VAR );
    #define TgALLOCA( TYPE, COUNT, VAR )                                                                                                                                   \
    {                                                                                                                                                                      \
        union                                                                                                                                                              \
        {                                                                                                                                                                  \
            P_TgUINT08 pui08;                                                                                                                                              \
            P_TgUINT32 pui32;                                                                                                                                              \
            P_TgVOID p;                                                                                                                                                    \
        } sMem;                                                                                                                                                            \
        sMem.p = _malloca( (COUNT) * sizeof(TYPE) );                                                                                                                       \
        VAR = (TYPE *)sMem.p;                                                                                                                                              \
        if ( VAR )                                                                                                                                                         \
        {                                                                                                                                                                  \
            sMem.pui08 -= _ALLOCA_S_MARKER_SIZE;                                                                                                                           \
            if (*sMem.pui32 == _ALLOCA_S_STACK_MARKER)                                                                                                                     \
            {                                                                                                                                                              \
                tgCN_PrintF( KTgCN_CHANEL_LOG_MEM | KTgCN_SEVERITY_6, TgT("Allocated %d  bytes of stack\n"),(COUNT) * sizeof(TYPE) );                                      \
            }                                                                                                                                                              \
            else                                                                                                                                                           \
            {                                                                                                                                                              \
                tgCN_PrintF( KTgCN_CHANEL_LOG_MEM | KTgCN_SEVERITY_6, TgT("Allocated %d  bytes of heap\n"), (COUNT) * sizeof(TYPE) );                                      \
            }                                                                                                                                                              \
        }                                                                                                                                                                  \
    }

    #define tgInterlockedAnd _InterlockedAnd
    #define tgInterlockedOr _InterlockedOr
    #define tgInterlockedXor _InterlockedXor
    #define tgInterlockedIncrement _InterlockedIncrement
    #define tgInterlockedDecrement _InterlockedDecrement
    #define tgInterlockedExchange _InterlockedExchange
    #define tgInterlockedExchangeAdd _InterlockedExchangeAdd
    #define tgInterlockedCompareExchange _InterlockedCompareExchange
    #define tgInterlockedCompareExchange64 _InterlockedCompareExchange64

    #if TgCOMPILE_16BIT_ATOMIC
    #define tgInterlockedAnd16 _InterlockedAnd16
    #define tgInterlockedOr16 _InterlockedOr16
    #define tgInterlockedXor16 _InterlockedXor16
    #define tgInterlockedIncrement16 _InterlockedIncrement16
    #define tgInterlockedDecrement16 _InterlockedDecrement16
    #endif

    #if TgCOMPILE_64BIT_ATOMIC
    #define tgInterlockedAnd64 _InterlockedAnd64
    #define tgInterlockedOr64 _InterlockedOr64
    #define tgInterlockedXor64 _InterlockedXor64
    #define tgInterlockedIncrement64 _InterlockedIncrement64
    #define tgInterlockedDecrement64 _InterlockedDecrement64
    #define tgInterlockedExchange64 _InterlockedExchange64
    #define tgInterlockedExchangeAdd64 _InterlockedExchangeAdd64
    #define tgInterlockedCompareExchange128 _InterlockedCompareExchange128
    #endif

    #define tgInterlockedExchangePointer(_Target, _Value) (void*)_InterlockedExchange64( (TgATOMIC_SINT64 volatile*)(_Target), (__int64)(_Value) )
    #define tgInterlockedCompareExchangePointer(_Target, _Exchange, _Comparand) \
        (void*)tgInterlockedCompareExchange64( (TgATOMIC_SINT64 volatile*)(_Target), (__int64)(_Exchange), (__int64)(_Comparand) )

    #define tgInterlockedIncrementSizeT(_Target) _InterlockedIncrement64((TgATOMIC_SINT64 volatile*)(_Target))
    #define tgInterlockedDecrementSizeT(_Target) _InterlockedDecrement64((TgATOMIC_SINT64 volatile*)(_Target))
    #define tgInterlockedCompareExchangeSizeT(_Target, _Exchange, _Comparand) \
        tgInterlockedCompareExchange64( (TgATOMIC_SINT64 volatile*)(_Target), (__int64)(_Exchange), (__int64)(_Comparand) )

#else

    #define TgGETTIME(...)                      clock_gettime(CLOCK_REALTIME, __VA_ARGS__)

    #define TgFOPEN(A,...)                      A = fopen( __VA_ARGS__ )

    #define TgMEMCPY(A,B,...)                   memcpy( (A), __VA_ARGS__ )
    #define TgMEMCPY_NOCACHE(A,B,...)           memcpy( (A), __VA_ARGS__)
    #define TgMEMMOVE(A,B,...)                  memmove( (A), __VA_ARGS__ )

    #define TgSTRUPR(A,B)                       strupr( (A) )
    #define TgSTRLWR(A,B)                       strlwr( (A) )
    #define TgSTRNCPY(A,B,...)                  strncpy( (A), __VA_ARGS__ )
    #define TgSTRNCAT(A,B,...)                  strncat( (A), __VA_ARGS__ )
    #define TgSNPRINTF(A,B,...)                 snprintf( (A), (B), __VA_ARGS__ )
    #define TgVSNPRINTF(A,B,...)                vsnprintf( (A), (B), __VA_ARGS__ )

    #define TgWCSUPR(A,B)                       wcsupr( (A)) )
    #define TgWCSLWR(A,B)                       wcslwr( (A) )
    #define TgWCSNCPY(A,B,...)                  wcsncpy( (A), __VA_ARGS__ )
    #define TgWCSNCAT(A,B,...)                  wcsncat( (A), __VA_ARGS__ )
    #define TgSNWPRINTF(A,B,...)                snwprintf( (A), (B), __VA_ARGS__ )
    #define TgVSNWPRINTF(A,B,...)               vsnwprintf( (A), (B), __VA_ARGS__ )

    #define TgFREEA( VAR )
    #define TgALLOCA( TYPE, COUNT, VAR )                                                                                                                                   \
    {                                                                                                                                                                      \
        VAR =  (TYPE *)__builtin_alloca( (COUNT) * sizeof(TYPE) );                                                                                                         \
    }

#endif



/* ====================================================================================================================================================================== */
#endif