Commit ec067322 authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[utils] Change ReadFile interface

ReadFile returned a Vector with ownership transfer, i.e. the client
needed to call Dispose to free the memory. This CL changes the interface
to return a std::string instead, which manages ownership. As it turns
out, there is only one user of ReadString that sometimes calls an API
function which expects to take ownership of its Vector argument.

Bug: v8:7932
Cq-Include-Trybots: luci.chromium.try:linux_chromium_headless_rel;luci.chromium.try:linux_chromium_rel_ng;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: Ie624a7a65cf5814fddce7a57bc557e4b9876bc53
Reviewed-on: https://chromium-review.googlesource.com/1155115
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54805}
parent 99422311
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <vector>
#include "src/base/functional.h" #include "src/base/functional.h"
#include "src/base/logging.h" #include "src/base/logging.h"
...@@ -200,82 +201,61 @@ char* ReadLine(const char* prompt) { ...@@ -200,82 +201,61 @@ char* ReadLine(const char* prompt) {
return result; return result;
} }
namespace {
char* ReadCharsFromFile(FILE* file, std::vector<char> ReadCharsFromFile(FILE* file, bool* exists, bool verbose,
int* size, const char* filename) {
int extra_space,
bool verbose,
const char* filename) {
if (file == nullptr || fseek(file, 0, SEEK_END) != 0) { if (file == nullptr || fseek(file, 0, SEEK_END) != 0) {
if (verbose) { if (verbose) {
base::OS::PrintError("Cannot read from file %s.\n", filename); base::OS::PrintError("Cannot read from file %s.\n", filename);
} }
return nullptr; *exists = false;
return std::vector<char>();
} }
// Get the size of the file and rewind it. // Get the size of the file and rewind it.
*size = static_cast<int>(ftell(file)); ptrdiff_t size = ftell(file);
rewind(file); rewind(file);
char* result = NewArray<char>(*size + extra_space); std::vector<char> result(size);
for (int i = 0; i < *size && feof(file) == 0;) { for (ptrdiff_t i = 0; i < size && feof(file) == 0;) {
int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); ptrdiff_t read = fread(result.data() + i, 1, size - i, file);
if (read != (*size - i) && ferror(file) != 0) { if (read != (size - i) && ferror(file) != 0) {
fclose(file); fclose(file);
DeleteArray(result); *exists = false;
return nullptr; return std::vector<char>();
} }
i += read; i += read;
} }
*exists = true;
return result; return result;
} }
std::vector<char> ReadCharsFromFile(const char* filename, bool* exists,
char* ReadCharsFromFile(const char* filename, bool verbose) {
int* size,
int extra_space,
bool verbose) {
FILE* file = base::OS::FOpen(filename, "rb"); FILE* file = base::OS::FOpen(filename, "rb");
char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename); std::vector<char> result = ReadCharsFromFile(file, exists, verbose, filename);
if (file != nullptr) fclose(file); if (file != nullptr) fclose(file);
return result; return result;
} }
std::string VectorToString(const std::vector<char>& chars) {
byte* ReadBytes(const char* filename, int* size, bool verbose) { if (chars.size() == 0) {
char* chars = ReadCharsFromFile(filename, size, 0, verbose); return std::string();
return reinterpret_cast<byte*>(chars);
}
static Vector<const char> SetVectorContents(char* chars,
int size,
bool* exists) {
if (!chars) {
*exists = false;
return Vector<const char>::empty();
} }
chars[size] = '\0'; return std::string(chars.begin(), chars.end());
*exists = true;
return Vector<const char>(chars, size);
} }
} // namespace
Vector<const char> ReadFile(const char* filename, std::string ReadFile(const char* filename, bool* exists, bool verbose) {
bool* exists, std::vector<char> result = ReadCharsFromFile(filename, exists, verbose);
bool verbose) { return VectorToString(result);
int size;
char* result = ReadCharsFromFile(filename, &size, 1, verbose);
return SetVectorContents(result, size, exists);
} }
std::string ReadFile(FILE* file, bool* exists, bool verbose) {
Vector<const char> ReadFile(FILE* file, std::vector<char> result = ReadCharsFromFile(file, exists, verbose, "");
bool* exists, return VectorToString(result);
bool verbose) {
int size;
char* result = ReadCharsFromFile(file, &size, 1, verbose, "");
return SetVectorContents(result, size, exists);
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <cmath> #include <cmath>
#include <string>
#include <type_traits> #include <type_traits>
#include "include/v8.h" #include "include/v8.h"
...@@ -1079,12 +1080,6 @@ inline void Flush() { ...@@ -1079,12 +1080,6 @@ inline void Flush() {
char* ReadLine(const char* prompt); char* ReadLine(const char* prompt);
// Read and return the raw bytes in a file. the size of the buffer is returned
// in size.
// The returned buffer must be freed by the caller.
byte* ReadBytes(const char* filename, int* size, bool verbose = true);
// Append size chars from str to the file given by filename. // Append size chars from str to the file given by filename.
// The file is overwritten. Returns the number of chars written. // The file is overwritten. Returns the number of chars written.
int AppendChars(const char* filename, int AppendChars(const char* filename,
...@@ -1228,16 +1223,11 @@ inline void MemsetPointer(T** dest, U* value, int counter) { ...@@ -1228,16 +1223,11 @@ inline void MemsetPointer(T** dest, U* value, int counter) {
#undef STOS #undef STOS
} }
// Simple support to read a file into std::string.
// Simple support to read a file into a 0-terminated C-string.
// The returned buffer must be freed by the caller.
// On return, *exits tells whether the file existed. // On return, *exits tells whether the file existed.
V8_EXPORT_PRIVATE Vector<const char> ReadFile(const char* filename, V8_EXPORT_PRIVATE std::string ReadFile(const char* filename, bool* exists,
bool* exists, bool verbose = true);
bool verbose = true); std::string ReadFile(FILE* file, bool* exists, bool verbose = true);
Vector<const char> ReadFile(FILE* file,
bool* exists,
bool verbose = true);
template <typename sourcechar, typename sinkchar> template <typename sourcechar, typename sinkchar>
V8_INLINE static void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, V8_INLINE static void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src,
......
...@@ -119,7 +119,6 @@ class ScopedLoggerInitializer { ...@@ -119,7 +119,6 @@ class ScopedLoggerInitializer {
if (temp_file_ != nullptr) fclose(temp_file_); if (temp_file_ != nullptr) fclose(temp_file_);
i::FLAG_prof = saved_prof_; i::FLAG_prof = saved_prof_;
i::FLAG_log = saved_log_; i::FLAG_log = saved_log_;
log_.Dispose();
} }
v8::Local<v8::Context>& env() { return env_; } v8::Local<v8::Context>& env() { return env_; }
...@@ -132,12 +131,12 @@ class ScopedLoggerInitializer { ...@@ -132,12 +131,12 @@ class ScopedLoggerInitializer {
void PrintLog(int requested_nof_lines = 0, const char* start = nullptr) { void PrintLog(int requested_nof_lines = 0, const char* start = nullptr) {
if (requested_nof_lines <= 0) { if (requested_nof_lines <= 0) {
printf("%s", log_.start()); printf("%s", log_.c_str());
return; return;
} }
// Try to print the last {requested_nof_lines} of the log. // Try to print the last {requested_nof_lines} of the log.
if (start == nullptr) start = log_.start(); if (start == nullptr) start = log_.c_str();
const char* current = log_.end(); const char* current = start + log_.length();
int nof_lines = requested_nof_lines; int nof_lines = requested_nof_lines;
while (current > start && nof_lines > 0) { while (current > start && nof_lines > 0) {
current--; current--;
...@@ -153,8 +152,9 @@ class ScopedLoggerInitializer { ...@@ -153,8 +152,9 @@ class ScopedLoggerInitializer {
} }
v8::Local<v8::String> GetLogString() { v8::Local<v8::String> GetLogString() {
return v8::String::NewFromUtf8(isolate_, log_.start(), int length = static_cast<int>(log_.size());
v8::NewStringType::kNormal, log_.length()) return v8::String::NewFromUtf8(isolate_, log_.c_str(),
v8::NewStringType::kNormal, length)
.ToLocalChecked(); .ToLocalChecked();
} }
...@@ -164,13 +164,13 @@ class ScopedLoggerInitializer { ...@@ -164,13 +164,13 @@ class ScopedLoggerInitializer {
CHECK(exists); CHECK(exists);
} }
const char* GetEndPosition() { return log_.start() + log_.length(); } const char* GetEndPosition() { return log_.c_str() + log_.size(); }
const char* FindLine(const char* prefix, const char* suffix = nullptr, const char* FindLine(const char* prefix, const char* suffix = nullptr,
const char* start = nullptr) { const char* start = nullptr) {
// Make sure that StopLogging() has been called before. // Make sure that StopLogging() has been called before.
CHECK(log_.size()); CHECK(log_.size());
if (start == nullptr) start = log_.start(); if (start == nullptr) start = log_.c_str();
const char* end = GetEndPosition(); const char* end = GetEndPosition();
return FindLogLine(start, end, prefix, suffix); return FindLogLine(start, end, prefix, suffix);
} }
...@@ -220,7 +220,7 @@ class ScopedLoggerInitializer { ...@@ -220,7 +220,7 @@ class ScopedLoggerInitializer {
const char* prefix, int field_index) { const char* prefix, int field_index) {
// Make sure that StopLogging() has been called before. // Make sure that StopLogging() has been called before.
CHECK(log_.size()); CHECK(log_.size());
const char* current = log_.start(); const char* current = log_.c_str();
while (current != nullptr) { while (current != nullptr) {
current = FindLine(prefix, nullptr, current); current = FindLine(prefix, nullptr, current);
if (current == nullptr) return; if (current == nullptr) return;
...@@ -257,7 +257,7 @@ class ScopedLoggerInitializer { ...@@ -257,7 +257,7 @@ class ScopedLoggerInitializer {
v8::HandleScope scope_; v8::HandleScope scope_;
v8::Local<v8::Context> env_; v8::Local<v8::Context> env_;
Logger* logger_; Logger* logger_;
i::Vector<const char> log_; std::string log_;
DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer); DISALLOW_COPY_AND_ASSIGN(ScopedLoggerInitializer);
}; };
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment