Mojo struct
OwnedDLHandle
struct OwnedDLHandle
Represents an owned handle to a dynamically linked library with RAII semantics.
Unlike DLHandle which is a non-owning reference, OwnedDLHandle owns the
library handle and automatically calls dlclose() when the object is
destroyed. This prevents resource leaks and double-free bugs.
Example usage:
from sys.ffi import OwnedDLHandle
fn main() raises:
var lib = OwnedDLHandle("libm.so")
var sqrt = lib.get_function[fn(Float64) -> Float64]("sqrt")
print(sqrt(4.0)) # Prints: 2.0
# Library automatically closed when lib goes out of scopeImplemented traits
AnyType,
Movable,
UnknownDestructibility
Aliases
__del__is_trivial
alias __del__is_trivial = False
__moveinit__is_trivial
alias __moveinit__is_trivial = True
Methods
__init__
__init__(out self, flags: Int = DEFAULT_RTLD)
Initialize an owned handle to all global symbols in the current process.
Args:
- flags (
Int): The flags to load the dynamic library.
Raises:
If dlopen(nullptr, flags) fails.
__init__[PathLike: PathLike, //](out self, path: PathLike, flags: Int = DEFAULT_RTLD)
Initialize an OwnedDLHandle by loading the dynamic library at the given path.
Parameters:
- PathLike (
PathLike): The type conforming to theos.PathLiketrait.
Args:
- path (
PathLike): The path to the dynamic library file. - flags (
Int): The flags to load the dynamic library.
Raises:
If dlopen(path, flags) fails.
__del__
__del__(var self)
Unload the associated dynamic library.
This automatically calls dlclose() on the underlying library handle.
__bool__
__bool__(self) -> Bool
Checks if the handle is valid.
Returns:
Bool: True if the handle is not null and False otherwise.
borrow
borrow(self) -> DLHandle
Returns a non-owning reference to this handle.
The returned DLHandle does not own the library and should not be
used after this OwnedDLHandle is destroyed.
Returns:
DLHandle: A non-owning reference to the library handle.
check_symbol
check_symbol(self, var name: String) -> Bool
Check that the symbol exists in the dynamic library.
Args:
- name (
String): The symbol to check.
Returns:
Bool: True if the symbol exists.
get_function
get_function[result_type: AnyTrivialRegType](self, var name: String) -> result_type
Returns a handle to the function with the given name in the dynamic library.
Parameters:
- result_type (
AnyTrivialRegType): The type of the function pointer to return.
Args:
- name (
String): The name of the function to get the handle for.
Returns:
result_type: A handle to the function.
get_symbol
get_symbol[result_type: AnyType](self, name: StringSlice[origin]) -> LegacyUnsafePointer[result_type]
Returns a pointer to the symbol with the given name in the dynamic library.
Parameters:
- result_type (
AnyType): The type of the symbol to return.
Args:
- name (
StringSlice): The name of the symbol to get the handle for.
Returns:
LegacyUnsafePointer: A pointer to the symbol.
get_symbol[result_type: AnyType](self, *, cstr_name: LegacyUnsafePointer[Int8, mut=False, origin=origin]) -> LegacyUnsafePointer[result_type]
Returns a pointer to the symbol with the given name in the dynamic library.
Parameters:
- result_type (
AnyType): The type of the symbol to return.
Args:
- cstr_name (
LegacyUnsafePointer): The name of the symbol to get the handle for.
Returns:
LegacyUnsafePointer: A pointer to the symbol.
call
call[name: StringSlice[StaticConstantOrigin], return_type: AnyTrivialRegType = NoneType, *T: AnyType = *?](self, *args: *T) -> return_type
Call a function with any amount of arguments.
Parameters:
- name (
StringSlice): The name of the function. - return_type (
AnyTrivialRegType): The return type of the function. - *T (
AnyType): The types ofargs.
Args:
- *args (
*T): The arguments.
Returns:
return_type: The result.
call[name: StringSlice[StaticConstantOrigin], return_type: AnyTrivialRegType = NoneType](self, args: VariadicPack[is_owned, origin, AnyType, element_types]) -> return_type
Call a function with any amount of arguments.
Parameters:
- name (
StringSlice): The name of the function. - return_type (
AnyTrivialRegType): The return type of the function.
Args:
- args (
VariadicPack): The arguments.
Returns:
return_type: The result.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!