strbuf: factor out buffer-growing

Signed-off-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Reviewed-By: Gert Wollny <gert.wollny@collabora.com>
macos/master
Erik Faye-Lund 6 years ago
parent 46f68df55c
commit 27fc721c70
  1. 16
      src/vrend_strbuf.h

@ -79,11 +79,8 @@ static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size)
/* this might need tuning */ /* this might need tuning */
#define STRBUF_MIN_MALLOC 1024 #define STRBUF_MIN_MALLOC 1024
static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len) static inline bool strbuf_grow(struct vrend_strbuf *sb, int len)
{ {
assert(!memchr(data, '\0', len));
if (strbuf_get_error(sb))
return;
if (sb->size + len + 1 > sb->alloc_size) { if (sb->size + len + 1 > sb->alloc_size) {
/* Reallocate to the larger size of current alloc + min realloc, /* Reallocate to the larger size of current alloc + min realloc,
* or the resulting string size if larger. * or the resulting string size if larger.
@ -92,11 +89,20 @@ static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *dat
char *new = realloc(sb->buf, new_size); char *new = realloc(sb->buf, new_size);
if (!new) { if (!new) {
strbuf_set_error(sb); strbuf_set_error(sb);
return; return false;
} }
sb->buf = new; sb->buf = new;
sb->alloc_size = new_size; sb->alloc_size = new_size;
} }
return true;
}
static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len)
{
assert(!memchr(data, '\0', len));
if (strbuf_get_error(sb) ||
!strbuf_grow(sb, len))
return;
memcpy(sb->buf + sb->size, data, len); memcpy(sb->buf + sb->size, data, len);
sb->size += len; sb->size += len;
sb->buf[sb->size] = '\0'; sb->buf[sb->size] = '\0';

Loading…
Cancel
Save