Skip to content

Commit

Permalink
Replace Marshal.SizeOf for primitive types (dotnet#33020)
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtn authored Mar 1, 2020
1 parent f9ad388 commit 6d9e503
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ internal static IntPtr AllocHGlobalIntPtrArray(int size)
{
checked
{
IntPtr intPtrArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * size);
IntPtr intPtrArray = Marshal.AllocHGlobal(IntPtr.Size * size);
for (int i = 0; i < size; i++)
{
IntPtr tempPtr = (IntPtr)((long)intPtrArray + Marshal.SizeOf(typeof(IntPtr)) * i);
IntPtr tempPtr = (IntPtr)((long)intPtrArray + IntPtr.Size * i);
Marshal.WriteIntPtr(tempPtr, IntPtr.Zero);
}
return intPtrArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ internal LdapControl[] BuildControlArray(DirectoryControlCollection controls, bo
managedControls[i].ldctl_value = new berval
{
bv_len = byteControlValue.Length,
bv_val = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * byteControlValue.Length)
bv_val = Marshal.AllocHGlobal(sizeof(byte) * byteControlValue.Length)
};
Marshal.Copy(byteControlValue, 0, managedControls[i].ldctl_value.bv_val, managedControls[i].ldctl_value.bv_len);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ internal void AddHelper(int count, IntPtr info, bool advanced)
{
if (advanced)
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA_2)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA_2)));

AttributeMetadata managedMetaData = new AttributeMetadata(addr, true, _server, _nameTable);
Add(managedMetaData.Name, managedMetaData);
}
else
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_ATTR_META_DATA)));

AttributeMetadata managedMetaData = new AttributeMetadata(addr, false, _server, _nameTable);
Add(managedMetaData.Name, managedMetaData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal void AddHelper(string partition, object cursors, bool advanced, IntPtr
{
if (advanced)
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR_3)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR_3)));
DS_REPL_CURSOR_3 cursor = new DS_REPL_CURSOR_3();
Marshal.PtrToStructure(addr, cursor);

Expand All @@ -70,7 +70,7 @@ internal void AddHelper(string partition, object cursors, bool advanced, IntPtr
}
else
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_CURSOR)));
DS_REPL_CURSOR cursor = new DS_REPL_CURSOR();
Marshal.PtrToStructure(addr, cursor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal void AddHelper(DS_REPL_KCC_DSA_FAILURES failures, IntPtr info)

for (int i = 0; i < count; i++)
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_KCC_DSA_FAILURE)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_KCC_DSA_FAILURE)));

ReplicationFailure managedFailure = new ReplicationFailure(addr, _server, _nameTable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal void AddHelper(DS_REPL_NEIGHBORS neighbors, IntPtr info)

for (int i = 0; i < count; i++)
{
addr = IntPtr.Add(info, Marshal.SizeOf(typeof(int)) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_NEIGHBOR)));
addr = IntPtr.Add(info, sizeof(int) * 2 + i * Marshal.SizeOf(typeof(DS_REPL_NEIGHBOR)));

ReplicationNeighbor managedNeighbor = new ReplicationNeighbor(addr, _server, _nameTable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ internal IntPtr ConvertToMemory()
int size = Marshal.SizeOf(typeof(EncoderParameter));

int length = _param.Length;
IntPtr memory = Marshal.AllocHGlobal(checked(length * size + Marshal.SizeOf(typeof(IntPtr))));
IntPtr memory = Marshal.AllocHGlobal(checked(length * size + IntPtr.Size));

Marshal.WriteIntPtr(memory, (IntPtr)length);

long arrayOffset = checked((long)memory + Marshal.SizeOf(typeof(IntPtr)));
long arrayOffset = checked((long)memory + IntPtr.Size);

for (int i = 0; i < length; i++)
{
Expand All @@ -86,7 +86,7 @@ internal static EncoderParameters ConvertFromMemory(IntPtr memory)

EncoderParameters p = new EncoderParameters(count);
int size = Marshal.SizeOf(typeof(EncoderParameter));
long arrayOffset = (long)memory + Marshal.SizeOf(typeof(IntPtr));
long arrayOffset = (long)memory + IntPtr.Size;

for (int i = 0; i < count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ public static StringCollection InstalledPrinters
// PRINTER_INFO_4 is 12 or 24 bytes in size depending on the architecture.
if (IntPtr.Size == 8)
{
sizeofstruct = (IntPtr.Size * 2) + (Marshal.SizeOf(typeof(int)) * 1) + Padding64Bit;
sizeofstruct = (IntPtr.Size * 2) + (sizeof(int) * 1) + Padding64Bit;
}
else
{
sizeofstruct = (IntPtr.Size * 2) + (Marshal.SizeOf(typeof(int)) * 1);
sizeofstruct = (IntPtr.Size * 2) + (sizeof(int) * 1);
}

int bufferSize;
Expand Down Expand Up @@ -454,7 +454,7 @@ public bool IsDirectPrintingSupported(ImageFormat imageFormat)
HandleRef hdc = new HandleRef(dc, dc.Hdc);
try
{
isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, Marshal.SizeOf(typeof(int)), ref nEscape, 0, out outData) > 0;
isDirectPrintingSupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0;
}
finally
{
Expand Down Expand Up @@ -495,10 +495,10 @@ public bool IsDirectPrintingSupported(Image image)
HandleRef hdc = new HandleRef(dc, dc.Hdc);
try
{
bool querySupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, Marshal.SizeOf(typeof(int)), ref nEscape, 0, out outData) > 0;
bool querySupported = SafeNativeMethods.ExtEscape(hdc, SafeNativeMethods.QUERYESCSUPPORT, sizeof(int), ref nEscape, 0, out outData) > 0;
if (querySupported)
{
isDirectPrintingSupported = (SafeNativeMethods.ExtEscape(hdc, nEscape, pvImageLen, pvImage, Marshal.SizeOf(typeof(int)), out outData) > 0)
isDirectPrintingSupported = (SafeNativeMethods.ExtEscape(hdc, nEscape, pvImageLen, pvImage, sizeof(int), out outData) > 0)
&& (outData == 1);
}
}
Expand Down

0 comments on commit 6d9e503

Please sign in to comment.