[ostree] core: Add macros for local allocation
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ostree] core: Add macros for local allocation
- Date: Mon, 9 Apr 2012 22:43:35 +0000 (UTC)
commit ca08ad6c5e72f1b28aa2dab1d543166abb9f8622
Author: Colin Walters <walters verbum org>
Date: Mon Apr 9 14:04:02 2012 -0400
core: Add macros for local allocation
This is GCC-specific, but it makes the code significantly
cleaner.
Makefile-otutil.am | 2 +
src/libotutil/ot-local-alloc.c | 67 ++++++++++++++++++++++++++++++++++++++
src/libotutil/ot-local-alloc.h | 44 +++++++++++++++++++++++++
src/libotutil/ot-variant-utils.c | 12 ++----
src/libotutil/otutil.h | 1 +
src/ostree/ot-builtin-ls.c | 12 ++----
6 files changed, 122 insertions(+), 16 deletions(-)
---
diff --git a/Makefile-otutil.am b/Makefile-otutil.am
index dbe1763..4493564 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 0000000..ab62e0d
--- /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 <walters verbum org>
+ *
+ * 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 <walters verbum org>
+ */
+
+#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 0000000..2aeea75
--- /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 <walters verbum org>.
+ *
+ * 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 <walters verbum org>
+ */
+
+#ifndef __OSTREE_LOCAL_ALLOC_H__
+#define __OSTREE_LOCAL_ALLOC_H__
+
+#include <gio/gio.h>
+
+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 655fd5f..f30cb17 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 478026c..d6c9fbc 100644
--- a/src/libotutil/otutil.h
+++ b/src/libotutil/otutil.h
@@ -41,6 +41,7 @@
} \
} G_STMT_END;
+#include <ot-local-alloc.h>
#include <ot-gio-utils.h>
#include <ot-glib-compat.h>
#include <ot-opt-utils.h>
diff --git a/src/ostree/ot-builtin-ls.c b/src/ostree/ot-builtin-ls.c
index 5dd29e8..4f57f7d 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;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]