![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef __PERF_STRBUF_H 0003 #define __PERF_STRBUF_H 0004 0005 /* 0006 * Strbuf's can be use in many ways: as a byte array, or to store arbitrary 0007 * long, overflow safe strings. 0008 * 0009 * Strbufs has some invariants that are very important to keep in mind: 0010 * 0011 * 1. the ->buf member is always malloc-ed, hence strbuf's can be used to 0012 * build complex strings/buffers whose final size isn't easily known. 0013 * 0014 * It is NOT legal to copy the ->buf pointer away. 0015 * `strbuf_detach' is the operation that detaches a buffer from its shell 0016 * while keeping the shell valid wrt its invariants. 0017 * 0018 * 2. the ->buf member is a byte array that has at least ->len + 1 bytes 0019 * allocated. The extra byte is used to store a '\0', allowing the ->buf 0020 * member to be a valid C-string. Every strbuf function ensure this 0021 * invariant is preserved. 0022 * 0023 * Note that it is OK to "play" with the buffer directly if you work it 0024 * that way: 0025 * 0026 * strbuf_grow(sb, SOME_SIZE); 0027 * ... Here, the memory array starting at sb->buf, and of length 0028 * ... strbuf_avail(sb) is all yours, and you are sure that 0029 * ... strbuf_avail(sb) is at least SOME_SIZE. 0030 * strbuf_setlen(sb, sb->len + SOME_OTHER_SIZE); 0031 * 0032 * Of course, SOME_OTHER_SIZE must be smaller or equal to strbuf_avail(sb). 0033 * 0034 * Doing so is safe, though if it has to be done in many places, adding the 0035 * missing API to the strbuf module is the way to go. 0036 * 0037 * XXX: do _not_ assume that the area that is yours is of size ->alloc - 1 0038 * even if it's true in the current implementation. Alloc is somehow a 0039 * "private" member that should not be messed with. 0040 */ 0041 0042 #include <assert.h> 0043 #include <stdarg.h> 0044 #include <stddef.h> 0045 #include <string.h> 0046 #include <linux/compiler.h> 0047 #include <sys/types.h> 0048 0049 extern char strbuf_slopbuf[]; 0050 struct strbuf { 0051 size_t alloc; 0052 size_t len; 0053 char *buf; 0054 }; 0055 0056 #define STRBUF_INIT { 0, 0, strbuf_slopbuf } 0057 0058 /*----- strbuf life cycle -----*/ 0059 int strbuf_init(struct strbuf *buf, ssize_t hint); 0060 void strbuf_release(struct strbuf *buf); 0061 char *strbuf_detach(struct strbuf *buf, size_t *); 0062 0063 /*----- strbuf size related -----*/ 0064 static inline ssize_t strbuf_avail(const struct strbuf *sb) { 0065 return sb->alloc ? sb->alloc - sb->len - 1 : 0; 0066 } 0067 0068 int strbuf_grow(struct strbuf *buf, size_t); 0069 0070 static inline int strbuf_setlen(struct strbuf *sb, size_t len) { 0071 if (!sb->alloc) { 0072 int ret = strbuf_grow(sb, 0); 0073 if (ret) 0074 return ret; 0075 } 0076 assert(len < sb->alloc); 0077 sb->len = len; 0078 sb->buf[len] = '\0'; 0079 return 0; 0080 } 0081 0082 /*----- add data in your buffer -----*/ 0083 int strbuf_addch(struct strbuf *sb, int c); 0084 0085 int strbuf_add(struct strbuf *buf, const void *, size_t); 0086 static inline int strbuf_addstr(struct strbuf *sb, const char *s) { 0087 return strbuf_add(sb, s, strlen(s)); 0088 } 0089 0090 int strbuf_addf(struct strbuf *sb, const char *fmt, ...) __printf(2, 3); 0091 0092 /* XXX: if read fails, any partial read is undone */ 0093 ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint); 0094 0095 #endif /* __PERF_STRBUF_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |