|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
|
|
|
|
|
|
|
|
| extern "C" _onexit_table_t __acrt_atexit_table{};
|
| extern "C" _onexit_table_t __acrt_at_quick_exit_table{};
|
|
|
|
|
|
|
| enum : size_t
|
| {
|
| initial_table_count = 32,
|
| minimum_table_increment = 4,
|
| maximum_table_increment = 512
|
| };
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl _crt_atexit(_PVFV const function)
|
| {
|
| return _register_onexit_function(&__acrt_atexit_table, reinterpret_cast<_onexit_t>(function));
|
| }
|
|
|
| extern "C" int __cdecl _crt_at_quick_exit(_PVFV const function)
|
| {
|
| return _register_onexit_function(&__acrt_at_quick_exit_table, reinterpret_cast<_onexit_t>(function));
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _initialize_onexit_table(_onexit_table_t* const table)
|
| {
|
| if (!table)
|
| {
|
| return -1;
|
| }
|
|
|
|
|
|
|
|
|
| if (table->_first != table->_end)
|
| {
|
| return 0;
|
| }
|
|
|
| _PVFV* const encoded_nullptr = __crt_fast_encode_pointer(nullptr);
|
|
|
| table->_first = encoded_nullptr;
|
| table->_last = encoded_nullptr;
|
| table->_end = encoded_nullptr;
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl _register_onexit_function(_onexit_table_t* const table, _onexit_t const function)
|
| {
|
| return __acrt_lock_and_call(__acrt_select_exit_lock(), [&]
|
| {
|
| if (!table)
|
| {
|
| return -1;
|
| }
|
|
|
| _PVFV* first = __crt_fast_decode_pointer(table->_first);
|
| _PVFV* last = __crt_fast_decode_pointer(table->_last);
|
| _PVFV* end = __crt_fast_decode_pointer(table->_end);
|
|
|
|
|
| if (last == end)
|
| {
|
| size_t const old_count = end - first;
|
|
|
| size_t const increment = old_count > maximum_table_increment ? maximum_table_increment : old_count;
|
|
|
|
|
| size_t new_count = old_count + increment;
|
| if (new_count == 0)
|
| {
|
| new_count = initial_table_count;
|
| }
|
|
|
| _PVFV* new_first = nullptr;
|
| if (new_count >= old_count)
|
| {
|
| new_first = _recalloc_crt_t(_PVFV, first, new_count).detach();
|
| }
|
|
|
|
|
| if (new_first == nullptr)
|
| {
|
| new_count = old_count + minimum_table_increment;
|
| new_first = _recalloc_crt_t(_PVFV, first, new_count).detach();
|
| }
|
|
|
| if (new_first == nullptr)
|
| {
|
| return -1;
|
| }
|
|
|
| first = new_first;
|
| last = new_first + old_count;
|
| end = new_first + new_count;
|
|
|
|
|
|
|
|
|
| _PVFV const encoded_nullptr = __crt_fast_encode_pointer(nullptr);
|
| for (auto it = last; it != end; ++it)
|
| {
|
| *it = encoded_nullptr;
|
| }
|
| }
|
|
|
| *last++ = reinterpret_cast<_PVFV>(__crt_fast_encode_pointer(function));
|
|
|
| table->_first = __crt_fast_encode_pointer(first);
|
| table->_last = __crt_fast_encode_pointer(last);
|
| table->_end = __crt_fast_encode_pointer(end);
|
|
|
| return 0;
|
| });
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl _execute_onexit_table(_onexit_table_t* const table)
|
| {
|
| return __acrt_lock_and_call(__acrt_select_exit_lock(), [&]
|
| {
|
| if (!table)
|
| {
|
| return -1;
|
| }
|
|
|
| _PVFV* first = __crt_fast_decode_pointer(table->_first);
|
| _PVFV* last = __crt_fast_decode_pointer(table->_last);
|
| if (!first || first == reinterpret_cast<_PVFV*>(-1))
|
| {
|
| return 0;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| {
|
| __crt_state_management::scoped_global_state_reset saved_state;
|
|
|
| _PVFV const encoded_nullptr = __crt_fast_encode_pointer(nullptr);
|
|
|
| _PVFV* saved_first = first;
|
| _PVFV* saved_last = last;
|
| for (;;)
|
| {
|
|
|
| while (--last >= first && *last == encoded_nullptr)
|
| {
|
|
|
| }
|
|
|
| if (last < first)
|
| {
|
|
|
| break;
|
| }
|
|
|
|
|
| _PVFV const function = __crt_fast_decode_pointer(*last);
|
| *last = encoded_nullptr;
|
|
|
| function();
|
|
|
| _PVFV* const new_first = __crt_fast_decode_pointer(table->_first);
|
| _PVFV* const new_last = __crt_fast_decode_pointer(table->_last);
|
|
|
|
|
| if (new_first != saved_first || new_last != saved_last)
|
| {
|
| first = saved_first = new_first;
|
| last = saved_last = new_last;
|
| }
|
| }
|
| }
|
|
|
| if (first != reinterpret_cast<_PVFV*>(-1))
|
| {
|
| _free_crt(first);
|
| }
|
|
|
| _PVFV* const encoded_nullptr = __crt_fast_encode_pointer(nullptr);
|
|
|
| table->_first = encoded_nullptr;
|
| table->_last = encoded_nullptr;
|
| table->_end = encoded_nullptr;
|
|
|
| return 0;
|
| });
|
| }
|
|
|