namespace Brutal.KtxApi;
///
/// Owns a native buffer returned by WriteToMemory/WriteKtx2ToMemory. The buffer is allocated with
/// malloc() inside the native KTX library, so it must be freed with the matching native free() -
/// Dispose() does this for you. Do not free the underlying pointer with any other allocator (e.g.
/// Brutal.Memory.Mem.Free), as that frees memory with a different allocator than the one that
/// allocated it, corrupting the heap.
///
public unsafe struct KtxNativeBuffer : IDisposable
{
private byte* _pointer;
/// The size of the buffer in bytes.
public ulong Length { get; }
/// Raw pointer to the native buffer.
public byte* Pointer => _pointer;
///
/// A read-only view of the buffer. Throws if Length exceeds int.MaxValue, since Span<T>
/// cannot address more than 2GiB - use Pointer and Length directly for buffers that large.
///
public ReadOnlySpan Span => new(_pointer, checked((int)Length));
internal KtxNativeBuffer(byte* pointer, ulong length)
{
_pointer = pointer;
Length = length;
}
public void Dispose()
{
if (_pointer is null)
return;
KtxNativeMemory.Free(_pointer);
_pointer = null;
}
}