2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
4 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
16 #include "private/gc_priv.h"
18 /* This used to be in dyn_load.c. It was extracted into a separate */
19 /* file to avoid having to link against libdl.{a,so} if the client */
20 /* doesn't call dlopen. Of course this fails if the collector is in */
21 /* a dynamic library. -HB */
22 #if defined(GC_PTHREADS) && !defined(GC_NO_DLOPEN)
24 #undef GC_MUST_RESTORE_REDEFINED_DLOPEN
25 #if defined(dlopen) && !defined(GC_USE_LD_WRAP)
26 /* To support various threads pkgs, gc.h interposes on dlopen by */
27 /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
28 /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
29 /* real system dlopen() in their implementation. We first remove */
30 /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
32 # define GC_MUST_RESTORE_REDEFINED_DLOPEN
35 /* Make sure we're not in the middle of a collection, and make sure we */
36 /* don't start any. This is invoked prior to a dlopen call to avoid */
37 /* synchronization issues. We can't just acquire the allocation lock, */
38 /* since startup code in dlopen may try to allocate. This solution */
39 /* risks heap growth (or, even, heap overflow) in the presence of many */
40 /* dlopen calls in either a multi-threaded environment, or if the */
41 /* library initialization code allocates substantial amounts of GC'ed */
43 #ifndef USE_PROC_FOR_LIBRARIES
44 static void disable_gc_for_dlopen(void)
48 while (GC_incremental && GC_collection_in_progress()) {
50 GC_collect_a_little_inner(1000);
58 /* Redefine dlopen to guarantee mutual exclusion with */
59 /* GC_register_dynamic_libraries. Should probably happen for */
60 /* other operating systems, too. */
62 /* This is similar to WRAP/REAL_FUNC() in pthread_support.c. */
64 # define WRAP_DLFUNC(f) __wrap_##f
65 # define REAL_DLFUNC(f) __real_##f
66 void * REAL_DLFUNC(dlopen)(const char *, int);
68 # define WRAP_DLFUNC(f) GC_##f
69 # define REAL_DLFUNC(f) f
72 GC_API void * WRAP_DLFUNC(dlopen)(const char *path, int mode)
76 # ifndef USE_PROC_FOR_LIBRARIES
77 /* Disable collections. This solution risks heap growth (or, */
78 /* even, heap overflow) but there seems no better solutions. */
79 disable_gc_for_dlopen();
81 result = REAL_DLFUNC(dlopen)(path, mode);
82 # ifndef USE_PROC_FOR_LIBRARIES
83 GC_enable(); /* undoes disable_gc_for_dlopen */
89 /* Define GC_ function as an alias for the plain one, which will be */
90 /* intercepted. This allows files which include gc.h, and hence */
91 /* generate references to the GC_ symbol, to see the right symbol. */
92 GC_API void *GC_dlopen(const char *path, int mode)
94 return dlopen(path, mode);
96 #endif /* GC_USE_LD_WRAP */
98 #ifdef GC_MUST_RESTORE_REDEFINED_DLOPEN
99 # define dlopen GC_dlopen
102 #endif /* GC_PTHREADS && !GC_NO_DLOPEN */