X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/31d0247cc58abc0b0720aa7e9972011c5a66995c..d93e9a33ee30faed3945b2a7acfc46b84197d432:/struct/buf.c diff --git a/struct/buf.c b/struct/buf.c index 9efc4fc..10e0544 100644 --- a/struct/buf.c +++ b/struct/buf.c @@ -27,10 +27,10 @@ /*----- Header files ------------------------------------------------------*/ -#include #include #include "buf.h" +#include "growbuf.h" #include "macros.h" /*----- Main code ---------------------------------------------------------*/ @@ -63,11 +63,7 @@ void buf_init(buf *b, void *p, size_t sz) * and ready for writing. */ -void dbuf_create(dbuf *db) -{ - db->_b.base = db->_b.p = db->_b.limit = 0; db->_b.f = BF_ALLOC | BF_WRITE; - db->a = &arena_stdlib; db->sz = 0; -} +void dbuf_create(dbuf *db) { DBCREATE(db); } /* --- @dbuf_reset@ --- * * @@ -89,11 +85,7 @@ void dbuf_reset(dbuf *db) { DBRESET(db); } * Use: Release all of the resources held by a dynamic buffer. */ -void dbuf_destroy(dbuf *db) -{ - if (db->_b.base) x_free(db->a, db->_b.base); - dbuf_create(db); -} +void dbuf_destroy(dbuf *db) { DBDESTROY(db); } /* --- @{,d}buf_break@ --- * * @@ -104,8 +96,8 @@ void dbuf_destroy(dbuf *db) * Use: Marks a buffer as broken. */ -int buf_break(buf *b) { b->f |= BF_BROKEN; return (-1); } -int (dbuf_break)(dbuf *db) { return (dbuf_break(db)); } +int buf_break(buf *b) { BBREAK(b); return (-1); } +int (dbuf_break)(dbuf *db) { DBBREAK(db); return (-1); } /* --- @{,d}buf_flip@ --- * * @@ -118,7 +110,7 @@ int (dbuf_break)(dbuf *db) { return (dbuf_break(db)); } */ void buf_flip(buf *b) { BFLIP(b); } -void (dbuf_flip)(dbuf *db) { dbuf_flip(db); } +void (dbuf_flip)(dbuf *db) { DBFLIP(db); } /* --- @{,d}buf_ensure@ --- * * @@ -147,22 +139,17 @@ int (dbuf_ensure)(dbuf *db, size_t sz) { return (dbuf_ensure(db, sz)); } int buf_tryextend(buf *b, size_t sz) { dbuf *db; - size_t newsz, len; - - if (~b->f&(BF_ALLOC | BF_WRITE)) - { b->f |= BF_BROKEN; return (-1); } - db = (dbuf *)b; - len = BLEN(&db->_b); sz += len; - if (db->sz >= sz) - newsz = db->sz; - else { - newsz = db->sz ? 2*db->sz : 64; - while (newsz < sz) { assert(newsz < ((size_t)-1)/2); newsz *= 2; } - if (!db->_b.base) db->_b.base = x_alloc(db->a, newsz); - else db->_b.base = x_realloc(db->a, db->_b.base, newsz, db->sz); - db->_b.p = db->_b.base + len; db->sz = newsz; + size_t want, len; + + if (sz <= BLEFT(b)) return (0); + if (~b->f&(BF_ALLOC | BF_WRITE)) { b->f |= BF_BROKEN; return (-1); } + + db = (dbuf *)b; len = DBLEN(db); want = sz + len; + if (db->sz < want) { + GROWBUF_EXTEND(size_t, db->a, db->_b.base, db->sz, want, 64, 1); + db->_b.p = db->_b.base + len; } - db->_b.limit = db->_b.base + newsz; + db->_b.limit = db->_b.base + db->sz; return (0); } int (dbuf_tryextend)(dbuf *db, size_t sz) @@ -213,6 +200,83 @@ int buf_put(buf *b, const void *p, size_t sz) int (dbuf_put)(dbuf *db, const void *p, size_t sz) { return (dbuf_put(db, p, sz)); } +/* --- @{,d}buf_fill@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @int ch@ = fill character + * @size_t sz@ = size to fill + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Write @sz@ bytes with value @ch@ to the buffer, as if with + * @memset@. + */ + +int buf_fill(buf *b, int ch, size_t sz) +{ + void *p; + + p = buf_get(b, sz); if (!p) return (-1); + if (sz) memset(p, ch, sz); + return (0); +} +int (dbuf_fill)(dbuf *db, int ch, size_t sz) + { return (dbuf_fill(db, ch, sz)); } + +/* --- @align_step@ --- * + * + * Arguments: @buf *b@ = pointer to a buffer block + * @size_t m, a@ = alignment parameters + * + * Returns: The number of bytes to skip or fill. + */ + +static size_t align_step(buf *b, size_t m, size_t a) +{ + if (m < 2) return (0); + else if (!(m&(m - 1))) return ((a - BLEN(b))&(m - 1)); + else return ((a + m - BLEN(b)%m)%m); +} + +/* --- @{,d}buf_alignskip@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @size_t m, a@ = alignment multiple and offset + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Advance the buffer position as little as possible such that + * it is @a@ greater than a multiple of @m@. This doesn't write + * anything to the buffer, so it's probably not suitable for + * output: use @buf_alignfill@ instead. + */ + +int buf_alignskip(buf *b, size_t m, size_t a) +{ + if (!buf_get(b, align_step(b, m, a))) return (-1); + else return (0); +} +int (dbuf_alignskip)(dbuf *db, size_t m, size_t a) + { return (dbuf_alignskip(db, m, a)); } + +/* --- @{,d}buf_alignfill@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @int ch@ = fill character + * @size_t m, a@ = alignment multiple and offset + * + * Returns: Zero if it worked, nonzero if there wasn't enough space. + * + * Use: Fill the buffer with as few copies of @ch@ as possible, as if + * by @memset@, to advance the buffer position to a value @a@ + * greater than a multiple of @m@. + */ + +int buf_alignfill(buf *b, int ch, size_t m, size_t a) + { return (buf_fill(b, ch, align_step(b, m, a))); } +int (dbuf_alignfill)(dbuf *db, int ch, size_t m, size_t a) + { return (dbuf_alignfill(db, ch, m, a)); } + /* --- @{,d}buf_getbyte@ --- * * * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block @@ -374,10 +438,7 @@ static int findz(buf *b, size_t *nn) { octet *p; - if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) { - buf_break(b); - return (-1); - } + if ((p = memchr(BCUR(b), 0, BLEFT(b))) == 0) { BBREAK(b); return (-1); } *nn = p - BCUR(b) + 1; return (0); } @@ -423,7 +484,7 @@ static void *getmem_k64(buf *b, size_t *nn_out, kludge64 k) size_t n; ASSIGN64(szmax, (size_t)-1); - if (CMP64(k, >, szmax)) { buf_break(b); return (-1); } + if (CMP64(k, >, szmax)) { BBREAK(b); return (-1); } n = GET64(size_t, k); *nn_out = n; return (buf_get(b, n)); } @@ -478,7 +539,7 @@ void *(dbuf_getmem64b)(dbuf *db, size_t *nn) { \ MUFFLE_WARNINGS_STMT \ (CLANG_WARNING("-Wtautological-constant-out-of-range-compare"), \ - { assert(sz <= MASK##W); }); \ + { if (sz > MASK##W) { BBREAK(b); return (-1); } }); \ if (buf_putu##w(b, sz) || buf_put(b, p, sz)) \ return (-1); \ return (0); \ @@ -526,7 +587,7 @@ int buf_putmemz(buf *b, const void *p, size_t n) { octet *q; - assert(!memchr(p, 0, n)); + if (memchr(p, 0, n)) { BBREAK(b); return (-1); } if ((q = buf_get(b, n + 1)) == 0) return (-1); memcpy(q, p, n);