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

namespace CommonHelpers {

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

public static int ElementSize()
{
   return HMemory.ElementSize<char>();
}

public static int CountToSize(int count)
{
   return HMemory.CountToSize<char>( count );
}

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

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

public static unsafe char* Copy( [HIn] char* pSourceChars,
                                 [HOut] char* pDestinationChars,
                                 int count )
{
   return (char*)HMemory.Copy<char>( (IntPtr)pSourceChars, (IntPtr)pDestinationChars, count );
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HUnsafeCharBuffer
//**************************************************************************************************

} // CommonHelpers
