/* basic pointer array growing in steps */
-
static int array_realloc(struct array *array, size_t new_total)
{
void *tmp;
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);
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;
/* 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;
/* 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;
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;
}
.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;
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--;
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;
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;
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)
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);
#include <stddef.h>
#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))
#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 <stdnoreturn.h> and even on older gcc
#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
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, \
+ }
#include <shared/missing.h>
#include <shared/util.h>
-#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 */
if (buf == NULL)
return NULL;
- for(;;) {
+ for (;;) {
int ch = getc_unlocked(fp);
- switch(ch) {
+ switch (ch) {
case EOF:
if (i == 0)
return NULL;
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)
/*
* 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;
#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
}
/* ************************************************************************ */
#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 */
/* 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)
{
/* misc */
/* ************************************************************************ */
-static inline void freep(void *p) {
- free(*(void**) p);
+static inline void freep(void *p)
+{
+ free(*(void **)p);
}
#define _cleanup_free_ _cleanup_(freep)