onexit.c 3.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * msvcrt onexit functions
 *
 * Copyright 2016 Nikolay Sivov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

/* these functions are part of the import lib for compatibility with the Mingw runtime */
#if 0
#pragma makedep implib
#endif

26
#include <process.h>
27 28 29 30 31 32 33
#include "msvcrt.h"
#include "mtdll.h"


/*********************************************************************
 *		_initialize_onexit_table (UCRTBASE.@)
 */
34
int __cdecl _initialize_onexit_table(_onexit_table_t *table)
35 36 37 38 39 40 41 42 43 44 45 46 47
{
    if (!table)
        return -1;

    if (table->_first == table->_end)
        table->_last = table->_end = table->_first = NULL;
    return 0;
}


/*********************************************************************
 *		_register_onexit_function (UCRTBASE.@)
 */
48
int __cdecl _register_onexit_function(_onexit_table_t *table, _onexit_t func)
49 50 51 52
{
    if (!table)
        return -1;

53
    _lock(_EXIT_LOCK1);
54 55 56 57 58
    if (!table->_first)
    {
        table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
        if (!table->_first)
        {
59
            _unlock(_EXIT_LOCK1);
60 61 62 63 64 65 66 67 68 69
            return -1;
        }
        table->_last = table->_first;
        table->_end = table->_first + 32;
    }

    /* grow if full */
    if (table->_last == table->_end)
    {
        int len = table->_end - table->_first;
70
        _PVFV *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
71 72
        if (!tmp)
        {
73
            _unlock(_EXIT_LOCK1);
74 75 76 77 78 79 80
            return -1;
        }
        table->_first = tmp;
        table->_end = table->_first + 2 * len;
        table->_last = table->_first + len;
    }

81
    *table->_last = (_PVFV)func;
82
    table->_last++;
83
    _unlock(_EXIT_LOCK1);
84 85 86 87 88 89 90
    return 0;
}


/*********************************************************************
 *		_execute_onexit_table (UCRTBASE.@)
 */
91
int __cdecl _execute_onexit_table(_onexit_table_t *table)
92
{
93 94
    _PVFV *func;
    _onexit_table_t copy;
95 96 97 98

    if (!table)
        return -1;

99
    _lock(_EXIT_LOCK1);
100 101
    if (!table->_first || table->_first >= table->_last)
    {
102
        _unlock(_EXIT_LOCK1);
103 104 105 106 107 108 109
        return 0;
    }
    copy._first = table->_first;
    copy._last  = table->_last;
    copy._end   = table->_end;
    memset(table, 0, sizeof(*table));
    _initialize_onexit_table(table);
110
    _unlock(_EXIT_LOCK1);
111 112 113 114 115 116 117 118 119 120

    for (func = copy._last - 1; func >= copy._first; func--)
    {
        if (*func)
           (*func)();
    }

    HeapFree(GetProcessHeap(), 0, copy._first);
    return 0;
}