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
#define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
24 25 26 27 28 29 30 31 32 33

/**
 * 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.
 */
34
#define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
35 36 37 38 39 40 41
    static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
    { \
        if (*_ptr) \
            (func)(*_ptr); \
        *_ptr = NULL; \
    }

42
#define VIR_AUTOCLEAN_FUNC_NAME(type) type##AutoClean
43 44 45 46 47 48 49 50 51 52

/**
 * 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.
 */
53
#define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
    { \
        (func)(_ptr); \
    }

/**
 * 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.
 */
71
#define VIR_AUTOPTR(type) \
72 73 74 75 76 77 78 79 80 81 82 83 84 85
    __attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *

/**
 * 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.
 */
86
#define VIR_AUTOCLEAN(type) \
87
    __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type