1 #ifndef __ALLOC_H__
2 #define __ALLOC_H__
3 
4 #include <gio/gio.h>
5 
6 G_BEGIN_DECLS
7 
8 #define DEFINE_CLEANUP_FUNCTION_FULL(Type, func, null_safe, args...)                                                   \
9 	static inline void __cleanup_##func(void *v)                                                                   \
10 	{                                                                                                              \
11 		if (null_safe || *(Type *)v)                                                                           \
12 			func(*(Type *)v, ##args);                                                                      \
13 	}
14 
15 #define DEFINE_CLEANUP_FUNCTION(Type, func) DEFINE_CLEANUP_FUNCTION_FULL(Type, func, TRUE)
16 
17 #define DEFINE_CLEANUP_FUNCTION_NULL(Type, func) DEFINE_CLEANUP_FUNCTION_FULL(Type, func, FALSE)
18 
19 #define CLEANUP(func) __attribute__((cleanup(__cleanup_##func)))
20 
21 DEFINE_CLEANUP_FUNCTION(void *, g_free)
22 #define gc_free CLEANUP(g_free)
23 
24 DEFINE_CLEANUP_FUNCTION(char **, g_strfreev)
25 #define gc_strfreev CLEANUP(g_strfreev)
26 
27 DEFINE_CLEANUP_FUNCTION_NULL(GError *, g_error_free)
28 #define gc_error_free CLEANUP(g_error_free)
29 
30 DEFINE_CLEANUP_FUNCTION_NULL(GArray *, g_array_unref)
31 #define gc_array_unref CLEANUP(g_array_unref)
32 
33 DEFINE_CLEANUP_FUNCTION_NULL(GPtrArray *, g_ptr_array_unref)
34 #define gc_ptr_array_unref CLEANUP(g_ptr_array_unref)
35 
36 DEFINE_CLEANUP_FUNCTION_NULL(GByteArray *, g_byte_array_unref)
37 #define gc_byte_array_unref CLEANUP(g_byte_array_unref)
38 
39 DEFINE_CLEANUP_FUNCTION_NULL(GHashTable *, g_hash_table_unref)
40 #define gc_hash_table_unref CLEANUP(g_hash_table_unref)
41 
42 DEFINE_CLEANUP_FUNCTION_NULL(GVariant *, g_variant_unref)
43 #define gc_variant_unref CLEANUP(g_variant_unref)
44 
45 DEFINE_CLEANUP_FUNCTION_NULL(GVariantIter *, g_variant_iter_free)
46 #define gc_variant_iter_free CLEANUP(g_variant_iter_free)
47 
48 DEFINE_CLEANUP_FUNCTION_NULL(GVariantBuilder *, g_variant_builder_unref)
49 #define gc_variant_builder_unref CLEANUP(g_variant_builder_unref)
50 
51 DEFINE_CLEANUP_FUNCTION_NULL(GBytes *, g_bytes_unref)
52 #define gc_bytes_unref CLEANUP(g_bytes_unref)
53 
54 DEFINE_CLEANUP_FUNCTION_NULL(GRegex *, g_regex_unref)
55 #define gc_regex_unref CLEANUP(g_regex_unref)
56 
57 DEFINE_CLEANUP_FUNCTION_NULL(GMatchInfo *, g_match_info_unref)
58 #define gc_match_info_unref CLEANUP(g_match_info_unref)
59 
60 DEFINE_CLEANUP_FUNCTION_NULL(GKeyFile *, g_key_file_unref)
61 #define gc_key_file_unref CLEANUP(g_key_file_unref)
62 
63 DEFINE_CLEANUP_FUNCTION_NULL(GChecksum *, g_checksum_free)
64 #define gc_checksum_free CLEANUP(g_checksum_free)
65 
66 DEFINE_CLEANUP_FUNCTION_NULL(GObject *, g_object_unref)
67 #define gc_object_unref CLEANUP(g_object_unref)
68 
69 DEFINE_CLEANUP_FUNCTION_FULL(GString *, g_string_free, FALSE, TRUE)
70 #define gc_string_free CLEANUP(g_string_free)
71 
72 G_END_DECLS
73 
74 #endif
75