# HG changeset patch # User moel.mich # Date 1351338028 0 # Node ID 76f859f4aea1cbedc92a7f47ad7d850e647c3aba # Parent ace2c5fc4b7f2ed4c484bb66f218b7c884870faf Reduced the amount of dynamic memory allocation when reading from the T-Balancer or creating tray icons. diff -r ace2c5fc4b7f -r 76f859f4aea1 Hardware/PInvokeDelegateFactory.cs --- a/Hardware/PInvokeDelegateFactory.cs Sun Oct 21 14:45:24 2012 +0000 +++ b/Hardware/PInvokeDelegateFactory.cs Sat Oct 27 11:40:28 2012 +0000 @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2009-2010 Michael Möller + Copyright (C) 2009-2012 Michael Möller */ @@ -13,6 +13,7 @@ using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; +using OpenHardwareMonitor.Collections; namespace OpenHardwareMonitor.Hardware { @@ -24,18 +25,20 @@ AssemblyBuilderAccess.Run).DefineDynamicModule( "PInvokeDelegateFactoryInternalModule"); - private static readonly IDictionary wrapperTypes = - new Dictionary(); + private static readonly IDictionary, Type> wrapperTypes = + new Dictionary, Type>(); public static void CreateDelegate(DllImportAttribute dllImportAttribute, out T newDelegate) where T : class { Type wrapperType; - wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType); + Pair key = + new Pair(dllImportAttribute, typeof(T)); + wrapperTypes.TryGetValue(key, out wrapperType); if (wrapperType == null) { wrapperType = CreateWrapperType(typeof(T), dllImportAttribute); - wrapperTypes.Add(dllImportAttribute, wrapperType); + wrapperTypes.Add(key, wrapperType); } newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType, diff -r ace2c5fc4b7f -r 76f859f4aea1 Hardware/TBalancer/FTD2XX.cs --- a/Hardware/TBalancer/FTD2XX.cs Sun Oct 21 14:45:24 2012 +0000 +++ b/Hardware/TBalancer/FTD2XX.cs Sat Oct 27 11:40:28 2012 +0000 @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2009-2010 Michael Möller + Copyright (C) 2009-2012 Michael Möller */ @@ -99,6 +99,8 @@ out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus); public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle, [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned); + public delegate FT_STATUS FT_ReadByteDelegate(FT_HANDLE handle, + out byte buffer, uint bytesToRead, out uint bytesReturned); public static readonly FT_CreateDeviceInfoListDelegate FT_CreateDeviceInfoList = CreateDelegate< @@ -136,6 +138,9 @@ public static readonly FT_ReadDelegate FT_Read = CreateDelegate< FT_ReadDelegate>("FT_Read"); + public static readonly FT_ReadByteDelegate + FT_ReadByte = CreateDelegate< + FT_ReadByteDelegate>("FT_Read"); private FTD2XX() { } @@ -162,12 +167,20 @@ } public static byte ReadByte(FT_HANDLE handle) { - byte[] buffer = new byte[1]; + byte buffer; uint bytesReturned; - FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned); + FT_STATUS status = FT_ReadByte(handle, out buffer, 1, out bytesReturned); if (status != FT_STATUS.FT_OK || bytesReturned != 1) throw new InvalidOperationException(); - return buffer[0]; + return buffer; + } + + public static void Read(FT_HANDLE handle, byte[] buffer) { + uint bytesReturned; + FT_STATUS status = + FT_Read(handle, buffer, (uint)buffer.Length, out bytesReturned); + if (status != FT_STATUS.FT_OK || bytesReturned != buffer.Length) + throw new InvalidOperationException(); } private static string GetDllName() { diff -r ace2c5fc4b7f -r 76f859f4aea1 Hardware/TBalancer/TBalancer.cs --- a/Hardware/TBalancer/TBalancer.cs Sun Oct 21 14:45:24 2012 +0000 +++ b/Hardware/TBalancer/TBalancer.cs Sat Oct 27 11:40:28 2012 +0000 @@ -30,8 +30,9 @@ private readonly List deactivating = new List(); private FT_HANDLE handle; - private int[] primaryData = new int[0]; - private int[] alternativeData = new int[0]; + private byte[] data = new byte[285]; + private byte[] primaryData = new byte[0]; + private byte[] alternativeData = new byte[0]; public const byte STARTFLAG = 100; public const byte ENDFLAG = 254; @@ -110,7 +111,7 @@ } } - private void ReadminiNG(int[] data, int number) { + private void ReadminiNG(int number) { int offset = 1 + number * 65; if (data[offset + 61] != ENDFLAG) @@ -147,9 +148,7 @@ } private void ReadData() { - int[] data = new int[285]; - for (int i = 0; i < data.Length; i++) - data[i] = FTD2XX.ReadByte(handle); + FTD2XX.Read(handle, data); if (data[0] != STARTFLAG) { FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX); @@ -161,7 +160,9 @@ if (data[274] != protocolVersion) return; - this.primaryData = data; + if (primaryData.Length == 0) + primaryData = new byte[data.Length]; + data.CopyTo(primaryData, 0); for (int i = 0; i < digitalTemperatures.Length; i++) if (data[238 + i] > 0) { @@ -223,12 +224,14 @@ } } else if (data[1] == 253) { // miniNG #1 - this.alternativeData = data; + if (alternativeData.Length == 0) + alternativeData = new byte[data.Length]; + data.CopyTo(alternativeData, 0); - ReadminiNG(data, 0); + ReadminiNG(0); if (data[66] == 253) // miniNG #2 - ReadminiNG(data, 1); + ReadminiNG(1); } } diff -r ace2c5fc4b7f -r 76f859f4aea1 Utilities/IconFactory.cs --- a/Utilities/IconFactory.cs Sun Oct 21 14:45:24 2012 +0000 +++ b/Utilities/IconFactory.cs Sat Oct 27 11:40:28 2012 +0000 @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2009-2010 Michael Möller + Copyright (C) 2009-2012 Michael Möller */ @@ -63,16 +63,13 @@ private struct ICONIMAGE { public BITMAPINFOHEADER Header; public byte[] Colors; - public byte[] XOR; - public byte[] AND; + public int MaskSize; public ICONIMAGE(int width, int height, byte[] colors) { this.Header = new BITMAPINFOHEADER(width, height << 1, (8 * colors.Length) / (width * height)); this.Colors = colors; - int maskSize = (width * height) >> 3; - this.XOR = new byte[maskSize]; - this.AND = new byte[maskSize]; + MaskSize = (width * height) >> 3; } public void Write(BinaryWriter bw) { @@ -80,8 +77,8 @@ int stride = Header.Width << 2; for (int i = (Header.Height >> 1) - 1; i >= 0; i--) bw.Write(Colors, i * stride, stride); - bw.Write(XOR); - bw.Write(AND); + for (int i = 0; i < 2 * MaskSize; i++) + bw.Write((byte)0); } } @@ -103,7 +100,7 @@ this.Planes = image.Header.Planes; this.BitCount = image.Header.BitCount; this.BytesInRes = (uint)(image.Header.Size + - image.Colors.Length + image.XOR.Length + image.AND.Length); + image.Colors.Length + image.MaskSize + image.MaskSize); this.ImageOffset = (uint)imageOffset; } @@ -149,6 +146,9 @@ (Entries.Length > 0 ? Entries[0].Size : 0)); } } } + + private static BinaryWriter binaryWriter = + new BinaryWriter(new MemoryStream()); public static Icon Create(byte[] colors, int width, int height, PixelFormat format) { @@ -161,13 +161,12 @@ dir.Entries[0].ImageOffset = dir.Size; Icon icon; - using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) { - dir.Write(bw); - image.Write(bw); + binaryWriter.BaseStream.Position = 0; + dir.Write(binaryWriter); + image.Write(binaryWriter); - bw.BaseStream.Position = 0; - icon = new Icon(bw.BaseStream); - } + binaryWriter.BaseStream.Position = 0; + icon = new Icon(binaryWriter.BaseStream); return icon; }