X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/7947d831c086f7cf5f60efb2c83a050f271f39d2..e82eb4b16c632551930ca8bfb2b4e2e58c1ee16a:/struct/buf.c?ds=inline diff --git a/struct/buf.c b/struct/buf.c index 10e0544..81c996f 100644 --- a/struct/buf.c +++ b/struct/buf.c @@ -238,6 +238,30 @@ static size_t align_step(buf *b, size_t m, size_t a) 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