virautoclean.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * virautoclean.h: automatic scope-based memory clearing helper macros for
 *                 use in header files
 *
 * 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.1 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, see
 * <http://www.gnu.org/licenses/>.
 *
 */

21
#pragma once
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * DEPRECATION WARNING
 *
 * The macros in this file should not be used in newly written code.
 * Use the equivalent GLib macros instead.
 *
 * For existing code, use of the libvirt and GLib macros must NEVER
 * be mixed within a single method.
 *
 * The use of the libvirt VIR_FREE macros should also not be mixed
 * with GLib auto-free macros and vice-verca.
 *
 * Existing code should be converted to the new GLib macros and
 * g_free APIs as needed.
 */
38 39 40 41 42 43 44 45 46 47

/**
 * VIR_DEFINE_AUTOPTR_FUNC:
 * @type: type of the variable to be freed automatically
 * @func: cleanup function to be automatically called
 *
 * This macro defines a function for automatic freeing of
 * resources allocated to a variable of type @type. This newly
 * defined function works as a necessary wrapper around @func.
 */
48 49
#define VIR_DEFINE_AUTOPTR_FUNC(t, f) \
    G_DEFINE_AUTOPTR_CLEANUP_FUNC(t, f)
50 51 52 53 54 55 56 57 58 59

/**
 * VIR_DEFINE_AUTOCLEAN_FUNC:
 * @type: type of the variable to be cleared automatically
 * @func: cleanup function to be automatically called
 *
 * This macro defines a function for automatic clearing of
 * resources in a stack'd variable of type @type. Note that @func must
 * take pointer to @type.
 */
60
#define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
61
    G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(type, func)
62 63 64 65 66 67 68 69 70 71 72 73 74

/**
 * VIR_AUTOPTR:
 * @type: type of the variable to be freed automatically
 *
 * Macro to automatically free the memory allocated to
 * the variable declared with it by calling the function
 * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
 * goes out of scope.
 *
 * Note that this macro must NOT be used with vectors! The freeing function
 * will not free any elements beyond the first.
 */
75
#define VIR_AUTOPTR(type) g_autoptr(type)
76 77 78 79 80 81 82 83 84 85 86 87 88

/**
 * VIR_AUTOCLEAN:
 * @type: type of the variable to be cleared automatically
 *
 * Macro to automatically call clearing function registered for variable of @type
 * when the variable goes out of scope.
 * The cleanup function is registered by VIR_DEFINE_AUTOCLEAN_FUNC macro for
 * the given type.
 *
 * Note that this macro must NOT be used with vectors! The cleaning function
 * will not clean any elements beyond the first.
 */
89
#define VIR_AUTOCLEAN(type) g_auto(type)