1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3 * Copyright (C) 2012 Colin Walters <walters@verbum.org>.
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.
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.
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.
21 #ifndef __GSYSTEM_LOCAL_ALLOC_H__
22 #define __GSYSTEM_LOCAL_ALLOC_H__
28 #define GS_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
29 static inline void name (void *v) \
34 #define GS_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \
35 static inline void name (void *v) \
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)
50 * Call g_free() on a variable location when it goes out of scope.
52 #define gs_free __attribute__ ((cleanup(gs_local_free)))
53 GS_DEFINE_CLEANUP_FUNCTION(void*, gs_local_free, g_free)
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
62 #define gs_unref_object __attribute__ ((cleanup(gs_local_obj_unref)))
63 GS_DEFINE_CLEANUP_FUNCTION0(GObject*, gs_local_obj_unref, g_object_unref)
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
72 #define gs_unref_variant __attribute__ ((cleanup(gs_local_variant_unref)))
73 GS_DEFINE_CLEANUP_FUNCTION0(GVariant*, gs_local_variant_unref, g_variant_unref)
76 * gs_free_variant_iter:
78 * Call g_variant_iter_free() on a variable location when it goes out of
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)
85 * gs_free_variant_builder:
87 * Call g_variant_builder_unref() on a variable location when it goes out of
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)
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
101 #define gs_unref_array __attribute__ ((cleanup(gs_local_array_unref)))
102 GS_DEFINE_CLEANUP_FUNCTION0(GArray*, gs_local_array_unref, g_array_unref)
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
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)
116 * gs_unref_hashtable:
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
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)
128 * Call g_list_free() on a variable location when it goes out
131 #define gs_free_list __attribute__ ((cleanup(gs_local_free_list)))
132 GS_DEFINE_CLEANUP_FUNCTION(GList*, gs_local_free_list, g_list_free)
137 * Call g_slist_free() on a variable location when it goes out
140 #define gs_free_slist __attribute__ ((cleanup(gs_local_free_slist)))
141 GS_DEFINE_CLEANUP_FUNCTION(GSList*, gs_local_free_slist, g_slist_free)
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
150 #define gs_free_checksum __attribute__ ((cleanup(gs_local_checksum_free)))
151 GS_DEFINE_CLEANUP_FUNCTION0(GChecksum*, gs_local_checksum_free, g_checksum_free)
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
160 #define gs_unref_bytes __attribute__ ((cleanup(gs_local_bytes_unref)))
161 GS_DEFINE_CLEANUP_FUNCTION0(GBytes*, gs_local_bytes_unref, g_bytes_unref)
166 * Call g_strfreev() on a variable location when it goes out of scope.
168 #define gs_strfreev __attribute__ ((cleanup(gs_local_strfreev)))
169 GS_DEFINE_CLEANUP_FUNCTION(char**, gs_local_strfreev, g_strfreev)
174 * Call g_error_free() on a variable location when it goes out of scope.
176 #define gs_free_error __attribute__ ((cleanup(gs_local_free_error)))
177 GS_DEFINE_CLEANUP_FUNCTION0(GError*, gs_local_free_error, g_error_free)
182 * Call g_key_file_unref() on a variable location when it goes out of scope.
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)
188 gs_cleanup_close_fdp (int *fdp)
202 * Call close() on a variable location when it goes out of scope.
204 #define gs_fd_close __attribute__((cleanup(gs_cleanup_close_fdp)))