From: Emil Velikov Date: Wed, 11 Sep 2024 16:30:21 +0000 (+0100) Subject: shared: mass convert with clang-format X-Git-Tag: v34~295 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1d0117f86acb4bffc4d4bac5009e3f26892dd66e;p=thirdparty%2Fkmod.git shared: mass convert with clang-format Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/118 Signed-off-by: Lucas De Marchi --- diff --git a/shared/array.c b/shared/array.c index 299021fc..964ab2f2 100644 --- a/shared/array.c +++ b/shared/array.c @@ -13,7 +13,6 @@ /* basic pointer array growing in steps */ - static int array_realloc(struct array *array, size_t new_total) { void *tmp; @@ -74,21 +73,22 @@ int array_append_unique(struct array *array, const void *element) return array_append(array, element); } -void array_pop(struct array *array) { +void array_pop(struct array *array) +{ if (array->count == 0) return; array->count--; array_trim(array); } -void array_free_array(struct array *array) { +void array_free_array(struct array *array) +{ free(array->array); array->array = NULL; array->count = 0; array->total = 0; } - void array_sort(struct array *array, int (*cmp)(const void *a, const void *b)) { qsort(array->array, array->count, sizeof(void *), cmp); diff --git a/shared/hash.c b/shared/hash.c index 2c77ef49..df35f453 100644 --- a/shared/hash.c +++ b/shared/hash.c @@ -30,14 +30,12 @@ struct hash { struct hash_bucket buckets[]; }; -struct hash *hash_new(unsigned int n_buckets, - void (*free_value)(void *value)) +struct hash *hash_new(unsigned int n_buckets, void (*free_value)(void *value)) { struct hash *hash; n_buckets = ALIGN_POWER2(n_buckets); - hash = calloc(1, sizeof(struct hash) + - n_buckets * sizeof(struct hash_bucket)); + hash = calloc(1, sizeof(struct hash) + n_buckets * sizeof(struct hash_bucket)); if (hash == NULL) return NULL; hash->n_buckets = n_buckets; @@ -84,7 +82,7 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len) /* Main loop */ for (; len > 0; len--) { - hash += get_unaligned((uint16_t *) key); + hash += get_unaligned((uint16_t *)key); tmp = (get_unaligned((uint16_t *)(key + 2)) << 11) ^ hash; hash = (hash << 16) ^ tmp; key += 4; @@ -94,14 +92,14 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len) /* Handle end cases */ switch (rem) { case 3: - hash += get_unaligned((uint16_t *) key); + hash += get_unaligned((uint16_t *)key); hash ^= hash << 16; hash ^= key[2] << 18; hash += hash >> 11; break; case 2: - hash += get_unaligned((uint16_t *) key); + hash += get_unaligned((uint16_t *)key); hash ^= hash << 11; hash += hash >> 17; break; @@ -232,8 +230,8 @@ void *hash_find(const struct hash *hash, const char *key) if (!bucket->entries) return NULL; - entry = bsearch(&se, bucket->entries, bucket->used, - sizeof(struct hash_entry), hash_entry_cmp); + entry = bsearch(&se, bucket->entries, bucket->used, sizeof(struct hash_entry), + hash_entry_cmp); return entry ? (void *)entry->value : NULL; } @@ -251,8 +249,8 @@ int hash_del(struct hash *hash, const char *key) .value = NULL, }; - entry = bsearch(&se, bucket->entries, bucket->used, - sizeof(struct hash_entry), hash_entry_cmp); + entry = bsearch(&se, bucket->entries, bucket->used, sizeof(struct hash_entry), + hash_entry_cmp); if (entry == NULL) return -ENOENT; @@ -260,8 +258,7 @@ int hash_del(struct hash *hash, const char *key) hash->free_value((void *)entry->value); entry_end = bucket->entries + bucket->used; - memmove(entry, entry + 1, - (entry_end - entry) * sizeof(struct hash_entry)); + memmove(entry, entry + 1, (entry_end - entry) * sizeof(struct hash_entry)); bucket->used--; hash->count--; @@ -269,8 +266,7 @@ int hash_del(struct hash *hash, const char *key) steps_used = bucket->used / hash->step; steps_total = bucket->total / hash->step; if (steps_used + 1 < steps_total) { - size_t size = (steps_used + 1) * - hash->step * sizeof(struct hash_entry); + size_t size = (steps_used + 1) * hash->step * sizeof(struct hash_entry); struct hash_entry *tmp = realloc(bucket->entries, size); if (tmp) { bucket->entries = tmp; @@ -293,8 +289,7 @@ void hash_iter_init(const struct hash *hash, struct hash_iter *iter) iter->entry = -1; } -bool hash_iter_next(struct hash_iter *iter, const char **key, - const void **value) +bool hash_iter_next(struct hash_iter *iter, const char **key, const void **value) { const struct hash_bucket *b = iter->hash->buckets + iter->bucket; const struct hash_entry *e; @@ -305,7 +300,7 @@ bool hash_iter_next(struct hash_iter *iter, const char **key, iter->entry = 0; for (iter->bucket++; iter->bucket < iter->hash->n_buckets; - iter->bucket++) { + iter->bucket++) { b = iter->hash->buckets + iter->bucket; if (b->used > 0) diff --git a/shared/hash.h b/shared/hash.h index ca0af057..96dfa5d1 100644 --- a/shared/hash.h +++ b/shared/hash.h @@ -18,5 +18,4 @@ int hash_del(struct hash *hash, const char *key); void *hash_find(const struct hash *hash, const char *key); unsigned int hash_get_count(const struct hash *hash); void hash_iter_init(const struct hash *hash, struct hash_iter *iter); -bool hash_iter_next(struct hash_iter *iter, const char **key, - const void **value); +bool hash_iter_next(struct hash_iter *iter, const char **key, const void **value); diff --git a/shared/macro.h b/shared/macro.h index 5d08a63d..b7e91c74 100644 --- a/shared/macro.h +++ b/shared/macro.h @@ -7,27 +7,26 @@ #include #if defined(HAVE_STATIC_ASSERT) -#define assert_cc(expr) \ - _Static_assert((expr), #expr) +#define assert_cc(expr) _Static_assert((expr), #expr) #else -#define assert_cc(expr) \ - do { (void) sizeof(char [1 - 2*!(expr)]); } while(0) +#define assert_cc(expr) \ + do { \ + (void)sizeof(char[1 - 2 * !(expr)]); \ + } while (0) #endif -#define check_types_match(expr1, expr2) \ - ((typeof(expr1) *)0 != (typeof(expr2) *)0) - -#define container_of(member_ptr, containing_type, member) \ - ((containing_type *) \ - ((char *)(member_ptr) - offsetof(containing_type, member)) \ - - check_types_match(*(member_ptr), ((containing_type *)0)->member)) +#define check_types_match(expr1, expr2) ((typeof(expr1) *)0 != (typeof(expr2) *)0) +#define container_of(member_ptr, containing_type, member) \ + ((containing_type *)((char *)(member_ptr) - offsetof(containing_type, member)) - \ + check_types_match(*(member_ptr), ((containing_type *)0)->member)) /* Two gcc extensions. * &a[0] degrades to a pointer: a different type from an array */ -#define _array_size_chk(arr) ({ \ - assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ - 0; \ +#define _array_size_chk(arr) \ + ({ \ + assert_cc(!__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0]))); \ + 0; \ }) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr)) @@ -35,18 +34,18 @@ #define XSTRINGIFY(x) #x #define STRINGIFY(x) XSTRINGIFY(x) -#define XCONCATENATE(x, y) x ## y +#define XCONCATENATE(x, y) x##y #define CONCATENATE(x, y) XCONCATENATE(x, y) #define UNIQ(x) CONCATENATE(x, __COUNTER__) /* Attributes */ #define _must_check_ __attribute__((warn_unused_result)) -#define _printf_format_(a,b) __attribute__((format (printf, a, b))) +#define _printf_format_(a, b) __attribute__((format(printf, a, b))) #define _unused_ __attribute__((unused)) #define _always_inline_ __inline__ __attribute__((always_inline)) #define _cleanup_(x) __attribute__((cleanup(x))) -#define _nonnull_(...) __attribute__((nonnull (__VA_ARGS__))) +#define _nonnull_(...) __attribute__((nonnull(__VA_ARGS__))) #define _nonnull_all_ __attribute__((nonnull)) /* Define C11 noreturn without and even on older gcc diff --git a/shared/missing.h b/shared/missing.h index 9dbbd707..1f44f89e 100644 --- a/shared/missing.h +++ b/shared/missing.h @@ -16,8 +16,8 @@ #define MODULE_INIT_COMPRESSED_FILE 4 #ifndef __NR_finit_module -# warning __NR_finit_module missing - kmod might not work correctly -# define __NR_finit_module -1 +#warning __NR_finit_module missing - kmod might not work correctly +#define __NR_finit_module -1 #endif #ifndef HAVE_FINIT_MODULE diff --git a/shared/scratchbuf.h b/shared/scratchbuf.h index 27ea9d9f..7ba84373 100644 --- a/shared/scratchbuf.h +++ b/shared/scratchbuf.h @@ -24,8 +24,9 @@ static inline char *scratchbuf_str(struct scratchbuf *buf) return buf->bytes; } -#define SCRATCHBUF_INITIALIZER(buf_) { \ - .bytes = buf_, \ - .size = sizeof(buf_) + _array_size_chk(buf_), \ - .need_free = false, \ -} +#define SCRATCHBUF_INITIALIZER(buf_) \ + { \ + .bytes = buf_, \ + .size = sizeof(buf_) + _array_size_chk(buf_), \ + .need_free = false, \ + } diff --git a/shared/strbuf.c b/shared/strbuf.c index b032f40f..f89a97d7 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -111,4 +111,3 @@ void strbuf_clear(struct strbuf *buf) { buf->used = 0; } - diff --git a/shared/util.c b/shared/util.c index f83bc353..b9da7c4e 100644 --- a/shared/util.c +++ b/shared/util.c @@ -18,24 +18,24 @@ #include #include -#define USEC_PER_SEC 1000000ULL +#define USEC_PER_SEC 1000000ULL #define NSEC_PER_USEC 1000ULL static const struct kmod_ext { const char *ext; size_t len; } kmod_exts[] = { - {KMOD_EXTENSION_UNCOMPRESSED, sizeof(KMOD_EXTENSION_UNCOMPRESSED) - 1}, + { KMOD_EXTENSION_UNCOMPRESSED, sizeof(KMOD_EXTENSION_UNCOMPRESSED) - 1 }, #ifdef ENABLE_ZLIB - {".ko.gz", sizeof(".ko.gz") - 1}, + { ".ko.gz", sizeof(".ko.gz") - 1 }, #endif #ifdef ENABLE_XZ - {".ko.xz", sizeof(".ko.xz") - 1}, + { ".ko.xz", sizeof(".ko.xz") - 1 }, #endif #ifdef ENABLE_ZSTD - {".ko.zst", sizeof(".ko.zst") - 1}, + { ".ko.zst", sizeof(".ko.zst") - 1 }, #endif - { }, + {}, }; /* string handling functions and memory allocations */ @@ -292,10 +292,10 @@ char *freadline_wrapped(FILE *fp, unsigned int *linenum) if (buf == NULL) return NULL; - for(;;) { + for (;;) { int ch = getc_unlocked(fp); - switch(ch) { + switch (ch) { case EOF: if (i == 0) return NULL; @@ -453,14 +453,14 @@ int mkdir_parents(const char *path, mode_t mode) static unsigned long long ts_usec(const struct timespec *ts) { - return (unsigned long long) ts->tv_sec * USEC_PER_SEC + - (unsigned long long) ts->tv_nsec / NSEC_PER_USEC; + return (unsigned long long)ts->tv_sec * USEC_PER_SEC + + (unsigned long long)ts->tv_nsec / NSEC_PER_USEC; } static unsigned long long ts_msec(const struct timespec *ts) { - return (unsigned long long) ts->tv_sec * MSEC_PER_SEC + - (unsigned long long) ts->tv_nsec / NSEC_PER_MSEC; + return (unsigned long long)ts->tv_sec * MSEC_PER_SEC + + (unsigned long long)ts->tv_nsec / NSEC_PER_MSEC; } static struct timespec msec_ts(unsigned long long msec) @@ -487,8 +487,7 @@ int sleep_until_msec(unsigned long long msec) /* * Exponential retry backoff with tail */ -unsigned long long get_backoff_delta_msec(unsigned long long t0, - unsigned long long tend, +unsigned long long get_backoff_delta_msec(unsigned long long t0, unsigned long long tend, unsigned long long *delta) { unsigned long long t; @@ -534,6 +533,6 @@ unsigned long long stat_mstamp(const struct stat *st) #ifdef HAVE_STRUCT_STAT_ST_MTIM return ts_usec(&st->st_mtim); #else - return (unsigned long long) st->st_mtime; + return (unsigned long long)st->st_mtime; #endif } diff --git a/shared/util.h b/shared/util.h index 1b87be0b..bcd834e5 100644 --- a/shared/util.h +++ b/shared/util.h @@ -24,10 +24,13 @@ _nonnull_all_ void *memdup(const void *p, size_t n); /* ************************************************************************ */ #define KMOD_EXTENSION_UNCOMPRESSED ".ko" -_must_check_ _nonnull_(1, 2) int alias_normalize(const char *alias, char buf[static PATH_MAX], size_t *len); +_must_check_ _nonnull_(1, 2) int alias_normalize(const char *alias, + char buf[static PATH_MAX], size_t *len); _must_check_ int underscores(char *s); -_nonnull_(1, 2) char *modname_normalize(const char *modname, char buf[static PATH_MAX], size_t *len); -_nonnull_(2) char *path_to_modname(const char *path, char buf[static PATH_MAX], size_t *len); +_nonnull_(1, 2) char *modname_normalize(const char *modname, char buf[static PATH_MAX], + size_t *len); +_nonnull_(2) char *path_to_modname(const char *path, char buf[static PATH_MAX], + size_t *len); _nonnull_all_ bool path_ends_with_kmod_ext(const char *path, size_t len); /* read-like and fread-like functions */ @@ -47,36 +50,34 @@ unsigned long long stat_mstamp(const struct stat *st); /* time-related functions * ************************************************************************ */ -#define USEC_PER_SEC 1000000ULL -#define USEC_PER_MSEC 1000ULL -#define MSEC_PER_SEC 1000ULL -#define NSEC_PER_MSEC 1000000ULL +#define USEC_PER_SEC 1000000ULL +#define USEC_PER_MSEC 1000ULL +#define MSEC_PER_SEC 1000ULL +#define NSEC_PER_MSEC 1000000ULL unsigned long long now_usec(void); unsigned long long now_msec(void); int sleep_until_msec(unsigned long long msec); -unsigned long long get_backoff_delta_msec(unsigned long long t0, - unsigned long long tend, +unsigned long long get_backoff_delta_msec(unsigned long long t0, unsigned long long tend, unsigned long long *delta); - /* endianness and alignments */ /* ************************************************************************ */ -#define get_unaligned(ptr) \ -({ \ - struct __attribute__((packed)) { \ - typeof(*(ptr)) __v; \ - } *__p = (typeof(__p)) (ptr); \ - __p->__v; \ -}) +#define get_unaligned(ptr) \ + ({ \ + struct __attribute__((packed)) { \ + typeof(*(ptr)) __v; \ + } *__p = (typeof(__p))(ptr); \ + __p->__v; \ + }) -#define put_unaligned(val, ptr) \ -do { \ - struct __attribute__((packed)) { \ - typeof(*(ptr)) __v; \ - } *__p = (typeof(__p)) (ptr); \ - __p->__v = (val); \ -} while(0) +#define put_unaligned(val, ptr) \ + do { \ + struct __attribute__((packed)) { \ + typeof(*(ptr)) __v; \ + } *__p = (typeof(__p))(ptr); \ + __p->__v = (val); \ + } while (0) static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u) { @@ -85,8 +86,9 @@ static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u) /* misc */ /* ************************************************************************ */ -static inline void freep(void *p) { - free(*(void**) p); +static inline void freep(void *p) +{ + free(*(void **)p); } #define _cleanup_free_ _cleanup_(freep)