Skip to content

Commit 549c429

Browse files
author
James Forshaw
committed
Added alignment.
1 parent c5183b4 commit 549c429

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

NtApiDotNet/SafeBuffers.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ public sealed class SafeIoStatusBuffer : SafeStructureInOutBuffer<IoStatus>
6262

6363
private static readonly int _element_size = Marshal.SizeOf(typeof(T));
6464

65-
private static int GetArraySize(T[] array)
65+
private static int GetArraySize(int count, bool align)
6666
{
67-
return _element_size * array.Length;
67+
int array_size = _element_size * count;
68+
if (align)
69+
{
70+
// Align the array buffer to 8 byte alignment. Assumes that allocations
71+
// are always at least allocated on 8 byte boundaries.
72+
return (array_size + 7) & ~7;
73+
}
74+
return array_size;
6875
}
6976

7077
private SafeArrayBuffer() : base(IntPtr.Zero, 0, false)
@@ -77,7 +84,7 @@ private SafeArrayBuffer() : base(IntPtr.Zero, 0, false)
7784
/// <param name="array">Array of elements.</param>
7885
/// <param name="additional_size">Additional data to place after the array.</param>
7986
public SafeArrayBuffer(T[] array, int additional_size)
80-
: base(GetArraySize(array) + additional_size)
87+
: base(GetArraySize(array.Length, additional_size > 0) + additional_size)
8188
{
8289
Count = array.Length;
8390
IntPtr ptr = DangerousGetHandle();
@@ -106,7 +113,7 @@ public SafeHGlobalBuffer Data
106113
if (IsClosed || IsInvalid)
107114
throw new ObjectDisposedException("handle");
108115

109-
int size = Count * _element_size;
116+
int size = GetArraySize(Count, true);
110117
int length = Length - size;
111118
return new SafeHGlobalBuffer(handle + size, length < 0 ? 0 : length, false);
112119
}

0 commit comments

Comments
 (0)