Skip to content

Commit d18b763

Browse files
lavenzgfacebook-github-bot
authored andcommitted
Pass the pointer of owning object in GCHermesValueBase::set() (facebook#1512)
Summary: Pull Request resolved: facebook#1512 This variant will be used by write barriers that support large allocation in following diffs. Differential Revision: D62196480
1 parent 34dd464 commit d18b763

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

include/hermes/VM/HermesValue-inline.h

+30
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ GCHermesValueBase<HVType>::GCHermesValueBase(HVType hv, GC &gc) : HVType{hv} {
3737
gc.constructorWriteBarrier(this, hv);
3838
}
3939

40+
template <typename HVType>
41+
template <typename NeedsBarriers>
42+
GCHermesValueBase<HVType>::GCHermesValueBase(
43+
HVType hv,
44+
GC &gc,
45+
const GCCell *owningObj)
46+
: HVType{hv} {
47+
assert(!hv.isPointer() || hv.getPointer());
48+
if (NeedsBarriers::value)
49+
gc.constructorWriteBarrierForLargeObj(owningObj, this, hv);
50+
}
51+
4052
template <typename HVType>
4153
template <typename NeedsBarriers>
4254
GCHermesValueBase<HVType>::GCHermesValueBase(HVType hv, GC &gc, std::nullptr_t)
@@ -61,6 +73,24 @@ inline void GCHermesValueBase<HVType>::set(HVType hv, GC &gc) {
6173
HVType::setNoBarrier(hv);
6274
}
6375

76+
template <typename HVType>
77+
template <typename NeedsBarriers>
78+
inline void GCHermesValueBase<HVType>::setInLarge(
79+
HVType hv,
80+
GC &gc,
81+
const GCCell *owningObj) {
82+
if (hv.isPointer()) {
83+
HERMES_SLOW_ASSERT(
84+
gc.validPointer(hv.getPointer(gc.getPointerBase())) &&
85+
"Setting an invalid pointer into a GCHermesValue");
86+
}
87+
assert(NeedsBarriers::value || !gc.needsWriteBarrier(this, hv));
88+
if constexpr (NeedsBarriers::value) {
89+
gc.writeBarrierForLargeObj(owningObj, this, hv);
90+
}
91+
HVType::setNoBarrier(hv);
92+
}
93+
6494
template <typename HVType>
6595
void GCHermesValueBase<HVType>::setNonPtr(HVType hv, GC &gc) {
6696
assert(!hv.isPointer());

include/hermes/VM/HermesValue.h

+17-3
Original file line numberDiff line numberDiff line change
@@ -520,21 +520,35 @@ template <typename HVType>
520520
class GCHermesValueBase final : public HVType {
521521
public:
522522
GCHermesValueBase() : HVType(HVType::encodeUndefinedValue()) {}
523-
/// Initialize a GCHermesValue from another HV. Performs a write barrier.
523+
/// Initialize a GCHermesValue from another HV. Performs a write barrier. This
524+
/// must not be used if it lives in an object that supports large allocation.
524525
template <typename NeedsBarriers = std::true_type>
525526
GCHermesValueBase(HVType hv, GC &gc);
527+
/// Initialize a GCHermesValue from another HV. Performs a write barrier using
528+
/// \p owningObj, which owns this GCHermesValue and may support large
529+
/// allocation.
530+
template <typename NeedsBarriers = std::true_type>
531+
GCHermesValueBase(HVType hv, GC &gc, const GCCell *owningObj);
526532
/// Initialize a GCHermesValue from a non-pointer HV. Might perform a write
527533
/// barrier, depending on the GC.
528534
/// NOTE: The last parameter is unused, but acts as an overload selector.
529535
template <typename NeedsBarriers = std::true_type>
530536
GCHermesValueBase(HVType hv, GC &gc, std::nullptr_t);
531537
GCHermesValueBase(const HVType &) = delete;
532538

533-
/// The HermesValue \p hv may be an object pointer. Assign the
534-
/// value, and perform any necessary write barriers.
539+
/// The HermesValue \p hv may be an object pointer. Assign the value, and
540+
/// perform any necessary write barriers. This must not be used if it lives in
541+
/// an object that supports large allocation.
535542
template <typename NeedsBarriers = std::true_type>
536543
inline void set(HVType hv, GC &gc);
537544

545+
/// The HermesValue \p hv may be an object pointer. Assign the value, and
546+
/// perform any necessary write barriers. \p owningObj is the object that
547+
/// contains this GCHermesValueBase, and it may support large allocation.
548+
/// for which the object pointer is needed by writer barriers.
549+
template <typename NeedsBarriers = std::true_type>
550+
inline void setInLarge(HVType hv, GC &gc, const GCCell *owningObj);
551+
538552
/// The HermesValue \p hv must not be an object pointer. Assign the
539553
/// value.
540554
/// Some GCs still need to do a write barrier though, so pass a GC parameter.

0 commit comments

Comments
 (0)