//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.ComponentModel;

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HUnsafeMemory {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static unsafe void* Clear([HOut] void* pDestination,int? lengthInBytes)
{
   return (void*)HMemory.Clear( (IntPtr)pDestination, lengthInBytes );
}

public static unsafe void* Copy( [HIn] void* pSourceBlock,
                                 [HOut] void* pDestination,
                                 int lengthInBytes )
{
   return (void*)HMemory.Copy( (IntPtr)pSourceBlock, (IntPtr)pDestination, lengthInBytes );
}

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

/// <exception cref="HOperationFailedException" />
public static unsafe void* Allocate(int sizeInBytes)
{
   return (void*)HMemory.Allocate( sizeInBytes );
}

/// <exception cref="HOperationFailedException" />
public static unsafe void* ReAllocate([HIn] void* pAllocatedBlock,int newSizeInBytes)
{
   return (void*)HMemory.ReAllocate( (IntPtr)pAllocatedBlock, newSizeInBytes );
}

/// <exception cref="HOperationFailedException" />
public static unsafe void* ReAllocate([HIn] ref void* pAllocatedBlock,int newSizeInBytes)
{
   return (void*)HMemory.ReAllocate( (IntPtr)pAllocatedBlock, newSizeInBytes );
}

/// <exception cref="HOperationFailedException" />
public static unsafe void* Duplicate([HIn] void* pAllocatedBlock)
{
   return (void*)HMemory.Duplicate( (IntPtr)pAllocatedBlock );
}

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

/// <exception cref="HOperationFailedException" />
public static unsafe int GetAllocatedSize(void* pAllocatedBlock)
{
   return HMemory.GetAllocatedSize( (IntPtr)pAllocatedBlock );
}

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

/// <exception cref="Win32Exception" />
public static unsafe bool Free([HOptional] void* pAllocatedBlock)
{
   return HMemory.Free( (IntPtr)pAllocatedBlock );
}

/// <exception cref="Win32Exception" />
public static unsafe bool Free([HOptional][HIn][HOut][HInterlockedAccess] void** ppAllocatedBlock)
{
   return Free( HUnsafePtr.InterlockedNullValueExchange( ppAllocatedBlock ) );
}

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

static HUnsafeMemory()
{
   CommonHelpers_PAccess.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HUnsafeMemory
//**************************************************************************************************

} // CommonHelpers
