perf tools: Rewrite strbuf not to die()

Rewrite strbuf implementation not to use die() nor xrealloc().  Instead
of die(), now most of the API returns error code or 0 if succeeded.

Suggested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160510054658.6158.24080.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Masami Hiramatsu 2016-05-10 14:46:58 +09:00 committed by Arnaldo Carvalho de Melo
parent 9c7b37cd63
commit 5cea57f30a
2 changed files with 82 additions and 36 deletions

View file

@ -51,7 +51,7 @@ struct strbuf {
#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
/*----- strbuf life cycle -----*/
void strbuf_init(struct strbuf *buf, ssize_t hint);
int strbuf_init(struct strbuf *buf, ssize_t hint);
void strbuf_release(struct strbuf *buf);
char *strbuf_detach(struct strbuf *buf, size_t *);
@ -60,26 +60,31 @@ static inline ssize_t strbuf_avail(const struct strbuf *sb) {
return sb->alloc ? sb->alloc - sb->len - 1 : 0;
}
void strbuf_grow(struct strbuf *buf, size_t);
int strbuf_grow(struct strbuf *buf, size_t);
static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
if (!sb->alloc)
strbuf_grow(sb, 0);
static inline int strbuf_setlen(struct strbuf *sb, size_t len) {
int ret;
if (!sb->alloc) {
ret = strbuf_grow(sb, 0);
if (ret)
return ret;
}
assert(len < sb->alloc);
sb->len = len;
sb->buf[len] = '\0';
return 0;
}
/*----- add data in your buffer -----*/
void strbuf_addch(struct strbuf *sb, int c);
int strbuf_addch(struct strbuf *sb, int c);
void strbuf_add(struct strbuf *buf, const void *, size_t);
static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
strbuf_add(sb, s, strlen(s));
int strbuf_add(struct strbuf *buf, const void *, size_t);
static inline int strbuf_addstr(struct strbuf *sb, const char *s) {
return strbuf_add(sb, s, strlen(s));
}
__attribute__((format(printf,2,3)))
void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
int strbuf_addf(struct strbuf *sb, const char *fmt, ...);
/* XXX: if read fails, any partial read is undone */
ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint);