Skip to content

Commit

Permalink
Pass the pointer of owning object in ctor of GCPointer (facebook#1503)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: facebook#1503

Differential Revision: D62227037
  • Loading branch information
lavenzg authored and facebook-github-bot committed Nov 8, 2024
1 parent f6503cc commit b14797a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions include/hermes/VM/GCPointer-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ GCPointerBase::GCPointerBase(
}
}

template <typename NeedsBarriers>
GCPointerBase::GCPointerBase(
PointerBase &base,
GCCell *ptr,
GC &gc,
const GCCell *owningObj,
NeedsBarriers)
: CompressedPointer(CompressedPointer::encode(ptr, base)) {
assert(
(!ptr || gc.validPointer(ptr)) &&
"Cannot construct a GCPointer from an invalid pointer");
(void)owningObj;
if constexpr (NeedsBarriers::value) {
gc.constructorWriteBarrier(this, ptr);
} else {
assert(!gc.needsWriteBarrier(this, ptr));
}
}

template <typename MaybeLargeObj, typename NonNull>
inline void GCPointerBase::setImpl(
PointerBase &base,
Expand Down
24 changes: 23 additions & 1 deletion include/hermes/VM/GCPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class GCPointerBase : public CompressedPointer {
template <typename NeedsBarriers>
inline GCPointerBase(PointerBase &base, GCCell *ptr, GC &gc, NeedsBarriers);

template <typename NeedsBarriers>
inline GCPointerBase(
PointerBase &base,
GCCell *ptr,
GC &gc,
const GCCell *owningObj,
NeedsBarriers);

public:
// These classes are used as arguments to GCPointer constructors, to
// indicate whether write barriers are necessary in initializing the
Expand Down Expand Up @@ -97,12 +105,26 @@ class GCPointer : public GCPointerBase {
template <typename NeedsBarriers>
GCPointer(PointerBase &base, T *ptr, GC &gc, NeedsBarriers needsBarriers)
: GCPointerBase(base, ptr, gc, needsBarriers) {}
/// Pass the owning object pointer to perform barriers when the object
/// supports large allocation.
template <typename NeedsBarriers>
GCPointer(
PointerBase &base,
T *ptr,
GC &gc,
const GCCell *owningObj,
NeedsBarriers needsBarriers)
: GCPointerBase(base, ptr, gc, owningObj, needsBarriers) {}

/// Same as the constructor above, with the default for
/// NeedsBarriers as "YesBarriers". (We can't use default template
/// arguments with the idiom used above.)
inline GCPointer(PointerBase &base, T *ptr, GC &gc)
GCPointer(PointerBase &base, T *ptr, GC &gc)
: GCPointer<T>(base, ptr, gc, YesBarriers()) {}
/// Pass the owning object pointer to perform barriers when the object
/// supports large allocation.
GCPointer(PointerBase &base, T *ptr, GC &gc, const GCCell *owningObj)
: GCPointer<T>(base, ptr, gc, owningObj, YesBarriers()) {}

/// We are not allowed to copy-construct or assign GCPointers.
GCPointer(const GCPointerBase &) = delete;
Expand Down

0 comments on commit b14797a

Please sign in to comment.