plibsys 0.0.5
|
Shared library loader. More...
Go to the source code of this file.
Typedefs | |
typedef struct PLibraryLoader_ | PLibraryLoader |
Opaque data structure to handle a shared library. | |
typedef void(* | PFuncAddr) (void) |
Pointer to a function address. | |
Functions | |
P_LIB_API PLibraryLoader * | p_library_loader_new (const pchar *path) |
Loads a shared library. | |
P_LIB_API PFuncAddr | p_library_loader_get_symbol (PLibraryLoader *loader, const pchar *sym) |
Gets a pointer to a symbol in the loaded shared library. | |
P_LIB_API void | p_library_loader_free (PLibraryLoader *loader) |
Frees memory and allocated resources of PLibraryLoader. | |
P_LIB_API pchar * | p_library_loader_get_last_error (PLibraryLoader *loader) |
Gets the last occurred error. | |
P_LIB_API pboolean | p_library_loader_is_ref_counted (void) |
Checks whether library loading subsystem uses reference counting. | |
Shared library loader.
All modern operating systems support dynamic loadable objects. Such objects are compiled with special flags and can be loaded by other programs and libraries later at the runtime. These loadable objects often called as the shared libraries, though some platforms even allow to treat the program binary as a loadable object, too.
When the program is linked with a shared library its dependency would be resolved by the operating system automatically before starting the program. But in some circumstances you may need to load a shared library object explicitly (i.e. implementing a plugin subsystem, checking for API availability).
All functions and variables which a shared library is exporting are called symbols. Usually only the exported symbols are available from outside the shared library. Actually all those symbols represent a library API.
Use p_library_loader_new() to load a shared library and p_library_loader_get_symbol() to retrieve a pointer to a symbol within it. Close the library after usage with p_library_loader_free().
Please note the following platform specific differences:
Definition in file plibraryloader.h.
typedef void(* PFuncAddr) (void) |
Pointer to a function address.
Definition at line 83 of file plibraryloader.h.
typedef struct PLibraryLoader_ PLibraryLoader |
Opaque data structure to handle a shared library.
Definition at line 80 of file plibraryloader.h.
P_LIB_API void p_library_loader_free | ( | PLibraryLoader * | loader | ) |
Frees memory and allocated resources of PLibraryLoader.
loader | PLibraryLoader object to free. |
P_LIB_API pchar * p_library_loader_get_last_error | ( | PLibraryLoader * | loader | ) |
Gets the last occurred error.
loader | PLibraryLoader object to get error for. |
loader
parameter was added. The NULL result may indicate that no error was occurred since the last call.
Different operating systems have different behavior on error indicating. Some systems reset an error status before the call, some do not. Some systems write the successful call result (usually zero) to the error status, thus resetting an error from the previous call.
Some operating systems may return last error even if library handler was not created. In that case try to pass NULL value as a parameter.
P_LIB_API PFuncAddr p_library_loader_get_symbol | ( | PLibraryLoader * | loader, |
const pchar * | sym ) |
Gets a pointer to a symbol in the loaded shared library.
loader | Pointer to the loaded shared library handle. |
sym | Name of the symbol. |
Since the symbol may have a NULL value, the returned NULL value from this call actually doesn't mean the failed result. You can additionally check the error result using p_library_loader_get_last_error().
Checks whether library loading subsystem uses reference counting.
When reference counting is supported, the same shared library can be opened several times, but it would be completely unloaded from the memory only when the last reference to it is removed.
P_LIB_API PLibraryLoader * p_library_loader_new | ( | const pchar * | path | ) |
Loads a shared library.
path | Path to the shared library file. |
If you are loading the already loaded shared library, an operating system increments corresponding reference count and decrements it after freeing PLibraryLoader, thus the shared library would be unloaded from the address space only when the counter becomes zero.