private void EnsureCapacity(int capacity) { if (capacity > MaxLength) { throw new ArgumentOutOfRangeException(nameof(capacity), SR.ArgumentOutOfRange_Capacity); } Debug.Assert(_buffer != null); if ((uint)capacity * sizeof(char) <= _buffer.ByteLength) { return; } UnmanagedBuffer oldBuffer = _buffer; UnmanagedBuffer newBuffer = UnmanagedBuffer.Allocate(GetAlignedByteSize(capacity)); UnmanagedBuffer.Copy(oldBuffer, newBuffer, (uint)_decryptedLength * sizeof(char)); _buffer = newBuffer; oldBuffer.Dispose(); }
private bool DoTestExternalRam() { var result = true; if (this.startSdRamTest) { return(false); } this.startSdRamTest = true; new Thread(this.ThreadBlinkLed).Start(); var externalRam1 = new UnmanagedBuffer(16 * 1024 * 1024); var externalRam2 = new UnmanagedBuffer(14 * 1024 * 1024); byte[] buf1 = null; byte[] buf2 = null; var useUnmanagedHeap = false; if (GHIElectronics.TinyCLR.Native.Memory.UnmanagedMemory.FreeBytes == 0 && GHIElectronics.TinyCLR.Native.Memory.ManagedMemory.FreeBytes > 512 * 1024) { buf1 = new byte[16 * 1024 * 1024]; buf2 = new byte[14 * 1024 * 1024]; } else { buf1 = externalRam1.Bytes; buf2 = externalRam2.Bytes; useUnmanagedHeap = true; } var md5 = GHIElectronics.TinyCLR.Cryptography.MD5.Create(); var hashValue = md5.ComputeHash(buf1); //data is a byte array. var rd = new Random(3); this.UpdateStatusText("Testing external ram. It will take ~ 2 seconds to get test result.", true, System.Drawing.Color.White); this.UpdateStatusText(" ", false); this.UpdateStatusText("External ram test is starting...", false); this.UpdateStatusText(" ", false); this.UpdateStatusText("Ram test is failed if the led stops blinking ", false); this.UpdateStatusText("(or the test stop at this step more than 2 seconds) ", false); Thread.Sleep(100); rd.NextBytes(buf2); try { var hashValue2 = md5.ComputeHash(buf1); //data is a byte array. for (var i = 0; i < hashValue.Length; i++) { if (hashValue[i] != hashValue2[i]) { result = false; } } } catch { result = false; } if (useUnmanagedHeap) { externalRam1.Dispose(); externalRam2.Dispose(); } if (result) { this.UpdateStatusText("Test external ram passed.", false); } else { this.UpdateStatusText("Test external ram failed.", false, System.Drawing.Color.Red); } this.startSdRamTest = false; return(result); }
public void Dispose() { _buffer.Dispose(); }