public MemoryOwner <byte> GetDtoMemory() { var memory = MemoryOwner <byte> .Allocate(dtoBuffer.Length); dtoBuffer.Span.CopyTo(memory.Span); return(memory); }
public static MemoryOwner <LineModificationType> ComputeDiff(ReadOnlySpan <char> oldText, ReadOnlySpan <char> newText, char separator) { // If the new text is empty, no modifications are returned if (newText.IsEmpty) { return(MemoryOwner <LineModificationType> .Empty); } int oldNumberOfLines = oldText.Count(separator) + 1; int newNumberOfLines = newText.Count(separator) + 1; // Fast path if the input text segments have the same length and are short enough if (oldText.Length == newText.Length && oldNumberOfLines == newNumberOfLines && oldNumberOfLines <= ShortPathNumberOfLinesThreshold && oldText.SequenceEqual(newText)) { return(MemoryOwner <LineModificationType> .Allocate(newNumberOfLines, AllocationMode.Clear)); } using SpanOwner <object?> oldTemporaryValues = SpanOwner <object?> .Allocate(oldNumberOfLines); using SpanOwner <object?> newTemporaryValues = SpanOwner <object?> .Allocate(newNumberOfLines); ref object?oldTemporaryValuesRef = ref oldTemporaryValues.DangerousGetReference();
public MemoryOwner <byte> ReadBitsPooled(int numberOfBits) { var bytes = MemoryOwner <byte> .Allocate(GetByteArrayLengthFromBitLength(numberOfBits)); ReadBits(numberOfBits, bytes.Span); return(bytes); }
public static MemoryOwner <TType> CopyToMemoryOwner <TType>(this ReadOnlySequence <TType> sequence) { MemoryOwner <TType> owner = MemoryOwner <TType> .Allocate((int)sequence.Length); sequence.CopyTo(owner.Span); return(owner); }
public async Task RenderAsync(Image <Rgba32> image, CancellationToken token) { Debug.WriteLine("Begin render"); if (bitmap == null || bitmap.PixelHeight != image.Height || bitmap.PixelWidth != image.Width) { //Image = new SoftwareBitmapSource(); bitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, image.Width, image.Height, BitmapAlphaMode.Premultiplied); } using var buffer = MemoryOwner <Bgra32> .Allocate(bitmap.PixelWidth *bitmap.PixelHeight); Configuration configuration = new Configuration() { PreferContiguousImageBuffers = true }; image.CloneAs <Bgra32>(configuration).DangerousTryGetSinglePixelMemory(out var bgraBuffer); var tmpBuffer = MemoryMarshal.AsBytes(bgraBuffer.Span).ToArray(); bitmap.CopyFromBuffer(tmpBuffer.AsBuffer()); await Task.Factory.StartNew(() => { Image.SetBitmapAsync(bitmap); Debug.WriteLine("End render"); }, Task.Factory.CancellationToken, TaskCreationOptions.None, App.Current.UIScheduler); }
public void StartUp() { MemoryOwner <byte> owner = MemoryOwner <byte> .Allocate(ContentBytes.Length); ((Span <byte>)ContentBytes).CopyTo(owner.Span); _memory = owner; }
public void Test_MemoryOwnerOfT_DisposedSpan() { var buffer = MemoryOwner <int> .Allocate(127); buffer.Dispose(); _ = buffer.Span; }
public void Test_MemoryOwnerOfT_MultipleDispose() { var buffer = MemoryOwner <int> .Allocate(127); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); }
static MemoryOwner <T> CreateMemoryOwner <T>(T a1, T a2, T a3) { var leftOwner = MemoryOwner <T> .Allocate(3); leftOwner.Span[0] = a1; leftOwner.Span[1] = a2; leftOwner.Span[2] = a3; return(leftOwner); }
private static MemoryOwner <ushort> LoadDefinitionsTable(int functionsCount) { Assert(functionsCount >= 0); return(functionsCount switch { 0 => MemoryOwner <ushort> .Empty, _ => MemoryOwner <ushort> .Allocate(functionsCount) });
public void Test_MemoryExtensions_IMemoryOwnerStream() { MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(1024); Stream stream = buffer.AsStream(); Assert.IsNotNull(stream); Assert.AreEqual(stream.Length, buffer.Length); Assert.IsTrue(stream.CanWrite); }
public void Setup() { const int ItemCount = 16; using var message = new SecsMessage(s: 1, f: 2, replyExpected: false) { SecsItem = L( L(), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), F8(MemoryOwner <double> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount))), }; using var buffer = new ArrayPoolBufferWriter <byte>(); for (var i = 0; i < MessageCount; i++) { SecsGem.EncodeMessage(message, 1000 + i, deviceId: 0, buffer); } _encodedBytes = buffer.WrittenSpan.ToArray().Chunk(InputChunkSize).ToArray();
private static MemoryOwner <int> LoadJumpTable <TOpcode>( Span <TOpcode> opcodes, out int functionsCount) where TOpcode : unmanaged, IOpcode { MemoryOwner <int> jumpTable = MemoryOwner <int> .Allocate(opcodes.Length); Brainf_ckInterpreter.LoadJumpTable(opcodes, jumpTable.Span, out functionsCount); return(jumpTable); }
public void Test_MemoryOwnerOfT_AllocateAndGetMemoryAndSpan() { using var buffer = MemoryOwner <int> .Allocate(127); Assert.IsTrue(buffer.Length == 127); Assert.IsTrue(buffer.Memory.Length == 127); Assert.IsTrue(buffer.Span.Length == 127); buffer.Span.Fill(42); Assert.IsTrue(buffer.Memory.Span.ToArray().All(i => i == 42)); Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); }
public IMemoryOwner <int> GetBreakpoints() { int count = BreakpointIndicators.Count; if (count == 0) { return(MemoryOwner <int> .Empty); } // Rent a buffer to copy the line numbers with a breakpoint MemoryOwner <int> buffer = MemoryOwner <int> .Allocate(count); ref int bufferRef = ref buffer.DangerousGetReference();
public void Test_MemoryOwnerOfT_MultipleDispose() { var buffer = MemoryOwner <int> .Allocate(127); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); // This test consists in just getting here without crashes. // We're validating that calling Dispose multiple times // by accident doesn't cause issues, and just does nothing. }
public void EmptyStackTrace() { using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("++[>++>-]>+", out _); Assert.IsNotNull(operators); using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512); stackFrames.DangerousGetReference() = new StackFrame(new Range(0, operators !.Length), 10); HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, -1); Assert.IsNull(exceptionInfo); }
//[Benchmark] public int MemoryOwner() { var sum = 0; var repetitions = Repetitions; for (var i = 0; i < repetitions; i++) { using var buffer = MemoryOwner <byte> .Allocate(SizeInBytes, AllocationMode.Default); sum += buffer.Length; } return(sum); }
public static MemoryOwner <Brainf_ckOperator>?TryParse(ReadOnlySpan <char> source, out SyntaxValidationResult validationResult) { // Check the syntax of the input source code validationResult = ValidateSyntax(source); if (!validationResult.IsSuccess) { return(null); } // Allocate the buffer of binary items with the input operators MemoryOwner <Brainf_ckOperator> operators = MemoryOwner <Brainf_ckOperator> .Allocate(validationResult.OperatorsCount); ref char sourceRef = ref source.DangerousGetReference();
public ReadOnlySpan <byte> ApplyXmlFix() { ReadOnlySpan <byte> content = _processItem.Span; int index = content.IndexOf(Constants.StartTagBytes); content = content.Slice(index); int finalLength = content.Length + Constants.EndingTagBytes.Length; MemoryOwner <byte> finalXmlContent = MemoryOwner <byte> .Allocate(finalLength); content.CopyTo(finalXmlContent.Span); Constants.EndingTagBytes.CopyTo(finalXmlContent.Span.Slice(content.Length)); return(finalXmlContent.Span); }
static void Main(string[] args) { var dis = Levenshtein.Distance("aaaaa", "aaaab"); Console.WriteLine("Hello World!"); using MemoryOwner <int> own = MemoryOwner <int> .Allocate(1); _ = own.Span; _ = own.Memory; using SpanOwner <int> ownSpan = SpanOwner <int> .Allocate(1); _ = ownSpan.Span; // Create an array and run the callback float[] array = new float[10_000];
public void Test_IMemoryOwnerStream_Lifecycle() { MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(100); Stream stream = buffer.AsStream(); Assert.IsTrue(stream.CanRead); Assert.IsTrue(stream.CanSeek); Assert.IsTrue(stream.CanWrite); Assert.AreEqual(stream.Length, buffer.Length); Assert.AreEqual(stream.Position, 0); stream.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => buffer.Memory); }
public void Setup() { _message = new(s : 1, f : 2, replyExpected : false) { Name = "Test", SecsItem = L( L(), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), //JIS encoding cost more memory in coreclr F8(MemoryOwner <double> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), J(CreateString(ItemCount, Item.Jis8Encoding)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount))), }; _sml = _message.ToSml();
public void Setup() { _item = L( L(), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), A(CreateString(ItemCount, Encoding.ASCII)), //J(CreateString(Math.Min(ItemCount, 512))), //JIS encoding cost more memory in coreclr F8(MemoryOwner <double> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), L( I1(MemoryOwner <sbyte> .Allocate(ItemCount)), I2(MemoryOwner <short> .Allocate(ItemCount)), I4(MemoryOwner <int> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount)), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), //J(CreateString(Math.Min(ItemCount, 512))), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount)), L( A(CreateString(ItemCount, Encoding.ASCII)), //J(CreateString(Math.Min(ItemCount, 512))), Boolean(MemoryOwner <bool> .Allocate(ItemCount)), B(MemoryOwner <byte> .Allocate(ItemCount))), F8(MemoryOwner <double> .Allocate(ItemCount))), U1(MemoryOwner <byte> .Allocate(ItemCount)), U2(MemoryOwner <ushort> .Allocate(ItemCount)), U4(MemoryOwner <uint> .Allocate(ItemCount)), F4(MemoryOwner <float> .Allocate(ItemCount))); using var buffer = new ArrayPoolBufferWriter <byte>(); _item.EncodeTo(buffer); _encodedBytes = buffer.WrittenMemory.ToArray();
public void Test_MemoryOwnerOfT_PooledBuffersAndClear() { using (var buffer = MemoryOwner <int> .Allocate(127)) { buffer.Span.Fill(42); } using (var buffer = MemoryOwner <int> .Allocate(127)) { Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); } using (var buffer = MemoryOwner <int> .Allocate(127, AllocationMode.Clear)) { Assert.IsTrue(buffer.Span.ToArray().All(i => i == 0)); } }
public void RootBreakpoint() { using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("++[>++>-]>+", out _); Assert.IsNotNull(operators); using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512); stackFrames.DangerousGetReference() = new StackFrame(new Range(0, operators !.Length), 7); HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, 0); Assert.IsNotNull(exceptionInfo); Assert.AreEqual(exceptionInfo !.StackTrace.Count, 1); Assert.AreEqual(exceptionInfo.StackTrace[0], "++[>++>-"); Assert.AreEqual(exceptionInfo.HaltingOperator, '-'); Assert.AreEqual(exceptionInfo.HaltingOffset, 7); }
public void FunctionCallBreakpoint() { using MemoryOwner <Brainf_ckOperator>?operators = Brainf_ckParser.TryParse <Brainf_ckOperator>("(+>):+", out _); Assert.IsNotNull(operators); using MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(512); stackFrames.Span[0] = new StackFrame(new Range(0, operators !.Length), 5); stackFrames.Span[1] = new StackFrame(new Range(1, 3), 2); HaltedExecutionInfo?exceptionInfo = Brainf_ckInterpreter.LoadDebugInfo(operators.Span, stackFrames.Span, 1); Assert.IsNotNull(exceptionInfo); Assert.AreEqual(exceptionInfo !.StackTrace.Count, 2); Assert.AreEqual(exceptionInfo.StackTrace[0], "+>"); Assert.AreEqual(exceptionInfo.StackTrace[1], "(+>):"); Assert.AreEqual(exceptionInfo.HaltingOperator, '>'); Assert.AreEqual(exceptionInfo.HaltingOffset, 2); }
public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan() { var pool = new TrackingArrayPool <int>(); using (var buffer = MemoryOwner <int> .Allocate(127, pool)) { Assert.AreEqual(pool.RentedArrays.Count, 1); Assert.IsTrue(buffer.Length == 127); Assert.IsTrue(buffer.Memory.Length == 127); Assert.IsTrue(buffer.Span.Length == 127); buffer.Span.Fill(42); Assert.IsTrue(buffer.Memory.Span.ToArray().All(i => i == 42)); Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); } Assert.AreEqual(pool.RentedArrays.Count, 0); }
/// <summary> /// Creates a new Brainf*ck/PBrain session with the given parameters /// </summary> /// <param name="source">The source code to parse and execute</param> /// <param name="breakpoints">The sequence of indices for the breakpoints to apply to the script</param> /// <param name="stdin">The input buffer to read data from</param> /// <param name="machineState">The target machine state to use to run the script</param> /// <param name="executionToken">A <see cref="CancellationToken"/> that can be used to halt the execution</param> /// <param name="debugToken">A <see cref="CancellationToken"/> that is used to ignore/respect existing breakpoints</param> /// <returns>An <see cref="Option{T}"/> of <see cref="InterpreterSession"/> instance with the results of the execution</returns> public static Option <InterpreterSession> TryCreateSession( ReadOnlySpan <char> source, ReadOnlySpan <int> breakpoints, ReadOnlyMemory <char> stdin, TuringMachineState machineState, CancellationToken executionToken, CancellationToken debugToken) { MemoryOwner <Brainf_ckOperator> opcodes = Brainf_ckParser.TryParse <Brainf_ckOperator>(source, out SyntaxValidationResult validationResult) !; if (!validationResult.IsSuccess) { return(Option <InterpreterSession> .From(validationResult)); } // Initialize the temporary buffers MemoryOwner <bool> breakpointsTable = LoadBreakpointsTable(source, validationResult.OperatorsCount, breakpoints); MemoryOwner <int> jumpTable = LoadJumpTable(opcodes.Span, out int functionsCount); MemoryOwner <Range> functions = MemoryOwner <Range> .Allocate(ushort.MaxValue, AllocationMode.Clear); MemoryOwner <ushort> definitions = LoadDefinitionsTable(functionsCount); MemoryOwner <StackFrame> stackFrames = MemoryOwner <StackFrame> .Allocate(Specs.MaximumStackSize); // Initialize the root stack frame stackFrames.DangerousGetReference() = new StackFrame(new Range(0, opcodes.Length), 0); // Create the interpreter session InterpreterSession session = new InterpreterSession( opcodes, breakpointsTable, jumpTable, functions, definitions, stackFrames, stdin, machineState, executionToken, debugToken); return(Option <InterpreterSession> .From(validationResult, session)); }
public void Test_MemoryExtensions_IMemoryOwnerStream_DoesNotAlterExistingData() { MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(1024); // Fill the buffer with sample data for (int i = 0; i < buffer.Length; i++) { buffer.Span[i] = unchecked ((byte)(i & byte.MaxValue)); } Stream stream = buffer.AsStream(); Assert.IsNotNull(stream); Assert.AreEqual(stream.Length, buffer.Length); Assert.IsTrue(stream.CanWrite); // Validate that creating the stream doesn't alter the underlying buffer for (int i = 0; i < buffer.Length; i++) { Assert.AreEqual(buffer.Span[i], unchecked ((byte)(i & byte.MaxValue))); } }