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
This commit is contained in:
antonm@chromium.org 2010-05-05 12:25:58 +00:00
parent 92a9e30cd8
commit 1e744a3011
13 changed files with 46 additions and 66 deletions

View File

@ -2185,10 +2185,10 @@ Local<String> v8::Object::ObjectProtoToString() {
int postfix_len = i::StrLength(postfix);
int buf_len = prefix_len + str_len + postfix_len;
char* buf = i::NewArray<char>(buf_len);
i::ScopedVector<char> 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<String> v8::Object::ObjectProtoToString() {
memcpy(ptr, postfix, postfix_len * v8::internal::kCharSize);
// Copy the buffer into a heap-allocated string and return it.
Local<String> result = v8::String::New(buf, buf_len);
i::DeleteArray(buf);
Local<String> result = v8::String::New(buf.start(), buf_len);
return result;
}
}

View File

@ -377,7 +377,7 @@ static Object* CallJsBuiltin(const char* name,
name);
ASSERT(js_builtin->IsJSFunction());
Handle<JSFunction> function(Handle<JSFunction>::cast(js_builtin));
Vector<Object**> argv(Vector<Object**>::New(args.length() - 1));
ScopedVector<Object**> argv(args.length() - 1);
int n_args = args.length() - 1;
for (int i = 0; i < n_args; i++) {
argv[i] = args.at<Object>(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;
}

View File

@ -181,15 +181,15 @@ void DebuggerAgentSession::Run() {
buf.GetNext();
len++;
}
int16_t* temp = NewArray<int16_t>(len + 1);
ScopedVector<int16_t> 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<const uint16_t *>(temp), len);
DeleteArray(temp);
v8::Debug::SendCommand(reinterpret_cast<const uint16_t *>(temp.start()),
len);
}
}

View File

@ -52,14 +52,13 @@ namespace internal {
#ifdef ENABLE_DEBUGGER_SUPPORT
static void PrintLn(v8::Local<v8::Value> value) {
v8::Local<v8::String> s = value->ToString();
char* data = NewArray<char>(s->Length() + 1);
if (data == NULL) {
ScopedVector<char> 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());
}

View File

@ -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<char>(len + 1);
memcpy(copy0, str, len);
ScopedVector<char> 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<char*>(argc);
ScopedVector<char*> 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;
}

View File

@ -1313,9 +1313,8 @@ void Logger::LogCodeObjects() {
void Logger::LogCompiledFunctions() {
HandleScope scope;
const int compiled_funcs_count = EnumerateCompiledFunctions(NULL);
Handle<SharedFunctionInfo>* sfis =
NewArray< Handle<SharedFunctionInfo> >(compiled_funcs_count);
EnumerateCompiledFunctions(sfis);
ScopedVector< Handle<SharedFunctionInfo> > 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);
}

View File

@ -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<size_t>(this->length()) == resource->length());
SmartPointer<uc16> smart_chars(NewArray<uc16>(this->length()));
String::WriteToFlat(this, *smart_chars, 0, this->length());
ASSERT(memcmp(*smart_chars,
ScopedVector<uc16> 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<size_t>(this->length()) == resource->length());
SmartPointer<char> smart_chars(NewArray<char>(this->length()));
String::WriteToFlat(this, *smart_chars, 0, this->length());
ASSERT(memcmp(*smart_chars,
ScopedVector<char> 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

View File

@ -286,14 +286,12 @@ void OS::LogSharedLibraryAddresses() {
int OS::StackWalk(Vector<OS::StackFrame> frames) {
int frames_size = frames.length();
void** addresses = NewArray<void*>(frames_size);
ScopedVector<void*> 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<OS::StackFrame> frames) {
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
}
DeleteArray(addresses);
free(symbols);
return frames_count;

View File

@ -376,14 +376,12 @@ int OS::StackWalk(Vector<OS::StackFrame> frames) {
// backtrace is a glibc extension.
#ifdef __GLIBC__
int frames_size = frames.length();
void** addresses = NewArray<void*>(frames_size);
ScopedVector<void*> 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<OS::StackFrame> frames) {
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
}
DeleteArray(addresses);
free(symbols);
return frames_count;

View File

@ -283,13 +283,12 @@ int OS::StackWalk(Vector<StackFrame> frames) {
return 0;
int frames_size = frames.length();
void** addresses = NewArray<void*>(frames_size);
int frames_count = backtrace(addresses, frames_size);
ScopedVector<void*> 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<StackFrame> frames) {
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
}
DeleteArray(addresses);
free(symbols);
return frames_count;

View File

@ -233,14 +233,12 @@ void OS::LogSharedLibraryAddresses() {
int OS::StackWalk(Vector<OS::StackFrame> frames) {
int frames_size = frames.length();
void** addresses = NewArray<void*>(frames_size);
ScopedVector<void*> 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<OS::StackFrame> frames) {
frames[i].text[kStackWalkMaxTextLen - 1] = '\0';
}
DeleteArray(addresses);
free(symbols);
return frames_count;

View File

@ -1249,16 +1249,16 @@ int OS::StackWalk(Vector<OS::StackFrame> frames) {
// Try to locate a symbol for this frame.
DWORD64 symbol_displacement;
IMAGEHLP_SYMBOL64* symbol = NULL;
symbol = NewArray<IMAGEHLP_SYMBOL64>(kStackWalkMaxNameLen);
SmartPointer<IMAGEHLP_SYMBOL64> symbol(
NewArray<IMAGEHLP_SYMBOL64>(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<OS::StackFrame> frames) {
// module will never be found).
int err = GetLastError();
if (err != ERROR_MOD_NOT_FOUND) {
DeleteArray(symbol);
break;
}
}
DeleteArray(symbol);
frames_count++;
}

View File

@ -412,6 +412,9 @@ class ScopedVector : public Vector<T> {
~ScopedVector() {
DeleteArray(this->start());
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
};