summaryrefslogtreecommitdiff
path: root/cmt/cleanup.c
blob: a0797d9fa6d2e545f0c7f2f94a73a44e8561fa02 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* cleanup.c -- registers work to do upon exit */

#include "stdio.h"
#include "cext.h"

typedef struct cleanup_struct {
    struct cleanup_struct *next;
    cu_fn_type fn;
    cu_parm_type obj;
} cleanup_node, *cleanup_type;

cleanup_type cleanup_list = NULL;

/* cu_register -- remember function and arg to call in order to clean up */
/**/
void cu_register(cu_fn_type fn, cu_parm_type obj)
{
    cleanup_type cu = (cleanup_type) memget(sizeof(cleanup_node));
    cu->fn = fn;
    cu->obj = obj;
    cu->next = cleanup_list;
    cleanup_list = cu;
}


/* cu_unregister -- erase memory of obj (should be unique in cleanup list) */
/**/
void cu_unregister(obj)
  void *obj;
{
    cleanup_type *cu = &cleanup_list;
    while (*cu) {
        if ((*cu)->obj == obj) {
            cleanup_type found = *cu;
            *cu = (*cu)->next;	/* splice out found */
            memfree((char *) found, sizeof(cleanup_node));
            return;
        }
        cu = &((*cu)->next);
    }
}


/* cu_cleanup -- call the registered functions */
/**/
void cu_cleanup()
{
    while (cleanup_list) {
        cleanup_type cu = cleanup_list;
#ifdef CU_TRACE
        gprintf(GTRANS, "cu_cleanup: node %lx fn %lx obj %lx\n",
                cu, cu->fn, cu->obj);
#endif
        cu->fn(cu->obj);
        cleanup_list = cu->next;
        memfree((char *) cu, sizeof(cleanup_node));
    }
#ifdef CU_TRACE
    gprintf(GTRANS, "cu_cleanup done.\n");
    fflush(stdout);
#endif
}