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 @@
#include <stdarg.h>
#include <sys/stat.h>
#include <vector>
#include "src/base/functional.h"
#include "src/base/logging.h"
......@@ -200,82 +201,61 @@ char* ReadLine(const char* prompt) {
return result;
}
namespace {
char* ReadCharsFromFile(FILE* file,
int* size,
int extra_space,
bool verbose,
const char* filename) {
std::vector<char> ReadCharsFromFile(FILE* file, bool* exists, bool verbose,
const char* filename) {
if (file == nullptr || fseek(file, 0, SEEK_END) != 0) {
if (verbose) {
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.
*size = static_cast<int>(ftell(file));
ptrdiff_t size = ftell(file);
rewind(file);
char* result = NewArray<char>(*size + extra_space);
for (int i = 0; i < *size && feof(file) == 0;) {
int read = static_cast<int>(fread(&result[i], 1, *size - i, file));
if (read != (*size - i) && ferror(file) != 0) {
std::vector<char> result(size);
for (ptrdiff_t i = 0; i < size && feof(file) == 0;) {
ptrdiff_t read = fread(result.data() + i, 1, size - i, file);
if (read != (size - i) && ferror(file) != 0) {
fclose(file);
DeleteArray(result);
return nullptr;
*exists = false;
return std::vector<char>();
}
i += read;
}
*exists = true;
return result;
}
char* ReadCharsFromFile(const char* filename,
int* size,
int extra_space,
bool verbose) {
std::vector<char> ReadCharsFromFile(const char* filename, bool* exists,
bool verbose) {
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);
return result;
}
byte* ReadBytes(const char* filename, int* size, bool verbose) {
char* chars = ReadCharsFromFile(filename, size, 0, verbose);
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();
std::string VectorToString(const std::vector<char>& chars) {
if (chars.size() == 0) {
return std::string();
}
chars[size] = '\0';
*exists = true;
return Vector<const char>(chars, size);
return std::string(chars.begin(), chars.end());
}
} // namespace
Vector<const char> ReadFile(const char* filename,
bool* exists,
bool verbose) {
int size;
char* result = ReadCharsFromFile(filename, &size, 1, verbose);
return SetVectorContents(result, size, exists);
std::string ReadFile(const char* filename, bool* exists, bool verbose) {
std::vector<char> result = ReadCharsFromFile(filename, exists, verbose);
return VectorToString(result);
}
Vector<const char> ReadFile(FILE* file,
bool* exists,
bool verbose) {
int size;
char* result = ReadCharsFromFile(file, &size, 1, verbose, "");
return SetVectorContents(result, size, exists);
std::string ReadFile(FILE* file, bool* exists, bool verbose) {
std::vector<char> result = ReadCharsFromFile(file, exists, verbose, "");
return VectorToString(result);
}
......
......@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <string>
#include <type_traits>
#include "include/v8.h"
......@@ -1079,12 +1080,6 @@ inline void Flush() {
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.
// The file is overwritten. Returns the number of chars written.
int AppendChars(const char* filename,
......@@ -1228,16 +1223,11 @@ inline void MemsetPointer(T** dest, U* value, int counter) {
#undef STOS
}
// Simple support to read a file into a 0-terminated C-string.
// The returned buffer must be freed by the caller.
// Simple support to read a file into std::string.
// On return, *exits tells whether the file existed.
V8_EXPORT_PRIVATE Vector<const char> ReadFile(const char* filename,
bool* exists,
bool verbose = true);
Vector<const char> ReadFile(FILE* file,
bool* exists,
bool verbose = true);
V8_EXPORT_PRIVATE std::string ReadFile(const char* filename, bool* exists,
bool verbose = true);
std::string ReadFile(FILE* file, bool* exists, bool verbose = true);
template <typename sourcechar, typename sinkchar>
V8_INLINE static void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src,
......
......@@ -119,7 +119,6 @@ class ScopedLoggerInitializer {
if (temp_file_ != nullptr) fclose(temp_file_);
i::FLAG_prof = saved_prof_;
i::FLAG_log = saved_log_;
log_.Dispose();
}
v8::Local<v8::Context>& env() { return env_; }
......@@ -132,12 +131,12 @@ class ScopedLoggerInitializer {
void PrintLog(int requested_nof_lines = 0, const char* start = nullptr) {
if (requested_nof_lines <= 0) {
printf("%s", log_.start());
printf("%s", log_.c_str());
return;
}
// Try to print the last {requested_nof_lines} of the log.
if (start == nullptr) start = log_.start();
const char* current = log_.end();
if (start == nullptr) start = log_.c_str();
const char* current = start + log_.length();
int nof_lines = requested_nof_lines;
while (current > start && nof_lines > 0) {
current--;
......@@ -153,8 +152,9 @@ class ScopedLoggerInitializer {
}
v8::Local<v8::String> GetLogString() {
return v8::String::NewFromUtf8(isolate_, log_.start(),
v8::NewStringType::kNormal, log_.length())
int length = static_cast<int>(log_.size());
return v8::String::NewFromUtf8(isolate_, log_.c_str(),
v8::NewStringType::kNormal, length)
.ToLocalChecked();
}
......@@ -164,13 +164,13 @@ class ScopedLoggerInitializer {
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* start = nullptr) {
// Make sure that StopLogging() has been called before.
CHECK(log_.size());
if (start == nullptr) start = log_.start();
if (start == nullptr) start = log_.c_str();
const char* end = GetEndPosition();
return FindLogLine(start, end, prefix, suffix);
}
......@@ -220,7 +220,7 @@ class ScopedLoggerInitializer {
const char* prefix, int field_index) {
// Make sure that StopLogging() has been called before.
CHECK(log_.size());
const char* current = log_.start();
const char* current = log_.c_str();
while (current != nullptr) {
current = FindLine(prefix, nullptr, current);
if (current == nullptr) return;
......@@ -257,7 +257,7 @@ class ScopedLoggerInitializer {
v8::HandleScope scope_;
v8::Local<v8::Context> env_;
Logger* logger_;
i::Vector<const char> log_;
std::string log_;
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