1 /* Handle loading/unloading of shared object for transformation.
2 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include <bits/libc-lock.h>
27 #include <sys/param.h>
29 #include <gconv_int.h>
31 /* This is a tuning parameter. If a transformation module is not used
32 anymore it gets not immediately unloaded. Instead we wait a certain
33 number of load attempts for further modules. If none of the
34 subsequent load attempts name the same object it finally gets unloaded.
35 Otherwise it is still available which hopefully is the frequent case.
36 The following number is the number of unloading attempts we wait
38 #define TRIES_BEFORE_UNLOAD 2
40 /* Array of loaded objects. This is shared by all threads so we have
41 to use semaphores to access it. */
44 /* Comparison function for searching `loaded_object' tree. */
46 known_compare (const void *p1
, const void *p2
)
48 const struct __gconv_loaded_object
*s1
=
49 (const struct __gconv_loaded_object
*) p1
;
50 const struct __gconv_loaded_object
*s2
=
51 (const struct __gconv_loaded_object
*) p2
;
53 return (intptr_t) s1
->handle
- (intptr_t) s2
->handle
;
56 /* Open the gconv database if necessary. A non-negative return value
58 struct __gconv_loaded_object
*
60 __gconv_find_shlib (const char *name
)
62 struct __gconv_loaded_object
*found
;
65 /* Search the tree of shared objects previously requested. Data in
66 the tree are `loaded_object' structures, whose first member is a
67 `const char *', the lookup key. The search returns a pointer to
68 the tree node structure; the first member of the is a pointer to
69 our structure (i.e. what will be a `loaded_object'); since the
70 first member of that is the lookup key string, &FCT_NAME is close
71 enough to a pointer to our structure to use as a lookup key that
72 will be passed to `known_compare' (above). */
74 keyp
= __tfind (&name
, &loaded
, known_compare
);
77 /* This name was not known before. */
78 found
= malloc (sizeof (struct __gconv_loaded_object
));
81 /* Point the tree node at this new structure. */
83 found
->counter
= -TRIES_BEFORE_UNLOAD
- 1;
86 if (__tsearch (found
, &loaded
, known_compare
) == NULL
)
88 /* Something went wrong while inserting the entry. */
95 found
= *(struct __gconv_loaded_object
**) keyp
;
97 /* Try to load the shared object if the usage count is 0. This
98 implies that if the shared object is not loadable, the handle is
99 NULL and the usage count > 0. */
102 if (found
->counter
< -TRIES_BEFORE_UNLOAD
)
104 found
->handle
= __libc_dlopen (found
->name
);
105 if (found
->handle
!= NULL
)
107 found
->fct
= __libc_dlsym (found
->handle
, "gconv");
108 if (found
->fct
== NULL
)
110 /* Argh, no conversion function. There is something
112 __gconv_release_shlib (found
);
117 found
->init_fct
= __libc_dlsym (found
->handle
, "gconv_init");
118 found
->end_fct
= __libc_dlsym (found
->handle
, "gconv_end");
120 /* We have succeeded in loading the shared object. */
125 /* Error while loading the shared object. */
128 else if (found
->handle
!= NULL
)
129 found
->counter
= MAX (found
->counter
+ 1, 1);
136 /* This is very ugly but the tsearch functions provide no way to pass
137 information to the walker function. So we use a global variable.
138 It is MT safe since we use a lock. */
139 static struct __gconv_loaded_object
*release_handle
;
142 do_release_shlib (const void *nodep
, VISIT value
, int level
)
144 struct __gconv_loaded_object
*obj
= *(struct __gconv_loaded_object
**) nodep
;
146 if (value
!= preorder
&& value
!= leaf
)
149 if (obj
== release_handle
)
150 /* This is the object we want to unload. Now set the release
153 else if (obj
->counter
<= 0)
155 if (--obj
->counter
< -TRIES_BEFORE_UNLOAD
&& obj
->handle
!= NULL
)
157 /* Unload the shared object. */
158 __libc_dlclose (obj
->handle
);
165 /* Notify system that a shared object is not longer needed. */
168 __gconv_release_shlib (struct __gconv_loaded_object
*handle
)
170 /* Urgh, this is ugly but we have no other possibility. */
171 release_handle
= handle
;
173 /* Process all entries. Please note that we also visit entries
174 with release counts <= 0. This way we can finally unload them
176 __twalk (loaded
, do_release_shlib
);
182 /* We run this if we debug the memory allocation. */
184 do_release_all (void *nodep
)
186 struct __gconv_loaded_object
*obj
= (struct __gconv_loaded_object
*) nodep
;
188 /* Unload the shared object. */
189 if (obj
->handle
!= NULL
)
190 __libc_dlclose (obj
->handle
);
195 static void __attribute__ ((unused
))
198 __tdestroy (loaded
, do_release_all
);
200 text_set_element (__libc_subfreeres
, free_mem
);