From 1e744a3011ca5d8b52a760796e5b561ef13ce8a1 Mon Sep 17 00:00:00 2001 From: "antonm@chromium.org" Date: Wed, 5 May 2010 12:25:58 +0000 Subject: [PATCH] Turn some usages of NewArray with DeleteArray in the same scope into ScopedVector or SmartPointer. That makes it easier to maintain the code---one should care less about releasing the memory as smart pointers would take care of this. Switch to ScopedVector instead Vector for the same semantics in src/builtins.cc Review URL: http://codereview.chromium.org/1737023 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4593 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/api.cc | 7 +++---- src/builtins.cc | 3 +-- src/debug-agent.cc | 6 +++--- src/debug.cc | 9 ++++----- src/flags.cc | 14 +++++--------- src/log.cc | 7 ++----- src/objects.cc | 16 ++++++++-------- src/platform-freebsd.cc | 9 +++------ src/platform-linux.cc | 9 +++------ src/platform-macos.cc | 10 ++++------ src/platform-solaris.cc | 9 +++------ src/platform-win32.cc | 10 ++++------ src/utils.h | 3 +++ 13 files changed, 46 insertions(+), 66 deletions(-) diff --git a/src/api.cc b/src/api.cc index 4709a156b4..0e5db2c7fa 100644 --- a/src/api.cc +++ b/src/api.cc @@ -2185,10 +2185,10 @@ Local v8::Object::ObjectProtoToString() { int postfix_len = i::StrLength(postfix); int buf_len = prefix_len + str_len + postfix_len; - char* buf = i::NewArray(buf_len); + i::ScopedVector buf(buf_len); // Write prefix. - char* ptr = buf; + char* ptr = buf.start(); memcpy(ptr, prefix, prefix_len * v8::internal::kCharSize); ptr += prefix_len; @@ -2200,8 +2200,7 @@ Local v8::Object::ObjectProtoToString() { memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize); // Copy the buffer into a heap-allocated string and return it. - Local result = v8::String::New(buf, buf_len); - i::DeleteArray(buf); + Local result = v8::String::New(buf.start(), buf_len); return result; } } diff --git a/src/builtins.cc b/src/builtins.cc index e6cbd94f8c..4971275792 100644 --- a/src/builtins.cc +++ b/src/builtins.cc @@ -377,7 +377,7 @@ static Object* CallJsBuiltin(const char* name, name); ASSERT(js_builtin->IsJSFunction()); Handle function(Handle::cast(js_builtin)); - Vector argv(Vector::New(args.length() - 1)); + ScopedVector argv(args.length() - 1); int n_args = args.length() - 1; for (int i = 0; i < n_args; i++) { argv[i] = args.at(i + 1).location(); @@ -388,7 +388,6 @@ static Object* CallJsBuiltin(const char* name, n_args, argv.start(), &pending_exception); - argv.Dispose(); if (pending_exception) return Failure::Exception(); return *result; } diff --git a/src/debug-agent.cc b/src/debug-agent.cc index 41151d8076..e2d9304338 100644 --- a/src/debug-agent.cc +++ b/src/debug-agent.cc @@ -181,15 +181,15 @@ void DebuggerAgentSession::Run() { buf.GetNext(); len++; } - int16_t* temp = NewArray(len + 1); + ScopedVector temp(len + 1); buf.Reset(*message, StrLength(*message)); for (int i = 0; i < len; i++) { temp[i] = buf.GetNext(); } // Send the request received to the debugger. - v8::Debug::SendCommand(reinterpret_cast(temp), len); - DeleteArray(temp); + v8::Debug::SendCommand(reinterpret_cast(temp.start()), + len); } } diff --git a/src/debug.cc b/src/debug.cc index 729f0ab304..18c321bda3 100644 --- a/src/debug.cc +++ b/src/debug.cc @@ -52,14 +52,13 @@ namespace internal { #ifdef ENABLE_DEBUGGER_SUPPORT static void PrintLn(v8::Local value) { v8::Local s = value->ToString(); - char* data = NewArray(s->Length() + 1); - if (data == NULL) { + ScopedVector data(s->Length() + 1); + if (data.start() == NULL) { V8::FatalProcessOutOfMemory("PrintLn"); return; } - s->WriteAscii(data); - PrintF("%s\n", data); - DeleteArray(data); + s->WriteAscii(data.start()); + PrintF("%s\n", data.start()); } diff --git a/src/flags.cc b/src/flags.cc index d444c976fb..bbe6bb720e 100644 --- a/src/flags.cc +++ b/src/flags.cc @@ -470,12 +470,12 @@ static char* SkipBlackSpace(char* p) { // static int FlagList::SetFlagsFromString(const char* str, int len) { // make a 0-terminated copy of str - char* copy0 = NewArray(len + 1); - memcpy(copy0, str, len); + ScopedVector copy0(len + 1); + memcpy(copy0.start(), str, len); copy0[len] = '\0'; // strip leading white space - char* copy = SkipWhiteSpace(copy0); + char* copy = SkipWhiteSpace(copy0.start()); // count the number of 'arguments' int argc = 1; // be compatible with SetFlagsFromCommandLine() @@ -485,7 +485,7 @@ int FlagList::SetFlagsFromString(const char* str, int len) { } // allocate argument array - char** argv = NewArray(argc); + ScopedVector argv(argc); // split the flags string into arguments argc = 1; // be compatible with SetFlagsFromCommandLine() @@ -497,11 +497,7 @@ int FlagList::SetFlagsFromString(const char* str, int len) { } // set the flags - int result = SetFlagsFromCommandLine(&argc, argv, false); - - // cleanup - DeleteArray(argv); - DeleteArray(copy0); + int result = SetFlagsFromCommandLine(&argc, argv.start(), false); return result; } diff --git a/src/log.cc b/src/log.cc index e1ebc87c65..891b0e2b82 100644 --- a/src/log.cc +++ b/src/log.cc @@ -1313,9 +1313,8 @@ void Logger::LogCodeObjects() { void Logger::LogCompiledFunctions() { HandleScope scope; const int compiled_funcs_count = EnumerateCompiledFunctions(NULL); - Handle* sfis = - NewArray< Handle >(compiled_funcs_count); - EnumerateCompiledFunctions(sfis); + ScopedVector< Handle > sfis(compiled_funcs_count); + EnumerateCompiledFunctions(sfis.start()); // During iteration, there can be heap allocation due to // GetScriptLineNumber call. @@ -1360,8 +1359,6 @@ void Logger::LogCompiledFunctions() { Logger::LAZY_COMPILE_TAG, shared->code(), *func_name)); } } - - DeleteArray(sfis); } diff --git a/src/objects.cc b/src/objects.cc index 459c8aacab..f4b4367d1c 100644 --- a/src/objects.cc +++ b/src/objects.cc @@ -682,11 +682,11 @@ bool String::MakeExternal(v8::String::ExternalStringResource* resource) { if (FLAG_enable_slow_asserts) { // Assert that the resource and the string are equivalent. ASSERT(static_cast(this->length()) == resource->length()); - SmartPointer smart_chars(NewArray(this->length())); - String::WriteToFlat(this, *smart_chars, 0, this->length()); - ASSERT(memcmp(*smart_chars, + ScopedVector smart_chars(this->length()); + String::WriteToFlat(this, smart_chars.start(), 0, this->length()); + ASSERT(memcmp(smart_chars.start(), resource->data(), - resource->length() * sizeof(**smart_chars)) == 0); + resource->length() * sizeof(smart_chars[0])) == 0); } #endif // DEBUG @@ -728,11 +728,11 @@ bool String::MakeExternal(v8::String::ExternalAsciiStringResource* resource) { if (FLAG_enable_slow_asserts) { // Assert that the resource and the string are equivalent. ASSERT(static_cast(this->length()) == resource->length()); - SmartPointer smart_chars(NewArray(this->length())); - String::WriteToFlat(this, *smart_chars, 0, this->length()); - ASSERT(memcmp(*smart_chars, + ScopedVector smart_chars(this->length()); + String::WriteToFlat(this, smart_chars.start(), 0, this->length()); + ASSERT(memcmp(smart_chars.start(), resource->data(), - resource->length()*sizeof(**smart_chars)) == 0); + resource->length() * sizeof(smart_chars[0])) == 0); } #endif // DEBUG diff --git a/src/platform-freebsd.cc b/src/platform-freebsd.cc index 67e52ce947..b1075cf321 100644 --- a/src/platform-freebsd.cc +++ b/src/platform-freebsd.cc @@ -286,14 +286,12 @@ void OS::LogSharedLibraryAddresses() { int OS::StackWalk(Vector frames) { int frames_size = frames.length(); - void** addresses = NewArray(frames_size); + ScopedVector addresses(frames_size); - int frames_count = backtrace(addresses, frames_size); + int frames_count = backtrace(addresses.start(), frames_size); - char** symbols; - symbols = backtrace_symbols(addresses, frames_count); + char** symbols = backtrace_symbols(addresses, frames_count); if (symbols == NULL) { - DeleteArray(addresses); return kStackWalkError; } @@ -308,7 +306,6 @@ int OS::StackWalk(Vector frames) { frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; } - DeleteArray(addresses); free(symbols); return frames_count; diff --git a/src/platform-linux.cc b/src/platform-linux.cc index b28597d034..fca218fe9e 100644 --- a/src/platform-linux.cc +++ b/src/platform-linux.cc @@ -376,14 +376,12 @@ int OS::StackWalk(Vector frames) { // backtrace is a glibc extension. #ifdef __GLIBC__ int frames_size = frames.length(); - void** addresses = NewArray(frames_size); + ScopedVector addresses(frames_size); - int frames_count = backtrace(addresses, frames_size); + int frames_count = backtrace(addresses.start(), frames_size); - char** symbols; - symbols = backtrace_symbols(addresses, frames_count); + char** symbols = backtrace_symbols(addresses.start(), frames_count); if (symbols == NULL) { - DeleteArray(addresses); return kStackWalkError; } @@ -398,7 +396,6 @@ int OS::StackWalk(Vector frames) { frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; } - DeleteArray(addresses); free(symbols); return frames_count; diff --git a/src/platform-macos.cc b/src/platform-macos.cc index 551690989f..14207b0a6a 100644 --- a/src/platform-macos.cc +++ b/src/platform-macos.cc @@ -283,13 +283,12 @@ int OS::StackWalk(Vector frames) { return 0; int frames_size = frames.length(); - void** addresses = NewArray(frames_size); - int frames_count = backtrace(addresses, frames_size); + ScopedVector addresses(frames_size); - char** symbols; - symbols = backtrace_symbols(addresses, frames_count); + int frames_count = backtrace(addresses.start(), frames_size.start()); + + char** symbols = backtrace_symbols(addresses.start(), frames_count); if (symbols == NULL) { - DeleteArray(addresses); return kStackWalkError; } @@ -305,7 +304,6 @@ int OS::StackWalk(Vector frames) { frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; } - DeleteArray(addresses); free(symbols); return frames_count; diff --git a/src/platform-solaris.cc b/src/platform-solaris.cc index 1fa652d9ee..0d9547b8bd 100644 --- a/src/platform-solaris.cc +++ b/src/platform-solaris.cc @@ -233,14 +233,12 @@ void OS::LogSharedLibraryAddresses() { int OS::StackWalk(Vector frames) { int frames_size = frames.length(); - void** addresses = NewArray(frames_size); + ScopedVector addresses(frames_size); - int frames_count = backtrace(addresses, frames_size); + int frames_count = backtrace(addresses.start(), frames_size); - char** symbols; - symbols = backtrace_symbols(addresses, frames_count); + char** symbols = backtrace_symbols(addresses.start(), frames_count); if (symbols == NULL) { - DeleteArray(addresses); return kStackWalkError; } @@ -255,7 +253,6 @@ int OS::StackWalk(Vector frames) { frames[i].text[kStackWalkMaxTextLen - 1] = '\0'; } - DeleteArray(addresses); free(symbols); return frames_count; diff --git a/src/platform-win32.cc b/src/platform-win32.cc index d03a0a964e..d55cde91f8 100644 --- a/src/platform-win32.cc +++ b/src/platform-win32.cc @@ -1249,16 +1249,16 @@ int OS::StackWalk(Vector frames) { // Try to locate a symbol for this frame. DWORD64 symbol_displacement; - IMAGEHLP_SYMBOL64* symbol = NULL; - symbol = NewArray(kStackWalkMaxNameLen); + SmartPointer symbol( + NewArray(kStackWalkMaxNameLen)); if (!symbol) return kStackWalkError; // Out of memory. - memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64) + kStackWalkMaxNameLen); + memset(*symbol, 0, sizeof(IMAGEHLP_SYMBOL64) + kStackWalkMaxNameLen); symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64); symbol->MaxNameLength = kStackWalkMaxNameLen; ok = _SymGetSymFromAddr64(process_handle, // hProcess stack_frame.AddrPC.Offset, // Address &symbol_displacement, // Displacement - symbol); // Symbol + *symbol); // Symbol if (ok) { // Try to locate more source information for the symbol. IMAGEHLP_LINE64 Line; @@ -1294,11 +1294,9 @@ int OS::StackWalk(Vector frames) { // module will never be found). int err = GetLastError(); if (err != ERROR_MOD_NOT_FOUND) { - DeleteArray(symbol); break; } } - DeleteArray(symbol); frames_count++; } diff --git a/src/utils.h b/src/utils.h index fa249475ae..7c818671d5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -412,6 +412,9 @@ class ScopedVector : public Vector { ~ScopedVector() { DeleteArray(this->start()); } + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); };