Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: clean up ArrayBufferAllocator #7082

Merged
merged 4 commits into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ Buffer.prototype.swap32 = function swap32() {
return swap32n.apply(this);
};

const flags = bindingObj.flags;
const kNoZeroFill = 0;
// |binding.zeroFill| can be undefined when running inside an isolate where we
// do not own the ArrayBuffer allocator. Zero fill is always on in that case.
const zeroFill = bindingObj.zeroFill || [0];

function createBuffer(size, noZeroFill) {
flags[kNoZeroFill] = noZeroFill ? 1 : 0;
try {
const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
} finally {
flags[kNoZeroFill] = 0;
}
if (noZeroFill)
zeroFill[0] = 0; // Reset by the runtime.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why drop kNoZeroFill?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the constant? No real reason except that it's a single-element array now.

const ui8 = new Uint8Array(size);
Object.setPrototypeOf(ui8, Buffer.prototype);
return ui8;
Copy link
Member

@ChALkeR ChALkeR Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was try-finally dropped here? It breaks things.

}

function createPool() {
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const binding = process.binding('util');
const prefix = `(${process.release.name}:${process.pid}) `;

const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];

exports.getHiddenValue = binding.getHiddenValue;
exports.setHiddenValue = binding.setHiddenValue;

Expand Down Expand Up @@ -65,14 +68,14 @@ exports._deprecate = function(fn, msg) {

exports.decorateErrorStack = function decorateErrorStack(err) {
if (!(exports.isError(err) && err.stack) ||
exports.getHiddenValue(err, 'node:decorated') === true)
exports.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
return;

const arrow = exports.getHiddenValue(err, 'node:arrowMessage');
const arrow = exports.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);

if (arrow) {
err.stack = arrow + err.stack;
exports.setHiddenValue(err, 'node:decorated', true);
exports.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
}
};

Expand Down
5 changes: 3 additions & 2 deletions src/debug-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ void Agent::WorkerRun() {
Isolate::Scope isolate_scope(isolate);

HandleScope handle_scope(isolate);
IsolateData isolate_data(isolate, &child_loop_,
array_buffer_allocator.zero_fill_field());
Local<Context> context = Context::New(isolate);

Context::Scope context_scope(context);
Environment* env = CreateEnvironment(
isolate,
&child_loop_,
&isolate_data,
context,
arraysize(argv),
argv,
Expand Down
94 changes: 20 additions & 74 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,6 @@

namespace node {

inline Environment::IsolateData* Environment::IsolateData::Get(
v8::Isolate* isolate) {
return static_cast<IsolateData*>(isolate->GetData(kIsolateSlot));
}

inline Environment::IsolateData* Environment::IsolateData::GetOrCreate(
v8::Isolate* isolate, uv_loop_t* loop) {
IsolateData* isolate_data = Get(isolate);
if (isolate_data == nullptr) {
isolate_data = new IsolateData(isolate, loop);
isolate->SetData(kIsolateSlot, isolate_data);
}
isolate_data->ref_count_ += 1;
return isolate_data;
}

inline void Environment::IsolateData::Put() {
if (--ref_count_ == 0) {
isolate()->SetData(kIsolateSlot, nullptr);
delete this;
}
}

// Create string properties as internalized one byte strings.
//
// Internalized because it makes property lookups a little faster and because
Expand All @@ -47,14 +24,13 @@ inline void Environment::IsolateData::Put() {
//
// One byte because our strings are ASCII and we can safely skip V8's UTF-8
// decoding step. It's a one-time cost, but why pay it when you don't have to?
inline Environment::IsolateData::IsolateData(v8::Isolate* isolate,
uv_loop_t* loop)
: event_loop_(loop),
isolate_(isolate),
inline IsolateData::IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop,
uint32_t* zero_fill_field)
:
#define V(PropertyName, StringValue) \
PropertyName ## _( \
isolate, \
v8::Private::ForApi( \
v8::Private::New( \
isolate, \
v8::String::NewFromOneByte( \
isolate, \
Expand All @@ -73,14 +49,15 @@ inline Environment::IsolateData::IsolateData(v8::Isolate* isolate,
sizeof(StringValue) - 1).ToLocalChecked()),
PER_ISOLATE_STRING_PROPERTIES(V)
#undef V
ref_count_(0) {}
isolate_(isolate), event_loop_(event_loop),
zero_fill_field_(zero_fill_field) {}

inline uv_loop_t* Environment::IsolateData::event_loop() const {
inline uv_loop_t* IsolateData::event_loop() const {
return event_loop_;
}

inline v8::Isolate* Environment::IsolateData::isolate() const {
return isolate_;
inline uint32_t* IsolateData::zero_fill_field() const {
return zero_fill_field_;
}

inline Environment::AsyncHooks::AsyncHooks() {
Expand Down Expand Up @@ -157,30 +134,9 @@ inline void Environment::TickInfo::set_index(uint32_t value) {
fields_[kIndex] = value;
}

inline Environment::ArrayBufferAllocatorInfo::ArrayBufferAllocatorInfo() {
for (int i = 0; i < kFieldsCount; ++i)
fields_[i] = 0;
}

inline uint32_t* Environment::ArrayBufferAllocatorInfo::fields() {
return fields_;
}

inline int Environment::ArrayBufferAllocatorInfo::fields_count() const {
return kFieldsCount;
}

inline bool Environment::ArrayBufferAllocatorInfo::no_zero_fill() const {
return fields_[kNoZeroFill] != 0;
}

inline void Environment::ArrayBufferAllocatorInfo::reset_fill_flag() {
fields_[kNoZeroFill] = 0;
}

inline Environment* Environment::New(v8::Local<v8::Context> context,
uv_loop_t* loop) {
Environment* env = new Environment(context, loop);
inline Environment* Environment::New(IsolateData* isolate_data,
v8::Local<v8::Context> context) {
Environment* env = new Environment(isolate_data, context);
env->AssignToContext(context);
return env;
}
Expand Down Expand Up @@ -214,11 +170,11 @@ inline Environment* Environment::GetCurrent(
return static_cast<Environment*>(data.As<v8::External>()->Value());
}

inline Environment::Environment(v8::Local<v8::Context> context,
uv_loop_t* loop)
inline Environment::Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context)
: isolate_(context->GetIsolate()),
isolate_data_(IsolateData::GetOrCreate(context->GetIsolate(), loop)),
timer_base_(uv_now(loop)),
isolate_data_(isolate_data),
timer_base_(uv_now(isolate_data->event_loop())),
using_domains_(false),
printed_error_(false),
trace_sync_io_(false),
Expand Down Expand Up @@ -255,7 +211,6 @@ inline Environment::~Environment() {
#define V(PropertyName, TypeName) PropertyName ## _.Reset();
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V
isolate_data()->Put();

delete[] heap_statistics_buffer_;
delete[] heap_space_statistics_buffer_;
Expand Down Expand Up @@ -348,11 +303,6 @@ inline Environment::TickInfo* Environment::tick_info() {
return &tick_info_;
}

inline Environment::ArrayBufferAllocatorInfo*
Environment::array_buffer_allocator_info() {
return &array_buffer_allocator_info_;
}

inline uint64_t Environment::timer_base() const {
return timer_base_;
}
Expand Down Expand Up @@ -432,7 +382,7 @@ inline ares_task_list* Environment::cares_task_list() {
return &cares_task_list_;
}

inline Environment::IsolateData* Environment::isolate_data() const {
inline IsolateData* Environment::isolate_data() const {
return isolate_data_;
}

Expand Down Expand Up @@ -543,9 +493,9 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
inline \
v8::Local<TypeName> Environment::IsolateData::PropertyName() const { \
v8::Local<TypeName> IsolateData::PropertyName(v8::Isolate* isolate) const { \
/* Strings are immutable so casting away const-ness here is okay. */ \
return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate()); \
return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate); \
}
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
Expand All @@ -557,7 +507,7 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
inline v8::Local<TypeName> Environment::PropertyName() const { \
return isolate_data()->PropertyName(); \
return isolate_data()->PropertyName(isolate()); \
}
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
Expand All @@ -575,10 +525,6 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V

#undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES
#undef PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES
#undef PER_ISOLATE_STRING_PROPERTIES

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
Loading