2 * msvcrt onexit functions
4 * Copyright 2016 Nikolay Sivov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* these functions are part of the import lib for compatibility with the Mingw runtime */
23 #pragma makedep implib
30 /*********************************************************************
31 * _initialize_onexit_table (UCRTBASE.@)
33 int CDECL
_initialize_onexit_table(MSVCRT__onexit_table_t
*table
)
38 if (table
->_first
== table
->_end
)
39 table
->_last
= table
->_end
= table
->_first
= NULL
;
44 /*********************************************************************
45 * _register_onexit_function (UCRTBASE.@)
47 int CDECL
_register_onexit_function(MSVCRT__onexit_table_t
*table
, MSVCRT__onexit_t func
)
55 table
->_first
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 * sizeof(void *));
58 _munlock(_EXIT_LOCK1
);
61 table
->_last
= table
->_first
;
62 table
->_end
= table
->_first
+ 32;
66 if (table
->_last
== table
->_end
)
68 int len
= table
->_end
- table
->_first
;
69 MSVCRT__onexit_t
*tmp
= HeapReAlloc(GetProcessHeap(), 0, table
->_first
, 2 * len
* sizeof(void *));
72 _munlock(_EXIT_LOCK1
);
76 table
->_end
= table
->_first
+ 2 * len
;
77 table
->_last
= table
->_first
+ len
;
82 _munlock(_EXIT_LOCK1
);
87 /*********************************************************************
88 * _execute_onexit_table (UCRTBASE.@)
90 int CDECL
_execute_onexit_table(MSVCRT__onexit_table_t
*table
)
92 MSVCRT__onexit_t
*func
;
93 MSVCRT__onexit_table_t copy
;
99 if (!table
->_first
|| table
->_first
>= table
->_last
)
101 _munlock(_EXIT_LOCK1
);
104 copy
._first
= table
->_first
;
105 copy
._last
= table
->_last
;
106 copy
._end
= table
->_end
;
107 memset(table
, 0, sizeof(*table
));
108 _initialize_onexit_table(table
);
109 _munlock(_EXIT_LOCK1
);
111 for (func
= copy
._last
- 1; func
>= copy
._first
; func
--)
117 HeapFree(GetProcessHeap(), 0, copy
._first
);