Index: runtime/vm/json_stream.cc |
diff --git a/runtime/vm/json_stream.cc b/runtime/vm/json_stream.cc |
index d95345b4953c184401adf3e477710531c5b33f80..459d077dc622751639f6966a6d4eb389c2a752d8 100644 |
--- a/runtime/vm/json_stream.cc |
+++ b/runtime/vm/json_stream.cc |
@@ -23,6 +23,28 @@ namespace dart { |
#ifndef PRODUCT |
+class MaybeOnStackBuffer { |
+ public: |
+ explicit MaybeOnStackBuffer(intptr_t size) { |
+ if (size > kOnStackBufferCapacity) { |
+ p_ = reinterpret_cast<char*>(malloc(size)); |
+ } else { |
+ p_ = &buffer_[0]; |
+ } |
+ } |
+ ~MaybeOnStackBuffer() { |
+ if (p_ != &buffer_[0]) free(p_); |
+ } |
+ |
+ char* p() { return p_; } |
+ |
+ private: |
+ static const intptr_t kOnStackBufferCapacity = 4096; |
+ char* p_; |
+ char buffer_[kOnStackBufferCapacity]; |
+}; |
+ |
+ |
void AppendJSONStreamConsumer(Dart_StreamConsumer_State state, |
const char* stream_name, |
const uint8_t* buffer, |
@@ -496,15 +518,15 @@ void JSONStream::PrintfValue(const char* format, ...) { |
va_start(args, format); |
intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
va_end(args); |
- char* p = reinterpret_cast<char*>(malloc(len + 1)); |
+ MaybeOnStackBuffer mosb(len + 1); |
+ char* p = mosb.p(); |
va_start(args, format); |
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); |
va_end(args); |
ASSERT(len == len2); |
buffer_.AddChar('"'); |
- AddEscapedUTF8String(p); |
+ AddEscapedUTF8String(p, len); |
buffer_.AddChar('"'); |
- free(p); |
} |
@@ -717,15 +739,15 @@ void JSONStream::PrintfProperty(const char* name, const char* format, ...) { |
va_start(args, format); |
intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
va_end(args); |
- char* p = reinterpret_cast<char*>(malloc(len + 1)); |
+ MaybeOnStackBuffer mosb(len + 1); |
+ char* p = mosb.p(); |
va_start(args, format); |
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); |
va_end(args); |
ASSERT(len == len2); |
buffer_.AddChar('"'); |
- AddEscapedUTF8String(p); |
+ AddEscapedUTF8String(p, len); |
buffer_.AddChar('"'); |
- free(p); |
} |
@@ -920,15 +942,15 @@ void JSONObject::AddFixedServiceId(const char* format, ...) const { |
va_start(args, format); |
intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
va_end(args); |
- char* p = reinterpret_cast<char*>(malloc(len + 1)); |
+ MaybeOnStackBuffer mosb(len + 1); |
+ char* p = mosb.p(); |
va_start(args, format); |
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); |
va_end(args); |
ASSERT(len == len2); |
stream_->buffer_.AddChar('"'); |
- stream_->AddEscapedUTF8String(p); |
+ stream_->AddEscapedUTF8String(p, len); |
stream_->buffer_.AddChar('"'); |
- free(p); |
} |
@@ -994,15 +1016,15 @@ void JSONObject::AddPropertyF(const char* name, const char* format, ...) const { |
va_start(args, format); |
intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
va_end(args); |
- char* p = reinterpret_cast<char*>(malloc(len + 1)); |
+ MaybeOnStackBuffer mosb(len + 1); |
+ char* p = mosb.p(); |
va_start(args, format); |
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); |
va_end(args); |
ASSERT(len == len2); |
stream_->buffer_.AddChar('"'); |
- stream_->AddEscapedUTF8String(p); |
+ stream_->AddEscapedUTF8String(p, len); |
stream_->buffer_.AddChar('"'); |
- free(p); |
} |
@@ -1012,15 +1034,15 @@ void JSONArray::AddValueF(const char* format, ...) const { |
va_start(args, format); |
intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
va_end(args); |
- char* p = reinterpret_cast<char*>(malloc(len + 1)); |
+ MaybeOnStackBuffer mosb(len + 1); |
+ char* p = mosb.p(); |
va_start(args, format); |
intptr_t len2 = OS::VSNPrint(p, len + 1, format, args); |
va_end(args); |
ASSERT(len == len2); |
stream_->buffer_.AddChar('"'); |
- stream_->AddEscapedUTF8String(p); |
+ stream_->AddEscapedUTF8String(p, len); |
stream_->buffer_.AddChar('"'); |
- free(p); |
} |
#endif // !PRODUCT |