X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=c_src%2Ftools-util.h;fp=tools-util.h;h=4682406ee96f2b4b01c9e916a4b8254011ae748d;hb=5fe6f426350503aa6c0823e7e6bf81fd7f138d71;hp=7a04c1080beb9ae5dc0df82ae40579ad3c20c7d4;hpb=dfd3fabdb57c524b66e6c86e91a1124d6e80fcbb;p=bcachefs-tools-debian diff --git a/tools-util.h b/c_src/tools-util.h similarity index 86% rename from tools-util.h rename to c_src/tools-util.h index 7a04c10..4682406 100644 --- a/tools-util.h +++ b/c_src/tools-util.h @@ -20,7 +20,7 @@ #include #include "libbcachefs/bcachefs.h" #include "libbcachefs/bbpos.h" -#include "libbcachefs/darray.h" +#include "linux/darray.h" #define noreturn __attribute__((noreturn)) @@ -28,15 +28,42 @@ void die(const char *, ...) __attribute__ ((format (printf, 1, 2))) noreturn; char *mprintf(const char *, ...) __attribute__ ((format (printf, 1, 2))); -void *xcalloc(size_t, size_t); -void *xmalloc(size_t); -void *xrealloc(void *, size_t); void xpread(int, void *, size_t, off_t); void xpwrite(int, const void *, size_t, off_t, const char *); struct stat xfstatat(int, const char *, int); struct stat xfstat(int); struct stat xstat(const char *); +static inline void *xmalloc(size_t size) +{ + void *p = malloc(size); + + if (!p) + die("insufficient memory"); + + memset(p, 0, size); + return p; +} + +static inline void *xcalloc(size_t count, size_t size) +{ + void *p = calloc(count, size); + + if (!p) + die("insufficient memory"); + + return p; +} + +static inline void *xrealloc(void *p, size_t size) +{ + p = realloc(p, size); + if (!p) + die("insufficient memory"); + + return p; +} + #define xopenat(_dirfd, _path, ...) \ ({ \ int _fd = openat((_dirfd), (_path), __VA_ARGS__); \ @@ -172,4 +199,13 @@ do { \ struct bpos bpos_parse(char *); struct bbpos bbpos_parse(char *); +struct bbpos_range { + struct bbpos start; + struct bbpos end; +}; + +struct bbpos_range bbpos_range_parse(char *); + +darray_str get_or_split_cmdline_devs(int argc, char *argv[]); + #endif /* _TOOLS_UTIL_H */