1 /* 2 * Copyright (c) 2025-2026 The FreeBSD Foundation 3 * Copyright (c) 2025-2026 Jean-Sébastien Pédron <dumbbell@FreeBSD.org> 4 * 5 * This software was developed by Jean-Sébastien Pédron under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * SPDX-License-Identifier: BSD-2-Clause 9 */ 10 11 #ifndef _LINUXKPI_LINUX_SEQ_BUF_H_ 12 #define _LINUXKPI_LINUX_SEQ_BUF_H_ 13 14 #include <linux/bug.h> 15 #include <linux/minmax.h> 16 #include <linux/seq_file.h> 17 #include <linux/types.h> 18 19 struct seq_buf { 20 char *buffer; 21 size_t size; 22 size_t len; 23 }; 24 25 #define DECLARE_SEQ_BUF(NAME, SIZE) \ 26 struct seq_buf NAME = { \ 27 .buffer = (char[SIZE]) { 0 }, \ 28 .size = SIZE, \ 29 } 30 31 static inline void 32 seq_buf_clear(struct seq_buf *s) 33 { 34 s->len = 0; 35 if (s->size > 0) 36 s->buffer[0] = '\0'; 37 } 38 39 static inline void 40 seq_buf_set_overflow(struct seq_buf *s) 41 { 42 s->len = s->size + 1; 43 } 44 45 static inline bool 46 seq_buf_has_overflowed(struct seq_buf *s) 47 { 48 return (s->len > s->size); 49 } 50 51 static inline bool 52 seq_buf_buffer_left(struct seq_buf *s) 53 { 54 if (seq_buf_has_overflowed(s)) 55 return (0); 56 57 return (s->size - s->len); 58 } 59 60 #define seq_buf_init(s, buf, size) linuxkpi_seq_buf_init((s), (buf), (size)) 61 void linuxkpi_seq_buf_init(struct seq_buf *s, char *buf, unsigned int size); 62 63 #define seq_buf_printf(s, f, ...) linuxkpi_seq_buf_printf((s), (f), __VA_ARGS__) 64 int linuxkpi_seq_buf_printf(struct seq_buf *s, const char *fmt, ...) \ 65 __printflike(2, 3); 66 67 #define seq_buf_vprintf(s, f, a) linuxkpi_seq_buf_vprintf((s), (f), (a)) 68 int linuxkpi_seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args); 69 70 #define seq_buf_str(s) linuxkpi_seq_buf_str((s)) 71 const char * linuxkpi_seq_buf_str(struct seq_buf *s); 72 73 #endif 74