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
6*/
7
8/** \file kos/library.h
9 \brief Dynamically loadable library support.
10 \ingroup system_libraries
11
12 This file contains definitions for accessing loadable libraries at runtime.
13 Each library has a name and a version number that it can be referenced by.
14 One must be careful with these dynamic libraries as there is no private
15 storage per instance, and all memory space is shared.
16
17 Libraries can both export and import symbols. Imported symbols may require
18 other libraries to be loaded earlier. Libraries are reference counted so
19 that they can be opened multiple times without actually loading them
20 multiple times, and so that a close will act as expected in situations like
21 this.
22
23 \author Megan Potter
24*/
25
26#ifndef __KOS_LIBRARY_H
27#define __KOS_LIBRARY_H
28
29#include <sys/cdefs.h>
30__BEGIN_DECLS
31
32#include <kos/thread.h>
33#include <kos/elf.h>
34#include <kos/fs.h>
35
36/** \defgroup system_libraries Libraries
37 \brief API for managing dynamically loaded libraries
38 \ingroup system
39
40 @{
41*/
42
43/** \cond */
44/* Pre-define list/queue types */
45struct klibrary;
46TAILQ_HEAD(klqueue, klibrary);
47LIST_HEAD(kllist, klibrary);
48/** \endcond */
49
50/* Thread IDs are ok for us */
51typedef tid_t 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 %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(*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. If the library is already opened, this
125 may only involve increasing the reference count.
126
127 \param lib The library structure
128 \return Values >= 0 indicate success, < 0 indicates failure.
129 A failure on the first lib_open is indicative that
130 the library should be removed from memory.
131 */
132 int (*lib_open)(struct klibrary * lib);
133
134 /** \brief Close an opened library.
135
136 This function must be implemented by all loadable libraries to close and
137 deinitialize a library. If the library's reference count is > 1 when
138 this function is called, this may involve simply decrementing the
139 reference count.
140
141 \param lib The library structure
142 \return Values >= 0 indicate success, < 0 indicates failure
143 */
144 int (*lib_close)(struct klibrary * lib);
145} klibrary_t;
146
147/* Library flag values */
148#define LIBRARY_DEFAULTS 0 /**< \brief Defaults: no flags */
149
150/** \cond */
151/* Library list; note: do not manipulate directly */
152extern struct kllist library_list;
153/** \endcond */
154
155/** \brief Look up a library by ID.
156
157 This function looks up a library by its library ID.
158
159 \param libid The library ID to look up
160 \return The specified library, or NULL if not found
161*/
163
164/** \brief Create a new library shell.
165
166 This function creates a new library, adding it to the list of libraries. You
167 generally should not call this function directly, unless you have some good
168 reason to do so.
169
170 \param flags Flags to create the library with.
171 \return The newly created library, or NULL on error
172*/
174
175/** \brief Destroy a library.
176
177 This function will take a loaded library and destroy it, unloading it
178 completely. Generally, you should not call this function, but rather use
179 library_close() to make sure that you're not closing something that is still
180 in use.
181
182 \param lib The library to close
183 \retval 0 Upon successfully destroying the library
184*/
186
187/** \brief Try to open a library by name.
188
189 This function attempts to open a library by its name. If it cannot be found
190 by name, this function will attempt to load the library from the specified
191 filename.
192
193 \param name The symbolic name of the library
194 \param fn The filename to load the library from
195 \return A handle to the library, or NULL on error with errno
196 set as appropriate
197
198 \par Error Conditions:
199 \em EINVAL - the library was found or loaded, but invalid \n
200 \em ENOMEM - out of memory \n
201 \em ENOENT - library not found and no filename given
202*/
203klibrary_t * library_open(const char * name, const char * fn);
204
205/** \brief Look up a library by name.
206
207 This function looks up a library by its symbolic name without trying to
208 actually load or open it. This is useful if you want to open a library but
209 not keep around a handle to it (which isn't necessarily encouraged).
210
211 \param name The name of the library to search for
212 \return The library, if found. NULL if not found, errno set
213 as appropriate.
214
215 \par Error Conditions:
216 \em ENOENT - the library was not found
217*/
218klibrary_t * library_lookup(const char * name);
219
220/** \brief Close a previously opened library.
221
222 This function will close the specified library. This may involve simply
223 decrementing its reference count, however, it may also involve actually
224 closing and freeing the library. Thus, don't try to use the library after
225 calling this without reopening it first.
226
227 \param lib The library to close
228 \retval 0 On success
229 \retval -1 On error, errno may be set to an appropriate code
230
231 \par Error Conditions:
232 \em EINVAL - the library is not valid
233*/
235
236/** \brief Retrieve the specified library's runtime-assigned ID.
237 \param lib The library to examine
238 \return The library's ID, or -1 on error
239
240 \par Error Conditions:
241 \em EINVAL - the library is not valid
242*/
244
245/** \brief Retrieve the specified library's reference count.
246 \param lib The library to examine
247 \return The library's reference count, or -1 on error
248
249 \par Error Conditions:
250 \em EINVAL - the library is not valid
251*/
253
254/** \brief Retrieve the specified library's name.
255 \param lib The library to examine
256 \return The library's symbolic name, or NULL on error
257
258 \par Error Conditions:
259 \em EINVAL - the library is not valid
260*/
261const char * library_get_name(klibrary_t * lib);
262
263/** \brief Retrieve the specified library's version.
264 \param lib The library to examine
265 \return The library's version number, or 0 on error
266
267 \par Error Conditions
268 \em EINVAL - the library is not valid
269*/
271
272/** \cond */
273/* Init */
274int library_init(void);
275
276/* Shutdown */
277void library_shutdown(void);
278/** \endcond */
279
280/** @} */
281
282__END_DECLS
283
284#endif /* __KOS_LIBRARY_H */
285
ELF binary loading support.
Virtual filesystem support.
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.
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(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.
uint32 library_get_version(klibrary_t *lib)
Retrieve the specified library's version.
tid_t libid_t
Library ID type.
Definition library.h:51
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.
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
handle_t tid_t
Thread ID type.
Definition types.h:85
Kernel-specific definition of a loaded ELF binary.
Definition elf.h:304
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
int refcnt
Library reference count.
Definition library.h:96
uint32 flags
Library flags.
Definition library.h:82
LIST_ENTRY(klibrary) list
Library list handle.
Threading support.