Index: runtime/vm/os.h |
diff --git a/runtime/vm/os.h b/runtime/vm/os.h |
index 60456e4200add3ae15ecf75667a1e2ab1d291b5a..e26f565e46ce79274aa81ff34527176fd011371e 100644 |
--- a/runtime/vm/os.h |
+++ b/runtime/vm/os.h |
@@ -7,6 +7,37 @@ |
#include "vm/globals.h" |
+// SNPRINT is a macro wrapping OS::SNPrint which makes two calls. The first call |
+// is with NULL for the buffer. This call returns the number of bytes that would |
+// have been written had the buffer not been NULL. The macro allocates that many |
+// bytes plus room for the null terminator, and makes a second call to |
+// OS::SNPrint, passing the allocated buffer. |
+// |
+// The reason for wrapping OS::SNPrint in a macro is so that the format string |
+// literal can be passed directly to both calls without doing a copy-paste. |
+// The format string literal must be passed directly to both calls to make sure |
+// that we get compiler errors when the format string does not match the passed |
+// arguments. |
+#if defined(_MSC_VER_) |
+#define SNPRINT(buffer, allocator, format, ...) \ |
+ { \ |
+ intptr_t len = OS::SNPrint(NULL, 0, (format), __VA_ARGS__); \ |
+ (buffer) = reinterpret_cast<char*>((allocator)(len + 1)); \ |
+ ASSERT(buffer != NULL); \ |
+ OS::SNPrint((buffer), len + 1, (format), __VA_ARGS__); \ |
+ } \ |
+ |
+#else |
+#define SNPRINT(buffer, allocator, format, ...) \ |
+ { \ |
+ intptr_t len = OS::SNPrint(NULL, 0, (format), ##__VA_ARGS__); \ |
+ (buffer) = reinterpret_cast<char*>((allocator)(len + 1)); \ |
+ ASSERT(buffer != NULL); \ |
+ OS::SNPrint((buffer), len + 1, (format), ##__VA_ARGS__); \ |
+ } \ |
+ |
+#endif |
+ |
// Forward declarations. |
struct tm; |