lib_freeall.c revision 76726
1/****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Author: Thomas E. Dickey <dickey@clark.net> 1996,1997                   *
31 ****************************************************************************/
32
33#include <curses.priv.h>
34#include <term_entry.h>
35
36#if HAVE_NC_FREEALL
37
38#if HAVE_LIBDBMALLOC
39extern int malloc_errfd;	/* FIXME */
40#endif
41
42MODULE_ID("$Id: lib_freeall.c,v 1.18 2000/12/10 02:43:27 tom Exp $")
43
44static void
45free_slk(SLK * p)
46{
47    if (p != 0) {
48	FreeIfNeeded(p->ent);
49	FreeIfNeeded(p->buffer);
50	free(p);
51    }
52}
53
54static void
55free_tries(struct tries *p)
56{
57    struct tries *q;
58
59    while (p != 0) {
60	q = p->sibling;
61	if (p->child != 0)
62	    free_tries(p->child);
63	free(p);
64	p = q;
65    }
66}
67
68/*
69 * Free all ncurses data.  This is used for testing only (there's no practical
70 * use for it as an extension).
71 */
72NCURSES_EXPORT(void)
73_nc_freeall(void)
74{
75    WINDOWLIST *p, *q;
76
77#if NO_LEAKS
78    _nc_free_tparm();
79#endif
80    if (SP != 0) {
81	while (_nc_windows != 0) {
82	    /* Delete only windows that're not a parent */
83	    for (p = _nc_windows; p != 0; p = p->next) {
84		bool found = FALSE;
85
86		for (q = _nc_windows; q != 0; q = q->next) {
87		    if ((p != q)
88			&& (q->win->_flags & _SUBWIN)
89			&& (p->win == q->win->_parent)) {
90			found = TRUE;
91			break;
92		    }
93		}
94
95		if (!found) {
96		    delwin(p->win);
97		    break;
98		}
99	    }
100	}
101
102	free_tries(SP->_keytry);
103	free_tries(SP->_key_ok);
104	free_slk(SP->_slk);
105	FreeIfNeeded(SP->_color_pairs);
106	FreeIfNeeded(SP->_color_table);
107#if !BROKEN_LINKER
108	FreeAndNull(SP);
109#endif
110    }
111
112    if (cur_term != 0) {
113	_nc_free_termtype(&(cur_term->type));
114	free(cur_term);
115    }
116#ifdef TRACE
117    (void) _nc_trace_buf(-1, 0);
118#endif
119#if HAVE_LIBDBMALLOC
120    malloc_dump(malloc_errfd);
121#elif HAVE_LIBDMALLOC
122#elif HAVE_PURIFY
123    purify_all_inuse();
124#endif
125}
126
127NCURSES_EXPORT(void)
128_nc_free_and_exit(int code)
129{
130    _nc_freeall();
131    exit(code);
132}
133
134#else
135NCURSES_EXPORT(void)
136_nc_freeall(void)
137{
138}
139#endif
140