File: tagged-member.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2025 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_TAGGED_MEMBER_H_
#define INCLUDE_CPPGC_TAGGED_MEMBER_H_

#include <atomic>
#include <concepts>
#include <cstddef>
#include <type_traits>

#include "cppgc/internal/api-constants.h"
#include "cppgc/macros.h"
#include "cppgc/member.h"
#include "cppgc/visitor.h"

namespace cppgc::subtle {

// The class allows to store a Member along with a single bit tag. It uses
// distinct tag types, Tag1 and Tag2, to represent the two states of the tag.
// The tag is stored in the least significant bit of the pointer.
//
// Example usage:
//   struct ParentTag {};
//   struct ShadowHostTag {};
//
//   /* Constructs a member with the pointer to parent tag: */
//   TaggedUncompressedMember<Node, ParentTag, ShadowHostTag>
//       m(ParentTag{}, parent);
template <typename Pointee, typename Tag1, typename Tag2>
class TaggedUncompressedMember final {
  CPPGC_DISALLOW_NEW();
  static constexpr uintptr_t kTagBit = 0b1;
  static_assert(kTagBit < internal::api_constants::kAllocationGranularity,
                "The tag must live in the alignment bits of the pointer.");

 public:
  TaggedUncompressedMember(Tag1, Pointee* ptr) : ptr_(ptr) {}
  TaggedUncompressedMember(Tag2, Pointee* ptr)
      : ptr_(reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(ptr) |
                                        kTagBit)) {}

  template <typename Tag>
  Pointee* GetAs() const {
    auto* raw = ptr_.Get();
    if constexpr (std::same_as<Tag, Tag1>) {
      CPPGC_DCHECK(Is<Tag1>());
      return raw;
    } else {
      static_assert(std::same_as<Tag, Tag2>);
      CPPGC_DCHECK(Is<Tag2>());
      return GetUntagged();
    }
  }

  template <typename Tag>
  Pointee* TryGetAs() const {
    auto* raw = ptr_.Get();
    if constexpr (std::same_as<Tag, Tag1>) {
      return (reinterpret_cast<uintptr_t>(raw) & kTagBit) ? nullptr : raw;
    } else {
      static_assert(std::same_as<Tag, Tag2>);
      return (reinterpret_cast<uintptr_t>(raw) & kTagBit)
                 ? reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(raw) &
                                              ~kTagBit)
                 : nullptr;
    }
  }

  Pointee* GetUntagged() const {
    return reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(ptr_.Get()) &
                                      ~kTagBit);
  }

  template <typename Tag>
  void SetAs(Pointee* pointee) {
    if constexpr (std::same_as<Tag, Tag1>) {
      ptr_ = pointee;
    } else {
      static_assert(std::same_as<Tag, Tag2>);
      ptr_ = reinterpret_cast<Pointee*>(reinterpret_cast<uintptr_t>(pointee) |
                                        kTagBit);
    }
  }

  template <typename Tag>
  bool Is() const {
    const bool tag_set = reinterpret_cast<uintptr_t>(ptr_.Get()) & kTagBit;
    if constexpr (std::same_as<Tag, Tag1>) {
      return !tag_set;
    } else {
      static_assert(std::same_as<Tag, Tag2>);
      return tag_set;
    }
  }

  void Trace(Visitor* v) const {
    // Construct an untagged pointer and pass it to Visitor::Trace(). The plugin
    // would warn that ptr_ is untraced, which is why CPPGC_PLUGIN_IGNORE is
    // used.
    auto* untagged = reinterpret_cast<Pointee*>(
        reinterpret_cast<uintptr_t>(ptr_.GetRawAtomic()) & ~kTagBit);
    UncompressedMember<Pointee> temp(untagged);
    v->Trace(temp);
  }

 private:
  CPPGC_PLUGIN_IGNORE("See Trace()") UncompressedMember<Pointee> ptr_;
};

}  // namespace cppgc::subtle

#endif  // INCLUDE_CPPGC_TAGGED_MEMBER_H_