X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/beee81ec8fc4857080e809c759575c4267f7bd1f..e82eb4b16c632551930ca8bfb2b4e2e58c1ee16a:/struct/buf.c?ds=inline diff --git a/struct/buf.c b/struct/buf.c index 4b02b28..81c996f 100644 --- a/struct/buf.c +++ b/struct/buf.c @@ -223,6 +223,84 @@ int buf_fill(buf *b, int ch, size_t sz) 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_align@ --- * + * + * Arguments: @buf *b@ or @dbuf *db@ = pointer to a buffer block + * @size_t m, a@ = alignment multiple and offset + * @size_t *sz_out@ = where to put the length + * + * Returns: Pointer to previous buffer position, or null on error. + * + * Use: Advance the buffer position as little as possible such that + * it is @a@ greater than a multiple of @m@, returning the + * (possibly empty) portion of the buffer passed over. + */ + +void *buf_align(buf *b, size_t m, size_t a, size_t *sz_out) +{ + size_t sz; + + if (BBAD(b)) return (0); + sz = align_step(b, m, a); *sz_out = sz; + return (buf_get(b, sz)); +} +void *(dbuf_align)(dbuf *db, size_t m, size_t a, size_t *sz_out) + { return (dbuf_align(db, m, a, sz_out)); } + +/* --- @{,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