Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1739)

Unified Diff: runtime/vm/os.h

Issue 1331623002: Uses SNPRINT macro where possible. Otherwise uses #define for format. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
« runtime/vm/assembler.cc ('K') | « runtime/vm/object.cc ('k') | runtime/vm/os_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698