Skip to content

Commit ea5021d

Browse files
authored
Multitargeting and net6.0 support (#5)
* Bump to net6.0 and update packages * Add LibDeflate.Buffers to avoid WCT dependency * Use IMemoryOwner<T> for public APIs * Use file-scoped namespace in Compressor * Use file-scoped namespace in Decompression * Use file-scoped namespace in Constants * Use file-scoped namespace in Compression * Use file-scoped namespace in Adler32 * Use file-scoped namespace in Crc32 * Use file-scoped namespace in CustomMemoryAllocator * Add WCT perf types * Use file-scoped namespace * Use dispose pattern in Compressor * Update DecompressorTests to use IMemoryOwner * Update LibDeflate to support multitargeting * Function pointer experiment * Multitarget tests * Break the world and target v1.7.1 * Add benchmark project stub * Update solution * Add editorconfig * Add file-scoped namespace rule to editorconfig * Use file-scoped namespaces
1 parent 5091537 commit ea5021d

27 files changed

Lines changed: 1553 additions & 720 deletions

.editorconfig

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# You can modify the rules from these initially generated values to suit your own policies
2+
# You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
3+
4+
###############################
5+
# Core EditorConfig Options #
6+
###############################
7+
root = true
8+
# All files
9+
[*]
10+
indent_style = space
11+
12+
# XML project files
13+
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
14+
indent_size = 2
15+
16+
# XML config files
17+
[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}]
18+
indent_size = 2
19+
20+
[*.cs]
21+
#Core editorconfig formatting - indentation
22+
23+
#use soft tabs (spaces) for indentation
24+
indent_style = space
25+
26+
#Formatting - indentation options
27+
28+
#indent switch case contents.
29+
csharp_indent_case_contents = true
30+
#indent switch labels
31+
csharp_indent_switch_labels = true
32+
33+
#Formatting - new line options
34+
35+
#place catch statements on a new line
36+
csharp_new_line_before_catch = true
37+
#place else statements on a new line
38+
csharp_new_line_before_else = true
39+
#require members of object initializers to be on the same line
40+
csharp_new_line_before_members_in_object_initializers = false
41+
#require braces to be on a new line for object_collection_array_initializers, accessors, methods, properties, control_blocks, types, and lambdas (also known as "Allman" style)
42+
csharp_new_line_before_open_brace = object_collection_array_initializers, accessors, methods, properties, control_blocks, types, lambdas
43+
44+
#Formatting - organize using options
45+
46+
#do not place System.* using directives before other using directives
47+
dotnet_sort_system_directives_first = false
48+
49+
#Formatting - spacing options
50+
51+
#require NO space between a cast and the value
52+
csharp_space_after_cast = false
53+
#require a space before the colon for bases or interfaces in a type declaration
54+
csharp_space_after_colon_in_inheritance_clause = true
55+
#require a space after a keyword in a control flow statement such as a for loop
56+
csharp_space_after_keywords_in_control_flow_statements = true
57+
#require a space before the colon for bases or interfaces in a type declaration
58+
csharp_space_before_colon_in_inheritance_clause = true
59+
#remove space within empty argument list parentheses
60+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
61+
#remove space between method call name and opening parenthesis
62+
csharp_space_between_method_call_name_and_opening_parenthesis = false
63+
#do not place space characters after the opening parenthesis and before the closing parenthesis of a method call
64+
csharp_space_between_method_call_parameter_list_parentheses = false
65+
#remove space within empty parameter list parentheses for a method declaration
66+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
67+
#place a space character after the opening parenthesis and before the closing parenthesis of a method declaration parameter list.
68+
csharp_space_between_method_declaration_parameter_list_parentheses = false
69+
70+
#Formatting - wrapping options
71+
72+
#leave code block on single line
73+
csharp_preserve_single_line_blocks = true
74+
#leave statements and member declarations on the same line
75+
csharp_preserve_single_line_statements = true
76+
77+
#Style - Code block preferences
78+
79+
#prefer curly braces even for one line of code
80+
csharp_prefer_braces = true:suggestion
81+
82+
#Style - expression bodied member options
83+
84+
#prefer expression-bodied members for accessors
85+
csharp_style_expression_bodied_accessors = true:suggestion
86+
#prefer expression-bodied members for constructors
87+
csharp_style_expression_bodied_constructors = true:suggestion
88+
#prefer expression-bodied members for indexers
89+
csharp_style_expression_bodied_indexers = true:suggestion
90+
#prefer expression-bodied members for methods
91+
csharp_style_expression_bodied_methods = true:suggestion
92+
#prefer expression-bodied members for properties
93+
csharp_style_expression_bodied_properties = true:suggestion
94+
95+
# IDE0160: Convert to file-scoped namespace
96+
csharp_style_namespace_declarations = file_scoped:warning
97+
98+
#Style - expression level options
99+
100+
#prefer out variables to be declared inline in the argument list of a method call when possible
101+
csharp_style_inlined_variable_declaration = true:suggestion
102+
#prefer tuple names to ItemX properties
103+
dotnet_style_explicit_tuple_names = true:suggestion
104+
#prefer the language keyword for member access expressions, instead of the type name, for types that have a keyword to represent them
105+
dotnet_style_predefined_type_for_member_access = true:suggestion
106+
107+
#Style - Expression-level preferences
108+
109+
#prefer default over default(T)
110+
csharp_prefer_simple_default_expression = true:suggestion
111+
#prefer objects to be initialized using object initializers when possible
112+
dotnet_style_object_initializer = true:suggestion
113+
#prefer inferred tuple element names
114+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
115+
116+
#Style - language keyword and framework type options
117+
118+
#prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them
119+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
120+
121+
#Style - Miscellaneous preferences
122+
123+
#prefer local functions over anonymous functions
124+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
125+
126+
#Style - modifier options
127+
128+
#prefer accessibility modifiers to be declared except for public interface members. This will currently not differ from always and will act as future proofing for if C# adds default interface methods.
129+
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
130+
131+
#Style - Modifier preferences
132+
133+
#when this rule is set to a list of modifiers, prefer the specified ordering.
134+
csharp_preferred_modifier_order = public,protected,private,internal,static,readonly,sealed,override:suggestion
135+
136+
#Style - qualification options
137+
138+
#prefer fields to be prefaced with this. in C# or Me. in Visual Basic
139+
dotnet_style_qualification_for_field = false:suggestion
140+
#prefer methods not to be prefaced with this. or Me. in Visual Basic
141+
dotnet_style_qualification_for_method = false:suggestion
142+
#prefer properties not to be prefaced with this. or Me. in Visual Basic
143+
dotnet_style_qualification_for_property = false:suggestion

LibDeflate.sln

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.31004.235
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32104.313
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibDeflate", "src\LibDeflate\LibDeflate.csproj", "{8146F335-F7EB-431F-97FA-8E055ADBC87C}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibDeflate.Tests", "test\LibDeflate.Tests\LibDeflate.Tests.csproj", "{99A0514A-4F2D-48C7-9430-9F8E8C7BA04D}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibDeflate.DangerousTests", "test\LibDeflate.DangerousTests\LibDeflate.DangerousTests.csproj", "{D655D50E-0159-424B-A24D-C15952BB02DC}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibDeflate.DangerousTests", "test\LibDeflate.DangerousTests\LibDeflate.DangerousTests.csproj", "{D655D50E-0159-424B-A24D-C15952BB02DC}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibDeflate.Benchmarks", "bench\LibDeflate.Benchmarks\LibDeflate.Benchmarks.csproj", "{46C3591F-ED82-47E0-81E4-0B79ED037C52}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{9099F387-9C1C-4EF5-A834-86109FFEA802}"
15+
ProjectSection(SolutionItems) = preProject
16+
.editorconfig = .editorconfig
17+
EndProjectSection
1118
EndProject
1219
Global
1320
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +34,10 @@ Global
2734
{D655D50E-0159-424B-A24D-C15952BB02DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
2835
{D655D50E-0159-424B-A24D-C15952BB02DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
2936
{D655D50E-0159-424B-A24D-C15952BB02DC}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{46C3591F-ED82-47E0-81E4-0B79ED037C52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{46C3591F-ED82-47E0-81E4-0B79ED037C52}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{46C3591F-ED82-47E0-81E4-0B79ED037C52}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{46C3591F-ED82-47E0-81E4-0B79ED037C52}.Release|Any CPU.Build.0 = Release|Any CPU
3041
EndGlobalSection
3142
GlobalSection(SolutionProperties) = preSolution
3243
HideSolutionNode = FALSE
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace LibDeflate.Buffers;
6+
7+
/// <summary>
8+
/// An <see langword="enum"/> that indicates a mode to use when allocating buffers.
9+
/// </summary>
10+
internal enum AllocationMode
11+
{
12+
/// <summary>
13+
/// The default allocation mode for pooled memory (rented buffers are not cleared).
14+
/// </summary>
15+
Default,
16+
17+
/// <summary>
18+
/// Clear pooled buffers when renting them.
19+
/// </summary>
20+
Clear
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Diagnostics.Contracts;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
6+
namespace LibDeflate.Buffers;
7+
8+
internal static class ArrayExtensions
9+
{
10+
#if NETCOREAPP3_1
11+
// Description taken from CoreCLR: see https://source.dot.net/#System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs,285.
12+
// CLR arrays are laid out in memory as follows (multidimensional array bounds are optional):
13+
// [ sync block || pMethodTable || num components || MD array bounds || array data .. ]
14+
// ^ ^ ^ returned reference
15+
// | \-- ref Unsafe.As<RawArrayData>(array).Data
16+
// \-- array
17+
// The base size of an array includes all the fields before the array data,
18+
// including the sync block and method table. The reference to RawData.Data
19+
// points at the number of components, skipping over these two pointer-sized fields.
20+
[StructLayout(LayoutKind.Sequential)]
21+
private sealed class RawArrayData
22+
{
23+
#pragma warning disable CS0649 // Unassigned fields
24+
public IntPtr Length;
25+
public byte Data;
26+
#pragma warning restore CS0649
27+
}
28+
#endif
29+
30+
/// <summary>
31+
/// Returns a reference to an element at a specified index within a given <typeparamref name="T"/> array, with no bounds checks.
32+
/// </summary>
33+
/// <typeparam name="T">The type of elements in the input <typeparamref name="T"/> array instance.</typeparam>
34+
/// <param name="array">The input <typeparamref name="T"/> array instance.</param>
35+
/// <param name="i">The index of the element to retrieve within <paramref name="array"/>.</param>
36+
/// <returns>A reference to the element within <paramref name="array"/> at the index specified by <paramref name="i"/>.</returns>
37+
/// <remarks>This method doesn't do any bounds checks, therefore it is responsibility of the caller to ensure the <paramref name="i"/> parameter is valid.</remarks>
38+
[Pure]
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public static ref T DangerousGetReferenceAt<T>(this T[] array, int i)
41+
{
42+
#if NET5_0_OR_GREATER
43+
ref T r0 = ref MemoryMarshal.GetArrayDataReference(array);
44+
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
45+
46+
return ref ri;
47+
#elif NETCOREAPP3_1
48+
RawArrayData? arrayData = Unsafe.As<RawArrayData>(array)!;
49+
ref T r0 = ref Unsafe.As<byte, T>(ref arrayData.Data);
50+
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
51+
52+
return ref ri;
53+
#else
54+
IntPtr offset = RuntimeHelpers.GetArrayDataByteOffset<T>();
55+
ref T r0 = ref ObjectMarshal.DangerousGetObjectDataReferenceAt<T>(array, offset);
56+
ref T ri = ref Unsafe.Add(ref r0, (nint)(uint)i);
57+
58+
return ref ri;
59+
#endif
60+
}
61+
62+
}

0 commit comments

Comments
 (0)