public static Scope <UnmanagedBuffer> Alloc <TStruct>(EHeap mem, TStruct init) where TStruct : struct { var buf = Alloc(mem, typeof(TStruct), 1); buf.Value.Init(init); return(buf); }
/// <summary>Copy a managed string into unmanaged memory, converting to UTF8 format if required</summary> public static Scope <IntPtr> AllocUTF8String(EHeap mem, string str) { return(Scope.Create( () => mem switch { EHeap.HGlobal => Marshal.StringToHGlobalAnsi(str), EHeap.CoTaskMem => Marshal.StringToCoTaskMemAnsi(str), _ => throw new Exception($"Unknown global memory type: {mem}"), },
public static Scope <UnmanagedBuffer> Alloc(EHeap mem, Type type, int count = 1) { return(Scope.Create( () => new UnmanagedBuffer(mem, type, count), buf => buf.Dispose())); }
public static Scope <UnmanagedBuffer> Alloc <TStruct>(EHeap mem, int count = 1) where TStruct : struct { return(Alloc(mem, typeof(TStruct), count)); }
/// <summary>RAII scope for allocated global memory</summary> public static Scope <UnmanagedBuffer> Alloc(EHeap mem, int size_in_bytes) { return(Alloc(mem, typeof(byte), size_in_bytes)); }