aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-01-15 19:32:30 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2024-01-15 19:32:30 -0500
commitf4e4db7c5320976406feb252ae8f8bdbe9b3e351 (patch)
treebcec74e7fde4b083770dccedf7e93202d2eb657d /lib
parent1914bdb0e506aac53de6b886648b033d0229e9a1 (diff)
setup new lib import code attributes
Diffstat (limited to 'lib')
-rw-r--r--lib/Utils/src/ERRNO.cs2
-rw-r--r--lib/Utils/src/IO/FileOperations.cs12
-rw-r--r--lib/Utils/src/Memory/ForwardOnlyReader.cs5
-rw-r--r--lib/Utils/src/Memory/ForwardOnlyWriter.cs20
-rw-r--r--lib/Utils/src/Memory/MemoryUtil.cs7
-rw-r--r--lib/Utils/src/Memory/ProcessHeap.cs13
-rw-r--r--lib/Utils/src/Memory/UnsafeMemoryHandle.cs7
-rw-r--r--lib/Utils/src/Memory/VnString.cs27
-rw-r--r--lib/Utils/src/Memory/VnTable.cs8
-rw-r--r--lib/Utils/src/Memory/Win32PrivateHeap.cs36
-rw-r--r--lib/Utils/src/VnDisposeable.cs11
11 files changed, 63 insertions, 85 deletions
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<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) => ErrorCode.TryFormat(destination, out charsWritten, format, provider);
///<inheritdoc/>
- 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
/// <summary>
/// Contains cross-platform optimized filesystem operations.
/// </summary>
- 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
/// </summary>
/// <param name="buffer">The buffer to read from</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ForwardOnlyReader(ReadOnlySpan<T> buffer)
{
_segment = buffer;
@@ -57,6 +59,7 @@ namespace VNLib.Utils.Memory
/// </summary>
/// <param name="buffer">The buffer to read from</param>
/// <param name="offset">The offset within the supplied buffer to begin the reader at</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ForwardOnlyReader(ReadOnlySpan<T> 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 <see cref="ForwardOnlyWriter{T}"/> assigning the specified buffer
/// </summary>
/// <param name="buffer">The buffer to write data to</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ForwardOnlyWriter(Span<T> buffer)
{
Buffer = buffer;
@@ -67,6 +69,7 @@ namespace VNLib.Utils.Memory
/// </summary>
/// <param name="buffer">The buffer to write data to</param>
/// <param name="offset">The offset to begin the writer at</param>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ForwardOnlyWriter(Span<T> buffer, int offset)
{
Buffer = buffer[offset..];
@@ -87,10 +90,7 @@ namespace VNLib.Utils.Memory
public void Append(ReadOnlySpan<T> 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<T> 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
/// <exception cref="ArgumentOutOfRangeException"></exception>
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<byte>.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
/// <param name="heap">The heap the initial memory block belongs to</param>
/// <param name="initial">A pointer to the unmanaged memory block</param>
/// <param name="elements">The number of elements this block points to</param>
+ [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<char> sub = _stringSequence.Slice((nuint)start, count);
@@ -449,12 +446,8 @@ namespace VNLib.Utils.Memory
/// </summary>
/// <returns><see cref="string"/> representation of internal data</returns>
/// <exception cref="ObjectDisposedException"></exception>
- public override string ToString()
- {
- //Create a new
- return AsSpan().ToString();
- }
-
+ public override string ToString() => AsSpan().ToString();
+
/// <summary>
/// Gets the value of the character at the specified index
/// </summary>
@@ -471,7 +464,7 @@ namespace VNLib.Utils.Memory
///<inheritdoc/>
public override bool Equals(object? obj)
{
- if(obj == null)
+ if(obj is null)
{
return false;
}
@@ -484,9 +477,9 @@ namespace VNLib.Utils.Memory
};
}
///<inheritdoc/>
- public bool Equals(VnString? other) => !ReferenceEquals(other, null) && Equals(other.AsSpan());
+ public bool Equals(VnString? other) => other is not null && Equals(other.AsSpan());
///<inheritdoc/>
- 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);
///<inheritdoc/>
public bool Equals(string? other) => Equals(other.AsSpan());
///<inheritdoc/>
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;
}
}
-
+
///<inheritdoc/>
- 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
/// </remarks>
[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
/// </summary>
/// <exception cref="ObjectDisposedException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- protected virtual void Check()
- {
- if (Disposed)
- {
- throw new ObjectDisposedException("Object has been disposed");
- }
- }
+ protected virtual void Check() => ObjectDisposedException.ThrowIf(Disposed, this);
/// <summary>
/// Sets the internal state to diposed without calling <see cref="Free"/> operation.
@@ -59,6 +53,7 @@ namespace VNLib.Utils
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void SetDisposedState() => Disposed = true;
+
///<inheritdoc/>
protected virtual void Dispose(bool disposing)
{