Reduced the amount of dynamic memory allocation when reading from the T-Balancer or creating tray icons.
1.1 --- a/Hardware/PInvokeDelegateFactory.cs Sun Oct 21 14:45:24 2012 +0000
1.2 +++ b/Hardware/PInvokeDelegateFactory.cs Sat Oct 27 11:40:28 2012 +0000
1.3 @@ -4,7 +4,7 @@
1.4 License, v. 2.0. If a copy of the MPL was not distributed with this
1.5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
1.6
1.7 - Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
1.8 + Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
1.9
1.10 */
1.11
1.12 @@ -13,6 +13,7 @@
1.13 using System.Reflection;
1.14 using System.Reflection.Emit;
1.15 using System.Runtime.InteropServices;
1.16 +using OpenHardwareMonitor.Collections;
1.17
1.18 namespace OpenHardwareMonitor.Hardware {
1.19
1.20 @@ -24,18 +25,20 @@
1.21 AssemblyBuilderAccess.Run).DefineDynamicModule(
1.22 "PInvokeDelegateFactoryInternalModule");
1.23
1.24 - private static readonly IDictionary<DllImportAttribute, Type> wrapperTypes =
1.25 - new Dictionary<DllImportAttribute, Type>();
1.26 + private static readonly IDictionary<Pair<DllImportAttribute, Type>, Type> wrapperTypes =
1.27 + new Dictionary<Pair<DllImportAttribute, Type>, Type>();
1.28
1.29 public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
1.30 out T newDelegate) where T : class
1.31 {
1.32 Type wrapperType;
1.33 - wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
1.34 + Pair<DllImportAttribute, Type> key =
1.35 + new Pair<DllImportAttribute, Type>(dllImportAttribute, typeof(T));
1.36 + wrapperTypes.TryGetValue(key, out wrapperType);
1.37
1.38 if (wrapperType == null) {
1.39 wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
1.40 - wrapperTypes.Add(dllImportAttribute, wrapperType);
1.41 + wrapperTypes.Add(key, wrapperType);
1.42 }
1.43
1.44 newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
2.1 --- a/Hardware/TBalancer/FTD2XX.cs Sun Oct 21 14:45:24 2012 +0000
2.2 +++ b/Hardware/TBalancer/FTD2XX.cs Sat Oct 27 11:40:28 2012 +0000
2.3 @@ -4,7 +4,7 @@
2.4 License, v. 2.0. If a copy of the MPL was not distributed with this
2.5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
2.6
2.7 - Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
2.8 + Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
2.9
2.10 */
2.11
2.12 @@ -99,6 +99,8 @@
2.13 out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
2.14 public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle,
2.15 [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
2.16 + public delegate FT_STATUS FT_ReadByteDelegate(FT_HANDLE handle,
2.17 + out byte buffer, uint bytesToRead, out uint bytesReturned);
2.18
2.19 public static readonly FT_CreateDeviceInfoListDelegate
2.20 FT_CreateDeviceInfoList = CreateDelegate<
2.21 @@ -136,6 +138,9 @@
2.22 public static readonly FT_ReadDelegate
2.23 FT_Read = CreateDelegate<
2.24 FT_ReadDelegate>("FT_Read");
2.25 + public static readonly FT_ReadByteDelegate
2.26 + FT_ReadByte = CreateDelegate<
2.27 + FT_ReadByteDelegate>("FT_Read");
2.28
2.29 private FTD2XX() { }
2.30
2.31 @@ -162,12 +167,20 @@
2.32 }
2.33
2.34 public static byte ReadByte(FT_HANDLE handle) {
2.35 - byte[] buffer = new byte[1];
2.36 + byte buffer;
2.37 uint bytesReturned;
2.38 - FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
2.39 + FT_STATUS status = FT_ReadByte(handle, out buffer, 1, out bytesReturned);
2.40 if (status != FT_STATUS.FT_OK || bytesReturned != 1)
2.41 throw new InvalidOperationException();
2.42 - return buffer[0];
2.43 + return buffer;
2.44 + }
2.45 +
2.46 + public static void Read(FT_HANDLE handle, byte[] buffer) {
2.47 + uint bytesReturned;
2.48 + FT_STATUS status =
2.49 + FT_Read(handle, buffer, (uint)buffer.Length, out bytesReturned);
2.50 + if (status != FT_STATUS.FT_OK || bytesReturned != buffer.Length)
2.51 + throw new InvalidOperationException();
2.52 }
2.53
2.54 private static string GetDllName() {
3.1 --- a/Hardware/TBalancer/TBalancer.cs Sun Oct 21 14:45:24 2012 +0000
3.2 +++ b/Hardware/TBalancer/TBalancer.cs Sat Oct 27 11:40:28 2012 +0000
3.3 @@ -30,8 +30,9 @@
3.4 private readonly List<ISensor> deactivating = new List<ISensor>();
3.5
3.6 private FT_HANDLE handle;
3.7 - private int[] primaryData = new int[0];
3.8 - private int[] alternativeData = new int[0];
3.9 + private byte[] data = new byte[285];
3.10 + private byte[] primaryData = new byte[0];
3.11 + private byte[] alternativeData = new byte[0];
3.12
3.13 public const byte STARTFLAG = 100;
3.14 public const byte ENDFLAG = 254;
3.15 @@ -110,7 +111,7 @@
3.16 }
3.17 }
3.18
3.19 - private void ReadminiNG(int[] data, int number) {
3.20 + private void ReadminiNG(int number) {
3.21 int offset = 1 + number * 65;
3.22
3.23 if (data[offset + 61] != ENDFLAG)
3.24 @@ -147,9 +148,7 @@
3.25 }
3.26
3.27 private void ReadData() {
3.28 - int[] data = new int[285];
3.29 - for (int i = 0; i < data.Length; i++)
3.30 - data[i] = FTD2XX.ReadByte(handle);
3.31 + FTD2XX.Read(handle, data);
3.32
3.33 if (data[0] != STARTFLAG) {
3.34 FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX);
3.35 @@ -161,7 +160,9 @@
3.36 if (data[274] != protocolVersion)
3.37 return;
3.38
3.39 - this.primaryData = data;
3.40 + if (primaryData.Length == 0)
3.41 + primaryData = new byte[data.Length];
3.42 + data.CopyTo(primaryData, 0);
3.43
3.44 for (int i = 0; i < digitalTemperatures.Length; i++)
3.45 if (data[238 + i] > 0) {
3.46 @@ -223,12 +224,14 @@
3.47 }
3.48
3.49 } else if (data[1] == 253) { // miniNG #1
3.50 - this.alternativeData = data;
3.51 + if (alternativeData.Length == 0)
3.52 + alternativeData = new byte[data.Length];
3.53 + data.CopyTo(alternativeData, 0);
3.54
3.55 - ReadminiNG(data, 0);
3.56 + ReadminiNG(0);
3.57
3.58 if (data[66] == 253) // miniNG #2
3.59 - ReadminiNG(data, 1);
3.60 + ReadminiNG(1);
3.61 }
3.62 }
3.63
4.1 --- a/Utilities/IconFactory.cs Sun Oct 21 14:45:24 2012 +0000
4.2 +++ b/Utilities/IconFactory.cs Sat Oct 27 11:40:28 2012 +0000
4.3 @@ -4,7 +4,7 @@
4.4 License, v. 2.0. If a copy of the MPL was not distributed with this
4.5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
4.6
4.7 - Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
4.8 + Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
4.9
4.10 */
4.11
4.12 @@ -63,16 +63,13 @@
4.13 private struct ICONIMAGE {
4.14 public BITMAPINFOHEADER Header;
4.15 public byte[] Colors;
4.16 - public byte[] XOR;
4.17 - public byte[] AND;
4.18 + public int MaskSize;
4.19
4.20 public ICONIMAGE(int width, int height, byte[] colors) {
4.21 this.Header = new BITMAPINFOHEADER(width, height << 1,
4.22 (8 * colors.Length) / (width * height));
4.23 this.Colors = colors;
4.24 - int maskSize = (width * height) >> 3;
4.25 - this.XOR = new byte[maskSize];
4.26 - this.AND = new byte[maskSize];
4.27 + MaskSize = (width * height) >> 3;
4.28 }
4.29
4.30 public void Write(BinaryWriter bw) {
4.31 @@ -80,8 +77,8 @@
4.32 int stride = Header.Width << 2;
4.33 for (int i = (Header.Height >> 1) - 1; i >= 0; i--)
4.34 bw.Write(Colors, i * stride, stride);
4.35 - bw.Write(XOR);
4.36 - bw.Write(AND);
4.37 + for (int i = 0; i < 2 * MaskSize; i++)
4.38 + bw.Write((byte)0);
4.39 }
4.40 }
4.41
4.42 @@ -103,7 +100,7 @@
4.43 this.Planes = image.Header.Planes;
4.44 this.BitCount = image.Header.BitCount;
4.45 this.BytesInRes = (uint)(image.Header.Size +
4.46 - image.Colors.Length + image.XOR.Length + image.AND.Length);
4.47 + image.Colors.Length + image.MaskSize + image.MaskSize);
4.48 this.ImageOffset = (uint)imageOffset;
4.49 }
4.50
4.51 @@ -149,6 +146,9 @@
4.52 (Entries.Length > 0 ? Entries[0].Size : 0)); }
4.53 }
4.54 }
4.55 +
4.56 + private static BinaryWriter binaryWriter =
4.57 + new BinaryWriter(new MemoryStream());
4.58
4.59 public static Icon Create(byte[] colors, int width, int height,
4.60 PixelFormat format) {
4.61 @@ -161,13 +161,12 @@
4.62 dir.Entries[0].ImageOffset = dir.Size;
4.63
4.64 Icon icon;
4.65 - using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
4.66 - dir.Write(bw);
4.67 - image.Write(bw);
4.68 + binaryWriter.BaseStream.Position = 0;
4.69 + dir.Write(binaryWriter);
4.70 + image.Write(binaryWriter);
4.71
4.72 - bw.BaseStream.Position = 0;
4.73 - icon = new Icon(bw.BaseStream);
4.74 - }
4.75 + binaryWriter.BaseStream.Position = 0;
4.76 + icon = new Icon(binaryWriter.BaseStream);
4.77
4.78 return icon;
4.79 }