The Pedigree Project 0.1
Module.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "pedigree/kernel/utilities/List.h"
21
22#include "modules/Module.h"
23
24#ifndef STATIC_DRIVERS
25
26extern "C" {
27
28typedef void (*__cxa_atexit_func_type)(void*);
29
31 __cxa_atexit_func_type func;
32 void* arg;
33};
34
35static List<__cxa_atexit_record>* g_AtExitEntries = 0;
36
37// We provide an implementation of __cxa_atexit for each module, so that we can
38// run the finalizers correctly.
39void __cxa_atexit(void (*f)(void*), void* arg, void* dso_handle);
40void __cxa_atexit(void (*f)(void*), void* arg, void* dso_handle) {
41 if (!g_AtExitEntries) {
42 g_AtExitEntries = new List<__cxa_atexit_record>;
43 }
44
46 rec.func = f;
47 rec.arg = arg;
48
49 g_AtExitEntries->pushBack(rec);
50}
51
52// This is the lowest priority we can assign a destructor. This runs on
53// termination to call all the atexit()s set up by __cxa_atexit above.
54void __perform_module_exit() __attribute__((destructor(101)));
55void __perform_module_exit() {
56 if (!g_AtExitEntries)
57 return;
58
59 for (auto it = g_AtExitEntries->begin(); it != g_AtExitEntries->end(); ++it) {
60 (*it).func((*it).arg);
61 }
62
63 delete g_AtExitEntries;
64}
65
66} // extern "C"
67
68#endif
Definition List.h:61