diff options
Diffstat (limited to 'src/Runtime')
-rw-r--r-- | src/Runtime/NMWException.h | 16 | ||||
-rw-r--r-- | src/Runtime/abort_exit.c | 17 | ||||
-rw-r--r-- | src/Runtime/global_destructor_chain.c | 22 |
3 files changed, 55 insertions, 0 deletions
diff --git a/src/Runtime/NMWException.h b/src/Runtime/NMWException.h new file mode 100644 index 0000000..49b623e --- /dev/null +++ b/src/Runtime/NMWException.h @@ -0,0 +1,16 @@ +#ifndef NMWEXCEPTION_H +#define NMWEXCEPTION_H + +typedef void (dtor_func)(void *object, int free_object); +typedef struct destructor_chain dtor_chain; + +struct destructor_chain { + dtor_chain *next; + dtor_func *destructor; + void *object; +}; + +void *__register_global_object(void *object, dtor_func *destructor, dtor_chain *regmem); +void __destroy_global_chain(); + +#endif diff --git a/src/Runtime/abort_exit.c b/src/Runtime/abort_exit.c new file mode 100644 index 0000000..43b1a0c --- /dev/null +++ b/src/Runtime/abort_exit.c @@ -0,0 +1,17 @@ +void __destroy_global_chain(); + +extern void (*_dtors[])(void); + +int __aborting; + + +void exit(int state) { + void (**dtor)(void); + if (!__aborting) { + dtor = _dtors; + while (*dtor != 0) { + (*dtor)(); + dtor++; + } + } +} diff --git a/src/Runtime/global_destructor_chain.c b/src/Runtime/global_destructor_chain.c new file mode 100644 index 0000000..b96c8f3 --- /dev/null +++ b/src/Runtime/global_destructor_chain.c @@ -0,0 +1,22 @@ +#include "NMWException.h" + +dtor_chain *__global_destructor_chain; + +extern void *__register_global_object(void *object, dtor_func *destructor, dtor_chain *regmem) { + regmem->next = __global_destructor_chain; + regmem->destructor = destructor; + regmem->object = object; + __global_destructor_chain = regmem; + return object; +} + +extern void __destroy_global_chain() { + dtor_chain *iter = __global_destructor_chain; + while (__global_destructor_chain != 0) { + iter = __global_destructor_chain; + __global_destructor_chain = __global_destructor_chain->next; + iter->destructor(iter->object, -1); + } +} + +__declspec(section ".dtors") static void* const __destroy_global_chain_reference = __destroy_global_chain; |