18b40f521SJohn Kacur #ifndef __PERF_STRBUF_H 28b40f521SJohn Kacur #define __PERF_STRBUF_H 386470930SIngo Molnar 486470930SIngo Molnar /* 586470930SIngo Molnar * Strbuf's can be use in many ways: as a byte array, or to store arbitrary 686470930SIngo Molnar * long, overflow safe strings. 786470930SIngo Molnar * 886470930SIngo Molnar * Strbufs has some invariants that are very important to keep in mind: 986470930SIngo Molnar * 1086470930SIngo Molnar * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to 1186470930SIngo Molnar * build complex strings/buffers whose final size isn't easily known. 1286470930SIngo Molnar * 1386470930SIngo Molnar * It is NOT legal to copy the ->buf pointer away. 1486470930SIngo Molnar * `strbuf_detach' is the operation that detachs a buffer from its shell 1586470930SIngo Molnar * while keeping the shell valid wrt its invariants. 1686470930SIngo Molnar * 1786470930SIngo Molnar * 2. the ->buf member is a byte array that has at least ->len + 1 bytes 1886470930SIngo Molnar * allocated. The extra byte is used to store a '\0', allowing the ->buf 1986470930SIngo Molnar * member to be a valid C-string. Every strbuf function ensure this 2086470930SIngo Molnar * invariant is preserved. 2186470930SIngo Molnar * 2286470930SIngo Molnar * Note that it is OK to "play" with the buffer directly if you work it 2386470930SIngo Molnar * that way: 2486470930SIngo Molnar * 2586470930SIngo Molnar * strbuf_grow(sb, SOME_SIZE); 2686470930SIngo Molnar * ... Here, the memory array starting at sb->buf, and of length 2786470930SIngo Molnar * ... strbuf_avail(sb) is all yours, and you are sure that 2886470930SIngo Molnar * ... strbuf_avail(sb) is at least SOME_SIZE. 2986470930SIngo Molnar * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE); 3086470930SIngo Molnar * 3186470930SIngo Molnar * Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb). 3286470930SIngo Molnar * 3386470930SIngo Molnar * Doing so is safe, though if it has to be done in many places, adding the 3486470930SIngo Molnar * missing API to the strbuf module is the way to go. 3586470930SIngo Molnar * 3686470930SIngo Molnar * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1 3786470930SIngo Molnar * even if it's true in the current implementation. Alloc is somehow a 3886470930SIngo Molnar * "private" member that should not be messed with. 3986470930SIngo Molnar */ 4086470930SIngo Molnar 4186470930SIngo Molnar #include <assert.h> 42c7118369SNamhyung Kim #include <stdarg.h> 4386470930SIngo Molnar 4486470930SIngo Molnar extern char strbuf_slopbuf[]; 4586470930SIngo Molnar struct strbuf { 4686470930SIngo Molnar size_t alloc; 4786470930SIngo Molnar size_t len; 4886470930SIngo Molnar char *buf; 4986470930SIngo Molnar }; 5086470930SIngo Molnar 5186470930SIngo Molnar #define STRBUF_INIT { 0, 0, strbuf_slopbuf } 5286470930SIngo Molnar 5386470930SIngo Molnar /*----- strbuf life cycle -----*/ 54f37a291cSIngo Molnar extern void strbuf_init(struct strbuf *buf, ssize_t hint); 5586470930SIngo Molnar extern void strbuf_release(struct strbuf *); 5686470930SIngo Molnar extern char *strbuf_detach(struct strbuf *, size_t *); 5786470930SIngo Molnar 5886470930SIngo Molnar /*----- strbuf size related -----*/ 59f37a291cSIngo Molnar static inline ssize_t strbuf_avail(const struct strbuf *sb) { 6086470930SIngo Molnar return sb->alloc ? sb->alloc - sb->len - 1 : 0; 6186470930SIngo Molnar } 6286470930SIngo Molnar 6386470930SIngo Molnar extern void strbuf_grow(struct strbuf *, size_t); 6486470930SIngo Molnar 6586470930SIngo Molnar static inline void strbuf_setlen(struct strbuf *sb, size_t len) { 6686470930SIngo Molnar if (!sb->alloc) 6786470930SIngo Molnar strbuf_grow(sb, 0); 6886470930SIngo Molnar assert(len < sb->alloc); 6986470930SIngo Molnar sb->len = len; 7086470930SIngo Molnar sb->buf[len] = '\0'; 7186470930SIngo Molnar } 7286470930SIngo Molnar 7386470930SIngo Molnar /*----- add data in your buffer -----*/ 7486470930SIngo Molnar static inline void strbuf_addch(struct strbuf *sb, int c) { 7586470930SIngo Molnar strbuf_grow(sb, 1); 7686470930SIngo Molnar sb->buf[sb->len++] = c; 7786470930SIngo Molnar sb->buf[sb->len] = '\0'; 7886470930SIngo Molnar } 7986470930SIngo Molnar 8086470930SIngo Molnar extern void strbuf_remove(struct strbuf *, size_t pos, size_t len); 8186470930SIngo Molnar 8286470930SIngo Molnar extern void strbuf_add(struct strbuf *, const void *, size_t); 8386470930SIngo Molnar static inline void strbuf_addstr(struct strbuf *sb, const char *s) { 8486470930SIngo Molnar strbuf_add(sb, s, strlen(s)); 8586470930SIngo Molnar } 8686470930SIngo Molnar 8786470930SIngo Molnar __attribute__((format(printf,2,3))) 8886470930SIngo Molnar extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...); 89c7118369SNamhyung Kim extern void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap); 9086470930SIngo Molnar 9186470930SIngo Molnar /* XXX: if read fails, any partial read is undone */ 92f37a291cSIngo Molnar extern ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint); 9386470930SIngo Molnar 948b40f521SJohn Kacur #endif /* __PERF_STRBUF_H */ 95