From 96944c8f6522afa9598456f1cb2dcae058e5a9f8 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Mon, 4 Oct 2021 11:58:33 -0500 Subject: [PATCH] util/blob: Add align helpers Reviewed-By: Mike Blumenkrantz Reviewed-by: Connor Abbott Part-of: --- src/util/blob.c | 18 ++++++++++-------- src/util/blob.h | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/util/blob.c b/src/util/blob.c index db192146ac1..67f6bb2567c 100644 --- a/src/util/blob.c +++ b/src/util/blob.c @@ -82,9 +82,10 @@ grow_to_fit(struct blob *blob, size_t additional) * * \return True unless allocation fails */ -static bool -align_blob(struct blob *blob, size_t alignment) +bool +blob_align(struct blob *blob, size_t alignment) { + assert(align64((uintptr_t)blob->data, alignment) == (uintptr_t)blob->data); const size_t new_size = align64(blob->size, alignment); if (blob->size < new_size) { @@ -99,9 +100,10 @@ align_blob(struct blob *blob, size_t alignment) return true; } -static void -align_blob_reader(struct blob_reader *blob, size_t alignment) +void +blob_reader_align(struct blob_reader *blob, size_t alignment) { + assert(align64((uintptr_t)blob->data, alignment) == (uintptr_t)blob->data); blob->current = blob->data + align64(blob->current - blob->data, alignment); } @@ -186,14 +188,14 @@ blob_reserve_bytes(struct blob *blob, size_t to_write) intptr_t blob_reserve_uint32(struct blob *blob) { - align_blob(blob, sizeof(uint32_t)); + blob_align(blob, sizeof(uint32_t)); return blob_reserve_bytes(blob, sizeof(uint32_t)); } intptr_t blob_reserve_intptr(struct blob *blob) { - align_blob(blob, sizeof(intptr_t)); + blob_align(blob, sizeof(intptr_t)); return blob_reserve_bytes(blob, sizeof(intptr_t)); } @@ -201,7 +203,7 @@ blob_reserve_intptr(struct blob *blob) bool \ name(struct blob *blob, type value) \ { \ - align_blob(blob, sizeof(value)); \ + blob_align(blob, sizeof(value)); \ return blob_write_bytes(blob, &value, sizeof(value)); \ } @@ -319,7 +321,7 @@ name(struct blob_reader *blob) \ { \ type ret; \ int size = sizeof(ret); \ - align_blob_reader(blob, size); \ + blob_reader_align(blob, size); \ if (! ensure_can_read(blob, size)) \ return 0; \ ret = *((type*) blob->current); \ diff --git a/src/util/blob.h b/src/util/blob.h index 051eaa01674..91b05901534 100644 --- a/src/util/blob.h +++ b/src/util/blob.h @@ -123,6 +123,16 @@ blob_finish(struct blob *blob) void blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size); +/** + * Aligns the blob to the given alignment. + * + * \see blob_reader_align + * + * \return True unless allocation fails + */ +bool +blob_align(struct blob *blob, size_t alignment); + /** * Add some unstructured, fixed-size data to a blob. * @@ -316,6 +326,17 @@ blob_write_string(struct blob *blob, const char *str); void blob_reader_init(struct blob_reader *blob, const void *data, size_t size); +/** + * Align the current offset of the blob reader to the given alignment. + * + * This may be useful if you need the result of blob_read_bytes to have a + * particular alignment. Note that this only aligns relative to blob->data + * and the alignment of the resulting pointer is only guaranteed if blob->data + * is also aligned to the requested alignment. + */ +void +blob_reader_align(struct blob_reader *blob, size_t alignment); + /** * Read some unstructured, fixed-size data from the current location, (and * update the current location to just past this data).