Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* egg-cleanup.c - for data cleanup at end of program
3 :
4 : Copyright (C) 2007 Stefan Walter
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring 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 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stef@memberwebs.com>
21 : */
22 :
23 : #include "egg-cleanup.h"
24 :
25 : typedef struct _EggCleanup {
26 : GDestroyNotify notify;
27 : gpointer user_data;
28 : } EggCleanup;
29 :
30 : static GSList *registered_cleanups = NULL;
31 :
32 : void
33 290 : egg_cleanup_register (GDestroyNotify notify, gpointer user_data)
34 : {
35 290 : EggCleanup *cleanup = g_new0 (EggCleanup, 1);
36 :
37 290 : g_assert (notify);
38 290 : cleanup->notify = notify;
39 290 : cleanup->user_data = user_data;
40 :
41 : /* Note we're reversing the order, so calls happen that way */
42 290 : registered_cleanups = g_slist_prepend (registered_cleanups, cleanup);
43 290 : }
44 :
45 : void
46 2 : egg_cleanup_unregister (GDestroyNotify notify, gpointer user_data)
47 : {
48 : EggCleanup *cleanup;
49 : GSList *l;
50 :
51 2 : for (l = registered_cleanups; l; l = g_slist_next (l)) {
52 2 : cleanup = (EggCleanup*)l->data;
53 2 : if (cleanup->notify == notify && cleanup->user_data == user_data) {
54 2 : registered_cleanups = g_slist_remove (registered_cleanups, cleanup);
55 2 : g_free (cleanup);
56 2 : break;
57 : }
58 : }
59 2 : }
60 :
61 :
62 : void
63 31 : egg_cleanup_perform (void)
64 : {
65 : GSList *cleanups, *l;
66 : EggCleanup *cleanup;
67 :
68 62 : while (registered_cleanups) {
69 :
70 : /*
71 : * While performing cleanups, more cleanups may be registered.
72 : * So swap out the list, and keep going until empty.
73 : */
74 :
75 31 : cleanups = registered_cleanups;
76 31 : registered_cleanups = NULL;
77 :
78 319 : for (l = cleanups; l; l = g_slist_next (l)) {
79 288 : cleanup = (EggCleanup*)l->data;
80 288 : g_assert (cleanup->notify);
81 :
82 288 : (cleanup->notify) (cleanup->user_data);
83 288 : g_free (cleanup);
84 : }
85 :
86 31 : g_slist_free (cleanups);
87 : }
88 31 : }
|