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:
parent
c4ca059dee
commit
96944c8f65
|
@ -82,9 +82,10 @@ grow_to_fit(struct blob *blob, size_t additional)
|
||||||
*
|
*
|
||||||
* \return True unless allocation fails
|
* \return True unless allocation fails
|
||||||
*/
|
*/
|
||||||
static bool
|
bool
|
||||||
align_blob(struct blob *blob, size_t alignment)
|
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);
|
const size_t new_size = align64(blob->size, alignment);
|
||||||
|
|
||||||
if (blob->size < new_size) {
|
if (blob->size < new_size) {
|
||||||
|
@ -99,9 +100,10 @@ align_blob(struct blob *blob, size_t alignment)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
align_blob_reader(struct blob_reader *blob, size_t alignment)
|
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);
|
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
|
intptr_t
|
||||||
blob_reserve_uint32(struct blob *blob)
|
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));
|
return blob_reserve_bytes(blob, sizeof(uint32_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
intptr_t
|
intptr_t
|
||||||
blob_reserve_intptr(struct blob *blob)
|
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));
|
return blob_reserve_bytes(blob, sizeof(intptr_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +203,7 @@ blob_reserve_intptr(struct blob *blob)
|
||||||
bool \
|
bool \
|
||||||
name(struct blob *blob, type value) \
|
name(struct blob *blob, type value) \
|
||||||
{ \
|
{ \
|
||||||
align_blob(blob, sizeof(value)); \
|
blob_align(blob, sizeof(value)); \
|
||||||
return blob_write_bytes(blob, &value, sizeof(value)); \
|
return blob_write_bytes(blob, &value, sizeof(value)); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,7 +321,7 @@ name(struct blob_reader *blob) \
|
||||||
{ \
|
{ \
|
||||||
type ret; \
|
type ret; \
|
||||||
int size = sizeof(ret); \
|
int size = sizeof(ret); \
|
||||||
align_blob_reader(blob, size); \
|
blob_reader_align(blob, size); \
|
||||||
if (! ensure_can_read(blob, size)) \
|
if (! ensure_can_read(blob, size)) \
|
||||||
return 0; \
|
return 0; \
|
||||||
ret = *((type*) blob->current); \
|
ret = *((type*) blob->current); \
|
||||||
|
|
|
@ -123,6 +123,16 @@ blob_finish(struct blob *blob)
|
||||||
void
|
void
|
||||||
blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size);
|
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.
|
* Add some unstructured, fixed-size data to a blob.
|
||||||
*
|
*
|
||||||
|
@ -316,6 +326,17 @@ blob_write_string(struct blob *blob, const char *str);
|
||||||
void
|
void
|
||||||
blob_reader_init(struct blob_reader *blob, const void *data, size_t size);
|
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
|
* Read some unstructured, fixed-size data from the current location, (and
|
||||||
* update the current location to just past this data).
|
* update the current location to just past this data).
|
||||||
|
|
Loading…
Reference in New Issue