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

namespace CommonHelpers {

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

public static unsafe byte* Clear([HOut] byte* pDestinationBytes,int? count)
{
   return (byte*)HMemory.Clear<byte>( (IntPtr)pDestinationBytes, count );
}

public static unsafe byte* Copy([HIn] byte* pSourceBytes,[HOut] byte* pDestinationBytes,int count)
{
   return (byte*)HMemory.Copy<byte>( (IntPtr)pSourceBytes, (IntPtr)pDestinationBytes, count );
}

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

public static unsafe byte[] ToArray([HIn] byte* pSourceBytes,int count)
{
   return HUnsafeMemory.ToArray<byte>( pSourceBytes, count );
}

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

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

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

/// <exception cref="HOperationFailedException" />
public static unsafe byte* ReAllocate([HIn] ref byte* pAllocatedBuffer,int newCapacity)
{
   return pAllocatedBuffer = ReAllocate( pAllocatedBuffer, newCapacity );
}

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

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

/// <exception cref="HOperationFailedException" />
public static unsafe int GetAllocatedCapacity(byte* pAllocatedBuffer)
{
   return HMemory.GetAllocatedCapacity<byte>( (IntPtr)pAllocatedBuffer );
}

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

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

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HUnsafeBytesBuffer
//**************************************************************************************************

} // CommonHelpers
