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