diff options
author | Lucas De Marchi <lucas.demarchi@intel.com> | 2014-10-03 00:01:35 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@intel.com> | 2014-10-03 00:33:25 -0300 |
commit | 96573a02208abebe4a884c0bbd0d6ecde9047f37 (patch) | |
tree | 1435476e04e26e15bee1d7bd30af26a8c50a31ae /shared/util.h | |
parent | 8b7189bc25b7becb0e428a74528d8def08bd2f4a (diff) | |
download | kmod-96573a02208abebe4a884c0bbd0d6ecde9047f37.tar.gz kmod-96573a02208abebe4a884c0bbd0d6ecde9047f37.tar.bz2 kmod-96573a02208abebe4a884c0bbd0d6ecde9047f37.zip |
Move generic util functions to shared directory
Diffstat (limited to 'shared/util.h')
-rw-r--r-- | shared/util.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/shared/util.h b/shared/util.h new file mode 100644 index 0000000..1ed7214 --- /dev/null +++ b/shared/util.h @@ -0,0 +1,64 @@ +#pragma once + +#include <limits.h> +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <shared/macro.h> + +/* string handling functions and memory allocations */ +/* ************************************************************************ */ +#define streq(a, b) (strcmp((a), (b)) == 0) +#define strstartswith(a, b) (strncmp(a, b, strlen(b)) == 0) +char *strchr_replace(char *s, int c, char r); +void *memdup(const void *p, size_t n) __attribute__((nonnull(1))); + +/* read-like and fread-like functions */ +/* ************************************************************************ */ +ssize_t read_str_safe(int fd, char *buf, size_t buflen) _must_check_ __attribute__((nonnull(2))); +ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((nonnull(2))); +int read_str_long(int fd, long *value, int base) _must_check_ __attribute__((nonnull(2))); +int read_str_ulong(int fd, unsigned long *value, int base) _must_check_ __attribute__((nonnull(2))); +char *getline_wrapped(FILE *fp, unsigned int *linenum) __attribute__((nonnull(1))); + +/* path handling functions */ +/* ************************************************************************ */ +bool path_is_absolute(const char *p) _must_check_ __attribute__((nonnull(1))); +char *path_make_absolute_cwd(const char *p) _must_check_ __attribute__((nonnull(1))); +int mkdir_p(const char *path, int len, mode_t mode); +int mkdir_parents(const char *path, mode_t mode); +unsigned long long stat_mstamp(const struct stat *st); +unsigned long long ts_usec(const struct timespec *ts); + +/* endianess and alignments */ +/* ************************************************************************ */ +#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) + +static _always_inline_ unsigned int ALIGN_POWER2(unsigned int u) +{ + return 1 << ((sizeof(u) * 8) - __builtin_clz(u - 1)); +} + +/* misc */ +/* ************************************************************************ */ +static inline void freep(void *p) { + free(*(void**) p); +} +#define _cleanup_free_ _cleanup_(freep) |