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>
This commit is contained in:
Jason Ekstrand 2021-10-04 11:58:33 -05:00 committed by Marge Bot
parent c4ca059dee
commit 96944c8f65
2 changed files with 31 additions and 8 deletions

View File

@ -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); \

View File

@ -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).