1 // =================================
 2 // Copyright (c) 2022 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 namespace System
 7 {
 8     public delegate void ExitFunc(void* arg);
 9 
10     public class ExitEntry
11     {
12         public nothrow ExitEntry() : fun(null)arg(null)next(null)
13         {
14         }
15         public void* fun;
16         public void* arg;
17         public ExitEntry* next;
18     }
19     
20     private ExitEntry* atExitList = null;
21 
22     public nothrow void AtExit(ExitEntry* entryvoid* funvoid* arg)
23     {
24         entry->fun = fun;
25         entry->arg = arg;
26         entry->next = atExitList;
27         atExitList = entry;
28     }
29     
30     public nothrow void RunAtExits()
31     {
32         ExitEntry* entry = atExitList;
33         while (entry != null)
34         {
35             atExitList = entry->next;
36             ExitFunc fun = cast<ExitFunc>(entry->fun);
37             void* arg = entry->arg;
38             fun(arg);
39             entry = atExitList;
40         }
41     }
42 }
43 
44 public cdecl nothrow void at_exit(System.ExitEntry* entryvoid* funvoid* arg)
45 {
46     System.AtExit(entryfunarg);
47 }
48 
49 public cdecl nothrow void run_at_exits()
50 {
51     System.RunAtExits();
52 }