OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [iconv/] [gconv_dl.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/* Handle loading/unloading of shared object for transformation.
2
   Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
4
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
 
6
   The GNU C Library is free software; you can redistribute it and/or
7
   modify it under the terms of the GNU Lesser General Public
8
   License as published by the Free Software Foundation; either
9
   version 2.1 of the License, or (at your option) any later version.
10
 
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
   Lesser General Public License for more details.
15
 
16
   You should have received a copy of the GNU Lesser General Public
17
   License along with the GNU C Library; if not, write to the Free
18
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
   02111-1307 USA.  */
20
 
21
#include <assert.h>
22
#include <dlfcn.h>
23
#include <search.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <bits/libc-lock.h>
27
#include <sys/param.h>
28
#include <ltdl.h>
29
 
30
#include <gconv_int.h>
31
 
32
 
33
#ifdef DEBUG
34
/* For debugging purposes.  */
35
static void print_all (void);
36
#endif
37
 
38
 
39
/* This is a tuning parameter.  If a transformation module is not used
40
   anymore it gets not immediately unloaded.  Instead we wait a certain
41
   number of load attempts for further modules.  If none of the
42
   subsequent load attempts name the same object it finally gets unloaded.
43
   Otherwise it is still available which hopefully is the frequent case.
44
   The following number is the number of unloading attempts we wait
45
   before unloading.  */
46
#define TRIES_BEFORE_UNLOAD     2
47
 
48
/* Array of loaded objects.  This is shared by all threads so we have
49
   to use semaphores to access it.  */
50
static void *loaded;
51
 
52
/* Comparison function for searching `loaded_object' tree.  */
53
static int
54
known_compare (const void *p1, const void *p2)
55
{
56
  const struct __gconv_loaded_object *s1 =
57
    (const struct __gconv_loaded_object *) p1;
58
  const struct __gconv_loaded_object *s2 =
59
    (const struct __gconv_loaded_object *) p2;
60
 
61
  return strcmp (s1->name, s2->name);
62
}
63
 
64
/* Open the gconv database if necessary.  A non-negative return value
65
   means success.  */
66
struct __gconv_loaded_object *
67
internal_function
68
__gconv_find_shlib (const char *name)
69
{
70
  struct __gconv_loaded_object *found;
71
  void *keyp;
72
 
73
 
74
 
75
  /* Search the tree of shared objects previously requested.  Data in
76
     the tree are `loaded_object' structures, whose first member is a
77
     `const char *', the lookup key.  The search returns a pointer to
78
     the tree node structure; the first member of the is a pointer to
79
     our structure (i.e. what will be a `loaded_object'); since the
80
     first member of that is the lookup key string, &FCT_NAME is close
81
     enough to a pointer to our structure to use as a lookup key that
82
     will be passed to `known_compare' (above).  */
83
 
84
  keyp = tfind (&name, &loaded, known_compare);
85
  if (keyp == NULL)
86
    {
87
      /* This name was not known before.  */
88
      size_t namelen = strlen (name) + 1;
89
 
90
      found = malloc (sizeof (struct __gconv_loaded_object) + namelen);
91
      if (found != NULL)
92
        {
93
          /* Point the tree node at this new structure.  */
94
          found->name = (char *) memcpy (found + 1, name, namelen);
95
          found->counter = -TRIES_BEFORE_UNLOAD - 1;
96
          found->handle = NULL;
97
 
98
          if (__builtin_expect (tsearch (found, &loaded, known_compare)
99
                                == NULL, 0))
100
            {
101
              /* Something went wrong while inserting the entry.  */
102
              free (found);
103
              found = NULL;
104
            }
105
        }
106
    }
107
  else
108
    found = *(struct __gconv_loaded_object **) keyp;
109
 
110
  /* Try to load the shared object if the usage count is 0.  This
111
     implies that if the shared object is not loadable, the handle is
112
     NULL and the usage count > 0.  */
113
  if (found != NULL)
114
    {
115
      if (found->counter < -TRIES_BEFORE_UNLOAD)
116
        {
117
          assert (found->handle == NULL);
118
          found->handle = __libc_dlopen (found->name);
119
          if (found->handle != NULL)
120
            {
121
              found->fct = __libc_dlsym (found->handle, "gconv");
122
              if (found->fct == NULL)
123
                {
124
                  /* Argh, no conversion function.  There is something
125
                     wrong here.  */
126
                  __gconv_release_shlib (found);
127
                  found = NULL;
128
                }
129
              else
130
                {
131
                  found->init_fct = __libc_dlsym (found->handle, "gconv_init");
132
                  found->end_fct = __libc_dlsym (found->handle, "gconv_end");
133
 
134
                  /* We have succeeded in loading the shared object.  */
135
                  found->counter = 1;
136
                }
137
            }
138
          else
139
            /* Error while loading the shared object.  */
140
            found = NULL;
141
        }
142
      else if (found->handle != NULL)
143
        found->counter = MAX (found->counter + 1, 1);
144
    }
145
 
146
  return found;
147
}
148
 
149
 
150
/* This is very ugly but the tsearch functions provide no way to pass
151
   information to the walker function.  So we use a global variable.
152
   It is MT safe since we use a lock.  */
153
static struct __gconv_loaded_object *release_handle;
154
 
155
static void
156
do_release_shlib (void *nodep, VISIT value, int level)
157
{
158
  struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
159
 
160
 
161
 
162
  if (value != preorder && value != leaf)
163
    return;
164
 
165
  if (obj == release_handle)
166
    {
167
      /* This is the object we want to unload.  Now decrement the
168
         reference counter.  */
169
      assert (obj->counter > 0);
170
      --obj->counter;
171
    }
172
  else if (obj->counter <= 0 && obj->counter >= -TRIES_BEFORE_UNLOAD
173
           && --obj->counter < -TRIES_BEFORE_UNLOAD && obj->handle != NULL)
174
    {
175
      /* Unload the shared object.  */
176
      __libc_dlclose (obj->handle);
177
      obj->handle = NULL;
178
    }
179
}
180
 
181
 
182
/* Notify system that a shared object is not longer needed.  */
183
void
184
internal_function
185
__gconv_release_shlib (struct __gconv_loaded_object *handle)
186
{
187
  /* Urgh, this is ugly but we have no other possibility.  */
188
  release_handle = handle;
189
 
190
  /* Process all entries.  Please note that we also visit entries
191
     with release counts <= 0.  This way we can finally unload them
192
     if necessary.  */
193
  twalk (loaded, (void *) do_release_shlib);
194
}
195
 
196
 
197
/* We run this if we debug the memory allocation.  */
198
static void
199
do_release_all (void *nodep)
200
{
201
  struct __gconv_loaded_object *obj = (struct __gconv_loaded_object *) nodep;
202
 
203
 
204
  /* Unload the shared object.  */
205
  if (obj->handle != NULL)
206
    __libc_dlclose (obj->handle);
207
 
208
  free (obj);
209
}
210
 
211
static void __attribute__ ((unused))
212
free_mem (void)
213
{
214
  tdestroy (loaded, do_release_all);
215
}
216
text_set_element (__libc_subfreeres, free_mem);
217
 
218
 
219
#ifdef DEBUG
220
static void
221
do_print (const void *nodep, VISIT value, int level)
222
{
223
  struct __gconv_loaded_object *obj = *(struct __gconv_loaded_object **) nodep;
224
 
225
  printf ("%10s: \"%s\", %d\n",
226
          value == leaf ? "leaf" :
227
          value == preorder ? "preorder" :
228
          value == postorder ? "postorder" : "endorder",
229
          obj->name, obj->counter);
230
}
231
 
232
static void
233
print_all (void)
234
{
235
  __twalk (loaded, do_print);
236
}
237
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.