[PATCH] cleanup: Promote _auto_ to public API
Denis Kenzior <[email protected]> Tue, 30 Jan 2024 13:44:29 -0600
| Newsgroups | dev.linux.lists.ell |
|---|---|
| Message-ID | <[email protected]> |
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(-)
diff --git a/ell/cleanup.h b/ell/cleanup.h
index 337c8ad5d753..220240bdd4cf 100644
--- a/ell/cleanup.h
+++ b/ell/cleanup.h
@@ -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)
diff --git a/ell/useful.h b/ell/useful.h
index 5a4a5eba091c..a3d5ad916bd8 100644
--- a/ell/useful.h
+++ b/ell/useful.h
@@ -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
diff --git a/ell/util.h b/ell/util.h
index 5dfc35c89ea6..daec4024fdfa 100644
--- a/ell/util.h
+++ b/ell/util.h
@@ -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
--
2.43.0