summaryrefslogtreecommitdiff
path: root/upb/upb.c
diff options
context:
space:
mode:
authorJosh Haberman <jhaberman@gmail.com>2013-10-24 12:43:19 -0700
committerJosh Haberman <jhaberman@gmail.com>2013-10-24 12:43:19 -0700
commit26d98ca94f2f049e8767b4a9a33d185a3d7ea0fd (patch)
tree340bcf495f06ed05c9f3fb423f210caf4edce2b1 /upb/upb.c
parent61109fca1f967771c21dc7184aee35f3b439c577 (diff)
Merge from Google-internal development:
- rewritten decoder; interpreted decoder is bytecode-based, JIT decoder no longer falls back to the interpreter. - C++ improvements: C++11-compatible iterators, upb::reffed_ptr for RAII refcounting, better upcast/downcast support. - removed the gross upb_value abstraction from public upb.h.
Diffstat (limited to 'upb/upb.c')
-rw-r--r--upb/upb.c59
1 files changed, 30 insertions, 29 deletions
diff --git a/upb/upb.c b/upb/upb.c
index 226fc78..881615e 100644
--- a/upb/upb.c
+++ b/upb/upb.c
@@ -8,16 +8,41 @@
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "upb/upb.h"
-#ifdef NDEBUG
-upb_value UPB_NO_VALUE = {{0}};
-#else
-upb_value UPB_NO_VALUE = {{0}, -1};
-#endif
+// Like vasprintf (which allocates a string large enough for the result), but
+// uses *buf (which can be NULL) as a starting point and reallocates it only if
+// the new value will not fit. "size" is updated to reflect the allocated size
+// of the buffer. Starts writing at the given offset into the string; bytes
+// preceding this offset are unaffected. Returns the new length of the string,
+// or -1 on memory allocation failure.
+static int upb_vrprintf(char **buf, size_t *size, size_t ofs,
+ const char *fmt, va_list args) {
+ // Try once without reallocating. We have to va_copy because we might have
+ // to call vsnprintf again.
+ uint32_t len = *size - ofs;
+ va_list args_copy;
+ va_copy(args_copy, args);
+ uint32_t true_len = vsnprintf(*buf + ofs, len, fmt, args_copy);
+ va_end(args_copy);
+
+ // Resize to be the correct size.
+ if (true_len >= len) {
+ // Need to print again, because some characters were truncated. vsnprintf
+ // will not write the entire string unless you give it space to store the
+ // NULL terminator also.
+ *size = (ofs + true_len + 1);
+ char *newbuf = realloc(*buf, *size);
+ if (!newbuf) return -1;
+ vsnprintf(newbuf + ofs, true_len + 1, fmt, args);
+ *buf = newbuf;
+ }
+ return true_len;
+}
void upb_status_init(upb_status *status) {
status->buf = NULL;
@@ -104,27 +129,3 @@ void upb_status_seteof(upb_status *status) {
if (!status) return;
status->eof_ = true;
}
-
-int upb_vrprintf(char **buf, size_t *size, size_t ofs,
- const char *fmt, va_list args) {
- // Try once without reallocating. We have to va_copy because we might have
- // to call vsnprintf again.
- uint32_t len = *size - ofs;
- va_list args_copy;
- va_copy(args_copy, args);
- uint32_t true_len = vsnprintf(*buf + ofs, len, fmt, args_copy);
- va_end(args_copy);
-
- // Resize to be the correct size.
- if (true_len >= len) {
- // Need to print again, because some characters were truncated. vsnprintf
- // will not write the entire string unless you give it space to store the
- // NULL terminator also.
- *size = (ofs + true_len + 1);
- char *newbuf = realloc(*buf, *size);
- if (!newbuf) return -1;
- vsnprintf(newbuf + ofs, true_len + 1, fmt, args);
- *buf = newbuf;
- }
- return true_len;
-}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback