Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libc/stdlib/atexit.c

1/*-
2 * Copyright (c) 1990, 1993
3 *The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)atexit.c8.2 (Berkeley) 7/3/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: src/lib/libc/stdlib/atexit.c,v 1.8 2007/01/09 00:28:09 imp Exp $");
38
39#include "namespace.h"
40#include <stddef.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include "atexit.h"
44#include "un-namespace.h"
45
46#defineATEXIT_FN_EMPTY0
47#defineATEXIT_FN_STD1
48#defineATEXIT_FN_CXA2
49
50struct atexit {
51struct atexit *next;/* next in list */
52int ind;/* next index in this table */
53struct atexit_fn {
54int fn_type;/* ATEXIT_? from above */
55union {
56void (*std_func)(void);
57void (*cxa_func)(void *);
58} fn_ptr;/* function pointer */
59void *fn_arg;/* argument for CXA callback */
60void *fn_dso;/* shared module handle */
61} fns[ATEXIT_SIZE];/* the table itself */
62};
63
64static struct atexit *__atexit;/* points to head of LIFO stack */
65static int new_registration;
66
67/*
68 * Register the function described by 'fptr' to be called at application
69 * exit or owning shared object unload time. This is a helper function
70 * for atexit and __cxa_atexit.
71 */
72static int
73atexit_register(struct atexit_fn *fptr)
74{
75static struct atexit __atexit0;/* one guaranteed table */
76struct atexit *p;
77
78if ((p = __atexit) == NULL)
79__atexit = p = &__atexit0;
80else while (p->ind >= ATEXIT_SIZE) {
81struct atexit *old__atexit;
82old__atexit = __atexit;
83if ((p = (struct atexit *)malloc(sizeof(*p))) == NULL)
84return (-1);
85if (old__atexit != __atexit) {
86/* Lost race, retry operation */
87free(p);
88p = __atexit;
89continue;
90}
91p->ind = 0;
92p->next = __atexit;
93__atexit = p;
94}
95p->fns[p->ind++] = *fptr;
96new_registration = 1;
97return 0;
98}
99
100/*
101 * Register a function to be performed at exit.
102 */
103int
104atexit(void (*func)(void))
105{
106struct atexit_fn fn;
107//struct dl_info info; ??
108int error;
109
110fn.fn_type = ATEXIT_FN_STD;
111fn.fn_ptr.std_func = func;
112fn.fn_arg = NULL;
113fn.fn_dso = NULL;
114
115 error = atexit_register(&fn);
116return (error);
117}
118
119/*
120 * Register a function to be performed at exit or when an shared object
121 * with given dso handle is unloaded dynamically.
122 */
123int
124__cxa_atexit(void (*func)(void *), void *arg, void *dso)
125{
126struct atexit_fn fn;
127int error;
128
129fn.fn_type = ATEXIT_FN_CXA;
130fn.fn_ptr.cxa_func = func;;
131fn.fn_arg = arg;
132fn.fn_dso = dso;
133
134 error = atexit_register(&fn);
135return (error);
136}
137
138/*
139 * Call all handlers registered with __cxa_atexit for the shared
140 * object owning 'dso'. Note: if 'dso' is NULL, then all remaining
141 * handlers are called.
142 */
143void
144__cxa_finalize(const void *dso)
145{
146struct atexit *p;
147struct atexit_fn fn;
148int n;
149
150restart:
151for (p = __atexit; p; p = p->next) {
152for (n = p->ind; --n >= 0;) {
153if (p->fns[n].fn_type == ATEXIT_FN_EMPTY)
154continue; /* already been called */
155if (dso != NULL && dso != p->fns[n].fn_dso)
156continue; /* wrong DSO */
157fn = p->fns[n];
158/*
159 Mark entry to indicate that this particular handler
160 has already been called.
161*/
162p->fns[n].fn_type = ATEXIT_FN_EMPTY;
163new_registration = 0;
164
165/* Call the function of correct type. */
166if (fn.fn_type == ATEXIT_FN_CXA)
167fn.fn_ptr.cxa_func(fn.fn_arg);
168else if (fn.fn_type == ATEXIT_FN_STD)
169fn.fn_ptr.std_func();
170if (new_registration)
171 goto restart;
172}
173}
174}
175

Archive Download this file

Revision: 2182