-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper APIs for variable-length inline arrays
Closes #387
- Loading branch information
Showing
8 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Microsoft.Windows.CsWin32/templates/VariableLengthInlineArray`1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
internal struct VariableLengthInlineArray<T> | ||
where T : unmanaged | ||
{ | ||
internal T e0; | ||
|
||
#if canUseUnsafeAdd | ||
internal ref T this[int index] | ||
{ | ||
[UnscopedRef] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => ref Unsafe.Add(ref this.e0, index); | ||
} | ||
#endif | ||
|
||
#if canUseSpan | ||
[UnscopedRef] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal Span<T> AsSpan(int length) | ||
{ | ||
#if canCallCreateSpan | ||
return MemoryMarshal.CreateSpan(ref this.e0, length); | ||
#else | ||
unsafe | ||
{ | ||
fixed (void* p = &this.e0) | ||
{ | ||
return new Span<T>(p, length); | ||
} | ||
} | ||
#endif | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
using Windows.Win32.System.Ole; | ||
|
||
public class FlexibleArrayTests | ||
{ | ||
[Fact] | ||
public unsafe void FlexibleArraySizing() | ||
{ | ||
const int count = 3; | ||
PAGESET* pPageSet = (PAGESET*)Marshal.AllocHGlobal(PAGESET.SizeOf(count)); | ||
try | ||
{ | ||
pPageSet->rgPages[0].nFromPage = 0; | ||
|
||
Span<PAGERANGE> pageRange = pPageSet->rgPages.AsSpan(count); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
pageRange[i].nFromPage = i * 2; | ||
pageRange[i].nToPage = (i * 2) + 1; | ||
} | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal((IntPtr)pPageSet); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void SizeOf_Minimum1Element() | ||
{ | ||
Assert.Equal(PAGESET.SizeOf(1), PAGESET.SizeOf(0)); | ||
Assert.Equal(Marshal.SizeOf<PAGERANGE>(), PAGESET.SizeOf(2) - PAGESET.SizeOf(1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters