chiark / gitweb /
@@@ tty mess
[mLib] / struct / buf.c
index 10e05444b9b84051302b0ff719305063d6d69278..81c996f45465081f50ca743ff666519be435ff37 100644 (file)
@@ -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