^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef __PERF_STRBUF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define __PERF_STRBUF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Strbuf's can be use in many ways: as a byte array, or to store arbitrary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * long, overflow safe strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Strbufs has some invariants that are very important to keep in mind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * build complex strings/buffers whose final size isn't easily known.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * It is NOT legal to copy the ->buf pointer away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * `strbuf_detach' is the operation that detachs a buffer from its shell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * while keeping the shell valid wrt its invariants.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * 2. the ->buf member is a byte array that has at least ->len + 1 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * allocated. The extra byte is used to store a '\0', allowing the ->buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * member to be a valid C-string. Every strbuf function ensure this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * invariant is preserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Note that it is OK to "play" with the buffer directly if you work it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * that way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * strbuf_grow(sb, SOME_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * ... Here, the memory array starting at sb->buf, and of length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * ... strbuf_avail(sb) is all yours, and you are sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * ... strbuf_avail(sb) is at least SOME_SIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * Doing so is safe, though if it has to be done in many places, adding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * missing API to the strbuf module is the way to go.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * even if it's true in the current implementation. Alloc is somehow a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * "private" member that should not be messed with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) extern char strbuf_slopbuf[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct strbuf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) size_t alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define STRBUF_INIT { 0, 0, strbuf_slopbuf }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*----- strbuf life cycle -----*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int strbuf_init(struct strbuf *buf, ssize_t hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void strbuf_release(struct strbuf *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) char *strbuf_detach(struct strbuf *buf, size_t *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*----- strbuf size related -----*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static inline ssize_t strbuf_avail(const struct strbuf *sb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return sb->alloc ? sb->alloc - sb->len - 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int strbuf_grow(struct strbuf *buf, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static inline int strbuf_setlen(struct strbuf *sb, size_t len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!sb->alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int ret = strbuf_grow(sb, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) assert(len < sb->alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) sb->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) sb->buf[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*----- add data in your buffer -----*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int strbuf_addch(struct strbuf *sb, int c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int strbuf_add(struct strbuf *buf, const void *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static inline int strbuf_addstr(struct strbuf *sb, const char *s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return strbuf_add(sb, s, strlen(s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) int strbuf_addf(struct strbuf *sb, const char *fmt, ...) __printf(2, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* XXX: if read fails, any partial read is undone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #endif /* __PERF_STRBUF_H */