Skip to content

Commit bdaf51b

Browse files
committedApr 30, 2021
src: allow custom PageAllocator in NodePlatform
For certain embedder use cases there are more complex memory allocation requirements that the default V8 page allocator does not handle. For example, using MAP_JIT when running under a hardened runtime environment on macOS. This allows embedders like Electron to provide their own allocator that does handle these cases. PR-URL: nodejs#38362 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 746cc88 commit bdaf51b

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed
 

‎src/api/environment.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ MultiIsolatePlatform* CreatePlatform(
465465
MultiIsolatePlatform* CreatePlatform(
466466
int thread_pool_size,
467467
v8::TracingController* tracing_controller) {
468-
return MultiIsolatePlatform::Create(thread_pool_size, tracing_controller)
468+
return MultiIsolatePlatform::Create(thread_pool_size,
469+
tracing_controller)
469470
.release();
470471
}
471472

@@ -475,8 +476,11 @@ void FreePlatform(MultiIsolatePlatform* platform) {
475476

476477
std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create(
477478
int thread_pool_size,
478-
v8::TracingController* tracing_controller) {
479-
return std::make_unique<NodePlatform>(thread_pool_size, tracing_controller);
479+
v8::TracingController* tracing_controller,
480+
v8::PageAllocator* page_allocator) {
481+
return std::make_unique<NodePlatform>(thread_pool_size,
482+
tracing_controller,
483+
page_allocator);
480484
}
481485

482486
MaybeLocal<Object> GetPerContextExports(Local<Context> context) {

‎src/node.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
310310

311311
static std::unique_ptr<MultiIsolatePlatform> Create(
312312
int thread_pool_size,
313-
v8::TracingController* tracing_controller = nullptr);
313+
v8::TracingController* tracing_controller = nullptr,
314+
v8::PageAllocator* page_allocator = nullptr);
314315
};
315316

316317
enum IsolateSettingsFlags {

‎src/node_platform.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,17 @@ void PerIsolatePlatformData::DecreaseHandleCount() {
324324
}
325325

326326
NodePlatform::NodePlatform(int thread_pool_size,
327-
v8::TracingController* tracing_controller) {
327+
v8::TracingController* tracing_controller,
328+
v8::PageAllocator* page_allocator) {
328329
if (tracing_controller != nullptr) {
329330
tracing_controller_ = tracing_controller;
330331
} else {
331332
tracing_controller_ = new v8::TracingController();
332333
}
334+
335+
// V8 will default to its built in allocator if none is provided.
336+
page_allocator_ = page_allocator;
337+
333338
// TODO(addaleax): It's a bit icky that we use global state here, but we can't
334339
// really do anything about it unless V8 starts exposing a way to access the
335340
// current v8::Platform instance.
@@ -550,6 +555,10 @@ Platform::StackTracePrinter NodePlatform::GetStackTracePrinter() {
550555
};
551556
}
552557

558+
v8::PageAllocator* NodePlatform::GetPageAllocator() {
559+
return page_allocator_;
560+
}
561+
553562
template <class T>
554563
TaskQueue<T>::TaskQueue()
555564
: lock_(), tasks_available_(), tasks_drained_(),

‎src/node_platform.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class WorkerThreadsTaskRunner {
138138
class NodePlatform : public MultiIsolatePlatform {
139139
public:
140140
NodePlatform(int thread_pool_size,
141-
v8::TracingController* tracing_controller);
141+
v8::TracingController* tracing_controller,
142+
v8::PageAllocator* page_allocator = nullptr);
142143
~NodePlatform() override;
143144

144145
void DrainTasks(v8::Isolate* isolate) override;
@@ -170,6 +171,7 @@ class NodePlatform : public MultiIsolatePlatform {
170171
v8::Isolate* isolate) override;
171172

172173
Platform::StackTracePrinter GetStackTracePrinter() override;
174+
v8::PageAllocator* GetPageAllocator() override;
173175

174176
private:
175177
IsolatePlatformDelegate* ForIsolate(v8::Isolate* isolate);
@@ -181,6 +183,7 @@ class NodePlatform : public MultiIsolatePlatform {
181183
std::unordered_map<v8::Isolate*, DelegatePair> per_isolate_;
182184

183185
v8::TracingController* tracing_controller_;
186+
v8::PageAllocator* page_allocator_;
184187
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
185188
bool has_shut_down_ = false;
186189
};

0 commit comments

Comments
 (0)
Please sign in to comment.