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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 | /*
* panel-cleanup.c: utility to clean up things on exit
*
* Copyright (C) 2008 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Authors:
* Vincent Untz <vuntz@gnome.org>
*/
#include <glib.h>
#include <glib-object.h>
#include "panel-cleanup.h"
typedef struct {
PanelCleanFunc func;
gpointer data;
} PanelClean;
static GSList *cleaner;
void
panel_cleanup_do (void)
{
GSList *l;
if (!cleaner)
return;
for (l = cleaner; l; l = l->next) {
PanelClean *clean;
clean = l->data;
clean->func (clean->data);
g_slice_free (PanelClean, clean);
}
g_slist_free (cleaner);
cleaner = NULL;
}
void
panel_cleanup_register (PanelCleanFunc func,
gpointer data)
{
PanelClean *clean;
g_return_if_fail (func != NULL);
clean = g_slice_new (PanelClean);
clean->func = func;
clean->data = data;
cleaner = g_slist_prepend (cleaner, clean);
}
void
panel_cleanup_unregister (PanelCleanFunc func,
gpointer data)
{
GSList *l, *next;
PanelClean *clean;<--- The scope of the variable 'clean' can be reduced. [+]The scope of the variable 'clean' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
g_return_if_fail (func != NULL);
if (!cleaner)
return;
l = cleaner;
do {
next = l->next;
clean = l->data;
if (clean->func == func && clean->data == data) {
g_slice_free (PanelClean, clean);
cleaner = g_slist_delete_link (cleaner, l);
}
l = next;
} while (l);
}
void
panel_cleanup_unref_and_nullify (gpointer data)
{
GObject **obj;
g_return_if_fail (data != NULL);
obj = data;
g_object_unref (*obj);
*obj = NULL;
}
|