summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Ekstrand <jason@jlekstrand.net>2021-10-04 11:58:33 -0500
committerMarge Bot <emma+marge@anholt.net>2022-04-22 19:38:52 +0000
commit96944c8f6522afa9598456f1cb2dcae058e5a9f8 (patch)
treecb6e0ce44340b275477e7554b7d661c73d917881
parentc4ca059dee4c902c1a736b3f77bd938679411c0d (diff)
util/blob: Add align helpers
Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz@gmail.com> Reviewed-by: Connor Abbott <cwabbott0@gmail.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13184>
-rw-r--r--src/util/blob.c18
-rw-r--r--src/util/blob.h21
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
@@ -124,6 +124,16 @@ 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.
*
* \return True unless allocation failed.
@@ -317,6 +327,17 @@ 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).
*