2020-10-15 09:03:15 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_CPPGC_EPHEMERON_PAIR_H_
|
|
|
|
#define INCLUDE_CPPGC_EPHEMERON_PAIR_H_
|
|
|
|
|
2021-02-12 15:57:42 +00:00
|
|
|
#include "cppgc/liveness-broker.h"
|
2020-10-15 09:03:15 +00:00
|
|
|
#include "cppgc/member.h"
|
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An ephemeron pair is used to conditionally retain an object.
|
2020-10-19 08:12:55 +00:00
|
|
|
* The `value` will be kept alive only if the `key` is alive.
|
2020-10-15 09:03:15 +00:00
|
|
|
*/
|
|
|
|
template <typename K, typename V>
|
|
|
|
struct EphemeronPair {
|
|
|
|
EphemeronPair(K* k, V* v) : key(k), value(v) {}
|
|
|
|
WeakMember<K> key;
|
|
|
|
Member<V> value;
|
2021-02-12 15:57:42 +00:00
|
|
|
|
|
|
|
void ClearValueIfKeyIsDead(const LivenessBroker& broker) {
|
|
|
|
if (!broker.IsHeapObjectAlive(key)) value = nullptr;
|
|
|
|
}
|
2020-10-15 09:03:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_CPPGC_EPHEMERON_PAIR_H_
|