seq_buf.h 3.9 KB

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