Skip to content

Commit 4aea418

Browse files
committed
Move features to the separated classes
1 parent 159e2c8 commit 4aea418

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

src/DotNext/Buffers/SpanOwner.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ref struct SpanOwner<T>
3838
/// </remarks>
3939
[EditorBrowsable(EditorBrowsableState.Never)]
4040
[CLSCompliant(false)]
41-
public static int StackallocThreshold { get; } = 1 + (LibrarySettings.StackallocThreshold / Unsafe.SizeOf<T>());
41+
public static int StackallocThreshold { get; } = 1 + Features.StackallocThreshold / Unsafe.SizeOf<T>();
4242

4343
private readonly object? owner;
4444
private readonly Span<T> memory;
@@ -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.UseNativeAllocation && !RuntimeHelpers.IsReferenceOrContainsReferences<T>();
140+
=> Features.UseNativeAllocation && !RuntimeHelpers.IsReferenceOrContainsReferences<T>();
141141

142142
/// <summary>
143143
/// Gets the rented memory.
@@ -216,4 +216,31 @@ public void Dispose()
216216

217217
this = default;
218218
}
219+
}
220+
221+
file static class Features
222+
{
223+
private const string UseNativeAllocationFeature = "DotNext.Buffers.NativeAllocation";
224+
225+
// TODO: [FeatureSwitchDefinition(EnableNativeAllocationFeature)]
226+
internal static bool UseNativeAllocation
227+
=> LibraryFeature.IsSupported(UseNativeAllocationFeature);
228+
229+
internal static int StackallocThreshold
230+
{
231+
get
232+
{
233+
const string environmentVariableName = "DOTNEXT_STACK_ALLOC_THRESHOLD";
234+
const string configurationParameterName = "DotNext.Buffers.StackAllocThreshold";
235+
const int defaultValue = 511;
236+
const int minimumValue = 14;
237+
238+
if (AppContext.GetData(configurationParameterName) is not int result)
239+
{
240+
int.TryParse(Environment.GetEnvironmentVariable(environmentVariableName), out result);
241+
}
242+
243+
return result > minimumValue ? result : defaultValue;
244+
}
245+
}
219246
}

src/DotNext/LibraryFeature.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace DotNext;
4+
5+
internal static class LibraryFeature
6+
{
7+
internal static bool IsSupported([ConstantExpected] string featureName)
8+
=> !AppContext.TryGetSwitch(featureName, out var result) || result;
9+
}

src/DotNext/LibrarySettings.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/DotNext/RandomExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ ref MemoryMarshal.GetReference(allowedInput),
139139
new CachedRandomNumberGenerator<TRandom>(random, randomVectorBuffer.Span).Randomize(allowedInput, buffer);
140140
}
141141

142-
if (LibrarySettings.UseRandomStringInternalBufferCleanup)
142+
if (Features.UseRandomStringInternalBufferCleanup)
143143
randomVectorBuffer.Span.Clear();
144144

145145
static void FastPath(ref uint randomVectorPtr, ref T inputPtr, uint moduloOperand, Span<T> output)
@@ -468,4 +468,13 @@ public static Optional<T> Peek<T>(this Random random, ReadOnlySpan<T> span)
468468
_ => span[random.Next(length)],
469469
};
470470
}
471+
}
472+
473+
file static class Features
474+
{
475+
private const string UseRandomStringInternalBufferCleanupFeature = "DotNext.Security.RandomStringInternalBufferCleanup";
476+
477+
// TODO: [FeatureSwitchDefinition(UseRandomStringInternalBufferCleanupFeature)]
478+
internal static bool UseRandomStringInternalBufferCleanup
479+
=> LibraryFeature.IsSupported(UseRandomStringInternalBufferCleanupFeature);
471480
}

0 commit comments

Comments
 (0)