]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Update bcachefs sources to 3704d0779c bcachefs: Improved human readable integer parsing
[bcachefs-tools-debian] / libbcachefs / util.h
index 2c9e91023bb96a6c80bc4d81e43cce075b046549..1fe66fd91ccc724203d29b83d6787c94f9e1b5b6 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/sched/clock.h>
 #include <linux/llist.h>
 #include <linux/log2.h>
+#include <linux/printbuf.h>
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/ratelimit.h>
@@ -210,9 +211,11 @@ do {                                                                       \
                                                                        \
        BUG_ON(_i >= (h)->used);                                        \
        (h)->used--;                                                    \
-       heap_swap(h, _i, (h)->used, set_backpointer);                   \
-       heap_sift_up(h, _i, cmp, set_backpointer);                      \
-       heap_sift_down(h, _i, cmp, set_backpointer);                    \
+       if ((_i) < (h)->used) {                                         \
+               heap_swap(h, _i, (h)->used, set_backpointer);           \
+               heap_sift_up(h, _i, cmp, set_backpointer);              \
+               heap_sift_down(h, _i, cmp, set_backpointer);            \
+       }                                                               \
 } while (0)
 
 #define heap_pop(h, d, cmp, set_backpointer)                           \
@@ -235,124 +238,11 @@ do {                                                                     \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
-enum printbuf_units {
-       PRINTBUF_UNITS_RAW,
-       PRINTBUF_UNITS_BYTES,
-       PRINTBUF_UNITS_HUMAN_READABLE,
-};
-
-struct printbuf {
-       char                    *buf;
-       unsigned                size;
-       unsigned                pos;
-       unsigned                last_newline;
-       unsigned                last_field;
-       unsigned                indent;
-       enum printbuf_units     units:8;
-       u8                      atomic;
-       bool                    allocation_failure:1;
-       u8                      tabstop;
-       u8                      tabstops[4];
-};
-
-#define PRINTBUF ((struct printbuf) { NULL })
-
-static inline void printbuf_exit(struct printbuf *buf)
-{
-       kfree(buf->buf);
-       buf->buf = ERR_PTR(-EINTR); /* poison value */
-}
-
-static inline void printbuf_reset(struct printbuf *buf)
-{
-       buf->pos                = 0;
-       buf->last_newline       = 0;
-       buf->last_field         = 0;
-       buf->indent             = 0;
-       buf->tabstop            = 0;
-}
-
-static inline size_t printbuf_remaining(struct printbuf *buf)
-{
-       return buf->size - buf->pos;
-}
-
-static inline size_t printbuf_linelen(struct printbuf *buf)
-{
-       return buf->pos - buf->last_newline;
-}
-
-void bch2_pr_buf(struct printbuf *out, const char *fmt, ...);
-
-#define pr_buf(_out, ...) bch2_pr_buf(_out, __VA_ARGS__)
-
-static inline void pr_char(struct printbuf *out, char c)
-{
-       bch2_pr_buf(out, "%c", c);
-}
-
-static inline void pr_indent_push(struct printbuf *buf, unsigned spaces)
-{
-       buf->indent += spaces;
-       while (spaces--)
-               pr_char(buf, ' ');
-}
-
-static inline void pr_indent_pop(struct printbuf *buf, unsigned spaces)
-{
-       if (buf->last_newline + buf->indent == buf->pos) {
-               buf->pos -= spaces;
-               buf->buf[buf->pos] = 0;
-       }
-       buf->indent -= spaces;
-}
-
-static inline void pr_newline(struct printbuf *buf)
-{
-       unsigned i;
-
-       pr_char(buf, '\n');
-
-       buf->last_newline       = buf->pos;
-
-       for (i = 0; i < buf->indent; i++)
-               pr_char(buf, ' ');
-
-       buf->last_field         = buf->pos;
-       buf->tabstop = 0;
-}
-
-static inline void pr_tab(struct printbuf *buf)
-{
-       BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops));
-
-       while (printbuf_remaining(buf) > 1 &&
-              printbuf_linelen(buf) < buf->tabstops[buf->tabstop])
-               pr_char(buf, ' ');
-
-       buf->last_field = buf->pos;
-       buf->tabstop++;
-}
-
-void bch2_pr_tab_rjust(struct printbuf *);
-
-static inline void pr_tab_rjust(struct printbuf *buf)
-{
-       bch2_pr_tab_rjust(buf);
-}
-
-void bch2_pr_units(struct printbuf *, s64, s64);
-#define pr_units(...) bch2_pr_units(__VA_ARGS__)
-
-static inline void pr_sectors(struct printbuf *out, u64 v)
-{
-       bch2_pr_units(out, v, v << 9);
-}
 
 #ifdef __KERNEL__
 static inline void pr_time(struct printbuf *out, u64 time)
 {
-       pr_buf(out, "%llu", time);
+       prt_printf(out, "%llu", time);
 }
 #else
 #include <time.h>
@@ -363,16 +253,16 @@ static inline void pr_time(struct printbuf *out, u64 _time)
        struct tm *tm = localtime(&time);
        size_t err = strftime(time_str, sizeof(time_str), "%c", tm);
        if (!err)
-               pr_buf(out, "(formatting error)");
+               prt_printf(out, "(formatting error)");
        else
-               pr_buf(out, "%s", time_str);
+               prt_printf(out, "%s", time_str);
 }
 #endif
 
 #ifdef __KERNEL__
 static inline void uuid_unparse_lower(u8 *uuid, char *out)
 {
-       sprintf(out, "%plU", uuid);
+       sprintf(out, "%pUb", uuid);
 }
 #else
 #include <uuid/uuid.h>
@@ -383,7 +273,7 @@ static inline void pr_uuid(struct printbuf *out, u8 *uuid)
        char uuid_str[40];
 
        uuid_unparse_lower(uuid, uuid_str);
-       pr_buf(out, uuid_str);
+       prt_printf(out, "%s", uuid_str);
 }
 
 int bch2_strtoint_h(const char *, int *);
@@ -449,7 +339,7 @@ static inline int bch2_strtoul_h(const char *cp, long *res)
 })
 
 #define snprint(out, var)                                              \
-       pr_buf(out,                                                     \
+       prt_printf(out,                                                 \
                   type_is(var, int)            ? "%i\n"                \
                 : type_is(var, unsigned)       ? "%u\n"                \
                 : type_is(var, long)           ? "%li\n"               \
@@ -459,14 +349,8 @@ static inline int bch2_strtoul_h(const char *cp, long *res)
                 : type_is(var, char *)         ? "%s\n"                \
                 : "%i\n", var)
 
-void bch2_hprint(struct printbuf *, s64);
-
 bool bch2_is_zero(const void *, size_t);
 
-void bch2_string_opt_to_text(struct printbuf *,
-                            const char * const [], size_t);
-
-void bch2_flags_to_text(struct printbuf *, const char * const[], u64);
 u64 bch2_read_flag_list(char *, const char * const[]);
 
 #define NR_QUANTILES   15
@@ -803,6 +687,31 @@ do {                                                                       \
 #define array_remove_item(_array, _nr, _pos)                           \
        array_remove_items(_array, _nr, _pos, 1)
 
+static inline void __move_gap(void *array, size_t element_size,
+                             size_t nr, size_t size,
+                             size_t old_gap, size_t new_gap)
+{
+       size_t gap_end = old_gap + size - nr;
+
+       if (new_gap < old_gap) {
+               size_t move = old_gap - new_gap;
+
+               memmove(array + element_size * (gap_end - move),
+                       array + element_size * (old_gap - move),
+                               element_size * move);
+       } else if (new_gap > old_gap) {
+               size_t move = new_gap - old_gap;
+
+               memmove(array + element_size * old_gap,
+                       array + element_size * gap_end,
+                               element_size * move);
+       }
+}
+
+/* Move the gap in a gap buffer: */
+#define move_gap(_array, _nr, _size, _old_gap, _new_gap)       \
+       __move_gap(_array, sizeof(_array[0]), _nr, _size, _old_gap, _new_gap)
+
 #define bubble_sort(_base, _nr, _cmp)                                  \
 do {                                                                   \
        ssize_t _i, _end;                                               \