cleanup: Promote _auto_ to public API
PATCH 1/1 cleanup: Promote _auto_ to public API
Move this out of useful.h which is not installed by default and requires special build time hacks into cleanup.h, which is installed as part of public headers / api.
--- ell/cleanup.h | 6 ++++++ ell/useful.h | 20 -------------------- ell/util.h | 11 +++++++++++ 3 files changed, 17 insertions(+), 20 deletions(-)
ell/cleanup.h +6 -0
@@ -10,3 +10,9 @@#define DEFINE_CLEANUP_FUNC (func) \ inline __attribute__((always_inline)) \ void func ## _cleanup(void *p) { func(*(void **) p); } + +#define __AUTODESTRUCT (func) \ + __attribute((cleanup(func ## _cleanup))) + +#define _auto_ (func) \ + __AUTODESTRUCT(func)
ell/useful.h +0 -20
@@ -5,11 +5,6 @@* SPDX-License-Identifier: LGPL-2.1-or-later */ -#include <unistd.h> -#include <errno.h> - -#include <ell/util.h> -#define align_len (len, boundary) (((len)+(boundary)-1) & ~((boundary)-1))#define likely (x) __builtin_expect(!!(x), 1)
@@ -64,21 +59,6 @@ static inline unsigned char bit_field(const unsigned char oct, _x / _d; \ }) -#define __AUTODESTRUCT (func) \ - __attribute((cleanup(func ## _cleanup))) - -#define _auto_ (func) \ - __AUTODESTRUCT(func) - -/* Enables declaring _auto_(close) int fd = <-1 or L_TFR(open(...))>; */ -inline __attribute__((always_inline))void close_cleanup (void *p) -{ -int fd = *(int *) p; - -if (fd >=0 ) -L_TFR (close (fd)); -} -/* * Trick the compiler into thinking that var might be changed somehow by * the asm
ell/util.h +11 -0
@@ -15,6 +15,8 @@#include <endian.h> #include <byteswap.h> #include <sys/uio.h> +#include <errno.h> +#include <unistd.h> #include <ell/cleanup.h> #ifdef __cplusplus
@@ -437,6 +439,15 @@ int l_safe_atox32(const char *s, uint32_t *out_u);int l_safe_atox16 (const char *s,uint16_t *out_u);int l_safe_atox8 (const char *s,uint8_t *out_u); +/* Enables declaring _auto_(close) int fd = <-1 or L_TFR(open(...))>; */ +inline __attribute__((always_inline))void close_cleanup (void *p) +{ +int fd = *(int *) p; + +if (fd >=0 ) +L_TFR (close (fd)); +} + #ifdef __cplusplus }#endif
Signature · 1 line
2.43.0