Mesa (master): glsl: Add blob_overwrite_bytes and blob_overwrite_uint32

Carl Worth cworth at kemper.freedesktop.org
Fri Jan 16 21:58:39 UTC 2015


Module: Mesa
Branch: master
Commit: ffcad3a54839bd6704b4cac5dfe9f52a4f299dae
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=ffcad3a54839bd6704b4cac5dfe9f52a4f299dae

Author: Tapani Pälli <tapani.palli at intel.com>
Date:   Wed Nov 12 23:16:51 2014 -0800

glsl: Add blob_overwrite_bytes and blob_overwrite_uint32

These functions are useful when serializing an unknown number of items
to a blob. The caller can first save the current offset, write a
placeholder uint32, write out (and count) the items, then use
blob_overwrite_uint32 with the saved offset to replace the placeholder
value.

Then, when deserializing, the reader will first read the count and
know how many subsequent items to expect.

(I wrote this code after reading a very similar patch written by
Tapani when he wrote serialization code for IR. Since I re-used the
idea of his code so directly, I've credited him as the author of this
code. --Carl)

Reviewed-by: Jason Ekstrand <jason.ekstrand at intel.com>

---

 src/glsl/blob.c |   23 +++++++++++++++++++++++
 src/glsl/blob.h |   43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/src/glsl/blob.c b/src/glsl/blob.c
index a4003cf..dd4341b 100644
--- a/src/glsl/blob.c
+++ b/src/glsl/blob.c
@@ -101,6 +101,21 @@ blob_create(void *mem_ctx)
 }
 
 bool
+blob_overwrite_bytes(struct blob *blob,
+                     size_t offset,
+                     const void *bytes,
+                     size_t to_write)
+{
+   /* Detect an attempt to overwrite data out of bounds. */
+   if (offset < 0 || blob->size - offset < to_write)
+      return false;
+
+   memcpy(blob->data + offset, bytes, to_write);
+
+   return true;
+}
+
+bool
 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
 {
    if (! grow_to_fit(blob, to_write))
@@ -135,6 +150,14 @@ blob_write_uint32(struct blob *blob, uint32_t value)
 }
 
 bool
+blob_overwrite_uint32 (struct blob *blob,
+                       size_t offset,
+                       uint32_t value)
+{
+   return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
+}
+
+bool
 blob_write_uint64(struct blob *blob, uint64_t value)
 {
    align_blob(blob, sizeof(value));
diff --git a/src/glsl/blob.h b/src/glsl/blob.h
index af32d22..ec903ec 100644
--- a/src/glsl/blob.h
+++ b/src/glsl/blob.h
@@ -108,6 +108,24 @@ uint8_t *
 blob_reserve_bytes (struct blob *blob, size_t to_write);
 
 /**
+ * Overwrite some data previously written to the blob.
+ *
+ * Writes data to an existing portion of the blob at an offset of \offset.
+ * This data range must have previously been written to the blob by one of the
+ * blob_write_* calls.
+ *
+ * For example usage, see blob_overwrite_uint32
+ *
+ * \return True unless the requested offset or offset+to_write lie outside
+ * the current blob's size.
+ */
+bool
+blob_overwrite_bytes (struct blob *blob,
+                      size_t offset,
+                      const void *bytes,
+                      size_t to_write);
+
+/**
  * Add a uint32_t to a blob.
  *
  * \note This function will only write to a uint32_t-aligned offset from the
@@ -121,6 +139,31 @@ bool
 blob_write_uint32 (struct blob *blob, uint32_t value);
 
 /**
+ * Overwrite a uint32_t previously written to the blob.
+ *
+ * Writes a uint32_t value to an existing portion of the blob at an offset of
+ * \offset.  This data range must have previously been written to the blob by
+ * one of the blob_write_* calls.
+ *
+ *
+ * The expected usage is something like the following pattern:
+ *
+ *	size_t offset;
+ *
+ *	offset = blob->size;
+ *	blob_write_uint32 (blob, 0); // placeholder
+ *	... various blob write calls, writing N items ...
+ *	blob_overwrite_uint32 (blob, offset, N);
+ *
+ * \return True unless the requested position or position+to_write lie outside
+ * the current blob's size.
+ */
+bool
+blob_overwrite_uint32 (struct blob *blob,
+                       size_t offset,
+                       uint32_t value);
+
+/**
  * Add a uint64_t to a blob.
  *
  * \note This function will only write to a uint64_t-aligned offset from the




More information about the mesa-commit mailing list