From dfb33a9737e62ab872d3937b7690b252d2892fe8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Apr 2013 05:25:57 +0200 Subject: [PATCH] macro: rework how we define cleanup macros There's now a generic _cleanup_ macro with an argument. The macros for specific types are now defined using this macro, and in the header files where they belong. All cleanup handlers are now inline functions. --- src/journal/coredumpctl.c | 1 + src/journal/journal-internal.h | 6 +++++ src/journal/test-journal-enum.c | 1 + src/shared/logs-show.c | 1 + src/shared/macro.h | 12 +--------- src/shared/set.h | 3 +++ src/shared/strv.h | 2 ++ src/shared/util.c | 20 ---------------- src/shared/util.h | 41 +++++++++++++++++++++++---------- 9 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index 99ca26932..97d967db3 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -35,6 +35,7 @@ #include "path-util.h" #include "pager.h" #include "macro.h" +#include "journal-internal.h" static enum { ACTION_NONE, diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h index 3accf14c0..ff8b34a95 100644 --- a/src/journal/journal-internal.h +++ b/src/journal/journal-internal.h @@ -132,3 +132,9 @@ struct sd_journal { char *journal_make_match_string(sd_journal *j); void journal_print_header(sd_journal *j); + +static inline void journal_closep(sd_journal **j) { + sd_journal_close(*j); +} + +#define _cleanup_journal_close_ _cleanup_(journal_closep) diff --git a/src/journal/test-journal-enum.c b/src/journal/test-journal-enum.c index 88f583e6c..bd1f5199e 100644 --- a/src/journal/test-journal-enum.c +++ b/src/journal/test-journal-enum.c @@ -25,6 +25,7 @@ #include "sd-journal.h" #include "macro.h" #include "util.h" +#include "journal-internal.h" int main(int argc, char *argv[]) { unsigned n = 0; diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 8897a10c2..570032121 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -30,6 +30,7 @@ #include "util.h" #include "utf8.h" #include "hashmap.h" +#include "journal-internal.h" #define PRINT_THRESHOLD 128 #define JSON_THRESHOLD 4096 diff --git a/src/shared/macro.h b/src/shared/macro.h index 99dc73385..cca41a3f6 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -45,6 +45,7 @@ #define _weakref_(x) __attribute__((weakref(#x))) #define _introspect_(x) __attribute__((section("introspect." x))) #define _alignas_(x) __attribute__((aligned(__alignof(x)))) +#define _cleanup_(x) __attribute__((cleanup(x))) /* automake test harness */ #define EXIT_TEST_SKIP 77 @@ -214,17 +215,6 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) { return k; } -#define _cleanup_free_ __attribute__((cleanup(freep))) -#define _cleanup_fclose_ __attribute__((cleanup(fclosep))) -#define _cleanup_pclose_ __attribute__((cleanup(pclosep))) -#define _cleanup_close_ __attribute__((cleanup(closep))) -#define _cleanup_closedir_ __attribute__((cleanup(closedirp))) -#define _cleanup_umask_ __attribute__((cleanup(umaskp))) -#define _cleanup_set_free_ __attribute__((cleanup(set_freep))) -#define _cleanup_set_free_free_ __attribute__((cleanup(set_free_freep))) -#define _cleanup_strv_free_ __attribute__((cleanup(strv_freep))) -#define _cleanup_journal_close_ __attribute__((cleanup(journal_closep))) - #define VA_FORMAT_ADVANCE(format, ap) \ do { \ int _argtypes[128]; \ diff --git a/src/shared/set.h b/src/shared/set.h index 38c4b58a8..8864f7b34 100644 --- a/src/shared/set.h +++ b/src/shared/set.h @@ -77,3 +77,6 @@ char **set_get_strv(Set *s); #define SET_FOREACH_BACKWARDS(e, s, i) \ for ((i) = ITERATOR_LAST, (e) = set_iterate_backwards((s), &(i)); (e); (e) = set_iterate_backwards((s), &(i))) + +#define _cleanup_set_free_ _cleanup_(set_freep) +#define _cleanup_set_free_free_ _cleanup_(set_free_freep) diff --git a/src/shared/strv.h b/src/shared/strv.h index 4cd3865e1..92696b02f 100644 --- a/src/shared/strv.h +++ b/src/shared/strv.h @@ -34,6 +34,8 @@ static inline void strv_freep(char ***l) { strv_free(*l); } +#define _cleanup_strv_free_ _cleanup_(strv_freep) + char **strv_copy(char * const *l) _malloc_; unsigned strv_length(char * const *l); diff --git a/src/shared/util.c b/src/shared/util.c index 4eb64934e..53caa7f9e 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -5277,26 +5277,6 @@ int get_home_dir(char **_h) { return 0; } -void fclosep(FILE **f) { - if (*f) - fclose(*f); -} - -void pclosep(FILE **f) { - if (*f) - pclose(*f); -} - -void closep(int *fd) { - if (*fd >= 0) - close_nointr_nofail(*fd); -} - -void closedirp(DIR **d) { - if (*d) - closedir(*d); -} - bool filename_is_safe(const char *p) { if (isempty(p)) diff --git a/src/shared/util.h b/src/shared/util.h index 683ff5a4f..3aac165e6 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -38,7 +38,6 @@ #include #include -#include #include "macro.h" #include "time-util.h" @@ -527,19 +526,37 @@ static inline void freep(void *p) { free(*(void**) p); } -void fclosep(FILE **f); -void pclosep(FILE **f); -void closep(int *fd); -void closedirp(DIR **d); -static inline void umaskp(mode_t *u) { - umask(*u); +static inline void fclosep(FILE **f) { + if (*f) + fclose(*f); +} + +static inline void pclosep(FILE **f) { + if (*f) + pclose(*f); +} + +static inline void closep(int *fd) { + if (*fd >= 0) + close_nointr_nofail(*fd); +} + +static inline void closedirp(DIR **d) { + if (*d) + closedir(*d); } -static inline void journal_closep(sd_journal **j) { - sd_journal_close(*j); +static inline void umaskp(mode_t *u) { + umask(*u); } -#define _cleanup_globfree_ __attribute__((cleanup(globfree))) +#define _cleanup_free_ _cleanup_(freep) +#define _cleanup_fclose_ _cleanup_(fclosep) +#define _cleanup_pclose_ _cleanup_(pclosep) +#define _cleanup_close_ _cleanup_(closep) +#define _cleanup_closedir_ _cleanup_(closedirp) +#define _cleanup_umask_ _cleanup_(umaskp) +#define _cleanup_globfree_ _cleanup_(globfree) _malloc_ static inline void *malloc_multiply(size_t a, size_t b) { if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) @@ -608,7 +625,7 @@ int create_tmp_dir(char template[], char** dir_name); static inline void *mempset(void *s, int c, size_t n) { memset(s, c, n); - return (char*)s + n; + return (uint8_t*)s + n; } char *hexmem(const void *p, size_t l); @@ -619,7 +636,7 @@ char *strrep(const char *s, unsigned n); void* greedy_realloc(void **p, size_t *allocated, size_t need); #define GREEDY_REALLOC(array, allocated, need) \ - greedy_realloc((void**) &(array), &(allocated), (sizeof *array) * (need)) + greedy_realloc((void**) &(array), &(allocated), sizeof((array)[0]) * (need)) static inline void _reset_errno_(int *saved_errno) { errno = *saved_errno; -- 2.30.2