KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
library.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/library.h
4 Copyright (C) 2003 Megan Potter
5 Copyright (C) 2024 Ruslan Rostovtsev
6
7*/
8
9/** \file kos/library.h
10 \brief Dynamically loadable library support.
11 \ingroup system_libraries
12
13 This file contains definitions for accessing loadable libraries at runtime.
14 Each library has a name and a version number that it can be referenced by.
15 One must be careful with these dynamic libraries as there is no private
16 storage per instance, and all memory space is shared.
17
18 Libraries can both export and import symbols. Imported symbols may require
19 other libraries to be loaded earlier. Libraries are reference counted so
20 that they can be opened multiple times without actually loading them
21 multiple times, and so that a close will act as expected in situations like
22 this.
23
24 \author Megan Potter
25 \author Ruslan Rostovtsev
26*/
27
28#ifndef __KOS_LIBRARY_H
29#define __KOS_LIBRARY_H
30
31#include <kos/cdefs.h>
32__BEGIN_DECLS
33
34#include <kos/elf.h>
35#include <kos/fs.h>
36
37/** \defgroup system_libraries Libraries
38 \brief API for managing dynamically loaded libraries
39 \ingroup system
40
41 @{
42*/
43
44/** \cond */
45/* Pre-define list/queue types */
46struct klibrary;
47TAILQ_HEAD(klqueue, klibrary);
48LIST_HEAD(kllist, klibrary);
49/** \endcond */
50
51typedef int libid_t; /**< \brief Library ID type. */
52
53/** \brief Loaded library structure.
54
55 This structure represents a single loaded library. Each library is
56 essentially a loaded binary of code and a set of exported entry points that
57 are standardized.
58
59 Each loaded library should export at least the functions described in this
60 structure:
61 \li const char *lib_get_name()
62 \li uint32_t %lib_get_version()
63 \li int lib_open(struct klibrary *lib)
64 \li int lib_close(struct klibrary *lib)
65
66 You should not modify any members of this structure yourself (except if you
67 are implementing a library).
68
69 \headerfile kos/library.h
70*/
71typedef struct klibrary {
72 /** \brief Library list handle.
73
74 Contrary to what doxygen might think, this is not a function.
75 */
76 LIST_ENTRY(klibrary) list;
77
78 /** \brief Library ID (assigned at runtime). */
80
81 /** \brief Library flags. */
83
84 /** \brief ELF image for this library.
85
86 This can be used to look up additional entry points in the library.
87 */
89
90 /** \brief Library reference count.
91
92 This value is incremented every time the library is opened, and
93 decremented each time it is closed. Once the library's reference count
94 hits 0, a close will actually destroy the library.
95 */
96 int refcnt;
97
98 /* Standard library entry points. Every loaded library must provide
99 at least these things. */
100
101 /** \brief Retrieve the library's symbolic name.
102
103 This function must be implemented by all loadable libraries to fetch the
104 library's symbolic name. This function must work before calling
105 lib_open() on the library.
106
107 \return The library's symbolic name
108 */
109 const char *(*lib_get_name)(void);
110
111 /** \brief Retrieve the library's version.
112
113 This function must be implemented by all loadble libraries to fetch the
114 library's version number. This function must work before calling
115 lib_open() on the library.
116
117 \return The library's version number
118 */
119 uint32_t(*lib_get_version)(void);
120
121 /** \brief Open a library.
122
123 This function must be implemented by all loadable libraries to
124 initialize the library on load.
125
126 \param lib The library structure
127 \return Values >= 0 indicate success, < 0 indicates failure.
128 A failure on the first lib_open is indicative that
129 the library should be removed from memory.
130 */
131 int (*lib_open)(struct klibrary *lib);
132
133 /** \brief Close an opened library.
134
135 This function must be implemented by all loadable libraries to close and
136 deinitialize a library.
137
138 \param lib The library structure
139 \return Values >= 0 indicate success, < 0 indicates failure
140 */
141 int (*lib_close)(struct klibrary *lib);
142} klibrary_t;
143
144/* Library flag values */
145#define LIBRARY_DEFAULTS 0 /**< \brief Defaults: no flags */
146
147/** \cond */
148/* Library list; note: do not manipulate directly */
149extern struct kllist library_list;
150/** \endcond */
151
152/** \brief Look up a library by ID.
153
154 This function looks up a library by its library ID.
155
156 \param libid The library ID to look up
157 \return The specified library, or NULL if not found
158*/
160
161/** \brief Create a new library shell.
162
163 This function creates a new library, adding it to the list of libraries. You
164 generally should not call this function directly, unless you have some good
165 reason to do so.
166
167 \param flags Flags to create the library with.
168 \return The newly created library, or NULL on error
169*/
171
172/** \brief Destroy a library.
173
174 This function will take a loaded library and destroy it, unloading it
175 completely. Generally, you should not call this function, but rather use
176 library_close() to make sure that you're not closing something that is still
177 in use.
178
179 \param lib The library to close
180 \retval 0 Upon successfully destroying the library
181*/
183
184/** \brief Try to open a library by name.
185
186 This function attempts to open a library by its name. If it cannot be found
187 by name, this function will attempt to open by filename. If it cannot be found
188 by filename, this function will attempt to load the library from the specified
189 filename. If the library is already opened, this may only involve increasing
190 the reference count.
191
192 \param name The symbolic name of the library
193 \param fn The filename to load the library from
194 \return A handle to the library, or NULL on error with errno
195 set as appropriate
196
197 \par Error Conditions:
198 \em EINVAL - the library was found or loaded, but invalid \n
199 \em ENOMEM - out of memory \n
200 \em ENOENT - library not found and no filename given
201*/
202klibrary_t *library_open(const char *name, const char *fn);
203
204/** \brief Look up a library by name.
205
206 This is useful if you want to reuse opened library and
207 this is used for library_open().
208
209 \param name The name of the library to search for
210 \return The library, if found. NULL if not found, errno set
211 as appropriate.
212
213 \par Error Conditions:
214 \em ENOENT - the library was not found
215*/
216klibrary_t *library_lookup(const char *name);
217
218/** \brief Look up a library by filename.
219
220 This is useful if you want to reuse opened library and
221 this is used for library_open().
222
223 \param fn The filename of the library to search for
224 \return The library, if found. NULL if not found, errno set
225 as appropriate.
226
227 \par Error Conditions:
228 \em ENOENT - the library was not found
229*/
231
232/** \brief Close a previously opened library.
233
234 This function will close the specified library. This may involve simply
235 decrementing its reference count, however, it may also involve actually
236 closing and freeing the library. Thus, don't try to use the library after
237 calling this without reopening it first.
238
239 \param lib The library to close
240 \retval 0 On success
241 \retval -1 On error, errno may be set to an appropriate code
242
243 \par Error Conditions:
244 \em EINVAL - the library is not valid
245*/
247
248/** \brief Retrieve the specified library's runtime-assigned ID.
249 \param lib The library to examine
250 \return The library's ID, or -1 on error
251
252 \par Error Conditions:
253 \em EINVAL - the library is not valid
254*/
256
257/** \brief Retrieve the specified library's reference count.
258 \param lib The library to examine
259 \return The library's reference count, or -1 on error
260
261 \par Error Conditions:
262 \em EINVAL - the library is not valid
263*/
265
266/** \brief Retrieve the specified library's name.
267 \param lib The library to examine
268 \return The library's symbolic name, or NULL on error
269
270 \par Error Conditions:
271 \em EINVAL - the library is not valid
272*/
273const char *library_get_name(klibrary_t *lib);
274
275/** \brief Retrieve the specified library's version.
276 \param lib The library to examine
277 \return The library's version number, or 0 on error
278
279 \par Error Conditions
280 \em EINVAL - the library is not valid
281*/
283
284/** \cond */
285/* Init */
286void library_init(void);
287
288/* Shutdown */
289void library_shutdown(void);
290/** \endcond */
291
292/** @} */
293
294__END_DECLS
295
296#endif /* __KOS_LIBRARY_H */
Various common macros used throughout the codebase.
ELF binary loading support.
Virtual filesystem support.
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
const char * library_get_name(klibrary_t *lib)
Retrieve the specified library's name.
klibrary_t * library_open(const char *name, const char *fn)
Try to open a library by name.
int libid_t
Library ID type.
Definition library.h:51
libid_t library_get_libid(klibrary_t *lib)
Retrieve the specified library's runtime-assigned ID.
int library_close(klibrary_t *lib)
Close a previously opened library.
klibrary_t * library_lookup_fn(const char *fn)
Look up a library by filename.
uint32_t library_get_version(klibrary_t *lib)
Retrieve the specified library's version.
klibrary_t * library_lookup(const char *name)
Look up a library by name.
klibrary_t * library_by_libid(libid_t libid)
Look up a library by ID.
int library_destroy(klibrary_t *lib)
Destroy a library.
klibrary_t * library_create(int flags)
Create a new library shell.
int library_get_refcnt(klibrary_t *lib)
Retrieve the specified library's reference count.
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
Kernel-specific definition of a loaded ELF binary.
Definition elf.h:353
Loaded library structure.
Definition library.h:71
elf_prog_t image
ELF image for this library.
Definition library.h:88
libid_t libid
Library ID (assigned at runtime).
Definition library.h:79
uint32_t flags
Library flags.
Definition library.h:82
int refcnt
Library reference count.
Definition library.h:96
LIST_ENTRY(klibrary) list
Library list handle.