Remove support for malloc'd typed arrays

All typed arrays should be allocated through the array buffer allocator

BUG=none
R=dcarney@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/1110603005

Cr-Commit-Position: refs/heads/master@{#28105}
This commit is contained in:
jochen 2015-04-28 04:25:25 -07:00 committed by Commit bot
parent 6988aec61f
commit b584bab2f5
9 changed files with 68 additions and 31 deletions

View File

@ -29,12 +29,25 @@
#include <include/libplatform/libplatform.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
using namespace v8;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
// These interfaces represent an existing request processing interface.
// The idea is to imagine a real application that uses these interfaces
// and then add scripting capabilities that allow you to interact with
@ -648,6 +661,8 @@ int main(int argc, char* argv[]) {
v8::V8::InitializeICU();
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
ArrayBufferAllocator array_buffer_allocator;
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
v8::V8::Initialize();
map<string, string> options;
string file;

View File

@ -1603,13 +1603,12 @@ Data* SetBuiltinTypedArray(Isolate* isolate, Handle<JSBuiltinsObject> builtins,
size_t num_elements, const char* name) {
size_t byte_length = num_elements * sizeof(*data);
Handle<JSArrayBuffer> buffer = isolate->factory()->NewJSArrayBuffer();
bool should_be_freed = false;
if (data == NULL) {
data = reinterpret_cast<Data*>(malloc(byte_length));
should_be_freed = true;
bool is_external = data != nullptr;
if (!is_external) {
data = reinterpret_cast<Data*>(
V8::ArrayBufferAllocator()->Allocate(byte_length));
}
Runtime::SetupArrayBuffer(isolate, buffer, true, data, byte_length);
buffer->set_should_be_freed(should_be_freed);
Runtime::SetupArrayBuffer(isolate, buffer, is_external, data, byte_length);
Handle<JSTypedArray> typed_array =
isolate->factory()->NewJSTypedArray(type, buffer, 0, num_elements);

View File

@ -166,11 +166,9 @@ Persistent<Context> Shell::utility_context_;
Persistent<Context> Shell::evaluation_context_;
ShellOptions Shell::options;
const char* Shell::kPrompt = "d8> ";
#ifndef V8_SHARED
const int MB = 1024 * 1024;
#ifndef V8_SHARED
bool CounterMap::Match(void* key1, void* key2) {
const char* name1 = reinterpret_cast<const char*>(key1);
const char* name2 = reinterpret_cast<const char*>(key2);
@ -1599,8 +1597,14 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
void* Allocate(size_t) override { return malloc(1); }
void* AllocateUninitialized(size_t length) override { return malloc(1); }
void* Allocate(size_t length) override {
size_t actual_length = length > 10 * MB ? 1 : length;
void* data = AllocateUninitialized(actual_length);
return data == NULL ? data : memset(data, 0, actual_length);
}
void* AllocateUninitialized(size_t length) override {
return length > 10 * MB ? malloc(1) : malloc(length);
}
void Free(void* p, size_t) override { free(p); }
};

View File

@ -6447,16 +6447,6 @@ void JSArrayBuffer::set_is_external(bool value) {
}
bool JSArrayBuffer::should_be_freed() {
return ShouldBeFreed::decode(bit_field());
}
void JSArrayBuffer::set_should_be_freed(bool value) {
set_bit_field(ShouldBeFreed::update(bit_field(), value));
}
bool JSArrayBuffer::is_neuterable() {
return IsNeuterable::decode(bit_field());
}

View File

@ -10249,9 +10249,6 @@ class JSArrayBuffer: public JSObject {
inline bool is_external();
inline void set_is_external(bool value);
inline bool should_be_freed();
inline void set_should_be_freed(bool value);
inline bool is_neuterable();
inline void set_is_neuterable(bool value);
@ -10279,9 +10276,8 @@ class JSArrayBuffer: public JSObject {
kSize + v8::ArrayBuffer::kInternalFieldCount * kPointerSize;
class IsExternal : public BitField<bool, 1, 1> {};
class ShouldBeFreed : public BitField<bool, 2, 1> {};
class IsNeuterable : public BitField<bool, 3, 1> {};
class WasNeutered : public BitField<bool, 4, 1> {};
class IsNeuterable : public BitField<bool, 2, 1> {};
class WasNeutered : public BitField<bool, 3, 1> {};
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(JSArrayBuffer);

View File

@ -14,10 +14,6 @@ namespace internal {
void Runtime::FreeArrayBuffer(Isolate* isolate,
JSArrayBuffer* phantom_array_buffer) {
if (phantom_array_buffer->should_be_freed()) {
DCHECK(phantom_array_buffer->is_external());
free(phantom_array_buffer->backing_store());
}
if (phantom_array_buffer->is_external()) return;
size_t allocated_length =

View File

@ -109,6 +109,17 @@ class SnapshotWriter {
};
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
char* GetExtraCode(char* filename) {
if (filename == NULL || strlen(filename) == 0) return NULL;
::printf("Embedding extra script: %s\n", filename);
@ -153,6 +164,8 @@ int main(int argc, char** argv) {
V8::InitializeICU();
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
ArrayBufferAllocator array_buffer_allocator;
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
v8::V8::Initialize();
{

View File

@ -13,6 +13,16 @@
namespace {
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
class DefaultPlatformEnvironment final : public ::testing::Environment {
public:
DefaultPlatformEnvironment() : platform_(NULL) {}
@ -23,6 +33,7 @@ class DefaultPlatformEnvironment final : public ::testing::Environment {
platform_ = v8::platform::CreateDefaultPlatform();
ASSERT_TRUE(platform_ != NULL);
v8::V8::InitializePlatform(platform_);
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator_);
ASSERT_TRUE(v8::V8::Initialize());
}
@ -36,6 +47,7 @@ class DefaultPlatformEnvironment final : public ::testing::Environment {
private:
v8::Platform* platform_;
ArrayBufferAllocator array_buffer_allocator_;
};
} // namespace

View File

@ -45,6 +45,16 @@
using namespace v8::internal;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
class StringResource8 : public v8::String::ExternalOneByteStringResource {
public:
StringResource8(const char* data, int length)
@ -131,6 +141,8 @@ int main(int argc, char* argv[]) {
v8::V8::InitializeICU();
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
ArrayBufferAllocator array_buffer_allocator;
v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
v8::V8::Initialize();
Encoding encoding = LATIN1;
std::vector<std::string> fnames;