Skip to content

Commit e5c79eb

Browse files
Merge branch 'nstd.os.unix.shared_lib'
2 parents c06e5d3 + 98571a4 commit e5c79eb

File tree

9 files changed

+202
-2
lines changed

9 files changed

+202
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ nstd_heap_ptr = ["nstd_alloc", "nstd_core"]
3333
nstd_io = ["nstd_alloc", "nstd_core", "nstd_string", "nstd_vec", "std"]
3434
nstd_math = ["std"]
3535
nstd_os = []
36+
nstd_os_unix_shared_lib = ["libc", "nstd_core", "nstd_os"]
3637
nstd_os_windows_alloc = [
3738
"nstd_core", "nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_Memory"
3839
]
@@ -44,5 +45,8 @@ nstd_vec = ["nstd_alloc", "nstd_core"]
4445
[dependencies]
4546
libloading = { version = "0.7", optional = true }
4647

48+
[target.'cfg(target_family = "unix")'.dependencies]
49+
libc = { version = "0.2", optional = true, default-features = false }
50+
4751
[target.'cfg(target_os = "windows")'.dependencies]
4852
windows-sys = { version = "0.42", optional = true }

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int main()
4949
- `stdout` - A handle to the standard output stream.
5050
- `math` - High level math operations.
5151
- `os` - Operating system specific functionality.
52+
- `unix` - Low level Unix like operating system support.
53+
- `shared_lib` - Provides shared library access for Unix like systems.
5254
- `windows` - OS support for Windows.
5355
- `alloc` - Low level memory allocation for Windows.
5456
- `heap` - Process heap management for Windows.

include/nstd/os.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef NSTD_OS_H
22
#define NSTD_OS_H
33
#include "os/os.h"
4+
#include "os/unix.h"
45
#include "os/windows.h"
56
#endif

include/nstd/os/unix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef NSTD_OS_UNIX_H
2+
#define NSTD_OS_UNIX_H
3+
#include "unix/shared_lib.h"
4+
#endif

include/nstd/os/unix/shared_lib.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef NSTD_OS_UNIX_SHARED_LIB_H
2+
#define NSTD_OS_UNIX_SHARED_LIB_H
3+
#include "../../core/optional.h"
4+
#include "../../nstd.h"
5+
6+
/// Represents an owned handle to a dynamically loaded library.
7+
typedef struct {
8+
/// A raw handle to the shared library.
9+
NSTDAnyMut handle;
10+
} NSTDUnixSharedLib;
11+
12+
/// Represents an optional `NSTDUnixSharedLib`.
13+
NSTDOptional(NSTDUnixSharedLib) NSTDUnixOptionalSharedLib;
14+
15+
/// Loads a dynamically loaded shared library.
16+
///
17+
/// # Parameters:
18+
///
19+
/// - `const NSTDChar *path` - A path to the shared library to load.
20+
///
21+
/// # Returns
22+
///
23+
/// `NSTDUnixOptionalSharedLib lib` - A handle to the loaded library.
24+
///
25+
/// # Safety
26+
///
27+
/// See <https://man7.org/linux/man-pages/man3/dlopen.3.html>.
28+
NSTDAPI NSTDUnixOptionalSharedLib nstd_os_unix_shared_lib_load(const NSTDChar *path);
29+
30+
/// Returns an immutable opaque pointer to a symbol in a loaded library.
31+
///
32+
/// # Parameters:
33+
///
34+
/// - `const NSTDUnixSharedLib *lib` - The shared library.
35+
///
36+
/// - `const NSTDChar *symbol` - The symbol to retrieve a pointer to.
37+
///
38+
/// # Returns
39+
///
40+
/// `NSTDAny ptr` - A pointer to the loaded symbol, null on error.
41+
///
42+
/// # Safety
43+
///
44+
/// See <https://man7.org/linux/man-pages/man3/dlsym.3.html>.
45+
NSTDAPI NSTDAny nstd_os_unix_shared_lib_get(const NSTDUnixSharedLib *lib, const NSTDChar *symbol);
46+
47+
/// Returns a mutable opaque pointer to a symbol in a loaded library.
48+
///
49+
/// # Parameters:
50+
///
51+
/// - `NSTDUnixSharedLib *lib` - The shared library.
52+
///
53+
/// - `const NSTDChar *symbol` - The symbol to retrieve a pointer to.
54+
///
55+
/// # Returns
56+
///
57+
/// `NSTDAnyMut ptr` - A pointer to the loaded symbol, null on error.
58+
///
59+
/// # Safety
60+
///
61+
/// See <https://man7.org/linux/man-pages/man3/dlsym.3.html>.
62+
NSTDAPI NSTDAnyMut nstd_os_unix_shared_lib_get_mut(NSTDUnixSharedLib *lib, const NSTDChar *symbol);
63+
64+
/// Closes and frees a loaded shared library.
65+
///
66+
/// # Parameters:
67+
///
68+
/// - `NSTDUnixSharedLib lib` - A handle to the loaded library to unload.
69+
///
70+
/// # Safety
71+
///
72+
/// See <https://man7.org/linux/man-pages/man3/dlclose.3p.html>.
73+
NSTDAPI void nstd_os_unix_shared_lib_free(NSTDUnixSharedLib lib);
74+
75+
#endif

src/os.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Operating system specific functionality.
2+
#[cfg(target_family = "unix")]
3+
pub mod unix;
24
#[cfg(target_os = "windows")]
35
pub mod windows;
46

src/os/unix.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Low level Unix like operating system support.
2+
#[cfg(feature = "nstd_os_unix_shared_lib")]
3+
#[cfg_attr(doc_cfg, doc(cfg(feature = "nstd_os_unix_shared_lib")))]
4+
pub mod shared_lib;

src/os/unix/shared_lib.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//! Provides shared library access for Unix like systems.
2+
use crate::{core::optional::NSTDOptional, NSTDAny, NSTDAnyMut, NSTDChar};
3+
use libc::{dlclose, dlopen, dlsym, RTLD_LAZY, RTLD_LOCAL};
4+
5+
/// Represents an owned handle to a dynamically loaded library.
6+
#[repr(C)]
7+
pub struct NSTDUnixSharedLib {
8+
/// A raw handle to the shared library.
9+
handle: NSTDAnyMut,
10+
}
11+
impl Drop for NSTDUnixSharedLib {
12+
/// [NSTDUnixSharedLib]'s destructor.
13+
#[inline]
14+
fn drop(&mut self) {
15+
// SAFETY: `self.handle` is never null.
16+
unsafe { dlclose(self.handle) };
17+
}
18+
}
19+
20+
/// Represents an optional `NSTDUnixSharedLib`.
21+
pub type NSTDUnixOptionalSharedLib = NSTDOptional<NSTDUnixSharedLib>;
22+
23+
/// Loads a dynamically loaded shared library.
24+
///
25+
/// # Parameters:
26+
///
27+
/// - `const NSTDChar *path` - A path to the shared library to load.
28+
///
29+
/// # Returns
30+
///
31+
/// `NSTDUnixOptionalSharedLib lib` - A handle to the loaded library.
32+
///
33+
/// # Safety
34+
///
35+
/// See <https://man7.org/linux/man-pages/man3/dlopen.3.html>.
36+
#[inline]
37+
#[cfg_attr(feature = "clib", no_mangle)]
38+
pub unsafe extern "C" fn nstd_os_unix_shared_lib_load(
39+
path: *const NSTDChar,
40+
) -> NSTDUnixOptionalSharedLib {
41+
let handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL);
42+
if handle.is_null() {
43+
return NSTDOptional::None;
44+
}
45+
NSTDOptional::Some(NSTDUnixSharedLib { handle })
46+
}
47+
48+
/// Returns an immutable opaque pointer to a symbol in a loaded library.
49+
///
50+
/// # Parameters:
51+
///
52+
/// - `const NSTDUnixSharedLib *lib` - The shared library.
53+
///
54+
/// - `const NSTDChar *symbol` - The symbol to retrieve a pointer to.
55+
///
56+
/// # Returns
57+
///
58+
/// `NSTDAny ptr` - A pointer to the loaded symbol, null on error.
59+
///
60+
/// # Safety
61+
///
62+
/// See <https://man7.org/linux/man-pages/man3/dlsym.3.html>.
63+
#[inline]
64+
#[cfg_attr(feature = "clib", no_mangle)]
65+
pub unsafe extern "C" fn nstd_os_unix_shared_lib_get(
66+
lib: &NSTDUnixSharedLib,
67+
symbol: *const NSTDChar,
68+
) -> NSTDAny {
69+
dlsym(lib.handle, symbol)
70+
}
71+
72+
/// Returns a mutable opaque pointer to a symbol in a loaded library.
73+
///
74+
/// # Parameters:
75+
///
76+
/// - `NSTDUnixSharedLib *lib` - The shared library.
77+
///
78+
/// - `const NSTDChar *symbol` - The symbol to retrieve a pointer to.
79+
///
80+
/// # Returns
81+
///
82+
/// `NSTDAnyMut ptr` - A pointer to the loaded symbol, null on error.
83+
///
84+
/// # Safety
85+
///
86+
/// See <https://man7.org/linux/man-pages/man3/dlsym.3.html>.
87+
#[inline]
88+
#[cfg_attr(feature = "clib", no_mangle)]
89+
pub unsafe extern "C" fn nstd_os_unix_shared_lib_get_mut(
90+
lib: &mut NSTDUnixSharedLib,
91+
symbol: *const NSTDChar,
92+
) -> NSTDAnyMut {
93+
dlsym(lib.handle, symbol)
94+
}
95+
96+
/// Closes and frees a loaded shared library.
97+
///
98+
/// # Parameters:
99+
///
100+
/// - `NSTDUnixSharedLib lib` - A handle to the loaded library to unload.
101+
///
102+
/// # Safety
103+
///
104+
/// See <https://man7.org/linux/man-pages/man3/dlclose.3p.html>.
105+
#[inline]
106+
#[cfg_attr(feature = "clib", no_mangle)]
107+
#[allow(unused_variables)]
108+
pub unsafe extern "C" fn nstd_os_unix_shared_lib_free(lib: NSTDUnixSharedLib) {}

test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

33
FEATURES = ("nstd_alloc", "nstd_core", "nstd_cstring", "nstd_fs", "nstd_heap_ptr", "nstd_io",
4-
"nstd_math", "nstd_os_windows_alloc", "nstd_shared_lib", "nstd_shared_ptr",
5-
"nstd_string", "nstd_vec")
4+
"nstd_math", "nstd_os_unix_shared_lib", "nstd_os_windows_alloc", "nstd_shared_lib",
5+
"nstd_shared_ptr", "nstd_string", "nstd_vec")
66

77
TARGETS = ("x86_64-pc-windows-msvc", "x86_64-apple-darwin",
88
"x86_64-unknown-linux-gnu", "x86_64-apple-ios", "x86_64-linux-android")

0 commit comments

Comments
 (0)