forked from mellinoe/vk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor libary loader and fix mellinoe#34
- Loading branch information
Showing
8 changed files
with
273 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Vulkan | ||
{ | ||
internal class NativeLibDLGeneric : NativeLinkerLibDL.INativeLibDL | ||
{ | ||
public int dlclose(nint handle) | ||
{ | ||
return LibDL.dlclose(handle); | ||
} | ||
|
||
public string dlerror() | ||
{ | ||
return LibDL.dlerror(); | ||
} | ||
|
||
public nint dlopen(string fileName, int flags) | ||
{ | ||
return LibDL.dlopen(fileName, flags); | ||
} | ||
|
||
public nint dlsym(nint handle, string name) | ||
{ | ||
return LibDL.dlsym(handle, name); | ||
} | ||
|
||
private static class LibDL | ||
{ | ||
private const string Library = "libdl"; | ||
|
||
[DllImport(Library)] | ||
public static extern IntPtr dlopen(string fileName, int flags); | ||
|
||
[DllImport(Library)] | ||
public static extern IntPtr dlsym(IntPtr handle, string name); | ||
|
||
[DllImport(Library)] | ||
public static extern int dlclose(IntPtr handle); | ||
|
||
[DllImport(Library)] | ||
public static extern string dlerror(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Vulkan | ||
{ | ||
internal class NativeLibDLLinux : NativeLinkerLibDL.INativeLibDL | ||
{ | ||
public int dlclose(nint handle) | ||
{ | ||
return LibDL.dlclose(handle); | ||
} | ||
|
||
public string dlerror() | ||
{ | ||
return LibDL.dlerror(); | ||
} | ||
|
||
public nint dlopen(string fileName, int flags) | ||
{ | ||
return LibDL.dlopen(fileName, flags); | ||
} | ||
|
||
public nint dlsym(nint handle, string name) | ||
{ | ||
return LibDL.dlsym(handle, name); | ||
} | ||
|
||
private static class LibDL | ||
{ | ||
private const string Library = "libdl.so.2"; | ||
|
||
[DllImport(Library)] | ||
public static extern IntPtr dlopen(string fileName, int flags); | ||
|
||
[DllImport(Library)] | ||
public static extern IntPtr dlsym(IntPtr handle, string name); | ||
|
||
[DllImport(Library)] | ||
public static extern int dlclose(IntPtr handle); | ||
|
||
[DllImport(Library)] | ||
public static extern string dlerror(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Vulkan | ||
{ | ||
|
||
public interface INativeDynamicLinker | ||
{ | ||
public IntPtr LoadLibrary(string libraryName); | ||
public void FreeLibrary(IntPtr libraryHandle); | ||
public IntPtr LoadFunction(IntPtr libraryHandle, string functionName); | ||
|
||
private static INativeDynamicLinker _linker; | ||
|
||
public static INativeDynamicLinker GetInstance() | ||
{ | ||
if (_linker != null) return _linker; | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return _linker = new NativeLinkerWindows(); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) | ||
#if NET5_0 | ||
|| OperatingSystem.IsAndroid() | ||
#endif | ||
) | ||
{ | ||
return new NativeLinkerLibDL(new NativeLibDLGeneric()); | ||
} | ||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return new NativeLinkerLibDL(new NativeLibDLLinux()); | ||
} | ||
else | ||
{ | ||
throw new PlatformNotSupportedException("Cannot load native libraries on this platform: " + RuntimeInformation.OSDescription); | ||
} | ||
} | ||
} | ||
|
||
public class NativeLibrary : IDisposable | ||
{ | ||
public static NativeLibrary Load(string libraryName) | ||
{ | ||
if (libraryName == null) | ||
throw new ArgumentNullException(nameof(libraryName)); | ||
|
||
INativeDynamicLinker linker = INativeDynamicLinker.GetInstance(); | ||
IntPtr libraryHandle = linker.LoadLibrary(libraryName); | ||
|
||
if (libraryHandle == IntPtr.Zero) | ||
throw new InvalidOperationException("Could not load " + libraryName); | ||
|
||
return new NativeLibrary(libraryHandle, linker); | ||
} | ||
|
||
private readonly IntPtr _libraryHandle; | ||
private readonly INativeDynamicLinker _linker; | ||
|
||
public IntPtr NativeHandle => _libraryHandle; | ||
|
||
private NativeLibrary(IntPtr libraryHandle, INativeDynamicLinker linker) | ||
{ | ||
_libraryHandle = libraryHandle; | ||
_linker = linker; | ||
} | ||
|
||
public IntPtr LoadFunctionPointer(string functionName) | ||
{ | ||
if (functionName == null) | ||
{ | ||
throw new ArgumentNullException(nameof(functionName)); | ||
} | ||
|
||
return _linker.LoadFunction(_libraryHandle, functionName); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_linker.FreeLibrary(_libraryHandle); | ||
} | ||
} | ||
} |
Oops, something went wrong.