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 
26 extern "C" {
27 
28 typedef void (*__cxa_atexit_func_type)(void *);
29 
31 {
32  __cxa_atexit_func_type func;
33  void *arg;
34 };
35 
36 static List<__cxa_atexit_record> *g_AtExitEntries = 0;
37 
38 // We provide an implementation of __cxa_atexit for each module, so that we can
39 // run the finalizers correctly.
40 void __cxa_atexit(void (*f)(void *), void *arg, void *dso_handle);
41 void __cxa_atexit(void (*f)(void *), void *arg, void *dso_handle)
42 {
43  if (!g_AtExitEntries)
44  {
45  g_AtExitEntries = new List<__cxa_atexit_record>;
46  }
47 
49  rec.func = f;
50  rec.arg = arg;
51 
52  g_AtExitEntries->pushBack(rec);
53 }
54 
55 // This is the lowest priority we can assign a destructor. This runs on
56 // termination to call all the atexit()s set up by __cxa_atexit above.
57 void __perform_module_exit() __attribute__((destructor(101)));
58 void __perform_module_exit()
59 {
60  if (!g_AtExitEntries)
61  return;
62 
63  for (auto it = g_AtExitEntries->begin(); it != g_AtExitEntries->end(); ++it)
64  {
65  (*it).func((*it).arg);
66  }
67 
68  delete g_AtExitEntries;
69 }
70 
71 } // extern "C"
72 
73 #endif
void pushBack(const T &value)
Definition: List.h:232
Definition: List.h:64
Iterator begin()
Definition: List.h:123
Iterator end()
Definition: List.h:135