Import code from my Subversion repository
[black_box_cellml.git] / libltdl / lt_dlloader.c
blob411b5e010c859fe8b1b1269e73eb4e5be831b372
1 /* lt_dlloader.c -- dynamic library loader interface
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Originally by Gary V. Vaughan <gary@gnu.org>
5 NOTE: The canonical source of this file is maintained with the
6 GNU Libtool package. Report bugs to bug-libtool@gnu.org.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 As a special exception to the GNU Lesser General Public License,
14 if you distribute this file as part of a program or library that
15 is built using GNU libtool, you may include it under the same
16 distribution terms that you use for the rest of that program.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 02110-1301 USA
30 #include "lt_dlloader.h"
31 #include "lt__private.h"
33 #define RETURN_SUCCESS 0
34 #define RETURN_FAILURE 1
36 static void * loader_callback (SList *item, void *userdata);
38 /* A list of all the dlloaders we know about, each stored as a boxed
39 SList item: */
40 static SList *loaders = 0;
43 /* Return NULL, unless the loader in this ITEM has a matching name,
44 in which case we return the matching item so that its address is
45 passed back out (for possible freeing) by slist_remove. */
46 static void *
47 loader_callback (SList *item, void *userdata)
49 const lt_dlvtable *vtable = item->userdata;
50 const char * name = userdata;
52 assert (vtable);
54 return streq (vtable->name, name) ? (void *) item : 0;
58 /* Hook VTABLE into our global LOADERS list according to its own
59 PRIORITY field value. */
60 int
61 lt_dlloader_add (const lt_dlvtable *vtable)
63 SList *item;
65 if ((vtable == 0) /* diagnose invalid vtable fields */
66 || (vtable->module_open == 0)
67 || (vtable->module_close == 0)
68 || (vtable->find_sym == 0)
69 || ((vtable->priority != LT_DLLOADER_PREPEND) &&
70 (vtable->priority != LT_DLLOADER_APPEND)))
72 LT__SETERROR (INVALID_LOADER);
73 return RETURN_FAILURE;
76 item = slist_box (vtable);
77 if (!item)
79 (*lt__alloc_die) ();
81 /* Let the caller know something went wrong if lt__alloc_die
82 doesn't abort. */
83 return RETURN_FAILURE;
86 if (vtable->priority == LT_DLLOADER_PREPEND)
88 loaders = slist_cons (item, loaders);
90 else
92 assert (vtable->priority == LT_DLLOADER_APPEND);
93 loaders = slist_concat (loaders, item);
96 return RETURN_SUCCESS;
100 /* An iterator for the global loader list: if LOADER is NULL, then
101 return the first element, otherwise the following element. */
102 lt_dlloader
103 lt_dlloader_next (lt_dlloader loader)
105 SList *item = (SList *) loader;
106 return (lt_dlloader) (item ? item->next : loaders);
110 /* Non-destructive unboxing of a loader. */
111 const lt_dlvtable *
112 lt_dlloader_get (lt_dlloader loader)
114 return loader ? ((SList *) loader)->userdata : 0;
118 /* Return the contents of the first item in the global loader list
119 with a matching NAME after removing it from that list. If there
120 was no match, return NULL; if there is an error, return NULL and
121 set an error for lt_dlerror; in either case, the loader list is
122 not changed if NULL is returned. */
123 lt_dlvtable *
124 lt_dlloader_remove (char *name)
126 const lt_dlvtable * vtable = lt_dlloader_find (name);
127 lt__handle * handle = 0;
129 if (!vtable)
131 LT__SETERROR (INVALID_LOADER);
132 return 0;
135 /* Fail if there are any open modules which use this loader. */
136 for (handle = 0; handle; handle = handle->next)
138 if (handle->vtable == vtable)
140 LT__SETERROR (REMOVE_LOADER);
141 return 0;
145 /* Call the loader finalisation function. */
146 if (vtable && vtable->dlloader_exit)
148 if ((*vtable->dlloader_exit) (vtable->dlloader_data) != 0)
150 /* If there is an exit function, and it returns non-zero
151 then it must set an error, and we will not remove it
152 from the list. */
153 return 0;
157 /* If we got this far, remove the loader from our global list. */
158 return slist_unbox (slist_remove (&loaders, loader_callback, name));
162 const lt_dlvtable *
163 lt_dlloader_find (char *name)
165 return lt_dlloader_get (slist_find (loaders, loader_callback, name));