From f4e4db7c5320976406feb252ae8f8bdbe9b3e351 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 15 Jan 2024 19:32:30 -0500 Subject: setup new lib import code attributes --- lib/Utils/src/ERRNO.cs | 2 +- lib/Utils/src/IO/FileOperations.cs | 12 +++++----- lib/Utils/src/Memory/ForwardOnlyReader.cs | 5 ++++- lib/Utils/src/Memory/ForwardOnlyWriter.cs | 20 ++++++----------- lib/Utils/src/Memory/MemoryUtil.cs | 7 +++--- lib/Utils/src/Memory/ProcessHeap.cs | 13 +++++------ lib/Utils/src/Memory/UnsafeMemoryHandle.cs | 7 +++--- lib/Utils/src/Memory/VnString.cs | 27 +++++++++------------- lib/Utils/src/Memory/VnTable.cs | 8 ++----- lib/Utils/src/Memory/Win32PrivateHeap.cs | 36 +++++++++++++++--------------- lib/Utils/src/VnDisposeable.cs | 11 +++------ 11 files changed, 63 insertions(+), 85 deletions(-) (limited to 'lib') diff --git a/lib/Utils/src/ERRNO.cs b/lib/Utils/src/ERRNO.cs index 2a59ba6..684a3c7 100644 --- a/lib/Utils/src/ERRNO.cs +++ b/lib/Utils/src/ERRNO.cs @@ -148,7 +148,7 @@ namespace VNLib.Utils public readonly bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format, IFormatProvider? provider) => ErrorCode.TryFormat(destination, out charsWritten, format, provider); /// - public readonly string ToString(string format, IFormatProvider? formatProvider) => ErrorCode.ToString(format, formatProvider); + public readonly string ToString(string? format, IFormatProvider? formatProvider) => ErrorCode.ToString(format, formatProvider); public static ERRNO operator +(ERRNO err, int add) => new(err.ErrorCode + add); public static ERRNO operator +(ERRNO err, nint add) => new(err.ErrorCode + add); diff --git a/lib/Utils/src/IO/FileOperations.cs b/lib/Utils/src/IO/FileOperations.cs index a8cd258..0dd95e4 100644 --- a/lib/Utils/src/IO/FileOperations.cs +++ b/lib/Utils/src/IO/FileOperations.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -32,19 +32,19 @@ namespace VNLib.Utils.IO /// /// Contains cross-platform optimized filesystem operations. /// - public static class FileOperations + public static partial class FileOperations { public const int INVALID_FILE_ATTRIBUTES = -1; - [DllImport("Shlwapi", SetLastError = true, CharSet = CharSet.Auto)] + [LibraryImport("Shlwapi", EntryPoint = "PathFileExistsW", StringMarshalling = StringMarshalling.Utf16)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return:MarshalAs(UnmanagedType.Bool)] - private static unsafe extern bool PathFileExists([MarshalAs(UnmanagedType.LPWStr)] string path); + private static unsafe partial bool PathFileExists([MarshalAs(UnmanagedType.LPWStr)] string path); - [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)] + [LibraryImport("kernel32", EntryPoint = "GetFileAttributesW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return:MarshalAs(UnmanagedType.I4)] - private static unsafe extern int GetFileAttributes([MarshalAs(UnmanagedType.LPWStr)] string path); + private static unsafe partial int GetFileAttributes([MarshalAs(UnmanagedType.LPWStr)] string path); static readonly bool IsWindows = OperatingSystem.IsWindows(); diff --git a/lib/Utils/src/Memory/ForwardOnlyReader.cs b/lib/Utils/src/Memory/ForwardOnlyReader.cs index c109e4b..8a8ebc4 100644 --- a/lib/Utils/src/Memory/ForwardOnlyReader.cs +++ b/lib/Utils/src/Memory/ForwardOnlyReader.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -23,6 +23,7 @@ */ using System; +using System.Runtime.CompilerServices; namespace VNLib.Utils.Memory { @@ -43,6 +44,7 @@ namespace VNLib.Utils.Memory /// of the specified type using the specified internal buffer /// /// The buffer to read from + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ForwardOnlyReader(ReadOnlySpan buffer) { _segment = buffer; @@ -57,6 +59,7 @@ namespace VNLib.Utils.Memory /// /// The buffer to read from /// The offset within the supplied buffer to begin the reader at + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ForwardOnlyReader(ReadOnlySpan buffer, int offset) { _segment = buffer[offset..]; diff --git a/lib/Utils/src/Memory/ForwardOnlyWriter.cs b/lib/Utils/src/Memory/ForwardOnlyWriter.cs index aa14a5f..d3c33a2 100644 --- a/lib/Utils/src/Memory/ForwardOnlyWriter.cs +++ b/lib/Utils/src/Memory/ForwardOnlyWriter.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -23,6 +23,7 @@ */ using System; +using System.Runtime.CompilerServices; namespace VNLib.Utils.Memory { @@ -55,6 +56,7 @@ namespace VNLib.Utils.Memory /// Creates a new assigning the specified buffer /// /// The buffer to write data to + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ForwardOnlyWriter(Span buffer) { Buffer = buffer; @@ -67,6 +69,7 @@ namespace VNLib.Utils.Memory /// /// The buffer to write data to /// The offset to begin the writer at + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ForwardOnlyWriter(Span buffer, int offset) { Buffer = buffer[offset..]; @@ -87,10 +90,7 @@ namespace VNLib.Utils.Memory public void Append(ReadOnlySpan data) { //Make sure the current window is large enough to buffer the new string - if (data.Length > RemainingSize) - { - throw new ArgumentOutOfRangeException(nameof(Remaining) ,"The internal buffer does not have enough buffer space"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThan(data.Length, RemainingSize, nameof(Remaining)); Span window = Buffer[Written..]; //write data to window data.CopyTo(window); @@ -106,10 +106,7 @@ namespace VNLib.Utils.Memory public void Append(T c) { //Make sure the current window is large enough to buffer the new string - if (RemainingSize == 0) - { - throw new ArgumentOutOfRangeException(nameof(Remaining), "The internal buffer does not have enough buffer space"); - } + ArgumentOutOfRangeException.ThrowIfZero(RemainingSize); //Write data to buffer and increment the buffer position Buffer[Written++] = c; } @@ -121,10 +118,7 @@ namespace VNLib.Utils.Memory /// public void Advance(int count) { - if (count > RemainingSize) - { - throw new ArgumentOutOfRangeException(nameof(Remaining), "The internal buffer does not have enough buffer space"); - } + ArgumentOutOfRangeException.ThrowIfGreaterThan(count, RemainingSize, nameof(Remaining)); Written += count; } diff --git a/lib/Utils/src/Memory/MemoryUtil.cs b/lib/Utils/src/Memory/MemoryUtil.cs index dacb6b4..ab1b0ec 100644 --- a/lib/Utils/src/Memory/MemoryUtil.cs +++ b/lib/Utils/src/Memory/MemoryUtil.cs @@ -1376,6 +1376,7 @@ namespace VNLib.Utils.Memory private static class CopyUtilCore { + const nuint _avx32ByteAlignment = 0x20u; [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public static bool MemmoveByRef(ref byte srcByte, ref byte dstByte, nuint byteCount, bool forceAcceleration) @@ -1396,7 +1397,7 @@ namespace VNLib.Utils.Memory if (forceAcceleration) { //not aligned, so we need to only copy the aligned portion - nuint remainder = byteCount % 0x20u; + nuint remainder = byteCount % _avx32ByteAlignment; nuint alignedCount = byteCount - remainder; //Copy aligned portion @@ -1439,8 +1440,7 @@ namespace VNLib.Utils.Memory return false; } } - - const nuint _avx32ByteAlignment = 0x20u; + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] private static void _avx32ByteCopy( @@ -1451,6 +1451,7 @@ namespace VNLib.Utils.Memory { Debug.Assert(Is32ByteAligned(count), "Byte count must be 32 byte aligned"); Debug.Assert(Avx2.IsSupported, "AVX2 is not supported on this platform"); + Debug.Assert(_avx32ByteAlignment == (nuint)Vector256.Count, "AVX2 vector size is not 32 bytes"); //determine the number of loops nuint loopCount = count / _avx32ByteAlignment; diff --git a/lib/Utils/src/Memory/ProcessHeap.cs b/lib/Utils/src/Memory/ProcessHeap.cs index 5d1bee6..35a2a71 100644 --- a/lib/Utils/src/Memory/ProcessHeap.cs +++ b/lib/Utils/src/Memory/ProcessHeap.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -93,16 +93,13 @@ namespace VNLib.Utils.Memory nuint bytes = checked(elements * size); //Alloc - void* newBlock = NativeMemory.Realloc(block.ToPointer(), bytes); - + nint newBlock = (nint)NativeMemory.Realloc(block.ToPointer(), bytes); + //Check - if (newBlock == null) - { - throw new NativeMemoryOutOfMemoryException("Failed to resize the allocated block"); - } + NativeMemoryOutOfMemoryException.ThrowIfNullPointer(newBlock, "Failed to resize the allocated block"); //Assign block ptr - block = (IntPtr)newBlock; + block = newBlock; } } } diff --git a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs index 164306a..6976e4f 100644 --- a/lib/Utils/src/Memory/UnsafeMemoryHandle.cs +++ b/lib/Utils/src/Memory/UnsafeMemoryHandle.cs @@ -112,6 +112,7 @@ namespace VNLib.Utils.Memory /// The heap the initial memory block belongs to /// A pointer to the unmanaged memory block /// The number of elements this block points to + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal UnsafeMemoryHandle(IUnmangedHeap heap, IntPtr initial, int elements) { _pool = null; @@ -156,10 +157,8 @@ namespace VNLib.Utils.Memory public readonly MemoryHandle Pin(int elementIndex) { //Guard size - if (elementIndex < 0 || elementIndex >= _length) - { - throw new ArgumentOutOfRangeException(nameof(elementIndex)); - } + ArgumentOutOfRangeException.ThrowIfNegative(elementIndex); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(elementIndex, _length); switch (_handleType) { diff --git a/lib/Utils/src/Memory/VnString.cs b/lib/Utils/src/Memory/VnString.cs index 6c79598..429de43 100644 --- a/lib/Utils/src/Memory/VnString.cs +++ b/lib/Utils/src/Memory/VnString.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -193,7 +193,7 @@ namespace VNLib.Utils.Memory //Make sure the stream is readable if (!stream.CanRead) { - throw new InvalidOperationException(); + throw new IOException("The input stream is not readable"); } //See if the stream is a vn memory stream if (stream is VnMemoryStream vnms) @@ -380,12 +380,9 @@ namespace VNLib.Utils.Memory { //Check Check(); - - //Check bounds - if (start < 0 || (start + count) >= Length) - { - throw new ArgumentOutOfRangeException(nameof(count)); - } + ArgumentOutOfRangeException.ThrowIfNegative(start, nameof(start)); + ArgumentOutOfRangeException.ThrowIfNegative(count, nameof(count)); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(start + count, Length, nameof(start)); //get sub-sequence slice for the current string SubSequence sub = _stringSequence.Slice((nuint)start, count); @@ -449,12 +446,8 @@ namespace VNLib.Utils.Memory /// /// representation of internal data /// - public override string ToString() - { - //Create a new - return AsSpan().ToString(); - } - + public override string ToString() => AsSpan().ToString(); + /// /// Gets the value of the character at the specified index /// @@ -471,7 +464,7 @@ namespace VNLib.Utils.Memory /// public override bool Equals(object? obj) { - if(obj == null) + if(obj is null) { return false; } @@ -484,9 +477,9 @@ namespace VNLib.Utils.Memory }; } /// - public bool Equals(VnString? other) => !ReferenceEquals(other, null) && Equals(other.AsSpan()); + public bool Equals(VnString? other) => other is not null && Equals(other.AsSpan()); /// - public bool Equals(VnString? other, StringComparison stringComparison) => !ReferenceEquals(other, null) && Equals(other.AsSpan(), stringComparison); + public bool Equals(VnString? other, StringComparison stringComparison) => other is not null && Equals(other.AsSpan(), stringComparison); /// public bool Equals(string? other) => Equals(other.AsSpan()); /// diff --git a/lib/Utils/src/Memory/VnTable.cs b/lib/Utils/src/Memory/VnTable.cs index 43e2c02..f844da8 100644 --- a/lib/Utils/src/Memory/VnTable.cs +++ b/lib/Utils/src/Memory/VnTable.cs @@ -193,12 +193,8 @@ namespace VNLib.Utils.Memory *(BufferHandle!.GetOffset(index)) = value; } } - + /// - protected override void Free() - { - //Dispose the buffer - BufferHandle?.Dispose(); - } + protected override void Free() => BufferHandle?.Dispose(); } } \ No newline at end of file diff --git a/lib/Utils/src/Memory/Win32PrivateHeap.cs b/lib/Utils/src/Memory/Win32PrivateHeap.cs index 42f0328..d2ab201 100644 --- a/lib/Utils/src/Memory/Win32PrivateHeap.cs +++ b/lib/Utils/src/Memory/Win32PrivateHeap.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -47,7 +47,7 @@ namespace VNLib.Utils.Memory /// [ComVisible(false)] [SupportedOSPlatform("Windows")] - public sealed class Win32PrivateHeap : UnmanagedHeapBase + public sealed partial class Win32PrivateHeap : UnmanagedHeapBase { private const string KERNEL_DLL = "Kernel32"; @@ -60,40 +60,40 @@ namespace VNLib.Utils.Memory public const DWORD HEAP_ZERO_MEMORY = 0x08; - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - private static extern LPVOID GetProcessHeap(); + private static partial LPVOID GetProcessHeap(); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - private static extern LPVOID HeapAlloc(IntPtr hHeap, DWORD flags, nuint dwBytes); + private static partial LPVOID HeapAlloc(IntPtr hHeap, DWORD flags, nuint dwBytes); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - private static extern LPVOID HeapReAlloc(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem, nuint dwBytes); + private static partial LPVOID HeapReAlloc(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem, nuint dwBytes); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool HeapFree(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem); + private static partial bool HeapFree(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - private static extern LPVOID HeapCreate(DWORD flOptions, nuint dwInitialSize, nuint dwMaximumSize); + private static partial LPVOID HeapCreate(DWORD flOptions, nuint dwInitialSize, nuint dwMaximumSize); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool HeapDestroy(IntPtr hHeap); + private static partial bool HeapDestroy(IntPtr hHeap); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool HeapValidate(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem); + private static partial bool HeapValidate(IntPtr hHeap, DWORD dwFlags, LPVOID lpMem); - [DllImport(KERNEL_DLL, SetLastError = true, ExactSpelling = true)] + [LibraryImport(KERNEL_DLL, SetLastError = true)] [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - private static extern nuint HeapSize(IntPtr hHeap, DWORD flags, LPVOID lpMem); + private static partial nuint HeapSize(IntPtr hHeap, DWORD flags, LPVOID lpMem); #endregion diff --git a/lib/Utils/src/VnDisposeable.cs b/lib/Utils/src/VnDisposeable.cs index 4230a13..47797ed 100644 --- a/lib/Utils/src/VnDisposeable.cs +++ b/lib/Utils/src/VnDisposeable.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -45,13 +45,7 @@ namespace VNLib.Utils /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected virtual void Check() - { - if (Disposed) - { - throw new ObjectDisposedException("Object has been disposed"); - } - } + protected virtual void Check() => ObjectDisposedException.ThrowIf(Disposed, this); /// /// Sets the internal state to diposed without calling operation. @@ -59,6 +53,7 @@ namespace VNLib.Utils /// [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void SetDisposedState() => Disposed = true; + /// protected virtual void Dispose(bool disposing) { -- cgit