<p dir="ltr"><br>
On Dec 11, 2014 11:55 AM, "Carl Worth" <<a href="mailto:cworth@cworth.org">cworth@cworth.org</a>> wrote:<br>
><br>
> From: Tapani Pälli <<a href="mailto:tapani.palli@intel.com">tapani.palli@intel.com</a>><br>
><br>
> These functions are useful when serializing an unknown number of items<br>
> to a blob. The caller can first save the current offset, write a<br>
> placeholder uint32, write out (and count) the items, then use<br>
> blob_overwrite_uint32 with the saved offset to replace the placeholder<br>
> value.<br>
><br>
> Then, when deserializing, the reader will first read the count and<br>
> know how many subsequent items to expect.<br>
><br>
> (I wrote this code after reading a very similar patch written by<br>
> Tapani when he wrote serialization code for IR. Since I re-used the<br>
> idea of his code so directly, I've credited him as the author of this<br>
> code. --Carl)<br>
> ---<br>
>  src/glsl/blob.c | 23 +++++++++++++++++++++++<br>
>  src/glsl/blob.h | 25 +++++++++++++++++++++++++<br>
>  2 files changed, 48 insertions(+)<br>
><br>
> diff --git a/src/glsl/blob.c b/src/glsl/blob.c<br>
> index 0af71fc..dbd698a 100644<br>
> --- a/src/glsl/blob.c<br>
> +++ b/src/glsl/blob.c<br>
> @@ -101,6 +101,21 @@ blob_create (void *mem_ctx)<br>
>  }<br>
><br>
>  bool<br>
> +blob_overwrite_bytes (struct blob *blob,<br>
> +                      size_t offset,<br>
> +                      const void *bytes,<br>
> +                      size_t to_write)<br>
> +{<br>
> +   /* Detect an attempt to overwrite data out of bounds. */<br>
> +   if (offset < 0 || blob->size - offset < to_write)<br>
> +      return false;<br>
> +<br>
> +   memcpy (blob->data + offset, bytes, to_write);<br>
> +<br>
> +   return true;<br>
> +}<br>
> +<br>
> +bool<br>
>  blob_write_bytes (struct blob *blob, const void *bytes, size_t to_write)<br>
>  {<br>
>     if (! grow_to_fit (blob, to_write))<br>
> @@ -135,6 +150,14 @@ blob_write_uint32 (struct blob *blob, uint32_t value)<br>
>  }<br>
><br>
>  bool<br>
> +blob_overwrite_uint32 (struct blob *blob,<br>
> +                       size_t offset,<br>
> +                       uint32_t value)<br>
> +{<br>
> +   return blob_overwrite_bytes(blob, offset, &value, sizeof(value));<br>
> +}<br>
> +<br>
> +bool<br>
>  blob_write_uint64 (struct blob *blob, uint64_t value)<br>
>  {<br>
>     align_blob(blob, sizeof(value));<br>
> diff --git a/src/glsl/blob.h b/src/glsl/blob.h<br>
> index 374b21e..165de13 100644<br>
> --- a/src/glsl/blob.h<br>
> +++ b/src/glsl/blob.h<br>
> @@ -138,6 +138,31 @@ bool<br>
>  blob_write_uint32 (struct blob *blob, uint32_t value);</p>
<p dir="ltr">The header entry for overwrite_bytes should go in this patch rather than the first one.</p>
<p dir="ltr">><br>
>  /**<br>
> + * Overwrite a uint32_t previously written to the blob.<br>
> + *<br>
> + * Writes a uint32_t value to an existing portion of the blob at an offset of<br>
> + * \offset.  This data range must have previously been written to the blob by<br>
> + * one of the blob_write_* calls.<br>
> + *<br>
> + *<br>
> + * The expected usage is something like the following pattern:<br>
> + *<br>
> + *     size_t offset;<br>
> + *<br>
> + *     offset = blob->size;<br>
> + *     blob_write_uint32 (blob, 0); // placeholder<br>
> + *     ... various blob write calls, writing N items ...<br>
> + *     blob_overwrite_uint32 (blob, offset, N);<br>
> + *<br>
> + * \return True unless the requested position or position+to_write lie outside<br>
> + * the current blob's size.<br>
> + */<br>
> +bool<br>
> +blob_overwrite_uint32 (struct blob *blob,<br>
> +                       size_t offset,<br>
> +                       uint32_t value);<br>
> +<br>
> +/**<br>
>   * Add a uint64_t to a blob.<br>
>   *<br>
>   * Note: This function will only write to a uint64_t-aligned offset from the<br>
> --<br>
> 2.1.1<br>
><br>
> _______________________________________________<br>
> mesa-dev mailing list<br>
> <a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
> <a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</p>