[Mesa-dev] [PATCH 3/8] compiler/blob: Rework blob_reserve_bytes
Connor Abbott
cwabbott0 at gmail.com
Fri Sep 22 14:18:29 UTC 2017
I like the version I wrote a little better. The main difference is
that instead of the alignment parameter, I added a
blob_reserve_intptr() function to match blob_read_intptr() and
blob_write_intptr(). I also updated the doxygen comment for the
function, and had a little more descriptive commit message:
https://cgit.freedesktop.org/~cwabbott0/mesa/commit/?h=nir-serialize&id=f2e1e0df8d0d24afc072bcd47d86338436b3fdc2
On Fri, Sep 15, 2017 at 9:39 PM, Jason Ekstrand <jason at jlekstrand.net> wrote:
> This commit primarily makes two changes. First, blob_reserve_bytes now
> takes an alignment parameter. Second, we now return an offset into the
> blob instead of a pointer and leave the pointer arithmetic to the
> caller. This way you can call blob_reserve_bytes, emit a bunch more
> stuff, and then come back and fill in the reserved data.
> ---
> src/compiler/blob.c | 14 ++++++++------
> src/compiler/blob.h | 8 ++++----
> src/compiler/glsl/tests/blob_test.c | 7 ++++---
> 3 files changed, 16 insertions(+), 13 deletions(-)
>
> diff --git a/src/compiler/blob.c b/src/compiler/blob.c
> index 3c4aed8..7324a6b 100644
> --- a/src/compiler/blob.c
> +++ b/src/compiler/blob.c
> @@ -139,18 +139,20 @@ blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
> return true;
> }
>
> -uint8_t *
> -blob_reserve_bytes(struct blob *blob, size_t to_write)
> +size_t
> +blob_reserve_bytes(struct blob *blob, size_t to_write, size_t align)
> {
> - uint8_t *ret;
> + size_t offset;
> +
> + align_blob(blob, align);
>
> if (! grow_to_fit (blob, to_write))
> - return NULL;
> + return (size_t)-1;
>
> - ret = blob->data + blob->size;
> + offset = blob->size;
> blob->size += to_write;
>
> - return ret;
> + return offset;
> }
>
> bool
> diff --git a/src/compiler/blob.h b/src/compiler/blob.h
> index 940c81e..e1f7fe7 100644
> --- a/src/compiler/blob.h
> +++ b/src/compiler/blob.h
> @@ -113,11 +113,11 @@ blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
> * that is not aware of the blob API, (so that blob_write_bytes cannot be
> * called).
> *
> - * \return A pointer to space allocated within \blob to which \to_write bytes
> - * can be written, (or NULL in case of any allocation error).
> + * \return The offset into \blob at which \to_write bytes can be written, (or
> + * NULL in case of any allocation error).
> */
> -uint8_t *
> -blob_reserve_bytes(struct blob *blob, size_t to_write);
> +size_t
> +blob_reserve_bytes(struct blob *blob, size_t to_write, size_t align);
>
> /**
> * Overwrite some data previously written to the blob.
> diff --git a/src/compiler/glsl/tests/blob_test.c b/src/compiler/glsl/tests/blob_test.c
> index df0e91a..e82ceb1 100644
> --- a/src/compiler/glsl/tests/blob_test.c
> +++ b/src/compiler/glsl/tests/blob_test.c
> @@ -120,7 +120,7 @@ test_write_and_read_functions (void)
> {
> struct blob *blob;
> struct blob_reader reader;
> - uint8_t *reserved;
> + size_t reserved_offset;
> size_t str_offset, uint_offset;
> uint8_t reserve_buf[sizeof(reserve_test_str)];
>
> @@ -130,8 +130,9 @@ test_write_and_read_functions (void)
>
> blob_write_bytes(blob, bytes_test_str, sizeof(bytes_test_str));
>
> - reserved = blob_reserve_bytes(blob, sizeof(reserve_test_str));
> - memcpy(reserved, reserve_test_str, sizeof(reserve_test_str));
> + reserved_offset = blob_reserve_bytes(blob, sizeof(reserve_test_str), 1);
> + memcpy(blob->data + reserved_offset,
> + reserve_test_str, sizeof(reserve_test_str));
>
> /* Write a placeholder, (to be replaced later via overwrite_bytes) */
> str_offset = blob->size;
> --
> 2.5.0.400.gff86faf
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
More information about the mesa-dev
mailing list