]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: rework how we define cleanup macros
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:25:57 +0000 (05:25 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:25:57 +0000 (05:25 +0200)
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
src/journal/journal-internal.h
src/journal/test-journal-enum.c
src/shared/logs-show.c
src/shared/macro.h
src/shared/set.h
src/shared/strv.h
src/shared/util.c
src/shared/util.h

index 99ca26932799d41ab54d389d7e4d22045bcd0d8d..97d967db3c1b40ee66cea052272237117d23edc1 100644 (file)
@@ -35,6 +35,7 @@
 #include "path-util.h"
 #include "pager.h"
 #include "macro.h"
+#include "journal-internal.h"
 
 static enum {
         ACTION_NONE,
index 3accf14c05ee98734d62b68cef11c8dc7b1248fb..ff8b34a951e76e701d3f2fd22975bdf26ad1b58d 100644 (file)
@@ -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)
index 88f583e6c7f4696e74b3fc52422e72b82823d72c..bd1f5199e553f803d355b4edd55aec36c4fd6fb2 100644 (file)
@@ -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;
index 8897a10c2b427750742667a06247629c3bb8bce5..570032121540ab3175d8456cd5a0679a7db8a7df 100644 (file)
@@ -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
index 99dc733853ea0dd8abe37db98a26d85f7c59f94f..cca41a3f6ad7311877211d788a6d0d076285c909 100644 (file)
@@ -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];                                             \
index 38c4b58a80835a41e2bc3609b2be034a2545a460..8864f7b34e86b887bf53bde36c79f9a879a0ff58 100644 (file)
@@ -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)
index 4cd3865e1db5238b50d19fe099109d0c496c3561..92696b02fa7709792678d2d480b3f7f017e92684 100644 (file)
@@ -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);
 
index 4eb64934ee4cf1437956f3629e3c6271e2b98077..53caa7f9e504ca8afaab61d96e1cf31f6af46070 100644 (file)
@@ -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))
index 683ff5a4fe03e4af1f8e6de4c2b672876b6c3511..3aac165e67d0792f11857bb1f830783bf1ab2f36 100644 (file)
@@ -38,7 +38,6 @@
 #include <stddef.h>
 #include <unistd.h>
 
-#include <systemd/sd-journal.h>
 #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;