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

Unified Diff: runtime/bin/utils_win.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/utils_win.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/utils_win.cc
diff --git a/runtime/bin/utils_win.cc b/runtime/bin/utils_win.cc
index 116ae56157a92739d8569134c04b33b4b60756aa..227cc243c19cabe103bc3ae8cbcc67465a69bd70 100644
--- a/runtime/bin/utils_win.cc
+++ b/runtime/bin/utils_win.cc
@@ -47,9 +47,9 @@ OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
FormatMessageIntoBuffer(code_, message, kMaxMessageLength);
char* utf8 = StringUtilsWin::WideToUtf8(message);
SetMessage(utf8);
- free(utf8);
}
+
void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
set_sub_system(sub_system);
set_code(code);
@@ -59,20 +59,22 @@ void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
FormatMessageIntoBuffer(code_, message, kMaxMessageLength);
char* utf8 = StringUtilsWin::WideToUtf8(message);
SetMessage(utf8);
- free(utf8);
}
+
char* StringUtils::ConsoleStringToUtf8(char* str,
intptr_t len,
intptr_t* result_len) {
int wide_len = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
- wchar_t* wide = new wchar_t[wide_len];
+ wchar_t* wide;
+ wide =
+ reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide)));
MultiByteToWideChar(CP_ACP, 0, str, len, wide, wide_len);
char* utf8 = StringUtilsWin::WideToUtf8(wide, wide_len, result_len);
- delete[] wide;
return utf8;
}
+
char* StringUtils::Utf8ToConsoleString(char* utf8,
intptr_t len,
intptr_t* result_len) {
@@ -80,19 +82,20 @@ char* StringUtils::Utf8ToConsoleString(char* utf8,
wchar_t* wide = StringUtilsWin::Utf8ToWide(utf8, len, &wide_len);
int system_len = WideCharToMultiByte(
CP_ACP, 0, wide, wide_len, NULL, 0, NULL, NULL);
- char* ansi = reinterpret_cast<char*>(malloc(system_len));
+ char* ansi;
+ ansi =
+ reinterpret_cast<char*>(Dart_ScopeAllocate(system_len * sizeof(*ansi)));
if (ansi == NULL) {
- free(wide);
return NULL;
}
WideCharToMultiByte(CP_ACP, 0, wide, wide_len, ansi, system_len, NULL, NULL);
- free(wide);
if (result_len != NULL) {
*result_len = system_len;
}
return ansi;
}
+
char* StringUtilsWin::WideToUtf8(wchar_t* wide,
intptr_t len,
intptr_t* result_len) {
@@ -100,7 +103,8 @@ char* StringUtilsWin::WideToUtf8(wchar_t* wide,
// NUL byte in the length.
int utf8_len = WideCharToMultiByte(
CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL);
- char* utf8 = reinterpret_cast<char*>(malloc(utf8_len));
+ char* utf8;
+ utf8 = reinterpret_cast<char*>(Dart_ScopeAllocate(utf8_len * sizeof(*utf8)));
WideCharToMultiByte(CP_UTF8, 0, wide, len, utf8, utf8_len, NULL, NULL);
if (result_len != NULL) {
*result_len = utf8_len;
@@ -115,8 +119,9 @@ wchar_t* StringUtilsWin::Utf8ToWide(char* utf8,
// If len is -1 then MultiByteToWideChar will include the terminating
// NUL byte in the length.
int wide_len = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
- wchar_t* wide =
- reinterpret_cast<wchar_t*>(malloc((wide_len) * sizeof(wchar_t)));
+ wchar_t* wide;
+ wide =
+ reinterpret_cast<wchar_t*>(Dart_ScopeAllocate(wide_len * sizeof(*wide)));
MultiByteToWideChar(CP_UTF8, 0, utf8, len, wide, wide_len);
if (result_len != NULL) {
*result_len = wide_len;
@@ -124,6 +129,7 @@ wchar_t* StringUtilsWin::Utf8ToWide(char* utf8,
return wide;
}
+
const char* StringUtils::Utf8ToConsoleString(
const char* utf8, intptr_t len, intptr_t* result_len) {
return const_cast<const char*>(
@@ -131,6 +137,7 @@ const char* StringUtils::Utf8ToConsoleString(
const_cast<char*>(utf8), len, result_len));
}
+
const char* StringUtils::ConsoleStringToUtf8(
const char* str, intptr_t len, intptr_t* result_len) {
return const_cast<const char*>(
@@ -138,23 +145,28 @@ const char* StringUtils::ConsoleStringToUtf8(
const_cast<char*>(str), len, result_len));
}
+
const char* StringUtilsWin::WideToUtf8(
const wchar_t* wide, intptr_t len, intptr_t* result_len) {
return const_cast<const char*>(
StringUtilsWin::WideToUtf8(const_cast<wchar_t*>(wide), len, result_len));
}
+
const wchar_t* StringUtilsWin::Utf8ToWide(
const char* utf8, intptr_t len, intptr_t* result_len) {
return const_cast<const wchar_t*>(
StringUtilsWin::Utf8ToWide(const_cast<char*>(utf8), len, result_len));
}
+
bool ShellUtils::GetUtf8Argv(int argc, char** argv) {
wchar_t* command_line = GetCommandLineW();
int unicode_argc;
wchar_t** unicode_argv = CommandLineToArgvW(command_line, &unicode_argc);
- if (unicode_argv == NULL) return false;
+ if (unicode_argv == NULL) {
+ return false;
+ }
// The argc passed to main should have the same argc as we get here.
ASSERT(argc == unicode_argc);
if (argc < unicode_argc) {
@@ -162,12 +174,17 @@ bool ShellUtils::GetUtf8Argv(int argc, char** argv) {
}
for (int i = 0; i < unicode_argc; i++) {
wchar_t* arg = unicode_argv[i];
- argv[i] = StringUtilsWin::WideToUtf8(arg);
+ int arg_len =
+ WideCharToMultiByte(CP_UTF8, 0, arg, -1, NULL, 0, NULL, NULL);
+ char* utf8_arg = reinterpret_cast<char*>(malloc(arg_len));
+ WideCharToMultiByte(CP_UTF8, 0, arg, -1, utf8_arg, arg_len, NULL, NULL);
+ argv[i] = utf8_arg;
}
LocalFree(unicode_argv);
return true;
}
+
static int64_t GetCurrentTimeMicros() {
static const int64_t kTimeEpoc = 116444736000000000LL;
static const int64_t kTimeScaler = 10; // 100 ns to us.
@@ -187,6 +204,7 @@ static int64_t GetCurrentTimeMicros() {
return (time.t_ - kTimeEpoc) / kTimeScaler;
}
+
static int64_t qpc_ticks_per_second = 0;
void TimerUtils::InitOnce() {
@@ -198,10 +216,12 @@ void TimerUtils::InitOnce() {
}
}
+
int64_t TimerUtils::GetCurrentMonotonicMillis() {
return GetCurrentMonotonicMicros() / 1000;
}
+
int64_t TimerUtils::GetCurrentMonotonicMicros() {
if (qpc_ticks_per_second == 0) {
// QueryPerformanceCounter not supported, fallback.
@@ -219,6 +239,7 @@ int64_t TimerUtils::GetCurrentMonotonicMicros() {
return result;
}
+
void TimerUtils::Sleep(int64_t millis) {
::Sleep(millis);
}
« no previous file with comments | « runtime/bin/utils_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698