diff options
author | Tapani Pälli <[email protected]> | 2014-11-12 23:16:51 -0800 |
---|---|---|
committer | Carl Worth <[email protected]> | 2015-01-16 13:47:40 -0800 |
commit | ffcad3a54839bd6704b4cac5dfe9f52a4f299dae (patch) | |
tree | adaa807fc49b62bb3a8cf2f0889733351accc031 /src/glsl/blob.c | |
parent | 1c9877327ead37b7dd5a0ca3a8c7912e924328e5 (diff) |
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 <[email protected]>
Diffstat (limited to 'src/glsl/blob.c')
-rw-r--r-- | src/glsl/blob.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/glsl/blob.c b/src/glsl/blob.c index a4003cf7178..dd4341be961 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)); |