// RuntimeForStatic base library | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
namespace RuntimeForStatic {
//==================================================================================================

PVOID HelpersRT::Allocate($AM,SIZE_T size $not_zero) $null_on_failure $needs_free
{
   return Allocate($ci,$ci, size );
}
PVOID HelpersRT::Allocate($AM,SIZE_T size $not_zero,int count $not_zero) $null_on_failure
                                                                         $needs_free
{
   return Allocate($ci,$ci, size, count );
}

PVOID HelpersRT::CleanAlloc($AM,SIZE_T size $not_zero) $null_on_failure $needs_free
{
   return CleanAlloc($ci,$ci, size );
}
PVOID HelpersRT::CleanAlloc($AM,SIZE_T size $not_zero,int count $not_zero) $null_on_failure
                                                                           $needs_free
{
   return CleanAlloc($ci,$ci, size, count );
}

PVOID HelpersRT::ReAllocate($AM, PVOID memoryBlock, SIZE_T newSize ) $null_on_failure $needs_free
{
   return ReAllocate($ci,$ci, memoryBlock, newSize );
}
PVOID HelpersRT::ReAllocate($AM, PVOID memoryBlock, SIZE_T size $not_zero, int newCount $not_zero )
                                 $null_on_failure
{
   return ReAllocate($ci,$ci, memoryBlock, size, newCount );
}

//==================================================================================================

PVOID HelpersRT::Allocate($A,$M,SIZE_T size $not_zero) $null_on_failure $needs_free
{
   $Memory::VerifyAllocationSize($CI, size );
#ifdef _DEBUG
   const RuntimeForStatic::$CallerInfo &m = $$MM;
   return std::_malloc_dbg( size, $ALLOCATION_BLOCK, m.SourceFileA, m.LineNo );
#else
   return std::malloc( size );
#endif // _DEBUG
}
PVOID HelpersRT::Allocate($A,$M, SIZE_T size $not_zero, int count $not_zero )
                                 $null_on_failure $needs_free
{
   $$VERIFY_NOT_ZERO_SIZE_ARG( size );
   $$VERIFY_POSITIVE_VALUE_ARG( count );
   SIZE_T sizeInBytes = size * count;
   if ( sizeInBytes / count != size )
      $Memory::Fail_AllocationSizeExcess($$CI);
   return Allocate($,$m, sizeInBytes );
}

//--------------------------------------------------------------------------------------------------

PVOID HelpersRT::CleanAlloc($A,$M,SIZE_T size $not_zero) $null_on_failure $needs_free
{
   PVOID memoryBlock = Allocate($ci,$m, size );
   if ( memoryBlock )
      ::ZeroMemory( memoryBlock, size );
   return memoryBlock;
}
PVOID HelpersRT::CleanAlloc($A,$M, SIZE_T size $not_zero, int count $not_zero )
                                 $null_on_failure $needs_free
{
   PVOID memoryBlock = Allocate($ci,$m, size, count );
   if ( memoryBlock )
      ::ZeroMemory( memoryBlock, size * count );
   return memoryBlock;
}

//--------------------------------------------------------------------------------------------------

PVOID HelpersRT::ReAllocate($A,$M,PVOID memoryBlock,SIZE_T newSize) $null_on_failure $needs_free
{
   $$VERIFY_POINTER_ARG( memoryBlock );
   $Memory::VerifyAllocationSize($CI, newSize );
#ifdef _DEBUG
   const RuntimeForStatic::$CallerInfo& m = $MM;
   return std::_realloc_dbg( memoryBlock, newSize, $ALLOCATION_BLOCK, m.SourceFileA, m.LineNo );
#else
   return std::realloc( memoryBlock, newSize );
#endif // _DEBUG
}
PVOID HelpersRT::ReAllocate($A,$M, PVOID memoryBlock, SIZE_T size $not_zero, int newCount $not_zero )
                                   $null_on_failure $needs_free
{
   $$VERIFY_POINTER_ARG( memoryBlock );
   $$VERIFY_NOT_ZERO_SIZE_ARG( size );
   $$VERIFY_POSITIVE_VALUE_ARG( newCount );
   SIZE_T newSizeInBytes = size * newCount;
   if ( newSizeInBytes / newCount != size )
      $Memory::Fail_AllocationSizeExcess($$CI);
   return ReAllocate($,$m, memoryBlock, newSizeInBytes );
}

//==================================================================================================
} // RuntimeForStatic
