shared/gsystem-local-alloc: strongly type gs_* cleanup functions

At various places, use the correct type for the pointer, this
allows the compiler to be more helpful.

For gs_free, gs_unref_object, and nm_auto_free, the pointer type is
of course still 'void *'.

This catches wrong uses like

   gs_strfreev char *wrong1 = NULL;
   gs_strfreev const char **wrong2 = NULL;
   gs_free_error GError **p_error = NULL;
   gs_unref_array GPtrArray *ptr_array = NULL;

Note that long time ago we copied "gsystem-local-alloc.h" header
from libgsystem library. Until now, we didn't apply any local
modification to this file, to keep it in sync with upstream.
However, upstream libgsystem is not maintained anymore, so there
is no reason to stay in sync with upstream.
This commit is contained in:
Thomas Haller
2018-07-11 14:41:22 +02:00
parent 7df4fc3849
commit 98c71df8a3
2 changed files with 40 additions and 21 deletions

View File

@@ -25,17 +25,36 @@
G_BEGIN_DECLS
#define GS_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
#define GS_DEFINE_CLEANUP_FUNCTION_VOID(CastType, name, func) \
static inline void name (void *v) \
{ \
func (*(Type*)v); \
func (*((CastType *) v)); \
}
#define GS_DEFINE_CLEANUP_FUNCTION_VOID0(CastType, name, func) \
static inline void name (void *v) \
{ \
if (*((CastType *) v)) \
func (*((CastType *) v)); \
}
#define GS_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
static inline void name (Type *v) \
{ \
func (*v); \
}
#define GS_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \
static inline void name (void *v) \
static inline void name (Type *v) \
{ \
if (*(Type*)v) \
func (*(Type*)v); \
if (*v) \
func (*v); \
}
#define GS_DEFINE_CLEANUP_FUNCTION_STRUCT(Type, name, func) \
static inline void name (Type *v) \
{ \
func (v); \
}
/* These functions shouldn't be invoked directly;
@@ -50,7 +69,7 @@ G_BEGIN_DECLS
* Call g_free() on a variable location when it goes out of scope.
*/
#define gs_free __attribute__ ((cleanup(gs_local_free)))
GS_DEFINE_CLEANUP_FUNCTION(void*, gs_local_free, g_free)
GS_DEFINE_CLEANUP_FUNCTION_VOID (void *, gs_local_free, g_free)
/**
* gs_unref_object:
@@ -60,7 +79,7 @@ GS_DEFINE_CLEANUP_FUNCTION(void*, gs_local_free, g_free)
* %NULL.
*/
#define gs_unref_object __attribute__ ((cleanup(gs_local_obj_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GObject*, gs_local_obj_unref, g_object_unref)
GS_DEFINE_CLEANUP_FUNCTION_VOID0 (GObject *, gs_local_obj_unref, g_object_unref)
/**
* gs_unref_variant:
@@ -70,7 +89,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GObject*, gs_local_obj_unref, g_object_unref)
* %NULL.
*/
#define gs_unref_variant __attribute__ ((cleanup(gs_local_variant_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GVariant*, gs_local_variant_unref, g_variant_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GVariant *, gs_local_variant_unref, g_variant_unref)
/**
* gs_free_variant_iter:
@@ -79,7 +98,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GVariant*, gs_local_variant_unref, g_variant_unref)
* scope.
*/
#define gs_free_variant_iter __attribute__ ((cleanup(gs_local_variant_iter_free)))
GS_DEFINE_CLEANUP_FUNCTION0(GVariantIter*, gs_local_variant_iter_free, g_variant_iter_free)
GS_DEFINE_CLEANUP_FUNCTION_STRUCT(GVariantIter, gs_local_variant_iter_free, g_variant_iter_free)
/**
* gs_free_variant_builder:
@@ -88,7 +107,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GVariantIter*, gs_local_variant_iter_free, g_variant
* scope.
*/
#define gs_unref_variant_builder __attribute__ ((cleanup(gs_local_variant_builder_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GVariantBuilder*, gs_local_variant_builder_unref, g_variant_builder_unref)
GS_DEFINE_CLEANUP_FUNCTION_STRUCT(GVariantBuilder, gs_local_variant_builder_unref, g_variant_builder_unref)
/**
* gs_unref_array:
@@ -99,7 +118,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GVariantBuilder*, gs_local_variant_builder_unref, g_
*/
#define gs_unref_array __attribute__ ((cleanup(gs_local_array_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GArray*, gs_local_array_unref, g_array_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GArray *, gs_local_array_unref, g_array_unref)
/**
* gs_unref_ptrarray:
@@ -110,7 +129,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GArray*, gs_local_array_unref, g_array_unref)
*/
#define gs_unref_ptrarray __attribute__ ((cleanup(gs_local_ptrarray_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, gs_local_ptrarray_unref, g_ptr_array_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GPtrArray *, gs_local_ptrarray_unref, g_ptr_array_unref)
/**
* gs_unref_hashtable:
@@ -120,7 +139,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, gs_local_ptrarray_unref, g_ptr_array_unr
* be %NULL.
*/
#define gs_unref_hashtable __attribute__ ((cleanup(gs_local_hashtable_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GHashTable*, gs_local_hashtable_unref, g_hash_table_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GHashTable *, gs_local_hashtable_unref, g_hash_table_unref)
/**
* gs_free_list:
@@ -129,7 +148,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GHashTable*, gs_local_hashtable_unref, g_hash_table_
* of scope.
*/
#define gs_free_list __attribute__ ((cleanup(gs_local_free_list)))
GS_DEFINE_CLEANUP_FUNCTION(GList*, gs_local_free_list, g_list_free)
GS_DEFINE_CLEANUP_FUNCTION (GList *, gs_local_free_list, g_list_free)
/**
* gs_free_slist:
@@ -138,7 +157,7 @@ GS_DEFINE_CLEANUP_FUNCTION(GList*, gs_local_free_list, g_list_free)
* of scope.
*/
#define gs_free_slist __attribute__ ((cleanup(gs_local_free_slist)))
GS_DEFINE_CLEANUP_FUNCTION(GSList*, gs_local_free_slist, g_slist_free)
GS_DEFINE_CLEANUP_FUNCTION (GSList *, gs_local_free_slist, g_slist_free)
/**
* gs_free_checksum:
@@ -148,7 +167,7 @@ GS_DEFINE_CLEANUP_FUNCTION(GSList*, gs_local_free_slist, g_slist_free)
* be %NULL.
*/
#define gs_free_checksum __attribute__ ((cleanup(gs_local_checksum_free)))
GS_DEFINE_CLEANUP_FUNCTION0(GChecksum*, gs_local_checksum_free, g_checksum_free)
GS_DEFINE_CLEANUP_FUNCTION0 (GChecksum *, gs_local_checksum_free, g_checksum_free)
/**
* gs_unref_bytes:
@@ -158,7 +177,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GChecksum*, gs_local_checksum_free, g_checksum_free)
* be %NULL.
*/
#define gs_unref_bytes __attribute__ ((cleanup(gs_local_bytes_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GBytes*, gs_local_bytes_unref, g_bytes_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GBytes *, gs_local_bytes_unref, g_bytes_unref)
/**
* gs_strfreev:
@@ -166,7 +185,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GBytes*, gs_local_bytes_unref, g_bytes_unref)
* Call g_strfreev() on a variable location when it goes out of scope.
*/
#define gs_strfreev __attribute__ ((cleanup(gs_local_strfreev)))
GS_DEFINE_CLEANUP_FUNCTION(char**, gs_local_strfreev, g_strfreev)
GS_DEFINE_CLEANUP_FUNCTION (char **, gs_local_strfreev, g_strfreev)
/**
* gs_free_error:
@@ -174,7 +193,7 @@ GS_DEFINE_CLEANUP_FUNCTION(char**, gs_local_strfreev, g_strfreev)
* Call g_error_free() on a variable location when it goes out of scope.
*/
#define gs_free_error __attribute__ ((cleanup(gs_local_free_error)))
GS_DEFINE_CLEANUP_FUNCTION0(GError*, gs_local_free_error, g_error_free)
GS_DEFINE_CLEANUP_FUNCTION0 (GError *, gs_local_free_error, g_error_free)
/**
* gs_unref_keyfile:
@@ -182,7 +201,7 @@ GS_DEFINE_CLEANUP_FUNCTION0(GError*, gs_local_free_error, g_error_free)
* Call g_key_file_unref() on a variable location when it goes out of scope.
*/
#define gs_unref_keyfile __attribute__ ((cleanup(gs_local_keyfile_unref)))
GS_DEFINE_CLEANUP_FUNCTION0(GKeyFile*, gs_local_keyfile_unref, g_key_file_unref)
GS_DEFINE_CLEANUP_FUNCTION0 (GKeyFile *, gs_local_keyfile_unref, g_key_file_unref)
static inline void
gs_cleanup_close_fdp (int *fdp)

View File

@@ -73,7 +73,7 @@ static inline int nm_close (int fd);
* Call free() on a variable location when it goes out of scope.
*/
#define nm_auto_free nm_auto(_nm_auto_free_impl)
GS_DEFINE_CLEANUP_FUNCTION(void*, _nm_auto_free_impl, free)
GS_DEFINE_CLEANUP_FUNCTION_VOID (void *, _nm_auto_free_impl, free)
static inline void
nm_free_secret (char *secret)