mirror of
https://github.com/JakeHillion/drgn.git
synced 2024-12-23 01:33:06 +00:00
ee51244dc1
Kevin Svetlitski suggested making use of __attribute__((__cleanup__)) a long time ago, and now that the kernel is doing it, I don't have a good excuse not to. There are surprisingly only a handful of places that it was straightforward to apply it to. A lot of potential uses are thwarted by our policy that out parameters can be clobbered on failure, so that may be something to revisit. Other cleanup guards will probably be more useful, but this is just laying the groundwork for the future. Signed-off-by: Omar Sandoval <osandov@osandov.com>
40 lines
952 B
C
40 lines
952 B
C
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
/**
|
|
* @file
|
|
*
|
|
* Cleanup functions.
|
|
*/
|
|
|
|
#ifndef DRGN_CLEANUP_H
|
|
#define DRGN_CLEANUP_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define _cleanup_(x) __attribute__((__cleanup__(x)))
|
|
|
|
/** Call @c free() when the variable goes out of scope. */
|
|
#define _cleanup_free_ _cleanup_(freep)
|
|
static inline void freep(void *p)
|
|
{
|
|
free(*(void **)p);
|
|
}
|
|
|
|
/**
|
|
* Get the value of a pointer variable and reset it to @c NULL.
|
|
*
|
|
* This can be used to avoid freeing a variable declared with @ref
|
|
* _cleanup_free_ or another scope guard that is a no-op for @c NULL.
|
|
*/
|
|
#define no_cleanup_ptr(p) ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
|
|
|
|
/**
|
|
* Return a pointer declared with @ref _cleanup_free_ without freeing it.
|
|
*
|
|
* This can also be used for other scope guards that are a no-op for @c NULL.
|
|
*/
|
|
#define return_ptr(p) return no_cleanup_ptr(p)
|
|
|
|
#endif /* DRGN_CLEANUP_H */
|