public MemoryOwner <byte> ReadBitsPooled(int numberOfBits) { var bytes = MemoryOwner <byte> .Allocate(GetByteArrayLengthFromBitLength(numberOfBits)); ReadBits(numberOfBits, bytes.Span); return(bytes); }
public override int Read(Span <char> buffer) { int writtenCount, result = 0; if (charPos < charLen) { ReadyToReadChars.CopyTo(buffer, out writtenCount); charPos += writtenCount; buffer = buffer.Slice(writtenCount); result += writtenCount; } while (!buffer.IsEmpty) { writtenCount = ReadBuffer(buffer); if (writtenCount == 0) { break; } buffer = buffer.Slice(writtenCount); result += writtenCount; } return(result); }
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); }
/// <summary> /// Creates a new <see cref="InterpreterSession"/> with the specified parameters /// </summary> /// <param name="opcodes">The sequence of parsed opcodes to execute</param> /// <param name="breakpoints">The table of breakpoints for the current executable</param> /// <param name="jumpTable">The jump table for loops and function declarations</param> /// <param name="functions">The mapping of functions for the current execution</param> /// <param name="definitions">The lookup table to check which functions are defined</param> /// <param name="stackFrames">The sequence of stack frames for the current execution</param> /// <param name="stdin">The input <see cref="ReadOnlyMemory{T}"/> to read characters 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> internal InterpreterSession( MemoryOwner <Brainf_ckOperator> opcodes, MemoryOwner <bool> breakpoints, MemoryOwner <int> jumpTable, MemoryOwner <Range> functions, MemoryOwner <ushort> definitions, MemoryOwner <StackFrame> stackFrames, ReadOnlyMemory <char> stdin, TuringMachineState machineState, CancellationToken executionToken, CancellationToken debugToken) { Opcodes = opcodes; Breakpoints = breakpoints; JumpTable = jumpTable; Functions = functions; Definitions = definitions; StackFrames = stackFrames; MachineState = machineState; StdinBuffer = new StdinBuffer(stdin); StdoutBuffer = StdoutBuffer.Allocate(); ExecutionToken = executionToken; DebugToken = debugToken; Stopwatch = new Stopwatch(); SourceCode = Brainf_ckParser.ExtractSource(opcodes.Span); }
private EventHeader GetEventHeader(ReadOnlySequence <byte> buffer) { using var memoryOwner = new MemoryOwner(buffer.Slice(0, EventConstants.HeaderSize)); var reader = new PacketReader(memoryOwner.Memory.Span); return(new EventHeader(ref reader)); }
public MemoryOwner <byte> GetDtoMemory() { var memory = MemoryOwner <byte> .Allocate(dtoBuffer.Length); dtoBuffer.Span.CopyTo(memory.Span); return(memory); }
public HandshakePacket(ReadOnlySequence <byte> buffer) { using var memoryOwner = new MemoryOwner(buffer); var reader = new PacketReader(memoryOwner.Memory.Span); ProtocolVersion = reader.ReadByte(); ServerVersion = reader.ReadNullTerminatedString(); ConnectionId = reader.ReadInt32LittleEndian(); Scramble = reader.ReadNullTerminatedString(); byte[] capabilityFlags1 = reader.ReadByteArraySlow(2); ServerCollation = reader.ReadByte(); StatusFlags = reader.ReadInt16LittleEndian(); byte[] capabilityFlags2 = reader.ReadByteArraySlow(2); AuthPluginLength = reader.ReadByte(); Filler = reader.ReadString(6); byte[] capabilityFlags3 = reader.ReadByteArraySlow(4); // Join lower and upper capability flags to a number var capabilityFlags = capabilityFlags1.Concat(capabilityFlags2).Concat(capabilityFlags3).ToArray(); for (int i = 0; i < capabilityFlags.Length; i++) { ServerCapabilities |= (long)capabilityFlags[i] << (i << 3); } // Handle specific conditions if ((ServerCapabilities & (int)CapabilityFlags.SECURE_CONNECTION) > 0) { Scramble += reader.ReadNullTerminatedString(); } if ((ServerCapabilities & (int)CapabilityFlags.PLUGIN_AUTH) > 0) { AuthPluginName = reader.ReadNullTerminatedString(); } }
public static string GetString(this MemoryOwner <byte> bytes, Encoding?encoding = null) { using (bytes) { return(encoding?.Let(e => e !.GetString(bytes.Span)) ?? Encoding.UTF8.GetString(bytes.Span)); } }
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();
private IBinlogEvent Deserialize(ReadOnlySequence <byte> buffer) { using var memoryOwner = new MemoryOwner(buffer); var reader = new PacketReader(memoryOwner.Memory.Span); return(_eventDeserializer.DeserializeEvent(ref reader)); }
private void ResetWhitespaceCharactersList() { _SpaceIndices = MemoryOwner <int> .Empty; _SpaceAreas = MemoryOwner <Rect> .Empty; _TabIndices = MemoryOwner <int> .Empty; _TabAreas = MemoryOwner <Rect> .Empty; }
public void StartUp() { MemoryOwner <byte> owner = MemoryOwner <byte> .Allocate(ContentBytes.Length); ((Span <byte>)ContentBytes).CopyTo(owner.Span); _memory = owner; }
public EndOfFilePacket(ReadOnlySequence <byte> buffer) { using var memoryOwner = new MemoryOwner(buffer); var reader = new PacketReader(memoryOwner.Memory.Span); WarningCount = reader.ReadUInt16LittleEndian(); ServerStatus = reader.ReadUInt16LittleEndian(); }
public void Test_MemoryOwnerOfT_DisposedSpan() { var buffer = MemoryOwner <int> .Allocate(127); buffer.Dispose(); _ = buffer.Span; }
internal Partition(DirectoryInfo location, int bufferSize, int recordsPerPartition, long partitionNumber, MemoryAllocator <LogEntryMetadata>?cachePool, int readersCount) : base(Path.Combine(location.FullName, partitionNumber.ToString(InvariantCulture)), bufferSize, readersCount, FileOptions.RandomAccess | FileOptions.WriteThrough | FileOptions.Asynchronous) { Capacity = recordsPerPartition; FirstIndex = partitionNumber * recordsPerPartition; lookupCache = cachePool is null ? default : cachePool(recordsPerPartition); }
public AuthPluginSwitchPacket(ReadOnlySequence <byte> buffer) { using var memoryOwner = new MemoryOwner(buffer); var reader = new PacketReader(memoryOwner.Memory.Span); AuthPluginName = reader.ReadNullTerminatedString(); AuthPluginData = reader.ReadNullTerminatedString(); }
public void Test_MemoryOwnerOfT_MultipleDispose() { var buffer = MemoryOwner <int> .Allocate(127); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); buffer.Dispose(); }
static void CheckForLeaks() { int leaks = MemoryOwner.LeakCount <byte>(); if (leaks != 0) { throw new InvalidOperationException($"Failed to dispose {leaks} byte-leases"); } }
private static MemoryOwner <ushort> LoadDefinitionsTable(int functionsCount) { Assert(functionsCount >= 0); return(functionsCount switch { 0 => MemoryOwner <ushort> .Empty, _ => MemoryOwner <ushort> .Allocate(functionsCount) });
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); }
public object ParseNewDecimal(ref PacketReader reader, int metadata) { int precision = metadata & 0xFF; int scale = metadata >> 8; int integral = precision - scale; int uncompressedIntegral = integral / DigitsPerInt; int uncompressedFractional = scale / DigitsPerInt; int compressedIntegral = integral - (uncompressedIntegral * DigitsPerInt); int compressedFractional = scale - (uncompressedFractional * DigitsPerInt); int length = (uncompressedIntegral << 2) + CompressedBytes[compressedIntegral] + (uncompressedFractional << 2) + CompressedBytes[compressedFractional]; byte[] value = reader.ReadByteArraySlow(length); var result = new StringBuilder(); bool negative = (value[0] & 0x80) == 0; value[0] ^= 0x80; if (negative) { result.Append("-"); for (int i = 0; i < value.Length; i++) { value[i] ^= 0xFF; } } using var memoryOwner = new MemoryOwner(new ReadOnlySequence <byte>(value)); var buffer = new PacketReader(memoryOwner.Memory.Span); int size = CompressedBytes[compressedIntegral]; if (size > 0) { result.Append(buffer.ReadIntBigEndian(size)); } for (int i = 0; i < uncompressedIntegral; i++) { result.Append(buffer.ReadInt32BigEndian().ToString("D9")); } result.Append("."); size = CompressedBytes[compressedFractional]; for (int i = 0; i < uncompressedFractional; i++) { result.Append(buffer.ReadInt32BigEndian().ToString("D9")); } if (size > 0) { result.Append(buffer.ReadIntBigEndian(size).ToString($"D{compressedFractional}")); } return(result.ToString()); }
public BsonDocument ConvertToBson(MemoryOwner <byte> item) { string patchedXml = PatchXmlData(item.Span); TextReader textReader = new StringReader(patchedXml); string hashId = item.GetByteHash(); XmlReader reader = XmlReader.Create(textReader, _settings); BsonDocument document = new() {
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 Test_IMemoryOwnerExtensions_EmptyIMemoryOwnerStream() { MemoryOwner <byte> buffer = MemoryOwner <byte> .Empty; 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); }
/// <summary> /// Parses MySQL binary format using the provided writer. /// </summary> public static void Parse(byte[] data, IJsonWriter writer) { var parser = new JsonParser(writer); using var memoryOwner = new MemoryOwner(new ReadOnlySequence <byte>(data)); var reader = new PacketReader(memoryOwner.Memory.Span); var valueType = parser.ReadValueType(ref reader); parser.ParseNode(ref reader, valueType); }
private static string RemoveSubstringFromByteString(MemoryOwner <byte> content, ReadOnlySpan <byte> delimiter) { int[] indexes = SpanUtility.FindAllDelimiterIndexes(content.Span, delimiter); int finalLength = content.Length - indexes.Length * delimiter.Length; ByteStringState stringState = new(content, indexes, delimiter.Length); string result = CreateString(finalLength, stringState); return(result); }
private SequencePosition ScanForDelimiter(ReadOnlySequence <byte> sequence) { SequenceReader <byte> reader = new SequenceReader <byte>(sequence); while (reader.TryReadTo(out ReadOnlySequence <byte> xmlEntry, Constants.EndingTagBytes)) { MemoryOwner <byte> memory = xmlEntry.CopyToMemoryOwner(); PresentEntry(memory); } ScanComplete(); return(reader.Position); }