// A method to copy the contents of a buffer to the our native buffer. // This isn't optimised, so it's simple and readable. private void Write(Span <byte> data, bool resized) { while (true) { var dataLen = data.Length; if (!_allocated) { // If the buffer isn't allocated then go and do that _value = Native.Alloc(new UIntPtr((uint)dataLen)); _allocated = true; resized = true; continue; } // If the buffer is allocated, make sure it has enough space var available = (int)_value.cap - (int)_value.len; if (dataLen > available) { // If there isn't enough space, resize and try write again if (resized) { throw new Exception("Already tried to resize ResizeBuf"); } var needed = dataLen - available; _value = Native.Reserve(_value, new UIntPtr((uint)needed)); resized = true; continue; } // If the buffer is allocated and there's space, then copy the bits Span <byte> slice; slice = ValueSpan().Slice((int)_value.len); data.CopyTo(slice); _value.len += dataLen; break; } }
internal static extern ResizeBuf Reserve(ResizeBuf buf, UIntPtr reserve);
internal static extern void Drop(ResizeBuf buf);