/// <summary> /// Copy a GrowableBuffer to a new array /// </summary> /// <param name="self">The GrowableBuffer to copy</param> /// <typeparam name="T">The element type of self</typeparam> /// <returns>A new array copied from self</returns> public static T[] ToArray <T>(this GrowableBuffer <T> self) where T : unmanaged { var array = new T[self.Count]; self.CopyTo(array, 0); return(array); }
public void WhenInsertingPastCapacity_ListExpands() { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { var capacity = growableBuffer.Capacity; while (capacity == growableBuffer.Capacity) { growableBuffer.Add(0); } } }
public static void Initialize() { if (s_Initialized) { return; } s_Initialized = true; Utility.EnableLeakTracking = true; s_GraphRegistry = new GrowableBuffer <DSPGraph>(Allocator.Persistent); AppDomain.CurrentDomain.DomainUnload += OnDomainUnload; }
public RequestBody(ArraySegment <byte> prefillBytes, long?expectedLength) { const int minBufferSize = 1024 * 16; int bufferSize = Math.Max(prefillBytes.Count, minBufferSize); Buffer = new GrowableBuffer <byte>(bufferSize); Buffer.Append(prefillBytes.Array, prefillBytes.Offset, prefillBytes.Count); ExpectedLength = expectedLength; Bytes = new Future <byte[]>(); }
public void CopyFromList() { var list = new List <int>(); for (int i = 0; i < 9; ++i) { list.Add(i); } using (var copiedList = new GrowableBuffer <int>(Allocator.Temp, list)) { CollectionAssert.AreEqual(list.ToArray(), copiedList.ToArray()); Assert.AreEqual(list.Count, copiedList.Capacity); } }
public void CapacityIsValidated(int capacity, bool shouldThrow) { GrowableBuffer <float> list; if (shouldThrow) { Assert.Throws <ArgumentOutOfRangeException>(() => new GrowableBuffer <float>(Allocator.TempJob, capacity)); } else { list = new GrowableBuffer <float>(Allocator.TempJob, capacity); Assert.AreEqual(capacity, list.Capacity); list.Dispose(); } }
public void CopyFromArray() { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { for (int i = 0; i < 9; ++i) { growableBuffer.Add(i); } using (var copiedList = new GrowableBuffer <int>(Allocator.Temp, growableBuffer.ToArray())) { CollectionAssert.AreEqual(growableBuffer.ToArray(), copiedList.ToArray()); Assert.AreEqual(growableBuffer.Count, copiedList.Capacity); } } }
public void AddRange() { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { for (int i = 0; i < 9; ++i) { growableBuffer.Add(i); } using (var copiedList = new GrowableBuffer <int>(Allocator.Temp)) { copiedList.AddRange(growableBuffer); CollectionAssert.AreEqual(growableBuffer.ToArray(), copiedList.ToArray()); } } }
public void CopyFromGrowableBuffer() { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { for (int i = 0; i < 9; ++i) { growableBuffer.Add(i); } using (var copiedList = new GrowableBuffer <int>(Allocator.Temp, growableBuffer)) { // CollectionAssert is broken with GrowableBuffer<T> even though it only wants IEnumerable<T>... CollectionAssert.AreEqual(growableBuffer.ToArray(), copiedList.ToArray()); Assert.AreEqual(growableBuffer.Count, copiedList.Capacity); } } }
public void Remove(int removalIndex) { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { var list = new List <int>(); for (int i = 0; i < 9; ++i) { growableBuffer.Add(i); list.Add(i); } growableBuffer.RemoveAt(removalIndex); list.RemoveAt(removalIndex); CollectionAssert.AreEqual(list.ToArray(), growableBuffer.ToArray()); } }
public void Insert(int insertionIndex) { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { var list = new List <int>(); for (int i = 0; i < 9; ++i) { growableBuffer.Add(i); list.Add(i); } growableBuffer.Insert(insertionIndex, -1); list.Insert(insertionIndex, -1); CollectionAssert.AreEqual(list.ToArray(), growableBuffer.ToArray()); } }
/// <summary> /// Completes the DSPCommandBlock and sends it for asynchronous atomic handling by the DSPGraph. Once this is called, the DSPCommandBlock is disposed and cannot be used again. /// </summary> public void Complete() { if (!m_Commands.Valid) { return; } QueueCommand(new CompleteCommand { m_Type = DSPCommandType.Complete, m_Graph = m_Graph, m_Handle = m_Handle, }); m_Graph.ScheduleCommandBuffer(m_Commands); m_Commands.Dispose(); m_Commands = default; }
public void Cancel() { if (!m_Commands.Valid) { return; } foreach (IntPtr command in m_Commands) { DSPCommand.Cancel((void *)command); m_Graph.ReleaseCommand((void *)command); } m_Commands.Dispose(); m_Commands = default; m_Graph.DisposeHandle(m_Handle); }
public void WhenInsertingPastCapacity_DescriptionIsStillValid() { using (var growableBuffer = new GrowableBuffer <int>(Allocator.TempJob)) { var description = growableBuffer.Description; var capacity = growableBuffer.Capacity; while (capacity == growableBuffer.Capacity) { growableBuffer.Add(0); } var rehydratedList = GrowableBuffer <int> .FromDescription(description); Assert.AreEqual(growableBuffer, rehydratedList); Assert.AreEqual(growableBuffer.Count, rehydratedList.Count); Assert.AreEqual(growableBuffer.Capacity, rehydratedList.Capacity); CollectionAssert.AreEqual(growableBuffer.ToArray(), rehydratedList.ToArray()); } }
private void Process() { var pendingEvents = new Queue(); var windPayloadBuffer = new GrowableBuffer(32, 1500 + 512); var readHeaderBuffer = new byte[4]; var syncRead = new byte[1]; var syncWrite = new byte[1]; while (this.running) { var hasWrite = this.activeCommand != null && !this.activeCommand.Sent; var hasIrq = this.irq.Read() == GpioPinValue.Low; if (hasIrq || hasWrite) { syncWrite[0] = (byte)(!hasIrq && hasWrite ? 0x02 : 0x00); this.spi.TransferFullDuplex(syncWrite, syncRead); if (!hasIrq && hasWrite && syncRead[0] != 0x02) { this.activeCommand.WriteHeader(this.spi.Write); if (this.activeCommand.HasWritePayload) { while (this.irq.Read() == GpioPinValue.High) { Thread.Sleep(0); } this.activeCommand.WritePayload(this.spi.Write); while (this.irq.Read() == GpioPinValue.Low) { Thread.Sleep(0); } } this.activeCommand.Sent = true; } else if (syncRead[0] == 0x02) { this.spi.Read(readHeaderBuffer); var status = readHeaderBuffer[0]; var ind = readHeaderBuffer[1]; var payloadLength = (readHeaderBuffer[3] << 8) | readHeaderBuffer[2]; var type = (status & 0b1111_0000) >> 4; this.State = (SPWF04SxWiFiState)(status & 0b0000_1111); if (type == 0x01 || type == 0x02) { if (payloadLength > 0) { windPayloadBuffer.EnsureSize(payloadLength, false); this.spi.Read(windPayloadBuffer.Data, 0, payloadLength); } var str = Encoding.UTF8.GetString(windPayloadBuffer.Data, 0, payloadLength); pendingEvents.Enqueue(type == 0x01 ? new SPWF04SxIndicationReceivedEventArgs((SPWF04SxIndication)ind, str) : (object)new SPWF04SxErrorReceivedEventArgs(ind, str)); } else if (type == 0x03) { if (this.activeCommand == null || !this.activeCommand.Sent) { throw new InvalidOperationException("Unexpected payload."); } //See https://github.com/ghi-electronics/TinyCLR-Drivers/issues/10 //switch (ind) { // case 0x00://AT-S.OK without payload // case 0x03://AT-S.OK with payload // case 0xFF://AT-S.x not maskable // case 0xFE://AT-S.x maskable // default://AT-S.ERROR x // break; //} this.activeCommand.ReadPayload(this.spi.Read, payloadLength); } else { throw new InvalidOperationException("Unexpected header."); } } } else { while (pendingEvents.Count != 0) { switch (pendingEvents.Dequeue()) { case SPWF04SxIndicationReceivedEventArgs e: this.IndicationReceived?.Invoke(this, e); break; case SPWF04SxErrorReceivedEventArgs e: this.ErrorReceived?.Invoke(this, e); break; } } } Thread.Sleep(0); } }
public RequestBody(ArraySegment<byte> prefillBytes, long? expectedLength) { const int minBufferSize = 1024 * 16; int bufferSize = Math.Max(prefillBytes.Count, minBufferSize); Buffer = new GrowableBuffer<byte>(bufferSize); Buffer.Append(prefillBytes.Array, prefillBytes.Offset, prefillBytes.Count); ExpectedLength = expectedLength; Bytes = new Future<byte[]>(); }
internal DSPCommandBlock(DSPGraph graph) { m_Graph = graph; m_Handle = graph.AllocateHandle(); m_Commands = new GrowableBuffer <IntPtr>(Allocator.Persistent); }