<div dir="ltr">On 14 January 2014 02:35, Tapani Pälli <span dir="ltr"><<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><div><div class="h5">
<div>On 01/13/2014 08:27 PM, Paul Berry
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">On 2 January 2014 03:58, Tapani Pälli <span dir="ltr"><<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>></span>
wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Class will be used by
the shader binary cache implementation.<br>
<br>
Signed-off-by: Tapani Pälli <<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>><br>
---<br>
src/glsl/memory_writer.h | 147
+++++++++++++++++++++++++++++++++++++++++++++++<br>
1 file changed, 147 insertions(+)<br>
create mode 100644 src/glsl/memory_writer.h<br>
<br>
diff --git a/src/glsl/memory_writer.h
b/src/glsl/memory_writer.h<br>
new file mode 100644<br>
index 0000000..a6c6b55<br>
--- /dev/null<br>
+++ b/src/glsl/memory_writer.h<br>
@@ -0,0 +1,147 @@<br>
+/* -*- c++ -*- */<br>
+/*<br>
+ * Copyright © 2013 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any
person obtaining a<br>
+ * copy of this software and associated documentation
files (the "Software"),<br>
+ * to deal in the Software without restriction, including
without limitation<br>
+ * the rights to use, copy, modify, merge, publish,
distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit
persons to whom the<br>
+ * Software is furnished to do so, subject to the
following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice
(including the next<br>
+ * paragraph) shall be included in all copies or
substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER<br>
+ * DEALINGS IN THE SOFTWARE.<br>
+ */<br>
+<br>
+#pragma once<br>
+#ifndef MEMORY_WRITER_H<br>
+#define MEMORY_WRITER_H<br>
+<br>
+#include <stdlib.h><br>
+#include <unistd.h><br>
+#include <string.h><br>
+<br>
+#ifdef __cplusplus<br>
+/**<br>
+ * Helper class for writing data to memory<br>
+ *<br>
+ * This class maintains a dynamically-sized memory buffer
and allows<br>
+ * for data to be efficiently appended to it with
automatic resizing.<br>
+ */<br>
+class memory_writer<br>
+{<br>
+public:<br>
+ memory_writer() :<br>
+ memory(NULL),<br>
+ curr_size(0),<br>
+ pos(0) {}<br>
+<br>
+ ~memory_writer()<br>
+ {<br>
+ free(memory);<br>
+ }<br>
+<br>
+ /* user wants to claim the memory */<br>
+ char *release_memory(size_t *size)<br>
+ {<br>
+ /* final realloc to free allocated but unused
memory */<br>
+ char *result = (char *) realloc(memory, pos);<br>
+ *size = pos;<br>
+ memory = NULL;<br>
+ curr_size = 0;<br>
+ pos = 0;<br>
+ return result;<br>
+ }<br>
+<br>
+/**<br>
+ * write functions per type<br>
+ */<br>
+#define DECL_WRITER(type) int write_ ##type (const type
data) {\<br>
+ return write(&data, sizeof(type));\<br>
+}<br>
+<br>
+ DECL_WRITER(int32_t);<br>
+ DECL_WRITER(int64_t);<br>
+ DECL_WRITER(uint8_t);<br>
+ DECL_WRITER(uint32_t);<br>
+<br>
+ int write_bool(bool data)<br>
+ {<br>
+ uint8_t val = data;<br>
+ return write_uint8_t(val);<br>
+ }<br>
+<br>
+ /* write function that reallocates more memory if
required */<br>
+ int write(const void *data, int32_t size)<br>
+ {<br>
+ if (!memory || pos > (int32_t)(curr_size -
size))<br>
+ if (grow(size))<br>
+ return -1;<br>
+<br>
+ memcpy(memory + pos, data, size);<br>
+<br>
+ pos += size;<br>
+ return 0;<br>
+ }<br>
+<br>
+ int overwrite(const void *data, int32_t size, int32_t
offset)<br>
+ {<br>
+ if (offset < 0 || offset + size > pos)<br>
+ return -1;<br>
+ memcpy(memory + offset, data, size);<br>
+ return 0;<br>
+ }<br>
+<br>
+ int write_string(const char *str)<br>
+ {<br>
+ if (!str)<br>
+ return -1;<br>
+ char terminator = '\0';<br>
+ write(str, strlen(str));<br>
+ write(&terminator, 1);<br>
</blockquote>
<div><br>
</div>
<div>C strings include a terminator, so there's no reason to
write out the string contents and the terminator
separtely. You can just do:<br>
<br>
</div>
<div>write(str, strlen(str) + 1);<br>
<br>
Also, don't forget to propagate the return code to the
caller:<br>
<br>
</div>
<div>return write(str, strlen(str) + 1);<br>
</div>
</div>
</div>
</div>
</blockquote>
<br></div></div>
Ah right, will do. It could be that this needs to be modified back
to how it was though (to write to length of the string too). I think
it is otherwise impossible for the reader side to know if terminator
really exists and it might continue to read forever (or call strlen
for unterminated string).</div></blockquote><div><br></div><div>I don't know of any C library function that does exactly what you want. Of course you could always code up your own loop. But I think I agree that storing the length first is the best approach.<br>
</div></div></div></div>