|
| 1 | +// Copyright 2019-2021 the Deno authors. All rights reserved. MIT license. |
| 2 | + |
| 3 | +use crate::support::Opaque; |
| 4 | + |
| 5 | +#[repr(C)] |
| 6 | +struct InternalIsolateGroup(Opaque); |
| 7 | + |
| 8 | +extern "C" { |
| 9 | + fn v8__IsolateGroup__GetDefault() -> *const InternalIsolateGroup; |
| 10 | + fn v8__IsolateGroup__CanCreateNewGroups() -> bool; |
| 11 | + fn v8__IsolateGroup__Create() -> *const InternalIsolateGroup; |
| 12 | + |
| 13 | + fn v8__IsolateGroup__DESTRUCT(this: *mut IsolateGroup); |
| 14 | + fn v8__IsolateGroup__EQ( |
| 15 | + this: *const IsolateGroup, |
| 16 | + other: *const IsolateGroup, |
| 17 | + ) -> bool; |
| 18 | +} |
| 19 | + |
| 20 | +/// The set of V8 isolates in a process is partitioned into groups. Each group |
| 21 | +/// has its own sandbox (if V8 was configured with support for the sandbox) and |
| 22 | +/// pointer-compression cage (if configured with pointer compression). |
| 23 | +/// |
| 24 | +/// By default, all isolates are placed in the same group. This is the most |
| 25 | +/// efficient configuration in terms of speed and memory use. However, with |
| 26 | +/// pointer compression enabled, total heap usage of isolates in a group cannot |
| 27 | +/// exceed 4 GB, not counting array buffers and other off-heap storage. Using |
| 28 | +/// multiple isolate groups can allow embedders to allocate more than 4GB of |
| 29 | +/// objects with pointer compression enabled, if the embedder's use case can |
| 30 | +/// span multiple isolates. |
| 31 | +/// |
| 32 | +/// Creating an isolate group reserves a range of virtual memory addresses. A |
| 33 | +/// group's memory mapping will be released when the last isolate in the group |
| 34 | +/// is disposed, and there are no more live IsolateGroup objects that refer to |
| 35 | +/// it. |
| 36 | +/// |
| 37 | +/// Note that Isolate groups are reference counted, and the IsolateGroup type is |
| 38 | +/// a reference to one. |
| 39 | +/// |
| 40 | +/// Note that it's not going to be possible to pass shared JS objects across |
| 41 | +/// IsolateGroup boundary. |
| 42 | +#[repr(C)] |
| 43 | +pub struct IsolateGroup(*const InternalIsolateGroup); |
| 44 | + |
| 45 | +unsafe impl Send for IsolateGroup {} |
| 46 | +unsafe impl Sync for IsolateGroup {} |
| 47 | + |
| 48 | +impl IsolateGroup { |
| 49 | + /// Return true if new isolate groups can be created at run-time, or false if |
| 50 | + /// all isolates must be in the same group. |
| 51 | + pub fn can_create_new_groups() -> bool { |
| 52 | + unsafe { v8__IsolateGroup__CanCreateNewGroups() } |
| 53 | + } |
| 54 | + |
| 55 | + /// Get the default isolate group. If this V8's build configuration only |
| 56 | + /// supports a single group, this is a reference to that single group. |
| 57 | + /// Otherwise this is a group like any other, distinguished only in that it is |
| 58 | + /// the first group. |
| 59 | + pub fn get_default() -> Self { |
| 60 | + IsolateGroup(unsafe { v8__IsolateGroup__GetDefault() }) |
| 61 | + } |
| 62 | + |
| 63 | + /// Create a new isolate group. If this V8's build configuration only supports |
| 64 | + /// a single group, abort. |
| 65 | + pub fn create() -> Self { |
| 66 | + IsolateGroup(unsafe { v8__IsolateGroup__Create() }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl Default for IsolateGroup { |
| 71 | + fn default() -> Self { |
| 72 | + IsolateGroup::get_default() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Drop for IsolateGroup { |
| 77 | + fn drop(&mut self) { |
| 78 | + unsafe { v8__IsolateGroup__DESTRUCT(self) } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Eq for IsolateGroup {} |
| 83 | + |
| 84 | +impl PartialEq for IsolateGroup { |
| 85 | + fn eq(&self, other: &Self) -> bool { |
| 86 | + unsafe { v8__IsolateGroup__EQ(self, other) } |
| 87 | + } |
| 88 | +} |
0 commit comments