core: Add macros for local allocation

This is GCC-specific, but it makes the code significantly
cleaner.
This commit is contained in:
Colin Walters 2012-04-09 14:04:02 -04:00
parent 72398ab62c
commit ca08ad6c5e
6 changed files with 122 additions and 16 deletions

View File

@ -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 \

View File

@ -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);
}

View File

@ -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

View File

@ -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;
}

View File

@ -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>

View File

@ -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;
}