ByteString.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. namespace GpuInterop.VulkanDemo;
  6. unsafe class ByteString : IDisposable
  7. {
  8. public IntPtr Pointer { get; }
  9. public ByteString(string s)
  10. {
  11. Pointer = Marshal.StringToHGlobalAnsi(s);
  12. }
  13. public void Dispose()
  14. {
  15. Marshal.FreeHGlobal(Pointer);
  16. }
  17. public static implicit operator byte*(ByteString h) => (byte*)h.Pointer;
  18. }
  19. unsafe class ByteStringList : IDisposable
  20. {
  21. private List<ByteString> _inner;
  22. private byte** _ptr;
  23. public ByteStringList(IEnumerable<string> items)
  24. {
  25. _inner = items.Select(x => new ByteString(x)).ToList();
  26. _ptr = (byte**)Marshal.AllocHGlobal(IntPtr.Size * _inner.Count + 1);
  27. for (var c = 0; c < _inner.Count; c++)
  28. _ptr[c] = (byte*)_inner[c].Pointer;
  29. }
  30. public int Count => _inner.Count;
  31. public uint UCount => (uint)_inner.Count;
  32. public void Dispose()
  33. {
  34. Marshal.FreeHGlobal(new IntPtr(_ptr));
  35. }
  36. public static implicit operator byte**(ByteStringList h) => h._ptr;
  37. }