1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
28b40f521SJohn Kacur #ifndef __PERF_STRBUF_H
38b40f521SJohn Kacur #define __PERF_STRBUF_H
486470930SIngo Molnar
586470930SIngo Molnar /*
686470930SIngo Molnar * Strbuf's can be use in many ways: as a byte array, or to store arbitrary
786470930SIngo Molnar * long, overflow safe strings.
886470930SIngo Molnar *
986470930SIngo Molnar * Strbufs has some invariants that are very important to keep in mind:
1086470930SIngo Molnar *
1186470930SIngo Molnar * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
1286470930SIngo Molnar * build complex strings/buffers whose final size isn't easily known.
1386470930SIngo Molnar *
1486470930SIngo Molnar * It is NOT legal to copy the ->buf pointer away.
15*4d39c89fSIngo Molnar * `strbuf_detach' is the operation that detaches a buffer from its shell
1686470930SIngo Molnar * while keeping the shell valid wrt its invariants.
1786470930SIngo Molnar *
1886470930SIngo Molnar * 2. the ->buf member is a byte array that has at least ->len + 1 bytes
1986470930SIngo Molnar * allocated. The extra byte is used to store a '\0', allowing the ->buf
2086470930SIngo Molnar * member to be a valid C-string. Every strbuf function ensure this
2186470930SIngo Molnar * invariant is preserved.
2286470930SIngo Molnar *
2386470930SIngo Molnar * Note that it is OK to "play" with the buffer directly if you work it
2486470930SIngo Molnar * that way:
2586470930SIngo Molnar *
2686470930SIngo Molnar * strbuf_grow(sb, SOME_SIZE);
2786470930SIngo Molnar * ... Here, the memory array starting at sb->buf, and of length
2886470930SIngo Molnar * ... strbuf_avail(sb) is all yours, and you are sure that
2986470930SIngo Molnar * ... strbuf_avail(sb) is at least SOME_SIZE.
3086470930SIngo Molnar * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE);
3186470930SIngo Molnar *
3286470930SIngo Molnar * Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb).
3386470930SIngo Molnar *
3486470930SIngo Molnar * Doing so is safe, though if it has to be done in many places, adding the
3586470930SIngo Molnar * missing API to the strbuf module is the way to go.
3686470930SIngo Molnar *
3786470930SIngo Molnar * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1
3886470930SIngo Molnar * even if it's true in the current implementation. Alloc is somehow a
3986470930SIngo Molnar * "private" member that should not be messed with.
4086470930SIngo Molnar */
4186470930SIngo Molnar
4286470930SIngo Molnar #include <assert.h>
43c7118369SNamhyung Kim #include <stdarg.h>
447ed0958aSArnaldo Carvalho de Melo #include <stddef.h>
457ed0958aSArnaldo Carvalho de Melo #include <string.h>
46afaed6d3SArnaldo Carvalho de Melo #include <linux/compiler.h>
477ed0958aSArnaldo Carvalho de Melo #include <sys/types.h>
4886470930SIngo Molnar
4986470930SIngo Molnar extern char strbuf_slopbuf[];
5086470930SIngo Molnar struct strbuf {
5186470930SIngo Molnar size_t alloc;
5286470930SIngo Molnar size_t len;
5386470930SIngo Molnar char *buf;
5486470930SIngo Molnar };
5586470930SIngo Molnar
5686470930SIngo Molnar #define STRBUF_INIT { 0, 0, strbuf_slopbuf }
5786470930SIngo Molnar
5886470930SIngo Molnar /*----- strbuf life cycle -----*/
595cea57f3SMasami Hiramatsu int strbuf_init(struct strbuf *buf, ssize_t hint);
603938bad4SArnaldo Carvalho de Melo void strbuf_release(struct strbuf *buf);
613938bad4SArnaldo Carvalho de Melo char *strbuf_detach(struct strbuf *buf, size_t *);
6286470930SIngo Molnar
6386470930SIngo Molnar /*----- strbuf size related -----*/
strbuf_avail(const struct strbuf * sb)64f37a291cSIngo Molnar static inline ssize_t strbuf_avail(const struct strbuf *sb) {
6586470930SIngo Molnar return sb->alloc ? sb->alloc - sb->len - 1 : 0;
6686470930SIngo Molnar }
6786470930SIngo Molnar
685cea57f3SMasami Hiramatsu int strbuf_grow(struct strbuf *buf, size_t);
6986470930SIngo Molnar
strbuf_setlen(struct strbuf * sb,size_t len)705cea57f3SMasami Hiramatsu static inline int strbuf_setlen(struct strbuf *sb, size_t len) {
715cea57f3SMasami Hiramatsu if (!sb->alloc) {
7218ef15c6SArnaldo Carvalho de Melo int ret = strbuf_grow(sb, 0);
735cea57f3SMasami Hiramatsu if (ret)
745cea57f3SMasami Hiramatsu return ret;
755cea57f3SMasami Hiramatsu }
7686470930SIngo Molnar assert(len < sb->alloc);
7786470930SIngo Molnar sb->len = len;
7886470930SIngo Molnar sb->buf[len] = '\0';
795cea57f3SMasami Hiramatsu return 0;
8086470930SIngo Molnar }
8186470930SIngo Molnar
8286470930SIngo Molnar /*----- add data in your buffer -----*/
835cea57f3SMasami Hiramatsu int strbuf_addch(struct strbuf *sb, int c);
8486470930SIngo Molnar
855cea57f3SMasami Hiramatsu int strbuf_add(struct strbuf *buf, const void *, size_t);
strbuf_addstr(struct strbuf * sb,const char * s)865cea57f3SMasami Hiramatsu static inline int strbuf_addstr(struct strbuf *sb, const char *s) {
875cea57f3SMasami Hiramatsu return strbuf_add(sb, s, strlen(s));
8886470930SIngo Molnar }
8986470930SIngo Molnar
90afaed6d3SArnaldo Carvalho de Melo int strbuf_addf(struct strbuf *sb, const char *fmt, ...) __printf(2, 3);
9186470930SIngo Molnar
9286470930SIngo Molnar /* XXX: if read fails, any partial read is undone */
933938bad4SArnaldo Carvalho de Melo ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint);
9486470930SIngo Molnar
958b40f521SJohn Kacur #endif /* __PERF_STRBUF_H */
96