diff --git a/Makefile-otutil.am b/Makefile-otutil.am index dbe1763b..4493564e 100644 --- a/Makefile-otutil.am +++ b/Makefile-otutil.am @@ -20,6 +20,8 @@ noinst_LTLIBRARIES += libotutil.la libotutil_la_SOURCES = \ + src/libotutil/ot-local-alloc.c \ + src/libotutil/ot-local-alloc.h \ src/libotutil/ot-opt-utils.c \ src/libotutil/ot-opt-utils.h \ src/libotutil/ot-unix-utils.c \ diff --git a/src/libotutil/ot-local-alloc.c b/src/libotutil/ot-local-alloc.c new file mode 100644 index 00000000..ab62e0d8 --- /dev/null +++ b/src/libotutil/ot-local-alloc.c @@ -0,0 +1,67 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2012 Colin Walters + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Colin Walters + */ + +#include "config.h" + +#include "otutil.h" + +void +ot_local_free (void *loc) +{ + void **location = loc; + if (location) + g_free (*location); +} + +#define _ot_local_free(type, function) do { \ + void **location = loc; \ + if (location) \ + { \ + type *value = *location; \ + if (value) \ + function (value); \ + } \ + } while (0) + +void +ot_local_obj_unref (void *loc) +{ + _ot_local_free(GObject, g_object_unref); +} + +void +ot_local_variant_unref (void *loc) +{ + _ot_local_free(GVariant, g_variant_unref); +} + +void +ot_local_ptrarray_unref (void *loc) +{ + _ot_local_free(GPtrArray, g_ptr_array_unref); +} + +void +ot_local_hashtable_unref (void *loc) +{ + _ot_local_free(GHashTable, g_hash_table_unref); +} diff --git a/src/libotutil/ot-local-alloc.h b/src/libotutil/ot-local-alloc.h new file mode 100644 index 00000000..2aeea757 --- /dev/null +++ b/src/libotutil/ot-local-alloc.h @@ -0,0 +1,44 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters . + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Colin Walters + */ + +#ifndef __OSTREE_LOCAL_ALLOC_H__ +#define __OSTREE_LOCAL_ALLOC_H__ + +#include + +G_BEGIN_DECLS + +void ot_local_free (void *loc); +void ot_local_obj_unref (void *loc); +void ot_local_variant_unref (void *loc); +void ot_local_ptrarray_unref (void *loc); +void ot_local_hashtable_unref (void *loc); + +#define ot_lfree __attribute__ ((cleanup(ot_local_free))) +#define ot_lobj __attribute__ ((cleanup(ot_local_obj_unref))) +#define ot_lvariant __attribute__ ((cleanup(ot_local_variant_unref))) +#define ot_lptrarray __attribute__ ((cleanup(ot_local_ptrarray_unref))) +#define ot_lhash __attribute__ ((cleanup(ot_local_hashtable_unref))) + +G_END_DECLS + +#endif diff --git a/src/libotutil/ot-variant-utils.c b/src/libotutil/ot-variant-utils.c index 655fd5ff..f30cb17a 100644 --- a/src/libotutil/ot-variant-utils.c +++ b/src/libotutil/ot-variant-utils.c @@ -66,7 +66,7 @@ ot_util_variant_save (GFile *dest, GError **error) { gboolean ret = FALSE; - GOutputStream *out = NULL; + ot_lobj GOutputStream *out = NULL; gsize bytes_written; out = (GOutputStream*)g_file_replace (dest, NULL, 0, FALSE, cancellable, error); @@ -85,7 +85,6 @@ ot_util_variant_save (GFile *dest, ret = TRUE; out: - g_clear_object (&out); return ret; } @@ -116,7 +115,7 @@ ot_util_variant_map (GFile *src, gboolean ret = FALSE; GMappedFile *mfile = NULL; const char *path = NULL; - GVariant *ret_variant = NULL; + ot_lvariant GVariant *ret_variant = NULL; path = ot_gfile_get_path_cached (src); mfile = g_mapped_file_new (path, FALSE, error); @@ -135,7 +134,6 @@ ot_util_variant_map (GFile *src, ret = TRUE; ot_transfer_out_value(out_variant, &ret_variant); out: - ot_clear_gvariant (&ret_variant); if (mfile) g_mapped_file_unref (mfile); return ret; @@ -156,8 +154,8 @@ ot_util_variant_from_stream (GInputStream *src, GError **error) { gboolean ret = FALSE; - GMemoryOutputStream *data_stream = NULL; - GVariant *ret_variant = NULL; + ot_lobj GMemoryOutputStream *data_stream = NULL; + ot_lvariant GVariant *ret_variant = NULL; data_stream = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free); @@ -175,7 +173,5 @@ ot_util_variant_from_stream (GInputStream *src, ret = TRUE; ot_transfer_out_value (out_variant, &ret_variant); out: - g_clear_object (&data_stream); - ot_clear_gvariant (&ret_variant); return ret; } diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h index 478026cb..d6c9fbcb 100644 --- a/src/libotutil/otutil.h +++ b/src/libotutil/otutil.h @@ -41,6 +41,7 @@ } \ } G_STMT_END; +#include #include #include #include diff --git a/src/ostree/ot-builtin-ls.c b/src/ostree/ot-builtin-ls.c index 5dd29e81..4f57f7de 100644 --- a/src/ostree/ot-builtin-ls.c +++ b/src/ostree/ot-builtin-ls.c @@ -197,12 +197,12 @@ ostree_builtin_ls (int argc, char **argv, GFile *repo_path, GError **error) { GOptionContext *context; gboolean ret = FALSE; - OstreeRepo *repo = NULL; + ot_lobj OstreeRepo *repo = NULL; const char *rev; int i; - GFile *root = NULL; - GFile *f = NULL; - GFileInfo *file_info = NULL; + ot_lobj GFile *root = NULL; + ot_lobj GFile *f = NULL; + ot_lobj GFileInfo *file_info = NULL; context = g_option_context_new ("COMMIT PATH [PATH...] - List file paths"); g_option_context_add_main_entries (context, options, NULL); @@ -247,11 +247,7 @@ ostree_builtin_ls (int argc, char **argv, GFile *repo_path, GError **error) ret = TRUE; out: - g_clear_object (&root); - g_clear_object (&f); - g_clear_object (&file_info); if (context) g_option_context_free (context); - g_clear_object (&repo); return ret; }