|
rpm-build |
c487f7 |
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* This library is free software; you can redistribute it and/or
|
|
rpm-build |
c487f7 |
* modify it under the terms of the GNU Lesser General Public
|
|
rpm-build |
c487f7 |
* License as published by the Free Software Foundation; either
|
|
rpm-build |
c487f7 |
* version 2 of the License, or (at your option) any later version.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* This library is distributed in the hope that it will be useful,
|
|
rpm-build |
c487f7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
rpm-build |
c487f7 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
rpm-build |
c487f7 |
* Lesser General Public License for more details.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* You should have received a copy of the GNU Lesser General Public
|
|
rpm-build |
c487f7 |
* License along with this library; if not, write to the
|
|
rpm-build |
c487f7 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
rpm-build |
c487f7 |
* Boston, MA 02111-1307, USA.
|
|
rpm-build |
c487f7 |
*/
|
|
rpm-build |
c487f7 |
|
|
rpm-build |
c487f7 |
#include "config.h"
|
|
rpm-build |
c487f7 |
|
|
rpm-build |
c487f7 |
#include "glnx-local-alloc.h"
|
|
rpm-build |
c487f7 |
|
|
rpm-build |
c487f7 |
/**
|
|
rpm-build |
c487f7 |
* SECTION:glnxlocalalloc
|
|
rpm-build |
c487f7 |
* @title: GLnx local allocation
|
|
rpm-build |
c487f7 |
* @short_description: Release local variables automatically when they go out of scope
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* These macros leverage the GCC extension __attribute__ ((cleanup))
|
|
rpm-build |
c487f7 |
* to allow calling a cleanup function such as g_free() when a
|
|
rpm-build |
c487f7 |
* variable goes out of scope. See
|
|
rpm-build |
c487f7 |
* url="http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html">
|
|
rpm-build |
c487f7 |
* for more information on the attribute.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* The provided macros make it easy to use the cleanup attribute for
|
|
rpm-build |
c487f7 |
* types that come with GLib. The primary two are #glnx_free and
|
|
rpm-build |
c487f7 |
* #glnx_unref_object, which correspond to g_free() and
|
|
rpm-build |
c487f7 |
* g_object_unref(), respectively.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* The rationale behind this is that particularly when handling error
|
|
rpm-build |
c487f7 |
* paths, it can be very tricky to ensure the right variables are
|
|
rpm-build |
c487f7 |
* freed. With this, one simply applies glnx_unref_object to a
|
|
rpm-build |
c487f7 |
* locally-allocated #GFile for example, and it will be automatically
|
|
rpm-build |
c487f7 |
* unreferenced when it goes out of scope.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* Note - you should only use these macros for <emphasis>stack
|
|
rpm-build |
c487f7 |
* allocated</emphasis> variables. They don't provide garbage
|
|
rpm-build |
c487f7 |
* collection or let you avoid freeing things. They're simply a
|
|
rpm-build |
c487f7 |
* compiler assisted deterministic mechanism for calling a cleanup
|
|
rpm-build |
c487f7 |
* function when a stack frame ends.
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* <example id="gs-lfree"><title>Calling g_free automatically</title>
|
|
rpm-build |
c487f7 |
* <programlisting>
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* GFile *
|
|
rpm-build |
c487f7 |
* create_file (GError **error)
|
|
rpm-build |
c487f7 |
* {
|
|
rpm-build |
c487f7 |
* glnx_free char *random_id = NULL;
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* if (!prepare_file (error))
|
|
rpm-build |
c487f7 |
* return NULL;
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* random_id = alloc_random_id ();
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
* return create_file_real (error);
|
|
rpm-build |
c487f7 |
* // Note that random_id is freed here automatically
|
|
rpm-build |
c487f7 |
* }
|
|
rpm-build |
c487f7 |
* </programlisting>
|
|
rpm-build |
c487f7 |
* </example>
|
|
rpm-build |
c487f7 |
*
|
|
rpm-build |
c487f7 |
*/
|