Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 1 | #ifndef _LINUX_SEQ_BUF_H |
| 2 | #define _LINUX_SEQ_BUF_H |
| 3 | |
| 4 | #include <linux/fs.h> |
| 5 | |
| 6 | /* |
| 7 | * Trace sequences are used to allow a function to call several other functions |
| 8 | * to create a string of data to use. |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * seq_buf - seq buffer structure |
| 13 | * @buffer: pointer to the buffer |
| 14 | * @size: size of the buffer |
| 15 | * @len: the amount of data inside the buffer |
| 16 | * @readpos: The next position to read in the buffer. |
| 17 | */ |
| 18 | struct seq_buf { |
Steven Rostedt (Red Hat) | 9a77779 | 2014-10-29 13:59:58 -0400 | [diff] [blame] | 19 | char *buffer; |
| 20 | size_t size; |
| 21 | size_t len; |
| 22 | loff_t readpos; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 23 | }; |
| 24 | |
Steven Rostedt (Red Hat) | 0736c03 | 2014-10-29 14:17:52 -0400 | [diff] [blame] | 25 | static inline void seq_buf_clear(struct seq_buf *s) |
| 26 | { |
| 27 | s->len = 0; |
| 28 | s->readpos = 0; |
| 29 | } |
| 30 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 31 | static inline void |
| 32 | seq_buf_init(struct seq_buf *s, unsigned char *buf, unsigned int size) |
| 33 | { |
| 34 | s->buffer = buf; |
| 35 | s->size = size; |
Steven Rostedt (Red Hat) | 0736c03 | 2014-10-29 14:17:52 -0400 | [diff] [blame] | 36 | seq_buf_clear(s); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* |
| 40 | * seq_buf have a buffer that might overflow. When this happens |
| 41 | * the len and size are set to be equal. |
| 42 | */ |
| 43 | static inline bool |
| 44 | seq_buf_has_overflowed(struct seq_buf *s) |
| 45 | { |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame] | 46 | return s->len > s->size; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static inline void |
| 50 | seq_buf_set_overflow(struct seq_buf *s) |
| 51 | { |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame] | 52 | s->len = s->size + 1; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
| 56 | * How much buffer is left on the seq_buf? |
| 57 | */ |
| 58 | static inline unsigned int |
| 59 | seq_buf_buffer_left(struct seq_buf *s) |
| 60 | { |
| 61 | if (seq_buf_has_overflowed(s)) |
| 62 | return 0; |
| 63 | |
Steven Rostedt (Red Hat) | 8cd709a | 2014-10-29 15:26:09 -0400 | [diff] [blame] | 64 | return s->size - s->len; |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Steven Rostedt (Red Hat) | eeab981 | 2014-11-06 16:38:28 -0500 | [diff] [blame] | 67 | /* How much buffer was written? */ |
| 68 | static inline unsigned int seq_buf_used(struct seq_buf *s) |
| 69 | { |
| 70 | return min(s->len, s->size); |
| 71 | } |
| 72 | |
Steven Rostedt (Red Hat) | 01cb06a | 2014-10-29 17:30:50 -0400 | [diff] [blame] | 73 | /** |
| 74 | * seq_buf_get_buf - get buffer to write arbitrary data to |
| 75 | * @s: the seq_buf handle |
| 76 | * @bufp: the beginning of the buffer is stored here |
| 77 | * |
| 78 | * Return the number of bytes available in the buffer, or zero if |
| 79 | * there's no space. |
| 80 | */ |
| 81 | static inline size_t seq_buf_get_buf(struct seq_buf *s, char **bufp) |
| 82 | { |
| 83 | WARN_ON(s->len > s->size + 1); |
| 84 | |
| 85 | if (s->len < s->size) { |
| 86 | *bufp = s->buffer + s->len; |
| 87 | return s->size - s->len; |
| 88 | } |
| 89 | |
| 90 | *bufp = NULL; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * seq_buf_commit - commit data to the buffer |
| 96 | * @s: the seq_buf handle |
| 97 | * @num: the number of bytes to commit |
| 98 | * |
| 99 | * Commit @num bytes of data written to a buffer previously acquired |
| 100 | * by seq_buf_get. To signal an error condition, or that the data |
| 101 | * didn't fit in the available space, pass a negative @num value. |
| 102 | */ |
| 103 | static inline void seq_buf_commit(struct seq_buf *s, int num) |
| 104 | { |
| 105 | if (num < 0) { |
| 106 | seq_buf_set_overflow(s); |
| 107 | } else { |
| 108 | /* num must be negative on overflow */ |
| 109 | BUG_ON(s->len + num > s->size); |
| 110 | s->len += num; |
| 111 | } |
| 112 | } |
| 113 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 114 | extern __printf(2, 3) |
| 115 | int seq_buf_printf(struct seq_buf *s, const char *fmt, ...); |
| 116 | extern __printf(2, 0) |
| 117 | int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 118 | extern int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s); |
| 119 | extern int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, |
| 120 | int cnt); |
| 121 | extern int seq_buf_puts(struct seq_buf *s, const char *str); |
| 122 | extern int seq_buf_putc(struct seq_buf *s, unsigned char c); |
| 123 | extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len); |
| 124 | extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, |
| 125 | unsigned int len); |
Steven Rostedt (Red Hat) | dd23180 | 2014-10-29 13:48:37 -0400 | [diff] [blame] | 126 | extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc); |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 127 | |
Steven Rostedt (Red Hat) | 2448913 | 2014-11-03 18:53:50 -0500 | [diff] [blame] | 128 | #ifdef CONFIG_BINARY_PRINTF |
| 129 | extern int |
| 130 | seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary); |
| 131 | #endif |
| 132 | |
Steven Rostedt (Red Hat) | 3a161d9 | 2014-06-25 15:54:42 -0400 | [diff] [blame] | 133 | #endif /* _LINUX_SEQ_BUF_H */ |