createrepo_c library 1.2.1 C library for metadata manipulation

cleanup.h
1 /* createrepo_c - Library of routines for manipulation with repodata
2 *
3 * Copyright (C) 2012 Colin Walters <walters@verbum.org>.
4 * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
5 * Copyright (C) 2012 Tomas Mlcoch
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22
23 /* This file was taken from libhif (https://github.com/hughsie/libhif) */
24
25 #ifndef __CR_CLEANUP_H__
26 #define __CR_CLEANUP_H__
27
28 #include <stdio.h>
29 #include <glib.h>
30 #include <unistd.h>
31
32 G_BEGIN_DECLS
33
34 static void
35 my_close(int fildes)
36 {
37 if (fildes < 0)
38 return;
39 close(fildes);
40 }
41
42 #define CR_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
43 static inline void name (void *v) \
44 { \
45 func (*(Type*)v); \
46 }
47
48 #define CR_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \
49 static inline void name (void *v) \
50 { \
51 if (*(Type*)v) \
52 func (*(Type*)v); \
53 }
54
55 #define CR_DEFINE_CLEANUP_FUNCTIONt(Type, name, func) \
56 static inline void name (void *v) \
57 { \
58 if (*(Type*)v) \
59 func (*(Type*)v, TRUE); \
60 }
61
62 CR_DEFINE_CLEANUP_FUNCTION0(FILE*, cr_local_file_fclose, fclose)
63 CR_DEFINE_CLEANUP_FUNCTION0(GArray*, cr_local_array_unref, g_array_unref)
64 CR_DEFINE_CLEANUP_FUNCTION0(GChecksum*, cr_local_checksum_free, g_checksum_free)
65 CR_DEFINE_CLEANUP_FUNCTION0(GDir*, cr_local_dir_close, g_dir_close)
66 CR_DEFINE_CLEANUP_FUNCTION0(GError*, cr_local_free_error, g_error_free)
67 CR_DEFINE_CLEANUP_FUNCTION0(GHashTable*, cr_local_hashtable_unref, g_hash_table_unref)
68 CR_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, cr_local_keyfile_free, g_key_file_free)
69 #if GLIB_CHECK_VERSION(2, 32, 0)
70 CR_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, cr_local_keyfile_unref, g_key_file_unref)
71 #endif
72 CR_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, cr_local_ptrarray_unref, g_ptr_array_unref)
73 CR_DEFINE_CLEANUP_FUNCTION0(GTimer*, cr_local_destroy_timer, g_timer_destroy)
74
75 CR_DEFINE_CLEANUP_FUNCTIONt(GString*, cr_local_free_string, g_string_free)
76
77 CR_DEFINE_CLEANUP_FUNCTION(char**, cr_local_strfreev, g_strfreev)
78 CR_DEFINE_CLEANUP_FUNCTION(GList*, cr_local_free_list, g_list_free)
79 CR_DEFINE_CLEANUP_FUNCTION(void*, cr_local_free, g_free)
80 CR_DEFINE_CLEANUP_FUNCTION(int, cr_local_file_close, my_close)
81
82 #define _cleanup_array_unref_ __attribute__ ((cleanup(cr_local_array_unref)))
83 #define _cleanup_checksum_free_ __attribute__ ((cleanup(cr_local_checksum_free)))
84 #define _cleanup_dir_close_ __attribute__ ((cleanup(cr_local_dir_close)))
85 #define _cleanup_error_free_ __attribute__ ((cleanup(cr_local_free_error)))
86 #define _cleanup_file_close_ __attribute__ ((cleanup(cr_local_file_close)))
87 #define _cleanup_file_fclose_ __attribute__ ((cleanup(cr_local_file_fclose)))
88 #define _cleanup_free_ __attribute__ ((cleanup(cr_local_free)))
89 #define _cleanup_hashtable_unref_ __attribute__ ((cleanup(cr_local_hashtable_unref)))
90 #define _cleanup_keyfile_free_ __attribute__ ((cleanup(cr_local_keyfile_free)))
91 #define _cleanup_keyfile_unref_ __attribute__ ((cleanup(cr_local_keyfile_unref)))
92 #define _cleanup_list_free_ __attribute__ ((cleanup(cr_local_free_list)))
93 #define _cleanup_ptrarray_unref_ __attribute__ ((cleanup(cr_local_ptrarray_unref)))
94 #define _cleanup_string_free_ __attribute__ ((cleanup(cr_local_free_string)))
95 #define _cleanup_strv_free_ __attribute__ ((cleanup(cr_local_strfreev)))
96 #define _cleanup_timer_destroy_ __attribute__ ((cleanup(cr_local_destroy_timer)))
97
98 G_END_DECLS
99
100 #endif