Skip to content

Commit

Permalink
Pass the pointer of owning object in GCPointer::set() part I (faceboo…
Browse files Browse the repository at this point in the history
…k#1502)

Summary: Pull Request resolved: facebook#1502

Differential Revision: D62222257
  • Loading branch information
lavenzg authored and facebook-github-bot committed Nov 8, 2024
1 parent 858949f commit 65d3797
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
30 changes: 20 additions & 10 deletions include/hermes/VM/GCPointer-inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,31 @@ GCPointerBase::GCPointerBase(
}
}

inline void GCPointerBase::set(PointerBase &base, GCCell *ptr, GC &gc) {
template <typename MaybeLargeObj, typename NonNull>
inline void GCPointerBase::setImpl(
PointerBase &base,
GCCell *ptr,
GC &gc,
const GCCell *owningObj) {
assert(
(!ptr || gc.validPointer(ptr)) &&
"Cannot set a GCPointer to an invalid pointer");
// Write barrier must happen before the write.
gc.writeBarrier(this, ptr);
setNoBarrier(CompressedPointer::encode(ptr, base));
}
(void)owningObj;
if constexpr (MaybeLargeObj::value) {
assert(
owningObj &&
"The cell pointer must be provided for cell kind that supports large allocation");

inline void GCPointerBase::setNonNull(PointerBase &base, GCCell *ptr, GC &gc) {
assert(
gc.validPointer(ptr) && "Cannot set a GCPointer to an invalid pointer");
// Write barrier must happen before the write.
gc.writeBarrier(this, ptr);
setNoBarrier(CompressedPointer::encodeNonNull(ptr, base));
gc.writeBarrier(this, ptr);
} else {
gc.writeBarrier(this, ptr);
}
if constexpr (NonNull::value) {
setNoBarrier(CompressedPointer::encodeNonNull(ptr, base));
} else {
setNoBarrier(CompressedPointer::encode(ptr, base));
}
}

inline void
Expand Down
49 changes: 45 additions & 4 deletions include/hermes/VM/GCPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,45 @@ class GCPointerBase : public CompressedPointer {
class NoBarriers : public std::false_type {};
class YesBarriers : public std::true_type {};

/// This must be used to assign a new value to this GCPointer.
/// This must be used to assign a new value to this GCPointer. This must not
/// be used if it lives in an object that supports large allocation.
/// \param ptr The memory being pointed to.
/// \param base The base of ptr.
/// \param gc Used for write barriers.
inline void set(PointerBase &base, GCCell *ptr, GC &gc);
inline void set(PointerBase &base, GCCell *ptr, GC &gc) {
setImpl<std::false_type, std::false_type>(base, ptr, gc, nullptr);
}
inline void set(PointerBase &base, CompressedPointer ptr, GC &gc);
inline void setNonNull(PointerBase &base, GCCell *ptr, GC &gc);
inline void setNonNull(PointerBase &base, GCCell *ptr, GC &gc) {
setImpl<std::false_type, std::true_type>(base, ptr, gc, nullptr);
}

/// This must be used to assign a new value to this GCPointer.
/// \param ptr The memory being pointed to.
/// \param base The base of ptr.
/// \param gc Used for write barriers.
/// \param owningObj The object that contains this GCPointer, used by the
/// writer barriers.
inline void
set(PointerBase &base, GCCell *ptr, GC &gc, const GCCell *owningObj) {
setImpl<std::true_type, std::false_type>(base, ptr, gc, owningObj);
}
inline void
setNonNull(PointerBase &base, GCCell *ptr, GC &gc, const GCCell *owningObj) {
setImpl<std::true_type, std::true_type>(base, ptr, gc, owningObj);
}

/// Set this pointer to null. This needs a write barrier in some types of
/// garbage collectors.
inline void setNull(GC &gc);

private:
/// Implementation details shared by all set* functions here.
/// \tparam MaybeLargeObj Whether the owning object supports large allocation.
/// If it is false, \p owningObj is not used by the writer barriers.
/// \tparam NonNull Whether \p is non null.
template <typename MaybeLargeObj, typename NonNull>
void setImpl(PointerBase &base, GCCell *ptr, GC &gc, const GCCell *owningObj);
};

/// A class to represent "raw" pointers to heap objects. Disallows assignment,
Expand Down Expand Up @@ -86,7 +114,8 @@ class GCPointer : public GCPointerBase {
return vmcast<T>(GCPointerBase::getNonNull(base));
}

/// Assign a new value to this GCPointer.
/// Assign a new value to this GCPointer. This must not be used if it lives in
/// an object that supports large allocation.
/// \param base The base of ptr.
/// \param ptr The memory being pointed to.
/// \param gc Used for write barriers.
Expand All @@ -97,6 +126,18 @@ class GCPointer : public GCPointerBase {
GCPointerBase::setNonNull(base, ptr, gc);
}

/// Assign a new value to this GCPointer.
/// \param ptr The memory being pointed to.
/// \param gc Used for write barriers.
/// \param owningObj The object that contains this GCPointer, used by the
/// writer barriers.
void set(PointerBase &base, T *ptr, GC &gc, const GCCell *owningObj) {
GCPointerBase::set(base, ptr, gc, owningObj);
}
void setNonNull(PointerBase &base, T *ptr, GC &gc, const GCCell *owningObj) {
GCPointerBase::setNonNull(base, ptr, gc, owningObj);
}

/// Convenience overload of GCPointer::set for other GCPointers.
void set(PointerBase &base, const GCPointer<T> &ptr, GC &gc) {
GCPointerBase::set(base, ptr, gc);
Expand Down

0 comments on commit 65d3797

Please sign in to comment.