diff --git a/src/util.cc b/src/util.cc index 01ea76a8f89..1c57a976e11 100644 --- a/src/util.cc +++ b/src/util.cc @@ -16,15 +16,20 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle value) // Allocate enough space to include the null terminator size_t len = StringBytes::StorageSize(val_, UTF8) + 1; - char* str = static_cast(calloc(1, len)); + char* str; + if (len > kStorageSize) + str = static_cast(malloc(len)); + else + str = str_st_; + CHECK_NE(str, NULL); int flags = WRITE_UTF8_FLAGS; - flags |= ~v8::String::NO_NULL_TERMINATION; length_ = val_->WriteUtf8(str, len, 0, flags); + str[length_] = '\0'; str_ = reinterpret_cast(str); } diff --git a/src/util.h b/src/util.h index 80eb2b1ad13..e0fa0fd0096 100644 --- a/src/util.h +++ b/src/util.h @@ -110,7 +110,8 @@ class Utf8Value { explicit Utf8Value(v8::Isolate* isolate, v8::Handle value); ~Utf8Value() { - free(str_); + if (str_ != str_st_) + free(str_); } char* operator*() { @@ -126,7 +127,9 @@ class Utf8Value { }; private: + static const int kStorageSize = 1024; size_t length_; + char str_st_[kStorageSize]; char* str_; };