diff --git a/StirlingLabs.Utilities.Extensions/StirlingLabs.Utilities.Extensions.csproj b/StirlingLabs.Utilities.Extensions/StirlingLabs.Utilities.Extensions.csproj index 2424409..9f929fa 100644 --- a/StirlingLabs.Utilities.Extensions/StirlingLabs.Utilities.Extensions.csproj +++ b/StirlingLabs.Utilities.Extensions/StirlingLabs.Utilities.Extensions.csproj @@ -17,9 +17,9 @@ - + - + diff --git a/StirlingLabs.Utilities.Magic/StirlingLabs.Utilities.Magic.csproj b/StirlingLabs.Utilities.Magic/StirlingLabs.Utilities.Magic.csproj index f90d270..300d1d2 100644 --- a/StirlingLabs.Utilities.Magic/StirlingLabs.Utilities.Magic.csproj +++ b/StirlingLabs.Utilities.Magic/StirlingLabs.Utilities.Magic.csproj @@ -17,9 +17,9 @@ - + - + diff --git a/StirlingLabs.Utilities.NativeLibrary/StirlingLabs.Utilities.NativeLibrary.csproj b/StirlingLabs.Utilities.NativeLibrary/StirlingLabs.Utilities.NativeLibrary.csproj index 0528074..108e1a8 100644 --- a/StirlingLabs.Utilities.NativeLibrary/StirlingLabs.Utilities.NativeLibrary.csproj +++ b/StirlingLabs.Utilities.NativeLibrary/StirlingLabs.Utilities.NativeLibrary.csproj @@ -12,7 +12,7 @@ - + diff --git a/StirlingLabs.Utilities.Simple.Tests/StirlingLabs.Utilities.Simple.Tests.csproj b/StirlingLabs.Utilities.Simple.Tests/StirlingLabs.Utilities.Simple.Tests.csproj index eece1cc..308c120 100644 --- a/StirlingLabs.Utilities.Simple.Tests/StirlingLabs.Utilities.Simple.Tests.csproj +++ b/StirlingLabs.Utilities.Simple.Tests/StirlingLabs.Utilities.Simple.Tests.csproj @@ -12,10 +12,10 @@ - - + + - + diff --git a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net60.csproj b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net60.csproj index 375c15a..256bc6c 100644 --- a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net60.csproj +++ b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net60.csproj @@ -49,16 +49,16 @@ - + - - + + - - - + + + diff --git a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net70.csproj b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net70.csproj index 9825c3a..37a55ee 100644 --- a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net70.csproj +++ b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Net70.csproj @@ -49,16 +49,16 @@ - + - - + + - - - + + + diff --git a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard20.csproj b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard20.csproj index f095d02..fa52e28 100644 --- a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard20.csproj +++ b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard20.csproj @@ -48,17 +48,17 @@ - + - - + + - - + + - + diff --git a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard21.csproj b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard21.csproj index cb236ff..51ddef8 100644 --- a/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard21.csproj +++ b/StirlingLabs.Utilities.Tests/StirlingLabs.Utilities.Tests.Netstandard21.csproj @@ -48,17 +48,17 @@ - + - - + + - - + + - + diff --git a/StirlingLabs.Utilities.Tests/UtilitiesTests.cs b/StirlingLabs.Utilities.Tests/UtilitiesTests.cs index bd0243f..b816e80 100644 --- a/StirlingLabs.Utilities.Tests/UtilitiesTests.cs +++ b/StirlingLabs.Utilities.Tests/UtilitiesTests.cs @@ -1,7 +1,6 @@ using System.Runtime.CompilerServices; using FluentAssertions; using NUnit.Framework; -using StirlingLabs.Native; using StirlingLabs.Utilities; namespace StirlingLabs.Utilities.Tests; diff --git a/StirlingLabs.Utilities.Text/ICU4X.cs b/StirlingLabs.Utilities.Text/ICU4X.cs index 1228512..d94abb9 100644 --- a/StirlingLabs.Utilities.Text/ICU4X.cs +++ b/StirlingLabs.Utilities.Text/ICU4X.cs @@ -11,7 +11,6 @@ using System.Text; using System.Text.RegularExpressions; using JetBrains.Annotations; -using NativeMemory = StirlingLabs.Native.NativeMemory; namespace StirlingLabs.Utilities.Text; diff --git a/StirlingLabs.Utilities.Text/StirlingLabs.Utilities.Text.csproj b/StirlingLabs.Utilities.Text/StirlingLabs.Utilities.Text.csproj index f999d74..1f2d3f7 100644 --- a/StirlingLabs.Utilities.Text/StirlingLabs.Utilities.Text.csproj +++ b/StirlingLabs.Utilities.Text/StirlingLabs.Utilities.Text.csproj @@ -18,7 +18,7 @@ - + diff --git a/StirlingLabs.Utilities.Yaml/StirlingLabs.Utilities.Yaml.csproj b/StirlingLabs.Utilities.Yaml/StirlingLabs.Utilities.Yaml.csproj index 12a3aff..5f2b5cd 100644 --- a/StirlingLabs.Utilities.Yaml/StirlingLabs.Utilities.Yaml.csproj +++ b/StirlingLabs.Utilities.Yaml/StirlingLabs.Utilities.Yaml.csproj @@ -15,8 +15,8 @@ - - + + diff --git a/StirlingLabs.Utilities/NativeMemory.cs b/StirlingLabs.Utilities/NativeMemory.cs index 2a778ba..71d9b86 100644 --- a/StirlingLabs.Utilities/NativeMemory.cs +++ b/StirlingLabs.Utilities/NativeMemory.cs @@ -5,8 +5,13 @@ using JetBrains.Annotations; using StirlingLabs.Utilities; -namespace StirlingLabs.Native; +namespace StirlingLabs.Utilities; +/// +/// A static utility class for interacting with unmanaged memory. Provides methods for allocating, deallocating, and manipulating memory. +/// It is recommended to use instead of this class if you don't require compatibility +/// all the way back to .Net Standard. +/// [PublicAPI] [SuppressMessage("ReSharper", "InconsistentNaming")] [SuppressMessage("ReSharper", "IdentifierTypo")] @@ -39,9 +44,20 @@ static unsafe NativeMemory() } } + /// + /// Allocates unmanaged memory without initializing it. + /// + /// The size of the memory to allocate. + /// A span of bytes representing the allocated memory. public static unsafe Span AllocUnsafe(nuint size) => new(New(size), (int)size); + /// + /// Allocates unmanaged memory and initializes it to zero. + /// + /// The number of elements to allocate. + /// The size of each element. + /// A span of bytes representing the allocated memory. public static unsafe Span Alloc(nuint count, nuint size) => new(New(count, size), (int)(count * size)); @@ -83,6 +99,11 @@ public static Span Alloc(nuint size) initializer(p); return p; } + + /// + /// Deallocates the specified memory. + /// + /// A pointer to the memory to deallocate. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void Free(void* ptr) => free(ptr); @@ -99,13 +120,31 @@ public static unsafe void Free(T* ptr) where T : unmanaged public static unsafe nuint SizeOf() where T : unmanaged => (nuint)sizeof(T); + /// + /// Copies memory from one location to another. + /// + /// A pointer to the source memory. + /// A pointer to the destination memory. + /// The size of the memory to copy. + /// A pointer to the destination memory. public static unsafe void* Copy(void* from, void* to, nuint size) => memmove(to, from, size); } +/// +/// A static utility class for interacting with unmanaged memory for a specific type. Provides methods for allocating, reallocating, and manipulating memory. +/// It is recommended to use instead of this class if you don't require compatibility +/// all the way back to .Net Standard. +/// +/// The type of the elements in the memory. [PublicAPI] public static class NativeMemory where T : unmanaged { + /// + /// Allocates memory for a specific number of items of type T. + /// + /// The number of items to allocate (default is 1). + /// A span representing the allocated memory. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe Span New(nuint count = 1) => new(NativeMemory.New(count, SizeOf()), (int)count); @@ -126,6 +165,12 @@ private static unsafe nuint MaxSizeOf() where TNew : unmanaged private static unsafe nuint MinSizeOf() where TNew : unmanaged => (nuint)(sizeof(T) > sizeof(TNew) ? sizeof(TNew) : sizeof(T)); + /// + /// Reallocates memory for a different type and/or size. + /// + /// The type of the new elements in the memory. + /// A pointer to the memory to reallocate. + /// A pointer to the reallocated memory. public static unsafe TNew* ReAlloc(T* ptr) where TNew : unmanaged => (TNew*)NativeMemory.realloc(ptr, (nuint)sizeof(TNew)); @@ -135,12 +180,25 @@ public static unsafe Span ReAlloc(Span span, nuint count) where T return new(NativeMemory.realloc(p, count * (nuint)sizeof(TNew)), (int)count); } + /// + /// Resizes the allocated memory for a span. + /// + /// The span to resize. + /// The new size for the span. + /// A span representing the resized memory. public static unsafe Span Resize(Span span, nuint count) { var p = Unsafe.AsPointer(ref span.GetPinnableReference()); return new(NativeMemory.realloc(p, count * (nuint)sizeof(T)), (int)count); } + /// + /// Copies memory from one location to another, potentially changing the type of the data. + /// + /// The type of the new elements in the memory. + /// A pointer to the source memory. + /// A pointer to the destination memory. + /// A pointer to the destination memory. public static unsafe TNew* Copy(T* from, TNew* to) where TNew : unmanaged => (TNew*)NativeMemory.memmove(to, from, MinSizeOf()); } \ No newline at end of file diff --git a/StirlingLabs.Utilities/StirlingLabs.Utilities.csproj b/StirlingLabs.Utilities/StirlingLabs.Utilities.csproj index d4a0d7f..3835dfd 100644 --- a/StirlingLabs.Utilities/StirlingLabs.Utilities.csproj +++ b/StirlingLabs.Utilities/StirlingLabs.Utilities.csproj @@ -17,9 +17,9 @@ - + - + diff --git a/StirlingLabs.Utilities/Utf8String.cs b/StirlingLabs.Utilities/Utf8String.cs index ca66072..36fe3e2 100644 --- a/StirlingLabs.Utilities/Utf8String.cs +++ b/StirlingLabs.Utilities/Utf8String.cs @@ -9,7 +9,6 @@ using System.Text; using System.Threading; using JetBrains.Annotations; -using NativeMemory = StirlingLabs.Native.NativeMemory; #if NET5_0_OR_GREATER #endif