]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: Move trivial cleanup/ref/unref macros from macro.h to memory-util.h
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 18 Apr 2025 09:01:10 +0000 (11:01 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 18 Apr 2025 11:59:04 +0000 (13:59 +0200)
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.

27 files changed:
src/analyze/analyze-time-data.h
src/basic/fd-util.h
src/basic/gcrypt-util.h
src/basic/log-context.h
src/basic/macro.h
src/basic/memory-util.h
src/fundamental/macro-fundamental.h
src/fundamental/memory-util-fundamental.h
src/hibernate-resume/hibernate-resume-config.h
src/libsystemd-network/icmp6-packet.h
src/libudev/libudev-util.h
src/nsresourced/userns-restrict.h
src/resolve/resolved-dns-search-domain.h
src/shared/acl-util.h
src/shared/apparmor-util.h
src/shared/barrier.h
src/shared/bus-wait-for-jobs.h
src/shared/bus-wait-for-units.h
src/shared/calendarspec.h
src/shared/format-table.h
src/shared/libarchive-util.h
src/shared/libmount-util.h
src/shared/module-util.h
src/shared/open-file.h
src/shared/selinux-util.h
src/udev/udev-ctrl.h
src/xdg-autostart-generator/xdg-autostart-service.h

index fd228b5929327928cadbcc2f0967c447fb112766..e8b924447648704d99dfa976bba721a46de8aaa3 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "sd-bus.h"
 
+#include "memory-util.h"
 #include "time-util.h"
 #include "unit-def.h"
 
index 93b254c6805b35eec263923993ab355e3c3a8d33..59c43e8140f43201c6d69b47f5bc9eec0d2f051e 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/socket.h>
 
 #include "macro.h"
+#include "memory-util.h"
 #include "missing_fcntl.h"
 #include "stdio-util.h"
 
index b8b9b0b1cb89473440cdd5a27fa25b917c7a238d..3e87493cdc31f94e4306d0f7025be21399485d53 100644 (file)
@@ -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);
index 7cff79cc314b638ba0e851034d310de06784bd75..2c15071f43c4a962165a79bb76026631d6272808 100644 (file)
@@ -5,6 +5,7 @@
 #include <stddef.h>
 
 #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
index 026ec136376169949ae131d8f3b0be5aa1daa98a..35b43f4a1b02e07f65cb5b1423f7ae4600f4553e 100644 (file)
@@ -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
index 0a035ef087a39335c12a7fa89011d241d7fceed4..524e827a6c30f67d7b0debb406cd26e34d7beb89 100644 (file)
@@ -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);
index 1d39d60ae857bb2ea69f1d412599aaf839a98ee7..6c6e02b6bc866d45c5ff53944a8ea6441459b9e0 100644 (file)
@@ -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[]
index 6c3aac34a20851132c2a5a1dd93c6e46d37164df..068f5ee215ee2904d48fbb231c7aba1194b4c240 100644 (file)
@@ -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);                                   \
+                }                                                       \
+        }
index 68ef0753559adb3ea48fabdcac5e15ef07ff5e66..24cc8dd2a47de92d15795310f03d23d47d3e3f5e 100644 (file)
@@ -6,6 +6,7 @@
 #include "sd-id128.h"
 
 #include "macro.h"
+#include "memory-util.h"
 
 typedef struct KernelHibernateLocation KernelHibernateLocation;
 
index b402255806591665c3a83bbf0db3ca7e7c586409..dc5c1ab5e74c84e95f0d66bf482830544f830a82 100644 (file)
@@ -5,6 +5,7 @@
 #include <netinet/in.h>
 
 #include "macro.h"
+#include "memory-util.h"
 #include "time-util.h"
 
 typedef struct ICMP6Pakcet {
index 0dc18d44be320b01dd5d922a9b2f2093c9578c75..a02021845d3ee1353d09b7f33292fb561dda5c8f 100644 (file)
@@ -4,6 +4,7 @@
 #include "libudev.h"
 
 #include "macro.h"
+#include "memory-util.h"
 
 /* Cleanup functions */
 DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev*, udev_unref);
index 37aed7b350fb1f00c8fcffc1511ce0c8219c1f7a..92c8c9f21105b086f04d92705d9425d0afcbbd6c 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 
 #include "macro.h"
+#include "memory-util.h"
 
 #if HAVE_VMLINUX_H
 #include "bpf/userns_restrict/userns-restrict-skel.h"
index 3e5229c12f566f61b7a6ed4f2cfecf3f342fea7b..fbd9a4490f1243f4074cf944d78375c509ec86b7 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "list.h"
 #include "macro.h"
+#include "memory-util.h"
 
 typedef struct DnsSearchDomain DnsSearchDomain;
 typedef struct Link Link;
index ef315c2f11d295e28dd691ceab1d1bb9e46cd27e..3fe3d6c735d8b15176d00c0f46200965f775c16b 100644 (file)
@@ -13,6 +13,7 @@ int fd_acl_make_writable_fallback(int fd);
 #include <sys/acl.h>
 
 #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);
index d19c903536ab49a61c7039ce0cb1172ee9032bc2..e88bf9d1116418cd1d3167dd5ad8eaca47bee8e0 100644 (file)
@@ -8,6 +8,8 @@
 #if HAVE_APPARMOR
 #  include <sys/apparmor.h>
 
+#include "memory-util.h"
+
 extern DLSYM_PROTOTYPE(aa_change_onexec);
 extern DLSYM_PROTOTYPE(aa_change_profile);
 extern DLSYM_PROTOTYPE(aa_features_new_from_kernel);
index 4ee2040195749974d3830a65ac6fa30de296e798..e108aebeb2d6204285801b77f211569f110a8b60 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 
+#include "memory-util.h"
 #include "macro.h"
 
 /* See source file for an API description. */
index 2336b1332a89479cbef259fd6fc62b69e7d509e8..ff15ede19cd9608fb3f43c1d164aac6af9110b72 100644 (file)
@@ -4,6 +4,7 @@
 #include "sd-bus.h"
 
 #include "macro.h"
+#include "memory-util.h"
 
 typedef struct BusWaitForJobs BusWaitForJobs;
 
index a4a4dc42a5be6ea6abee3802207b986edd5c6cc4..f1004ea36a3b59ed81dd3b30336d26c867dc5dd7 100644 (file)
@@ -2,6 +2,7 @@
 #pragma once
 
 #include "macro.h"
+#include "memory-util.h"
 #include "sd-bus.h"
 
 typedef struct BusWaitForUnits BusWaitForUnits;
index 60c1c792673032dff267e47da7669e26eb0f45d7..9a8bdbd21acbb40f8c0759d4279382c3a3b118a5 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <stdbool.h>
 
+#include "memory-util.h"
 #include "time-util.h"
 
 typedef struct CalendarComponent {
index bb2eb707599e6b0d1f0aa39c4e4cc59121b645e0..cc62b553a7f469041a37d46509975484485bda22 100644 (file)
@@ -8,6 +8,7 @@
 #include "sd-json.h"
 
 #include "macro.h"
+#include "memory-util.h"
 #include "pager.h"
 
 typedef enum TableDataType {
index fb2aae91ce7b01ca038587993dadc8619394ee0a..1efdacdd36846747695c2f2ff54f4232ea6f2e2d 100644 (file)
@@ -7,6 +7,8 @@
 #include <archive.h>
 #include <archive_entry.h>
 
+#include "memory-util.h"
+
 extern DLSYM_PROTOTYPE(archive_entry_free);
 extern DLSYM_PROTOTYPE(archive_entry_new);
 extern DLSYM_PROTOTYPE(archive_entry_set_ctime);
index 0986ed64bf58444f71c38e6467af75dc7625e1a3..b559fa59758f0953e8407d1d6eb1fefd397ae52d 100644 (file)
@@ -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);
index 59b2b7e083ed0641653556df2fa8db26dff040f8..8fd6a7f8e6bc2cca2464565928906f323bb138de 100644 (file)
@@ -8,6 +8,7 @@
 #include <libkmod.h>
 
 #include "macro.h"
+#include "memory-util.h"
 
 extern DLSYM_PROTOTYPE(kmod_list_next);
 extern DLSYM_PROTOTYPE(kmod_load_resources);
index 4999c96ae7f427bca9c3f9d852113b0711a62edf..fe38801001bccf719772cd855be71f8c933df41e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "list.h"
 #include "macro.h"
+#include "memory-util.h"
 
 typedef enum OpenFileFlag {
         OPENFILE_READ_ONLY = 1 << 0,
index 12627f94ecf2fb2cc90dec9ad0dc6db76babc16c..d47b9a235ac864ed5fa2b4c01fb0a6e8925e1092 100644 (file)
@@ -12,6 +12,8 @@
 #if HAVE_SELINUX
 #include <selinux/selinux.h>
 
+#include "memory-util.h"
+
 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(char*, freecon, NULL);
 #else
 static inline void freeconp(char **p) {
index bcdd849d0d94a42fec64289b2a893fe64b8b6877..0339d33d576147996963b4ca7af6cc7c51091c26 100644 (file)
@@ -4,6 +4,7 @@
 #include "sd-event.h"
 
 #include "macro.h"
+#include "memory-util.h"
 #include "time-util.h"
 
 typedef struct UdevCtrl UdevCtrl;
index 390670eedeaeea0a53bd71b761d86a342099176a..d540a7396cf83947b56204d0bb5fcb362748e5b1 100644 (file)
@@ -2,6 +2,7 @@
 #pragma once
 
 #include "macro.h"
+#include "memory-util.h"
 
 typedef struct XdgAutostartService {
         char *name;