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.
This commit is contained in:
parent
82da66fb75
commit
dfb33a9737
|
@ -35,6 +35,7 @@
|
|||
#include "path-util.h"
|
||||
#include "pager.h"
|
||||
#include "macro.h"
|
||||
#include "journal-internal.h"
|
||||
|
||||
static enum {
|
||||
ACTION_NONE,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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]; \
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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 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 umaskp(mode_t *u) {
|
||||
umask(*u);
|
||||
}
|
||||
|
||||
static inline void journal_closep(sd_journal **j) {
|
||||
sd_journal_close(*j);
|
||||
}
|
||||
|
||||
#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;
|
||||
|
|
Loading…
Reference in a new issue