Skip to content

Commit

Permalink
fix: let cppgc align to 16 bytes (#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek authored Jan 6, 2025
1 parent 25900a3 commit 8020991
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 153 deletions.
2 changes: 1 addition & 1 deletion examples/cppgc-object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
v8::V8::initialize_platform(platform.clone());
v8::V8::initialize();

v8::cppgc::initalize_process(platform.clone());
v8::cppgc::initialize_process(platform.clone());

{
let heap =
Expand Down
2 changes: 1 addition & 1 deletion examples/cppgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn main() {
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform.clone());
v8::V8::initialize();
v8::cppgc::initalize_process(platform.clone());
v8::cppgc::initialize_process(platform.clone());

{
// Create a managed heap.
Expand Down
16 changes: 13 additions & 3 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3976,9 +3976,19 @@ void cppgc__heap__collect_garbage_for_testing(
heap->CollectGarbageForTesting(stack_state);
}

RustObj* cppgc__make_garbage_collectable(v8::CppHeap* heap, size_t size) {
return cppgc::MakeGarbageCollected<RustObj>(heap->GetAllocationHandle(),
cppgc::AdditionalBytes(size));
class alignas(16) RustObjButAlign16 : public RustObj {};

RustObj* cppgc__make_garbage_collectable(v8::CppHeap* heap, size_t size,
size_t alignment) {
if (alignment <= 8) {
return cppgc::MakeGarbageCollected<RustObj>(heap->GetAllocationHandle(),
cppgc::AdditionalBytes(size));
}
if (alignment <= 16) {
return cppgc::MakeGarbageCollected<RustObjButAlign16>(
heap->GetAllocationHandle(), cppgc::AdditionalBytes(size));
}
return nullptr;
}

void cppgc__Visitor__Trace__Member(cppgc::Visitor* visitor,
Expand Down
13 changes: 11 additions & 2 deletions src/cppgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
fn cppgc__make_garbage_collectable(
heap: *mut Heap,
size: usize,
alignment: usize,
) -> *mut RustObj;

fn cppgc__heap__enable_detached_garbage_collections_for_testing(
Expand Down Expand Up @@ -145,7 +146,7 @@ unsafe fn get_object_from_rust_obj<T: GarbageCollected>(
/// creating a Heap.
///
/// Can be called multiple times when paired with `ShutdownProcess()`.
pub fn initalize_process(platform: SharedRef<Platform>) {
pub fn initialize_process(platform: SharedRef<Platform>) {
unsafe {
cppgc__initialize_process(&*platform as *const Platform as *mut _);
}
Expand All @@ -155,7 +156,7 @@ pub fn initalize_process(platform: SharedRef<Platform>) {
///
/// Must be called after destroying the last used heap. Some process-global
/// metadata may not be returned and reused upon a subsequent
/// `initalize_process()` call.
/// `initialize_process()` call.
pub unsafe fn shutdown_process() {
cppgc__shutdown_process();
}
Expand Down Expand Up @@ -343,6 +344,11 @@ pub unsafe fn make_garbage_collected<T: GarbageCollected + 'static>(
heap: &Heap,
obj: T,
) -> Ptr<T> {
const {
// max alignment in cppgc is 16
assert!(std::mem::align_of::<T>() <= 16);
}

let additional_bytes = (object_offset_for_rust_obj::<T>()
- std::mem::size_of::<RustObj>())
+ std::mem::size_of::<T>();
Expand All @@ -351,9 +357,12 @@ pub unsafe fn make_garbage_collected<T: GarbageCollected + 'static>(
cppgc__make_garbage_collectable(
heap as *const Heap as *mut _,
additional_bytes,
std::mem::align_of::<T>(),
)
};

assert!(!pointer.is_null());

unsafe {
let inner = get_object_from_rust_obj::<T>(pointer);
inner.write(obj);
Expand Down
4 changes: 2 additions & 2 deletions src/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ struct three_pointers_t {

#endif // SUPPORT_H_

class RustObj final : public cppgc::GarbageCollected<RustObj>,
public cppgc::NameProvider {
class RustObj : public cppgc::GarbageCollected<RustObj>,
public cppgc::NameProvider {
public:
~RustObj();
void Trace(cppgc::Visitor* visitor) const;
Expand Down
Loading

0 comments on commit 8020991

Please sign in to comment.