Line |
Branch |
Exec |
Source |
1 |
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- |
2 |
|
|
* |
3 |
|
|
* Copyright (C) 2012 Colin Walters <walters@verbum.org>. |
4 |
|
|
* |
5 |
|
|
* This library is free software; you can redistribute it and/or |
6 |
|
|
* modify it under the terms of the GNU Lesser General Public |
7 |
|
|
* License as published by the Free Software Foundation; either |
8 |
|
|
* version 2 of the License, or (at your option) any later version. |
9 |
|
|
* |
10 |
|
|
* This library is distributed in the hope that it will be useful, |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
|
|
* Lesser General Public License for more details. |
14 |
|
|
* |
15 |
|
|
* You should have received a copy of the GNU Lesser General Public |
16 |
|
|
* License along with this library; if not, write to the |
17 |
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
|
|
* Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifndef __GSYSTEM_LOCAL_ALLOC_H__ |
22 |
|
|
#define __GSYSTEM_LOCAL_ALLOC_H__ |
23 |
|
|
|
24 |
|
|
#include <gio/gio.h> |
25 |
|
|
|
26 |
|
|
G_BEGIN_DECLS |
27 |
|
|
|
28 |
|
|
#define GS_DEFINE_CLEANUP_FUNCTION(Type, name, func) \ |
29 |
|
|
static inline void name (void *v) \ |
30 |
|
|
{ \ |
31 |
|
|
func (*(Type*)v); \ |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
#define GS_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \ |
35 |
|
|
static inline void name (void *v) \ |
36 |
|
|
{ \ |
37 |
|
|
if (*(Type*)v) \ |
38 |
|
|
func (*(Type*)v); \ |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/* These functions shouldn't be invoked directly; |
42 |
|
|
* they are stubs that: |
43 |
|
|
* 1) Take a pointer to the location (typically itself a pointer). |
44 |
|
|
* 2) Provide %NULL-safety where it doesn't exist already (e.g. g_object_unref) |
45 |
|
|
*/ |
46 |
|
|
|
47 |
|
|
/** |
48 |
|
|
* gs_free: |
49 |
|
|
* |
50 |
|
|
* Call g_free() on a variable location when it goes out of scope. |
51 |
|
|
*/ |
52 |
|
|
#define gs_free __attribute__ ((cleanup(gs_local_free))) |
53 |
|
7 |
GS_DEFINE_CLEANUP_FUNCTION(void*, gs_local_free, g_free) |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* gs_unref_object: |
57 |
|
|
* |
58 |
|
|
* Call g_object_unref() on a variable location when it goes out of |
59 |
|
|
* scope. Note that unlike g_object_unref(), the variable may be |
60 |
|
|
* %NULL. |
61 |
|
|
*/ |
62 |
|
|
#define gs_unref_object __attribute__ ((cleanup(gs_local_obj_unref))) |
63 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GObject*, gs_local_obj_unref, g_object_unref) |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* gs_unref_variant: |
67 |
|
|
* |
68 |
|
|
* Call g_variant_unref() on a variable location when it goes out of |
69 |
|
|
* scope. Note that unlike g_variant_unref(), the variable may be |
70 |
|
|
* %NULL. |
71 |
|
|
*/ |
72 |
|
|
#define gs_unref_variant __attribute__ ((cleanup(gs_local_variant_unref))) |
73 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GVariant*, gs_local_variant_unref, g_variant_unref) |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* gs_free_variant_iter: |
77 |
|
|
* |
78 |
|
|
* Call g_variant_iter_free() on a variable location when it goes out of |
79 |
|
|
* scope. |
80 |
|
|
*/ |
81 |
|
|
#define gs_free_variant_iter __attribute__ ((cleanup(gs_local_variant_iter_free))) |
82 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GVariantIter*, gs_local_variant_iter_free, g_variant_iter_free) |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* gs_free_variant_builder: |
86 |
|
|
* |
87 |
|
|
* Call g_variant_builder_unref() on a variable location when it goes out of |
88 |
|
|
* scope. |
89 |
|
|
*/ |
90 |
|
|
#define gs_unref_variant_builder __attribute__ ((cleanup(gs_local_variant_builder_unref))) |
91 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GVariantBuilder*, gs_local_variant_builder_unref, g_variant_builder_unref) |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* gs_unref_array: |
95 |
|
|
* |
96 |
|
|
* Call g_array_unref() on a variable location when it goes out of |
97 |
|
|
* scope. Note that unlike g_array_unref(), the variable may be |
98 |
|
|
* %NULL. |
99 |
|
|
|
100 |
|
|
*/ |
101 |
|
|
#define gs_unref_array __attribute__ ((cleanup(gs_local_array_unref))) |
102 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GArray*, gs_local_array_unref, g_array_unref) |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* gs_unref_ptrarray: |
106 |
|
|
* |
107 |
|
|
* Call g_ptr_array_unref() on a variable location when it goes out of |
108 |
|
|
* scope. Note that unlike g_ptr_array_unref(), the variable may be |
109 |
|
|
* %NULL. |
110 |
|
|
|
111 |
|
|
*/ |
112 |
|
|
#define gs_unref_ptrarray __attribute__ ((cleanup(gs_local_ptrarray_unref))) |
113 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, gs_local_ptrarray_unref, g_ptr_array_unref) |
114 |
|
|
|
115 |
|
|
/** |
116 |
|
|
* gs_unref_hashtable: |
117 |
|
|
* |
118 |
|
|
* Call g_hash_table_unref() on a variable location when it goes out |
119 |
|
|
* of scope. Note that unlike g_hash_table_unref(), the variable may |
120 |
|
|
* be %NULL. |
121 |
|
|
*/ |
122 |
|
|
#define gs_unref_hashtable __attribute__ ((cleanup(gs_local_hashtable_unref))) |
123 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GHashTable*, gs_local_hashtable_unref, g_hash_table_unref) |
124 |
|
|
|
125 |
|
|
/** |
126 |
|
|
* gs_free_list: |
127 |
|
|
* |
128 |
|
|
* Call g_list_free() on a variable location when it goes out |
129 |
|
|
* of scope. |
130 |
|
|
*/ |
131 |
|
|
#define gs_free_list __attribute__ ((cleanup(gs_local_free_list))) |
132 |
|
|
GS_DEFINE_CLEANUP_FUNCTION(GList*, gs_local_free_list, g_list_free) |
133 |
|
|
|
134 |
|
|
/** |
135 |
|
|
* gs_free_slist: |
136 |
|
|
* |
137 |
|
|
* Call g_slist_free() on a variable location when it goes out |
138 |
|
|
* of scope. |
139 |
|
|
*/ |
140 |
|
|
#define gs_free_slist __attribute__ ((cleanup(gs_local_free_slist))) |
141 |
|
|
GS_DEFINE_CLEANUP_FUNCTION(GSList*, gs_local_free_slist, g_slist_free) |
142 |
|
|
|
143 |
|
|
/** |
144 |
|
|
* gs_free_checksum: |
145 |
|
|
* |
146 |
|
|
* Call g_checksum_free() on a variable location when it goes out |
147 |
|
|
* of scope. Note that unlike g_checksum_free(), the variable may |
148 |
|
|
* be %NULL. |
149 |
|
|
*/ |
150 |
|
|
#define gs_free_checksum __attribute__ ((cleanup(gs_local_checksum_free))) |
151 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GChecksum*, gs_local_checksum_free, g_checksum_free) |
152 |
|
|
|
153 |
|
|
/** |
154 |
|
|
* gs_unref_bytes: |
155 |
|
|
* |
156 |
|
|
* Call g_bytes_unref() on a variable location when it goes out |
157 |
|
|
* of scope. Note that unlike g_bytes_unref(), the variable may |
158 |
|
|
* be %NULL. |
159 |
|
|
*/ |
160 |
|
|
#define gs_unref_bytes __attribute__ ((cleanup(gs_local_bytes_unref))) |
161 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GBytes*, gs_local_bytes_unref, g_bytes_unref) |
162 |
|
|
|
163 |
|
|
/** |
164 |
|
|
* gs_strfreev: |
165 |
|
|
* |
166 |
|
|
* Call g_strfreev() on a variable location when it goes out of scope. |
167 |
|
|
*/ |
168 |
|
|
#define gs_strfreev __attribute__ ((cleanup(gs_local_strfreev))) |
169 |
|
|
GS_DEFINE_CLEANUP_FUNCTION(char**, gs_local_strfreev, g_strfreev) |
170 |
|
|
|
171 |
|
|
/** |
172 |
|
|
* gs_free_error: |
173 |
|
|
* |
174 |
|
|
* Call g_error_free() on a variable location when it goes out of scope. |
175 |
|
|
*/ |
176 |
|
|
#define gs_free_error __attribute__ ((cleanup(gs_local_free_error))) |
177 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GError*, gs_local_free_error, g_error_free) |
178 |
|
|
|
179 |
|
|
/** |
180 |
|
|
* gs_unref_keyfile: |
181 |
|
|
* |
182 |
|
|
* Call g_key_file_unref() on a variable location when it goes out of scope. |
183 |
|
|
*/ |
184 |
|
|
#define gs_unref_keyfile __attribute__ ((cleanup(gs_local_keyfile_unref))) |
185 |
|
|
GS_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, gs_local_keyfile_unref, g_key_file_unref) |
186 |
|
|
|
187 |
|
|
static inline void |
188 |
|
|
gs_cleanup_close_fdp (int *fdp) |
189 |
|
|
{ |
190 |
|
|
int fd; |
191 |
|
|
|
192 |
|
|
g_assert (fdp); |
193 |
|
|
|
194 |
|
|
fd = *fdp; |
195 |
|
|
if (fd != -1) |
196 |
|
|
(void) close (fd); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
|
|
* gs_fd_close: |
201 |
|
|
* |
202 |
|
|
* Call close() on a variable location when it goes out of scope. |
203 |
|
|
*/ |
204 |
|
|
#define gs_fd_close __attribute__((cleanup(gs_cleanup_close_fdp))) |
205 |
|
|
|
206 |
|
|
G_END_DECLS |
207 |
|
|
|
208 |
|
|
#endif |
209 |
|
|
|