Skip to content

Commit 159e2c8

Browse files
committed
Prepare for .NET 9 switches
1 parent 6f7a322 commit 159e2c8

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

src/DotNext/Buffers/SpanOwner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static unsafe Span<T> Allocate(int length)
137137
private static bool IsNaturalAlignment => Intrinsics.AlignOf<T>() <= nuint.Size;
138138

139139
private static bool UseNativeAllocation
140-
=> !LibrarySettings.DisableNativeAllocation && !RuntimeHelpers.IsReferenceOrContainsReferences<T>();
140+
=> LibrarySettings.UseNativeAllocation && !RuntimeHelpers.IsReferenceOrContainsReferences<T>();
141141

142142
/// <summary>
143143
/// Gets the rented memory.

src/DotNext/LibrarySettings.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
13
namespace DotNext;
24

35
internal static class LibrarySettings
46
{
7+
private const string UseNativeAllocationFeature = "DotNext.Buffers.NativeAllocation";
8+
private const string UseRandomStringInternalBufferCleanupFeature = "DotNext.Security.RandomStringInternalBufferCleanup";
9+
510
internal static int StackallocThreshold
611
{
712
get
@@ -19,32 +24,15 @@ internal static int StackallocThreshold
1924
return result > minimumValue ? result : defaultValue;
2025
}
2126
}
27+
28+
private static bool IsSupported([ConstantExpected] string featureName)
29+
=> !AppContext.TryGetSwitch(featureName, out var result) || result;
2230

23-
internal static bool DisableRandomStringInternalBufferCleanup
24-
{
25-
get
26-
{
27-
const string switchName = "DotNext.Security.DisableRandomStringInternalBufferCleanup";
28-
const bool defaultValue = false;
29-
30-
if (!AppContext.TryGetSwitch(switchName, out var result))
31-
result = defaultValue;
32-
33-
return result;
34-
}
35-
}
31+
// TODO: [FeatureSwitchDefinition(UseRandomStringInternalBufferCleanupFeature)]
32+
internal static bool UseRandomStringInternalBufferCleanup
33+
=> IsSupported(UseRandomStringInternalBufferCleanupFeature);
3634

37-
internal static bool DisableNativeAllocation
38-
{
39-
get
40-
{
41-
const string switchName = "DotNext.Buffers.DisableNativeAllocation";
42-
const bool defaultValue = false;
43-
44-
if (!AppContext.TryGetSwitch(switchName, out var result))
45-
result = defaultValue;
46-
47-
return result;
48-
}
49-
}
35+
// TODO: [FeatureSwitchDefinition(EnableNativeAllocationFeature)]
36+
internal static bool UseNativeAllocation
37+
=> IsSupported(UseNativeAllocationFeature);
5038
}

src/DotNext/RandomExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public static class RandomExtensions
1919
/// </summary>
2020
internal static readonly int BitwiseHashSalt = Random.Shared.Next();
2121

22-
private static readonly bool CleanupInternalBuffer = !LibrarySettings.DisableRandomStringInternalBufferCleanup;
23-
2422
private interface IRandomBytesSource
2523
{
2624
void GetBytes(Span<byte> bytes);
@@ -141,7 +139,7 @@ ref MemoryMarshal.GetReference(allowedInput),
141139
new CachedRandomNumberGenerator<TRandom>(random, randomVectorBuffer.Span).Randomize(allowedInput, buffer);
142140
}
143141

144-
if (CleanupInternalBuffer)
142+
if (LibrarySettings.UseRandomStringInternalBufferCleanup)
145143
randomVectorBuffer.Span.Clear();
146144

147145
static void FastPath(ref uint randomVectorPtr, ref T inputPtr, uint moduloOperand, Span<T> output)

0 commit comments

Comments
 (0)