#include "lib-lib.h"
-#define BUFSIZ_INCREMENT 1024
-
-void buffer_resize(buffer_t *buf, ssize_t newsize)
+void buffer_splice(buffer_t *buf, ssize_t pos, ssize_t len,
+ const void *data, ssize_t dlen)
{
- if (newsize >= buf->size) {
- /* rounds newsize to the 1024 multiple just after newsize+1 */
- newsize = (newsize + BUFSIZ_INCREMENT) & ~(BUFSIZ_INCREMENT - 1);
- p_realloc(&buf->data, newsize);
- }
+ if (dlen >= len)
+ buffer_extend(buf, dlen - len);
+ memmove(buf->data + pos + dlen,
+ buf->data + pos + len,
+ buf->len - pos - len);
+ memcpy(buf->data + pos, data, dlen);
+ buffer_setlen(buf, buf->len + dlen - len);
}
+ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list args)
+{
+ ssize_t len;
+ va_list ap;
+ va_copy(ap, args);
+ buffer_ensure(buf, BUFSIZ);
+
+ len = vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, args);
+ if (len < 0)
+ return len;
+ if (len >= buf->size - buf->len) {
+ buffer_ensure(buf, len);
+ vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, ap);
+ }
+ buf->len += len;
+ buf->data[buf->len] = '\0';
+ return len;
+}
+ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...)
+{
+ ssize_t res;
+ va_list args;
+ va_start(args, fmt);
+ res = buffer_addvf(buf, fmt, args);
+ va_end(args);
+ return res;
+}
/****** LEGACY BUFFERS *******/
/*
*buf->dptr = '\0';
}
-ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list args)
-{
- ssize_t len;
- va_list ap;
-
- va_copy(ap, args);
- buffer_ensure(buf, BUFSIZ);
-
- len = vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, args);
- if (len < 0)
- return len;
- if (len >= buf->size - buf->len) {
- buffer_ensure(buf, len);
- vsnprintf(buf->data + buf->len, buf->size - buf->len, fmt, ap);
- }
- buf->len += len;
- buf->data[buf->len] = '\0';
-
- return len;
-}
-
-ssize_t buffer_addf(buffer_t *buf, const char *fmt, ...)
-{
- ssize_t res;
- va_list args;
- va_start(args, fmt);
- res = buffer_addvf(buf, fmt, args);
- va_end(args);
- return res;
-}
}
-void buffer_resize(buffer_t *, ssize_t newsize);
+static inline void buffer_resize(buffer_t *buf, ssize_t newsize) {
+ p_allocgrow(&buf->data, newsize + 1, &buf->size);
+}
static inline void buffer_ensure(buffer_t *buf, ssize_t extra) {
assert (extra >= 0);
if (buf->len + extra >= buf->size) {
buffer_resize(buf, buf->len + extra);
}
}
+static inline void buffer_setlen(buffer_t *buf, ssize_t len) {
+ assert (buf->size > len);
+ buf->len = len;
+ buf->data[len] = '\0';
+}
static inline void buffer_extend(buffer_t *buf, ssize_t extra) {
buffer_ensure(buf, extra);
buf->len += extra;
}
}
+void buffer_splice(buffer_t *, ssize_t pos, ssize_t len, const void *, ssize_t);
+
ssize_t buffer_addvf(buffer_t *buf, const char *fmt, va_list)
__attribute__((format(printf, 2, 0)));
#define p_dupstr(p, len) xmemdupstr((p), (len))
#define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count))
+#define p_alloc_nr(x) (((x) + 16) * 3 / 2)
+
+#define p_allocgrow(pp, goalnb, allocnb) \
+ do { \
+ if ((goalnb) > *(allocnb)) { \
+ if (p_alloc_nr(*(allocnb)) < (goalnb)) { \
+ *(allocnb) = (goalnb); \
+ } else { \
+ *(allocnb) = p_alloc_nr(*(allocnb)); \
+ } \
+ p_realloc(pp, *(allocnb)); \
+ } \
+ } while (0)
+
#ifdef __GNUC__
# define p_delete(mem_pp) \