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

Unified Diff: runtime/bin/file.cc

Issue 1781883002: Fixes some memory leaks in //runtime/bin (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix tests on Windows Created 4 years, 9 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
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_android.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/file.cc
diff --git a/runtime/bin/file.cc b/runtime/bin/file.cc
index a1efe42c5fc6159e89123e0994c3c852894a2ab6..7cc3e7181198de86a31755b42c2e20ca44efcdd6 100644
--- a/runtime/bin/file.cc
+++ b/runtime/bin/file.cc
@@ -24,7 +24,6 @@ static bool capture_stdout = false;
// Are we capturing output from stderr for the VM service?
static bool capture_stderr = false;
-
void SetCaptureStdout(bool value) {
capture_stdout = value;
}
@@ -45,7 +44,6 @@ bool ShouldCaptureStderr() {
}
-
// The file pointer has been passed into Dart as an intptr_t and it is safe
// to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and
// from there to a File pointer.
@@ -83,11 +81,11 @@ bool File::WriteFully(const void* buffer, int64_t num_bytes) {
}
if (capture_stdout || capture_stderr) {
intptr_t fd = GetFD();
- if (fd == STDOUT_FILENO && capture_stdout) {
+ if ((fd == STDOUT_FILENO) && capture_stdout) {
Dart_ServiceSendDataEvent("Stdout", "WriteEvent",
reinterpret_cast<const uint8_t*>(buffer),
num_bytes);
- } else if (fd == STDERR_FILENO && capture_stderr) {
+ } else if ((fd == STDERR_FILENO) && capture_stderr) {
Dart_ServiceSendDataEvent("Stderr", "WriteEvent",
reinterpret_cast<const uint8_t*>(buffer),
num_bytes);
@@ -98,11 +96,11 @@ bool File::WriteFully(const void* buffer, int64_t num_bytes) {
File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) {
- ASSERT(mode == File::kDartRead ||
- mode == File::kDartWrite ||
- mode == File::kDartAppend ||
- mode == File::kDartWriteOnly ||
- mode == File::kDartWriteOnlyAppend);
+ ASSERT((mode == File::kDartRead) ||
+ (mode == File::kDartWrite) ||
+ (mode == File::kDartAppend) ||
+ (mode == File::kDartWriteOnly) ||
+ (mode == File::kDartWriteOnlyAppend));
if (mode == File::kDartWrite) {
return File::kWriteTruncate;
}
@@ -131,7 +129,7 @@ void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) {
// files. Directories can be opened for reading using the posix
// 'open' call.
File* file = NULL;
- file = File::Open(filename, file_mode);
+ file = File::ScopedOpen(filename, file_mode);
if (file != NULL) {
Dart_SetReturnValue(args,
Dart_NewInteger(reinterpret_cast<intptr_t>(file)));
@@ -219,7 +217,9 @@ void FUNCTION_NAME(File_Read)(Dart_NativeArguments args) {
// TODO(sgjesse): Cache the _makeUint8ListView function somewhere.
Dart_Handle io_lib =
Dart_LookupLibrary(DartUtils::NewString("dart:io"));
- if (Dart_IsError(io_lib)) Dart_PropagateError(io_lib);
+ if (Dart_IsError(io_lib)) {
+ Dart_PropagateError(io_lib);
+ }
Dart_Handle array_view =
Dart_Invoke(io_lib,
DartUtils::NewString("_makeUint8ListView"),
@@ -253,9 +253,11 @@ void FUNCTION_NAME(File_ReadInto)(Dart_NativeArguments args) {
intptr_t length = end - start;
intptr_t array_len = 0;
Dart_Handle result = Dart_ListLength(buffer_obj, &array_len);
- if (Dart_IsError(result)) Dart_PropagateError(result);
+ if (Dart_IsError(result)) {
+ Dart_PropagateError(result);
+ }
ASSERT(end <= array_len);
- uint8_t* buffer = new uint8_t[length];
+ uint8_t* buffer = Dart_ScopeAllocate(length);
int64_t bytes_read = file->Read(reinterpret_cast<void*>(buffer), length);
if (bytes_read >= 0) {
result = Dart_ListSetAsBytes(buffer_obj, start, buffer, bytes_read);
@@ -267,7 +269,6 @@ void FUNCTION_NAME(File_ReadInto)(Dart_NativeArguments args) {
} else {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
}
- delete[] buffer;
}
@@ -294,7 +295,9 @@ void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) {
void* buffer = NULL;
Dart_Handle result =
Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len);
- if (Dart_IsError(result)) Dart_PropagateError(result);
+ if (Dart_IsError(result)) {
+ Dart_PropagateError(result);
+ }
ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8);
ASSERT(end <= buffer_len);
@@ -305,7 +308,9 @@ void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) {
// Release the direct pointer acquired above.
result = Dart_TypedDataReleaseData(buffer_obj);
- if (Dart_IsError(result)) Dart_PropagateError(result);
+ if (Dart_IsError(result)) {
+ Dart_PropagateError(result);
+ }
if (!success) {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
@@ -466,12 +471,11 @@ void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) {
if (Dart_IsString(Dart_GetNativeArgument(args, 0))) {
const char* name =
DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
- char* target = File::LinkTarget(name);
+ const char* target = File::LinkTarget(name);
if (target == NULL) {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
} else {
Dart_SetReturnValue(args, DartUtils::NewString(target));
- free(target);
}
} else {
Dart_Handle err = DartUtils::NewDartArgumentError(
@@ -550,10 +554,9 @@ void FUNCTION_NAME(File_Copy)(Dart_NativeArguments args) {
void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) {
const char* str =
DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
- char* path = File::GetCanonicalPath(str);
+ const char* path = File::GetCanonicalPath(str);
if (path != NULL) {
Dart_SetReturnValue(args, DartUtils::NewString(path));
- free(path);
} else {
Dart_SetReturnValue(args, DartUtils::NewDartOSError());
}
@@ -605,7 +608,9 @@ void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) {
} else {
Dart_Handle returned_data = Dart_NewTypedData(Dart_TypedData_kInt64,
File::kStatSize);
- if (Dart_IsError(returned_data)) Dart_PropagateError(returned_data);
+ if (Dart_IsError(returned_data)) {
+ Dart_PropagateError(returned_data);
+ }
Dart_TypedData_Type data_type_unused;
void* data_location;
intptr_t data_length_unused;
@@ -613,10 +618,14 @@ void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) {
&data_type_unused,
&data_location,
&data_length_unused);
- if (Dart_IsError(status)) Dart_PropagateError(status);
+ if (Dart_IsError(status)) {
+ Dart_PropagateError(status);
+ }
memmove(data_location, stat_data, File::kStatSize * sizeof(int64_t));
status = Dart_TypedDataReleaseData(returned_data);
- if (Dart_IsError(status)) Dart_PropagateError(status);
+ if (Dart_IsError(status)) {
+ Dart_PropagateError(status);
+ }
Dart_SetReturnValue(args, returned_data);
}
} else {
@@ -669,7 +678,7 @@ File* CObjectToFilePointer(CObject* cobject) {
CObject* File::ExistsRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filename(request[0]);
bool result = File::Exists(filename.CString());
return CObject::Bool(result);
@@ -679,7 +688,7 @@ CObject* File::ExistsRequest(const CObjectArray& request) {
CObject* File::CreateRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filename(request[0]);
bool result = File::Create(filename.CString());
if (result) {
@@ -691,9 +700,10 @@ CObject* File::CreateRequest(const CObjectArray& request) {
return CObject::IllegalArgumentError();
}
+
CObject* File::OpenRequest(const CObjectArray& request) {
File* file = NULL;
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsInt32()) {
CObjectString filename(request[0]);
@@ -701,7 +711,7 @@ CObject* File::OpenRequest(const CObjectArray& request) {
File::DartFileOpenMode dart_file_mode =
static_cast<File::DartFileOpenMode>(mode.Value());
File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
- file = File::Open(filename.CString(), file_mode);
+ file = File::ScopedOpen(filename.CString(), file_mode);
if (file != NULL) {
return new CObjectIntptr(
CObject::NewIntptr(reinterpret_cast<intptr_t>(file)));
@@ -714,7 +724,7 @@ CObject* File::OpenRequest(const CObjectArray& request) {
CObject* File::DeleteRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filename(request[0]);
bool result = File::Delete(filename.CString());
if (result) {
@@ -728,13 +738,15 @@ CObject* File::DeleteRequest(const CObjectArray& request) {
CObject* File::RenameRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsString()) {
CObjectString old_path(request[0]);
CObjectString new_path(request[1]);
bool completed = File::Rename(old_path.CString(), new_path.CString());
- if (completed) return CObject::True();
+ if (completed) {
+ return CObject::True();
+ }
return CObject::NewOSError();
}
return CObject::IllegalArgumentError();
@@ -742,13 +754,15 @@ CObject* File::RenameRequest(const CObjectArray& request) {
CObject* File::CopyRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsString()) {
CObjectString old_path(request[0]);
CObjectString new_path(request[1]);
bool completed = File::Copy(old_path.CString(), new_path.CString());
- if (completed) return CObject::True();
+ if (completed) {
+ return CObject::True();
+ }
return CObject::NewOSError();
}
return CObject::IllegalArgumentError();
@@ -756,12 +770,11 @@ CObject* File::CopyRequest(const CObjectArray& request) {
CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filename(request[0]);
- char* result = File::GetCanonicalPath(filename.CString());
+ const char* result = File::GetCanonicalPath(filename.CString());
if (result != NULL) {
CObject* path = new CObjectString(CObject::NewString(result));
- free(result);
return path;
} else {
return CObject::NewOSError();
@@ -773,7 +786,7 @@ CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) {
CObject* File::CloseRequest(const CObjectArray& request) {
intptr_t return_value = -1;
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
File* file = CObjectToFilePointer(request[0]);
ASSERT(file != NULL);
delete file;
@@ -784,7 +797,7 @@ CObject* File::CloseRequest(const CObjectArray& request) {
CObject* File::PositionRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
File* file = CObjectToFilePointer(request[0]);
ASSERT(file != NULL);
if (!file->IsClosed()) {
@@ -803,7 +816,7 @@ CObject* File::PositionRequest(const CObjectArray& request) {
CObject* File::SetPositionRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64()) {
File* file = CObjectToFilePointer(request[0]);
@@ -824,7 +837,7 @@ CObject* File::SetPositionRequest(const CObjectArray& request) {
CObject* File::TruncateRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64()) {
File* file = CObjectToFilePointer(request[0]);
@@ -845,7 +858,7 @@ CObject* File::TruncateRequest(const CObjectArray& request) {
CObject* File::LengthRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
File* file = CObjectToFilePointer(request[0]);
ASSERT(file != NULL);
if (!file->IsClosed()) {
@@ -864,7 +877,7 @@ CObject* File::LengthRequest(const CObjectArray& request) {
CObject* File::LengthFromPathRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filepath(request[0]);
int64_t return_value = File::LengthFromPath(filepath.CString());
if (return_value >= 0) {
@@ -878,7 +891,7 @@ CObject* File::LengthFromPathRequest(const CObjectArray& request) {
CObject* File::LastModifiedRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString filepath(request[0]);
int64_t return_value = File::LastModified(filepath.CString());
if (return_value >= 0) {
@@ -892,7 +905,7 @@ CObject* File::LastModifiedRequest(const CObjectArray& request) {
CObject* File::FlushRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
File* file = CObjectToFilePointer(request[0]);
ASSERT(file != NULL);
if (!file->IsClosed()) {
@@ -910,7 +923,7 @@ CObject* File::FlushRequest(const CObjectArray& request) {
CObject* File::ReadByteRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsIntptr()) {
+ if ((request.Length() == 1) && request[0]->IsIntptr()) {
File* file = CObjectToFilePointer(request[0]);
ASSERT(file != NULL);
if (!file->IsClosed()) {
@@ -932,7 +945,7 @@ CObject* File::ReadByteRequest(const CObjectArray& request) {
CObject* File::WriteByteRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64()) {
File* file = CObjectToFilePointer(request[0]);
@@ -955,7 +968,7 @@ CObject* File::WriteByteRequest(const CObjectArray& request) {
CObject* File::ReadRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64()) {
File* file = CObjectToFilePointer(request[0]);
@@ -987,7 +1000,7 @@ CObject* File::ReadRequest(const CObjectArray& request) {
CObject* File::ReadIntoRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64()) {
File* file = CObjectToFilePointer(request[0]);
@@ -1045,7 +1058,7 @@ static int SizeInBytes(Dart_TypedData_Type type) {
CObject* File::WriteFromRequest(const CObjectArray& request) {
- if (request.Length() == 4 &&
+ if ((request.Length() == 4) &&
request[0]->IsIntptr() &&
(request[1]->IsTypedData() || request[1]->IsArray()) &&
request[2]->IsInt32OrInt64() &&
@@ -1064,14 +1077,13 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
buffer_start = typed_data.Buffer() + start;
} else {
CObjectArray array(request[1]);
- buffer_start = new uint8_t[length];
+ buffer_start = Dart_ScopeAllocate(length);
for (int i = 0; i < length; i++) {
if (array[i + start]->IsInt32OrInt64()) {
int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]);
buffer_start[i] = static_cast<uint8_t>(value & 0xFF);
} else {
// Unsupported type.
- delete[] buffer_start;
return CObject::IllegalArgumentError();
}
}
@@ -1079,9 +1091,6 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
}
bool success =
file->WriteFully(reinterpret_cast<void*>(buffer_start), length);
- if (!request[1]->IsTypedData()) {
- delete[] buffer_start;
- }
if (success) {
return new CObjectInt64(CObject::NewInt64(length));
} else {
@@ -1096,7 +1105,7 @@ CObject* File::WriteFromRequest(const CObjectArray& request) {
CObject* File::CreateLinkRequest(const CObjectArray& request) {
- if (request.Length() != 2 ||
+ if ((request.Length() != 2) ||
!request[0]->IsString() ||
!request[1]->IsString()) {
return CObject::IllegalArgumentError();
@@ -1112,7 +1121,7 @@ CObject* File::CreateLinkRequest(const CObjectArray& request) {
CObject* File::DeleteLinkRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString link_path(request[0]);
bool result = File::DeleteLink(link_path.CString());
if (result) {
@@ -1126,13 +1135,15 @@ CObject* File::DeleteLinkRequest(const CObjectArray& request) {
CObject* File::RenameLinkRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsString()) {
CObjectString old_path(request[0]);
CObjectString new_path(request[1]);
bool completed = File::RenameLink(old_path.CString(), new_path.CString());
- if (completed) return CObject::True();
+ if (completed) {
+ return CObject::True();
+ }
return CObject::NewOSError();
}
return CObject::IllegalArgumentError();
@@ -1140,12 +1151,11 @@ CObject* File::RenameLinkRequest(const CObjectArray& request) {
CObject* File::LinkTargetRequest(const CObjectArray& request) {
- if (request.Length() == 1 && request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
CObjectString link_path(request[0]);
- char* target = File::LinkTarget(link_path.CString());
+ const char* target = File::LinkTarget(link_path.CString());
if (target != NULL) {
CObject* result = new CObjectString(CObject::NewString(target));
- free(target);
return result;
} else {
return CObject::NewOSError();
@@ -1156,7 +1166,7 @@ CObject* File::LinkTargetRequest(const CObjectArray& request) {
CObject* File::TypeRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsBool()) {
CObjectString path(request[0]);
@@ -1169,7 +1179,7 @@ CObject* File::TypeRequest(const CObjectArray& request) {
CObject* File::IdenticalRequest(const CObjectArray& request) {
- if (request.Length() == 2 &&
+ if ((request.Length() == 2) &&
request[0]->IsString() &&
request[1]->IsString()) {
CObjectString path1(request[0]);
@@ -1189,8 +1199,7 @@ CObject* File::IdenticalRequest(const CObjectArray& request) {
CObject* File::StatRequest(const CObjectArray& request) {
- if (request.Length() == 1 &&
- request[0]->IsString()) {
+ if ((request.Length() == 1) && request[0]->IsString()) {
int64_t data[File::kStatSize];
CObjectString path(request[0]);
File::Stat(path.CString(), data);
@@ -1212,7 +1221,7 @@ CObject* File::StatRequest(const CObjectArray& request) {
CObject* File::LockRequest(const CObjectArray& request) {
- if (request.Length() == 4 &&
+ if ((request.Length() == 4) &&
request[0]->IsIntptr() &&
request[1]->IsInt32OrInt64() &&
request[2]->IsInt32OrInt64() &&
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698