00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __CR_CLEANUP_H__
00026 #define __CR_CLEANUP_H__
00027
00028 #include <stdio.h>
00029 #include <glib.h>
00030 #include <unistd.h>
00031
00032 G_BEGIN_DECLS
00033
00034 static void
00035 my_close(int fildes)
00036 {
00037 if (fildes < 0)
00038 return;
00039 close(fildes);
00040 }
00041
00042 #define CR_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
00043 static inline void name (void *v) \
00044 { \
00045 func (*(Type*)v); \
00046 }
00047
00048 #define CR_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \
00049 static inline void name (void *v) \
00050 { \
00051 if (*(Type*)v) \
00052 func (*(Type*)v); \
00053 }
00054
00055 #define CR_DEFINE_CLEANUP_FUNCTIONt(Type, name, func) \
00056 static inline void name (void *v) \
00057 { \
00058 if (*(Type*)v) \
00059 func (*(Type*)v, TRUE); \
00060 }
00061
00062 CR_DEFINE_CLEANUP_FUNCTION0(FILE*, cr_local_file_fclose, fclose)
00063 CR_DEFINE_CLEANUP_FUNCTION0(GArray*, cr_local_array_unref, g_array_unref)
00064 CR_DEFINE_CLEANUP_FUNCTION0(GChecksum*, cr_local_checksum_free, g_checksum_free)
00065 CR_DEFINE_CLEANUP_FUNCTION0(GDir*, cr_local_dir_close, g_dir_close)
00066 CR_DEFINE_CLEANUP_FUNCTION0(GError*, cr_local_free_error, g_error_free)
00067 CR_DEFINE_CLEANUP_FUNCTION0(GHashTable*, cr_local_hashtable_unref, g_hash_table_unref)
00068 CR_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, cr_local_keyfile_free, g_key_file_free)
00069 #if GLIB_CHECK_VERSION(2, 32, 0)
00070 CR_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, cr_local_keyfile_unref, g_key_file_unref)
00071 #endif
00072 CR_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, cr_local_ptrarray_unref, g_ptr_array_unref)
00073 CR_DEFINE_CLEANUP_FUNCTION0(GTimer*, cr_local_destroy_timer, g_timer_destroy)
00074
00075 CR_DEFINE_CLEANUP_FUNCTIONt(GString*, cr_local_free_string, g_string_free)
00076
00077 CR_DEFINE_CLEANUP_FUNCTION(char**, cr_local_strfreev, g_strfreev)
00078 CR_DEFINE_CLEANUP_FUNCTION(GList*, cr_local_free_list, g_list_free)
00079 CR_DEFINE_CLEANUP_FUNCTION(void*, cr_local_free, g_free)
00080 CR_DEFINE_CLEANUP_FUNCTION(int, cr_local_file_close, my_close)
00081
00082 #define _cleanup_array_unref_ __attribute__ ((cleanup(cr_local_array_unref)))
00083 #define _cleanup_checksum_free_ __attribute__ ((cleanup(cr_local_checksum_free)))
00084 #define _cleanup_dir_close_ __attribute__ ((cleanup(cr_local_dir_close)))
00085 #define _cleanup_error_free_ __attribute__ ((cleanup(cr_local_free_error)))
00086 #define _cleanup_file_close_ __attribute__ ((cleanup(cr_local_file_close)))
00087 #define _cleanup_file_fclose_ __attribute__ ((cleanup(cr_local_file_fclose)))
00088 #define _cleanup_free_ __attribute__ ((cleanup(cr_local_free)))
00089 #define _cleanup_hashtable_unref_ __attribute__ ((cleanup(cr_local_hashtable_unref)))
00090 #define _cleanup_keyfile_free_ __attribute__ ((cleanup(cr_local_keyfile_free)))
00091 #define _cleanup_keyfile_unref_ __attribute__ ((cleanup(cr_local_keyfile_unref)))
00092 #define _cleanup_list_free_ __attribute__ ((cleanup(cr_local_free_list)))
00093 #define _cleanup_ptrarray_unref_ __attribute__ ((cleanup(cr_local_ptrarray_unref)))
00094 #define _cleanup_string_free_ __attribute__ ((cleanup(cr_local_free_string)))
00095 #define _cleanup_strv_free_ __attribute__ ((cleanup(cr_local_strfreev)))
00096 #define _cleanup_timer_destroy_ __attribute__ ((cleanup(cr_local_destroy_timer)))
00097
00098 G_END_DECLS
00099
00100 #endif