Skip to content

Commit b14f74f

Browse files
committed
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.
1 parent a4bff6e commit b14f74f

27 files changed

+115
-89
lines changed

src/analyze/analyze-time-data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "sd-bus.h"
55

6+
#include "memory-util.h"
67
#include "time-util.h"
78
#include "unit-def.h"
89

src/basic/fd-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/socket.h>
99

1010
#include "macro.h"
11+
#include "memory-util.h"
1112
#include "missing_fcntl.h"
1213
#include "stdio-util.h"
1314

src/basic/gcrypt-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "dlfcn-util.h"
1313
#include "macro.h"
14+
#include "memory-util.h"
1415

1516
extern DLSYM_PROTOTYPE(gcry_md_close);
1617
extern DLSYM_PROTOTYPE(gcry_md_copy);

src/basic/log-context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stddef.h>
66

77
#include "macro.h"
8+
#include "memory-util.h"
89

910
/*
1011
* The log context allows attaching extra metadata to log messages written to the journal via log.h. We keep

src/basic/macro.h

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -251,59 +251,6 @@ static inline int __coverity_check_and_return__(int condition) {
251251
/* Pointers range from NULL to POINTER_MAX */
252252
#define POINTER_MAX ((void*) UINTPTR_MAX)
253253

254-
#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
255-
scope type *name##_ref(type *p) { \
256-
if (!p) \
257-
return NULL; \
258-
\
259-
/* For type check. */ \
260-
unsigned *q = &p->n_ref; \
261-
assert(*q > 0); \
262-
assert_se(*q < UINT_MAX); \
263-
\
264-
(*q)++; \
265-
return p; \
266-
}
267-
268-
#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
269-
scope type *name##_unref(type *p) { \
270-
if (!p) \
271-
return NULL; \
272-
\
273-
assert(p->n_ref > 0); \
274-
p->n_ref--; \
275-
if (p->n_ref > 0) \
276-
return NULL; \
277-
\
278-
return free_func(p); \
279-
}
280-
281-
#define DEFINE_TRIVIAL_REF_FUNC(type, name) \
282-
_DEFINE_TRIVIAL_REF_FUNC(type, name,)
283-
#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
284-
_DEFINE_TRIVIAL_REF_FUNC(type, name, static)
285-
#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
286-
_DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
287-
288-
#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
289-
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
290-
#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
291-
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
292-
#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
293-
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
294-
295-
#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
296-
DEFINE_TRIVIAL_REF_FUNC(type, name); \
297-
DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
298-
299-
#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
300-
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
301-
DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
302-
303-
#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
304-
DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
305-
DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);
306-
307254
/* A macro to force copying of a variable from memory. This is useful whenever we want to read something from
308255
* memory and want to make sure the compiler won't optimize away the destination variable for us. It's not
309256
* supposed to be a full CPU memory barrier, i.e. CPU is still allowed to reorder the reads, but it is not

src/basic/memory-util.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,56 @@ static inline void erase_char(char *p) {
120120

121121
/* Makes a copy of the buffer with reversed order of bytes */
122122
void* memdup_reverse(const void *mem, size_t size);
123+
124+
#define _DEFINE_TRIVIAL_REF_FUNC(type, name, scope) \
125+
scope type *name##_ref(type *p) { \
126+
if (!p) \
127+
return NULL; \
128+
\
129+
/* For type check. */ \
130+
unsigned *q = &p->n_ref; \
131+
assert(*q > 0); \
132+
assert_se(*q < UINT_MAX); \
133+
\
134+
(*q)++; \
135+
return p; \
136+
}
137+
138+
#define _DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, scope) \
139+
scope type *name##_unref(type *p) { \
140+
if (!p) \
141+
return NULL; \
142+
\
143+
assert(p->n_ref > 0); \
144+
p->n_ref--; \
145+
if (p->n_ref > 0) \
146+
return NULL; \
147+
\
148+
return free_func(p); \
149+
}
150+
151+
#define DEFINE_TRIVIAL_REF_FUNC(type, name) \
152+
_DEFINE_TRIVIAL_REF_FUNC(type, name,)
153+
#define DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name) \
154+
_DEFINE_TRIVIAL_REF_FUNC(type, name, static)
155+
#define DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name) \
156+
_DEFINE_TRIVIAL_REF_FUNC(type, name, _public_)
157+
158+
#define DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
159+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func,)
160+
#define DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func) \
161+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, static)
162+
#define DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func) \
163+
_DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func, _public_)
164+
165+
#define DEFINE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
166+
DEFINE_TRIVIAL_REF_FUNC(type, name); \
167+
DEFINE_TRIVIAL_UNREF_FUNC(type, name, free_func);
168+
169+
#define DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
170+
DEFINE_PRIVATE_TRIVIAL_REF_FUNC(type, name); \
171+
DEFINE_PRIVATE_TRIVIAL_UNREF_FUNC(type, name, free_func);
172+
173+
#define DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(type, name, free_func) \
174+
DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
175+
DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);

src/fundamental/macro-fundamental.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -521,42 +521,6 @@ static inline uint64_t ALIGN_OFFSET_U64(uint64_t l, uint64_t ali) {
521521
#define FLAGS_SET(v, flags) \
522522
((~(v) & (flags)) == 0)
523523

524-
/* A wrapper for 'func' to return void.
525-
* Only useful when a void-returning function is required by some API. */
526-
#define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
527-
static inline void name(type *p) { \
528-
func(p); \
529-
}
530-
531-
/* When func() returns the void value (NULL, -1, …) of the appropriate type */
532-
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
533-
static inline void func##p(type *p) { \
534-
if (*p) \
535-
*p = func(*p); \
536-
}
537-
538-
/* When func() doesn't return the appropriate type, set variable to empty afterwards.
539-
* The func() may be provided by a dynamically loaded shared library, hence add an assertion. */
540-
#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \
541-
static inline void func##p(type *p) { \
542-
if (*p != (empty)) { \
543-
DISABLE_WARNING_ADDRESS; \
544-
assert(func); \
545-
REENABLE_WARNING; \
546-
func(*p); \
547-
*p = (empty); \
548-
} \
549-
}
550-
551-
/* When func() doesn't return the appropriate type, and is also a macro, set variable to empty afterwards. */
552-
#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, func, empty) \
553-
static inline void func##p(type *p) { \
554-
if (*p != (empty)) { \
555-
func(*p); \
556-
*p = (empty); \
557-
} \
558-
}
559-
560524
/* Restriction/bug (see below) was fixed in GCC 15 and clang 19. */
561525
#if __GNUC__ >= 15 || (defined(__clang__) && __clang_major__ >= 19)
562526
#define DECLARE_FLEX_ARRAY(type, name) type name[]

src/fundamental/memory-util-fundamental.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,39 @@ static inline void array_cleanup(const ArrayCleanup *c) {
106106
_f; \
107107
}), \
108108
}
109+
110+
/* A wrapper for 'func' to return void.
111+
* Only useful when a void-returning function is required by some API. */
112+
#define DEFINE_TRIVIAL_DESTRUCTOR(name, type, func) \
113+
static inline void name(type *p) { \
114+
func(p); \
115+
}
116+
117+
/* When func() returns the void value (NULL, -1, …) of the appropriate type */
118+
#define DEFINE_TRIVIAL_CLEANUP_FUNC(type, func) \
119+
static inline void func##p(type *p) { \
120+
if (*p) \
121+
*p = func(*p); \
122+
}
123+
124+
/* When func() doesn't return the appropriate type, set variable to empty afterwards.
125+
* The func() may be provided by a dynamically loaded shared library, hence add an assertion. */
126+
#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(type, func, empty) \
127+
static inline void func##p(type *p) { \
128+
if (*p != (empty)) { \
129+
DISABLE_WARNING_ADDRESS; \
130+
assert(func); \
131+
REENABLE_WARNING; \
132+
func(*p); \
133+
*p = (empty); \
134+
} \
135+
}
136+
137+
/* When func() doesn't return the appropriate type, and is also a macro, set variable to empty afterwards. */
138+
#define DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_MACRO(type, func, empty) \
139+
static inline void func##p(type *p) { \
140+
if (*p != (empty)) { \
141+
func(*p); \
142+
*p = (empty); \
143+
} \
144+
}

src/hibernate-resume/hibernate-resume-config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "sd-id128.h"
77

88
#include "macro.h"
9+
#include "memory-util.h"
910

1011
typedef struct KernelHibernateLocation KernelHibernateLocation;
1112

src/libsystemd-network/icmp6-packet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <netinet/in.h>
66

77
#include "macro.h"
8+
#include "memory-util.h"
89
#include "time-util.h"
910

1011
typedef struct ICMP6Pakcet {

0 commit comments

Comments
 (0)