diff options
Diffstat (limited to 'lib/Utils/src/Native')
-rw-r--r-- | lib/Utils/src/Native/NativeMemoryOutOfMemoryException.cs | 16 | ||||
-rw-r--r-- | lib/Utils/src/Native/SafeLibraryHandle.cs | 11 |
2 files changed, 20 insertions, 7 deletions
diff --git a/lib/Utils/src/Native/NativeMemoryOutOfMemoryException.cs b/lib/Utils/src/Native/NativeMemoryOutOfMemoryException.cs index 5a836da..d257233 100644 --- a/lib/Utils/src/Native/NativeMemoryOutOfMemoryException.cs +++ b/lib/Utils/src/Native/NativeMemoryOutOfMemoryException.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -40,5 +40,19 @@ namespace VNLib.Utils.Native public NativeMemoryOutOfMemoryException() { } + + /// <summary> + /// Throws an <see cref="NativeMemoryOutOfMemoryException"/> if the pointer is null + /// </summary> + /// <param name="value">The pointer value to test</param> + /// <param name="message">The message to use if the pointer is null</param> + /// <exception cref="NativeMemoryOutOfMemoryException"></exception> + public static void ThrowIfNullPointer(nint value, string? message = null) + { + if (value == 0) + { + throw new NativeMemoryOutOfMemoryException(message ?? "Failed to allocate or reallocte memory region"); + } + } } } diff --git a/lib/Utils/src/Native/SafeLibraryHandle.cs b/lib/Utils/src/Native/SafeLibraryHandle.cs index 4772bd4..b3594e1 100644 --- a/lib/Utils/src/Native/SafeLibraryHandle.cs +++ b/lib/Utils/src/Native/SafeLibraryHandle.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Utils @@ -169,11 +169,10 @@ namespace VNLib.Utils.Native { //Increment handle count before obtaining a method bool success = false; - DangerousAddRef(ref success); - if (!success) - { - throw new ObjectDisposedException("The libary has been released!"); - } + DangerousAddRef(ref success); + + ObjectDisposedException.ThrowIf(success == false, "The libary has been released!"); + try { //Get the method pointer |