From 8aa2b2b58e58ce8e3a951602867a37aa33097a34 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Tue, 21 Sep 2021 17:32:06 -0500 Subject: [PATCH] RFC: useful: Remove nested function use --- ell/cert.h | 2 ++ ell/cleanup.h | 27 +++++++++++++++++++++++++++ ell/key.h | 3 +++ ell/useful.h | 11 +++-------- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 ell/cleanup.h diff --git a/ell/cert.h b/ell/cert.h index f3910bb33895..8dcd8e6415aa 100644 --- a/ell/cert.h +++ b/ell/cert.h @@ -28,6 +28,7 @@ extern "C" { #endif #include +#include struct l_queue; struct l_cert; @@ -49,6 +50,7 @@ enum l_cert_key_type l_cert_get_pubkey_type(struct l_cert *cert); struct l_key *l_cert_get_pubkey(struct l_cert *cert); void l_certchain_free(struct l_certchain *chain); +DEFINE_CLEANUP_FUNC(l_certchain_free) struct l_cert *l_certchain_get_leaf(struct l_certchain *chain); void l_certchain_walk_from_leaf(struct l_certchain *chain, diff --git a/ell/cleanup.h b/ell/cleanup.h new file mode 100644 index 000000000000..89b19816016f --- /dev/null +++ b/ell/cleanup.h @@ -0,0 +1,27 @@ +/* + * + * Embedded Linux library + * + * Copyright (C) 2021 Intel Corporation. All rights reserved. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#pragma once + +#define DEFINE_CLEANUP_FUNC(func) \ + inline __attribute__((always_inline)) \ + void func ## _cleanup(void *p) { func(*(void **) p); } diff --git a/ell/key.h b/ell/key.h index f1f95e11173a..ff3d9abbda4c 100644 --- a/ell/key.h +++ b/ell/key.h @@ -30,6 +30,7 @@ extern "C" { #include #include +#include #include struct l_key; @@ -108,7 +109,9 @@ bool l_keyring_restrict(struct l_keyring *keyring, enum l_keyring_restriction re const struct l_keyring *trust); void l_keyring_free(struct l_keyring *keyring); +DEFINE_CLEANUP_FUNC(l_keyring_free) void l_keyring_free_norevoke(struct l_keyring *keyring); +DEFINE_CLEANUP_FUNC(l_keyring_free_norevoke) bool l_keyring_link(struct l_keyring *keyring, const struct l_key *key); diff --git a/ell/useful.h b/ell/useful.h index b4783ce366cb..4c8b23eaa2d9 100644 --- a/ell/useful.h +++ b/ell/useful.h @@ -59,16 +59,11 @@ static inline unsigned char bit_field(const unsigned char oct, _x / _d; \ }) -#define __AUTODESTRUCT(var, func) \ - void cleanup_ ## var(void *ptr) \ - { func(*(void **) ptr); } \ - __attribute((cleanup(cleanup_ ## var))) - -#define _AUTODESTRUCT(var, func) \ - __AUTODESTRUCT(var, func) +#define __AUTODESTRUCT(func) \ + __attribute((cleanup(func ## _cleanup))) #define _auto_(func) \ - _AUTODESTRUCT(__COUNTER__, func) + __AUTODESTRUCT(func) /* * Trick the compiler into thinking that var might be changed somehow by -- 2.26.3