Reduced the amount of dynamic memory allocation when reading from the T-Balancer or creating tray icons.
This commit is contained in:
@@ -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 <mmoeller@openhardwaremonitor.org>
|
||||
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
|
||||
|
||||
*/
|
||||
|
||||
@@ -13,6 +13,7 @@ using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.InteropServices;
|
||||
using OpenHardwareMonitor.Collections;
|
||||
|
||||
namespace OpenHardwareMonitor.Hardware {
|
||||
|
||||
@@ -24,18 +25,20 @@ namespace OpenHardwareMonitor.Hardware {
|
||||
AssemblyBuilderAccess.Run).DefineDynamicModule(
|
||||
"PInvokeDelegateFactoryInternalModule");
|
||||
|
||||
private static readonly IDictionary<DllImportAttribute, Type> wrapperTypes =
|
||||
new Dictionary<DllImportAttribute, Type>();
|
||||
private static readonly IDictionary<Pair<DllImportAttribute, Type>, Type> wrapperTypes =
|
||||
new Dictionary<Pair<DllImportAttribute, Type>, Type>();
|
||||
|
||||
public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
|
||||
out T newDelegate) where T : class
|
||||
{
|
||||
Type wrapperType;
|
||||
wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
|
||||
Pair<DllImportAttribute, Type> key =
|
||||
new Pair<DllImportAttribute, Type>(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,
|
||||
|
||||
@@ -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 <mmoeller@openhardwaremonitor.org>
|
||||
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
|
||||
|
||||
*/
|
||||
|
||||
@@ -99,6 +99,8 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
@@ -30,8 +30,9 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
private readonly List<ISensor> deactivating = new List<ISensor>();
|
||||
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
}
|
||||
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
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 @@ namespace OpenHardwareMonitor.Hardware.TBalancer {
|
||||
}
|
||||
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-15
@@ -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 <mmoeller@openhardwaremonitor.org>
|
||||
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
|
||||
|
||||
*/
|
||||
|
||||
@@ -63,16 +63,13 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
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 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
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 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
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 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
(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 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user