diff --git a/core/admin.m b/core/admin.m index ce7821f48f8fbe7cad420c27b5c5a0705de5c463..73fb1654141c479ed998b3001e82aa3092658865 100644 --- a/core/admin.m +++ b/core/admin.m @@ -1452,8 +1452,7 @@ case 107: #line 199 "core/admin.rl" - fiber->rbuf->len -= (void *)pe - (void *)fiber->rbuf->data; - fiber->rbuf->data = pe; + tbuf_ltrim(fiber->rbuf, (void *)pe - (void *)fiber->rbuf->data); if (p != pe) { start(out); diff --git a/core/admin.rl b/core/admin.rl index a9a1c5f1e398d591011005625247fb609147d2af..2e10c8968c904a1324d24e2755cf0b2a47364091 100644 --- a/core/admin.rl +++ b/core/admin.rl @@ -198,8 +198,7 @@ admin_dispatch(void) write exec; }%% - fiber->rbuf->len -= (void *)pe - (void *)fiber->rbuf->data; - fiber->rbuf->data = pe; + tbuf_ltrim(fiber->rbuf, (void *)pe - (void *)fiber->rbuf->data); if (p != pe) { start(out); diff --git a/core/tbuf.m b/core/tbuf.m index baf47ab863e9553c752e0406cb4ce74a506bb851..64031e6b2e1e8a7ba41442a512f13b8e16ba443c 100644 --- a/core/tbuf.m +++ b/core/tbuf.m @@ -125,6 +125,18 @@ tbuf_peek(struct tbuf *b, size_t count) return NULL; } +/** Remove first count bytes from the beginning. */ + +void +tbuf_ltrim(struct tbuf *b, size_t count) +{ + tbuf_assert(b); + assert(count <= b->len); + + memmove(b->data, b->data + count, b->len - count); + b->len -= count; +} + size_t tbuf_reserve(struct tbuf *b, size_t count) { diff --git a/include/tbuf.h b/include/tbuf.h index ee07a38f10eb506febcdc0d49f16e2f8ff6743c1..63a45e344c7a3eb03ac9519f5465c2698df88ac9 100644 --- a/include/tbuf.h +++ b/include/tbuf.h @@ -63,6 +63,16 @@ size_t tbuf_reserve(struct tbuf *b, size_t count); void tbuf_reset(struct tbuf *b); void *tbuf_peek(struct tbuf *b, size_t count); +/** + * Remove count bytes from the beginning, and adjust all sizes + * accordingly. + * + * @param count the number of bytes to forget about. + * + * @pre 0 <= count <= tbuf->len + */ +void tbuf_ltrim(struct tbuf *b, size_t count); + void tbuf_append_field(struct tbuf *b, void *f); void tbuf_vprintf(struct tbuf *b, const char *format, va_list ap) __attribute__ ((format(FORMAT_PRINTF, 2, 0)));