xref: /linux-6.15/include/linux/seq_buf.h (revision 0736c033)
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 {
19 	char			*buffer;
20 	size_t			size;
21 	size_t			len;
22 	loff_t			readpos;
23 };
24 
25 static inline void seq_buf_clear(struct seq_buf *s)
26 {
27 	s->len = 0;
28 	s->readpos = 0;
29 }
30 
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;
36 	seq_buf_clear(s);
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 {
46 	return s->len == s->size;
47 }
48 
49 static inline void
50 seq_buf_set_overflow(struct seq_buf *s)
51 {
52 	s->len = s->size;
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 
64 	return (s->size - 1) - s->len;
65 }
66 
67 extern __printf(2, 3)
68 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...);
69 extern __printf(2, 0)
70 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args);
71 extern int
72 seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
73 extern int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s);
74 extern int seq_buf_to_user(struct seq_buf *s, char __user *ubuf,
75 			   int cnt);
76 extern int seq_buf_puts(struct seq_buf *s, const char *str);
77 extern int seq_buf_putc(struct seq_buf *s, unsigned char c);
78 extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
79 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
80 			      unsigned int len);
81 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
82 
83 extern int seq_buf_bitmask(struct seq_buf *s, const unsigned long *maskp,
84 			   int nmaskbits);
85 
86 #endif /* _LINUX_SEQ_BUF_H */
87