From: Daan De Meyer Date: Fri, 18 Apr 2025 09:01:10 +0000 (+0200) Subject: basic: Move trivial cleanup/ref/unref macros from macro.h to memory-util.h X-Git-Tag: v258-rc1~780^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b14f74f9aba163321753d8599c8cdf934af46d7a;p=thirdparty%2Fsystemd.git basic: Move trivial cleanup/ref/unref macros from macro.h to memory-util.h Let's keep macro.h for the extremely generic macros that don't fit anywhere else. Since CLEANUP_ARRAY() is already in memory-util-fundamental.h, we can make a good case for moving the other cleanup macros in there as well. --- diff --git a/src/analyze/analyze-time-data.h b/src/analyze/analyze-time-data.h index fd228b59293..e8b92444764 100644 --- a/src/analyze/analyze-time-data.h +++ b/src/analyze/analyze-time-data.h @@ -3,6 +3,7 @@ #include "sd-bus.h" +#include "memory-util.h" #include "time-util.h" #include "unit-def.h" diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 93b254c6805..59c43e8140f 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -8,6 +8,7 @@ #include #include "macro.h" +#include "memory-util.h" #include "missing_fcntl.h" #include "stdio-util.h" diff --git a/src/basic/gcrypt-util.h b/src/basic/gcrypt-util.h index b8b9b0b1cb8..3e87493cdc3 100644 --- a/src/basic/gcrypt-util.h +++ b/src/basic/gcrypt-util.h @@ -11,6 +11,7 @@ #include "dlfcn-util.h" #include "macro.h" +#include "memory-util.h" extern DLSYM_PROTOTYPE(gcry_md_close); extern DLSYM_PROTOTYPE(gcry_md_copy); diff --git a/src/basic/log-context.h b/src/basic/log-context.h index 7cff79cc314..2c15071f43c 100644 --- a/src/basic/log-context.h +++ b/src/basic/log-context.h @@ -5,6 +5,7 @@ #include #include "macro.h" +#include "memory-util.h" /* * The log context allows attaching extra metadata to log messages written to the journal via log.h. We keep diff --git a/src/basic/macro.h b/src/basic/macro.h index 026ec136376..35b43f4a1b0 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -251,59 +251,6 @@ static inline int __coverity_check_and_return__(int condition) { /* Pointers range from NULL to POINTER_MAX */ #define POINTER_MAX ((void*) UINTPTR_MAX) -#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \ - scope type *name##_ref(type *p) { \ - if (!p) \ - return NULL; \ - \ - /* For type check. */ \ - unsigned *q = &p->n_ref; \ - assert(*q > 0); \ - assert_se(*q < UINT_MAX); \ - \ - (*q)++; \ - return p; \ - } - -#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \ - scope type *name##_unref(type *p) { \ - if (!p) \ - return NULL; \ - \ - assert(p->n_ref > 0); \ - p->n_ref--; \ - if (p->n_ref > 0) \ - return NULL; \ - \ - return free_func(p); \ - } - -#define DEFINE_TRIVIAL_REF_FUNC(type, name) \ - _DEFINE_TRIVIAL_REF_FUNC(type, name,) -#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \ - _DEFINE_TRIVIAL_REF_FUNC(type, name, static) -#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \ - _DEFINE_TRIVIAL_REF_FUNC(type, name, _public_) - -#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ - _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,) -#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ - _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static) -#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \ - _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_) - -#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ - DEFINE_TRIVIAL_REF_FUNC(type, name); \ - DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func); - -#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ - DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \ - DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func); - -#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ - DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \ - DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func); - /* A macro to force copying of a variable from memory. This is useful whenever we want to read something from * memory and want to make sure the compiler won't optimize away the destination variable for us. It's not * supposed to be a full CPU memory barrier, i.e. CPU is still allowed to reorder the reads, but it is not diff --git a/src/basic/memory-util.h b/src/basic/memory-util.h index 0a035ef087a..524e827a6c3 100644 --- a/src/basic/memory-util.h +++ b/src/basic/memory-util.h @@ -120,3 +120,56 @@ static inline void erase_char(char *p) { /* Makes a copy of the buffer with reversed order of bytes */ void* memdup_reverse(const void *mem, size_t size); + +#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \ + scope type *name##_ref(type *p) { \ + if (!p) \ + return NULL; \ + \ + /* For type check. */ \ + unsigned *q = &p->n_ref; \ + assert(*q > 0); \ + assert_se(*q < UINT_MAX); \ + \ + (*q)++; \ + return p; \ + } + +#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \ + scope type *name##_unref(type *p) { \ + if (!p) \ + return NULL; \ + \ + assert(p->n_ref > 0); \ + p->n_ref--; \ + if (p->n_ref > 0) \ + return NULL; \ + \ + return free_func(p); \ + } + +#define DEFINE_TRIVIAL_REF_FUNC(type, name) \ + _DEFINE_TRIVIAL_REF_FUNC(type, name,) +#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \ + _DEFINE_TRIVIAL_REF_FUNC(type, name, static) +#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \ + _DEFINE_TRIVIAL_REF_FUNC(type, name, _public_) + +#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ + _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,) +#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \ + _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static) +#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \ + _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_) + +#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ + DEFINE_TRIVIAL_REF_FUNC(type, name); \ + DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func); + +#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ + DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \ + DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func); + +#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \ + DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \ + DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func); diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 1d39d60ae85..6c6e02b6bc8 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -521,42 +521,6 @@ static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) { #define FLAGS_SET(v, flags) \ ((~(v) & (flags)) == 0) -/* A wrapper for 'func' to return void. - * Only useful when a void-returning function is required by some API. */ -#define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \ - static inline void name(type *p) { \ - func(p); \ - } - -/* When func() returns the void value (NULL, -1, …) of the appropriate type */ -#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \ - static inline void func##p(type *p) { \ - if (*p) \ - *p = func(*p); \ - } - -/* When func() doesn't return the appropriate type, set variable to empty afterwards. - * The func() may be provided by a dynamically loaded shared library, hence add an assertion. */ -#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \ - static inline void func##p(type *p) { \ - if (*p != (empty)) { \ - DISABLE_WARNING_ADDRESS; \ - assert(func); \ - REENABLE_WARNING; \ - func(*p); \ - *p = (empty); \ - } \ - } - -/* When func() doesn't return the appropriate type, and is also a macro, set variable to empty afterwards. */ -#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, func, empty) \ - static inline void func##p(type *p) { \ - if (*p != (empty)) { \ - func(*p); \ - *p = (empty); \ - } \ - } - /* Restriction/bug (see below) was fixed in GCC 15 and clang 19. */ #if __GNUC__ >= 15 || (defined(__clang__) && __clang_major__ >= 19) #define DECLARE_FLEX_ARRAY(type, name) type name[] diff --git a/src/fundamental/memory-util-fundamental.h b/src/fundamental/memory-util-fundamental.h index 6c3aac34a20..068f5ee215e 100644 --- a/src/fundamental/memory-util-fundamental.h +++ b/src/fundamental/memory-util-fundamental.h @@ -106,3 +106,39 @@ static inline void array_cleanup(const ArrayCleanup *c) { _f; \ }), \ } + +/* A wrapper for 'func' to return void. + * Only useful when a void-returning function is required by some API. */ +#define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \ + static inline void name(type *p) { \ + func(p); \ + } + +/* When func() returns the void value (NULL, -1, …) of the appropriate type */ +#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \ + static inline void func##p(type *p) { \ + if (*p) \ + *p = func(*p); \ + } + +/* When func() doesn't return the appropriate type, set variable to empty afterwards. + * The func() may be provided by a dynamically loaded shared library, hence add an assertion. */ +#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \ + static inline void func##p(type *p) { \ + if (*p != (empty)) { \ + DISABLE_WARNING_ADDRESS; \ + assert(func); \ + REENABLE_WARNING; \ + func(*p); \ + *p = (empty); \ + } \ + } + +/* When func() doesn't return the appropriate type, and is also a macro, set variable to empty afterwards. */ +#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, func, empty) \ + static inline void func##p(type *p) { \ + if (*p != (empty)) { \ + func(*p); \ + *p = (empty); \ + } \ + } diff --git a/src/hibernate-resume/hibernate-resume-config.h b/src/hibernate-resume/hibernate-resume-config.h index 68ef0753559..24cc8dd2a47 100644 --- a/src/hibernate-resume/hibernate-resume-config.h +++ b/src/hibernate-resume/hibernate-resume-config.h @@ -6,6 +6,7 @@ #include "sd-id128.h" #include "macro.h" +#include "memory-util.h" typedef struct KernelHibernateLocation KernelHibernateLocation; diff --git a/src/libsystemd-network/icmp6-packet.h b/src/libsystemd-network/icmp6-packet.h index b4022558065..dc5c1ab5e74 100644 --- a/src/libsystemd-network/icmp6-packet.h +++ b/src/libsystemd-network/icmp6-packet.h @@ -5,6 +5,7 @@ #include #include "macro.h" +#include "memory-util.h" #include "time-util.h" typedef struct ICMP6Pakcet { diff --git a/src/libudev/libudev-util.h b/src/libudev/libudev-util.h index 0dc18d44be3..a02021845d3 100644 --- a/src/libudev/libudev-util.h +++ b/src/libudev/libudev-util.h @@ -4,6 +4,7 @@ #include "libudev.h" #include "macro.h" +#include "memory-util.h" /* Cleanup functions */ DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev*, udev_unref); diff --git a/src/nsresourced/userns-restrict.h b/src/nsresourced/userns-restrict.h index 37aed7b350f..92c8c9f2110 100644 --- a/src/nsresourced/userns-restrict.h +++ b/src/nsresourced/userns-restrict.h @@ -4,6 +4,7 @@ #include #include "macro.h" +#include "memory-util.h" #if HAVE_VMLINUX_H #include "bpf/userns_restrict/userns-restrict-skel.h" diff --git a/src/resolve/resolved-dns-search-domain.h b/src/resolve/resolved-dns-search-domain.h index 3e5229c12f5..fbd9a4490f1 100644 --- a/src/resolve/resolved-dns-search-domain.h +++ b/src/resolve/resolved-dns-search-domain.h @@ -5,6 +5,7 @@ #include "list.h" #include "macro.h" +#include "memory-util.h" typedef struct DnsSearchDomain DnsSearchDomain; typedef struct Link Link; diff --git a/src/shared/acl-util.h b/src/shared/acl-util.h index ef315c2f11d..3fe3d6c735d 100644 --- a/src/shared/acl-util.h +++ b/src/shared/acl-util.h @@ -13,6 +13,7 @@ int fd_acl_make_writable_fallback(int fd); #include #include "macro.h" +#include "memory-util.h" int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *entry); int calc_acl_mask_if_needed(acl_t *acl_p); diff --git a/src/shared/apparmor-util.h b/src/shared/apparmor-util.h index d19c903536a..e88bf9d1116 100644 --- a/src/shared/apparmor-util.h +++ b/src/shared/apparmor-util.h @@ -8,6 +8,8 @@ #if HAVE_APPARMOR # include +#include "memory-util.h" + extern DLSYM_PROTOTYPE(aa_change_onexec); extern DLSYM_PROTOTYPE(aa_change_profile); extern DLSYM_PROTOTYPE(aa_features_new_from_kernel); diff --git a/src/shared/barrier.h b/src/shared/barrier.h index 4ee20401957..e108aebeb2d 100644 --- a/src/shared/barrier.h +++ b/src/shared/barrier.h @@ -5,6 +5,7 @@ #include #include +#include "memory-util.h" #include "macro.h" /* See source file for an API description. */ diff --git a/src/shared/bus-wait-for-jobs.h b/src/shared/bus-wait-for-jobs.h index 2336b1332a8..ff15ede19cd 100644 --- a/src/shared/bus-wait-for-jobs.h +++ b/src/shared/bus-wait-for-jobs.h @@ -4,6 +4,7 @@ #include "sd-bus.h" #include "macro.h" +#include "memory-util.h" typedef struct BusWaitForJobs BusWaitForJobs; diff --git a/src/shared/bus-wait-for-units.h b/src/shared/bus-wait-for-units.h index a4a4dc42a5b..f1004ea36a3 100644 --- a/src/shared/bus-wait-for-units.h +++ b/src/shared/bus-wait-for-units.h @@ -2,6 +2,7 @@ #pragma once #include "macro.h" +#include "memory-util.h" #include "sd-bus.h" typedef struct BusWaitForUnits BusWaitForUnits; diff --git a/src/shared/calendarspec.h b/src/shared/calendarspec.h index 60c1c792673..9a8bdbd21ac 100644 --- a/src/shared/calendarspec.h +++ b/src/shared/calendarspec.h @@ -6,6 +6,7 @@ #include +#include "memory-util.h" #include "time-util.h" typedef struct CalendarComponent { diff --git a/src/shared/format-table.h b/src/shared/format-table.h index bb2eb707599..cc62b553a7f 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -8,6 +8,7 @@ #include "sd-json.h" #include "macro.h" +#include "memory-util.h" #include "pager.h" typedef enum TableDataType { diff --git a/src/shared/libarchive-util.h b/src/shared/libarchive-util.h index fb2aae91ce7..1efdacdd368 100644 --- a/src/shared/libarchive-util.h +++ b/src/shared/libarchive-util.h @@ -7,6 +7,8 @@ #include #include +#include "memory-util.h" + extern DLSYM_PROTOTYPE(archive_entry_free); extern DLSYM_PROTOTYPE(archive_entry_new); extern DLSYM_PROTOTYPE(archive_entry_set_ctime); diff --git a/src/shared/libmount-util.h b/src/shared/libmount-util.h index 0986ed64bf5..b559fa59758 100644 --- a/src/shared/libmount-util.h +++ b/src/shared/libmount-util.h @@ -6,6 +6,7 @@ #include "fstab-util.h" #include "macro.h" +#include "memory-util.h" DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct libmnt_table*, mnt_free_table, NULL); DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct libmnt_iter*, mnt_free_iter, NULL); diff --git a/src/shared/module-util.h b/src/shared/module-util.h index 59b2b7e083e..8fd6a7f8e6b 100644 --- a/src/shared/module-util.h +++ b/src/shared/module-util.h @@ -8,6 +8,7 @@ #include #include "macro.h" +#include "memory-util.h" extern DLSYM_PROTOTYPE(kmod_list_next); extern DLSYM_PROTOTYPE(kmod_load_resources); diff --git a/src/shared/open-file.h b/src/shared/open-file.h index 4999c96ae7f..fe38801001b 100644 --- a/src/shared/open-file.h +++ b/src/shared/open-file.h @@ -3,6 +3,7 @@ #include "list.h" #include "macro.h" +#include "memory-util.h" typedef enum OpenFileFlag { OPENFILE_READ_ONLY = 1 << 0, diff --git a/src/shared/selinux-util.h b/src/shared/selinux-util.h index 12627f94ecf..d47b9a235ac 100644 --- a/src/shared/selinux-util.h +++ b/src/shared/selinux-util.h @@ -12,6 +12,8 @@ #if HAVE_SELINUX #include +#include "memory-util.h" + DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(char*, freecon, NULL); #else static inline void freeconp(char **p) { diff --git a/src/udev/udev-ctrl.h b/src/udev/udev-ctrl.h index bcdd849d0d9..0339d33d576 100644 --- a/src/udev/udev-ctrl.h +++ b/src/udev/udev-ctrl.h @@ -4,6 +4,7 @@ #include "sd-event.h" #include "macro.h" +#include "memory-util.h" #include "time-util.h" typedef struct UdevCtrl UdevCtrl; diff --git a/src/xdg-autostart-generator/xdg-autostart-service.h b/src/xdg-autostart-generator/xdg-autostart-service.h index 390670eedea..d540a7396cf 100644 --- a/src/xdg-autostart-generator/xdg-autostart-service.h +++ b/src/xdg-autostart-generator/xdg-autostart-service.h @@ -2,6 +2,7 @@ #pragma once #include "macro.h" +#include "memory-util.h" typedef struct XdgAutostartService { char *name;