summaryrefslogtreecommitdiff
path: root/egg/egg-cleanup.c
blob: 3dec665b47480e3465f546cd31692d6db903a668 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* egg-cleanup.c - for data cleanup at end of program

   Copyright (C) 2007 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "egg-cleanup.h"

typedef struct _EggCleanup {
	GDestroyNotify notify;
	gpointer user_data;
} EggCleanup;

static GSList *registered_cleanups = NULL;

void    
egg_cleanup_register (GDestroyNotify notify, gpointer user_data)
{
	EggCleanup *cleanup = g_new0 (EggCleanup, 1);
	
	g_assert (notify);
	cleanup->notify = notify;
	cleanup->user_data = user_data;
	
	/* Note we're reversing the order, so calls happen that way */
	registered_cleanups = g_slist_prepend (registered_cleanups, cleanup);
}

void
egg_cleanup_unregister (GDestroyNotify notify, gpointer user_data)
{
	EggCleanup *cleanup;
	GSList *l;
	
	for (l = registered_cleanups; l; l = g_slist_next (l)) {
		cleanup = (EggCleanup*)l->data;
		if (cleanup->notify == notify && cleanup->user_data == user_data) {
			registered_cleanups = g_slist_remove (registered_cleanups, cleanup);
			g_free (cleanup);
			break;
		}
	}
}


void    
egg_cleanup_perform (void)
{
	GSList *cleanups, *l;
	EggCleanup *cleanup;
	
	while (registered_cleanups) {
		
		/* 
		 * While performing cleanups, more cleanups may be registered.
		 * So swap out the list, and keep going until empty.
		 */
	 
		cleanups = registered_cleanups;
		registered_cleanups = NULL;
		
		for (l = cleanups; l; l = g_slist_next (l)) {
			cleanup = (EggCleanup*)l->data;
			g_assert (cleanup->notify);
			
			(cleanup->notify) (cleanup->user_data);
			g_free (cleanup);
		}
		
		g_slist_free (cleanups);
	}
}