cpp

Coverage Report

Created: 2023-09-13 01:07

/home/andy/git/oilshell/oil/mycpp/gc_obj.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef MYCPP_GC_OBJ_H
2
#define MYCPP_GC_OBJ_H
3
4
#include <stdint.h>  // uint8_t
5
6
namespace HeapTag {
7
const int Global = 0;     // Don't mark or sweep.
8
const int Opaque = 1;     // e.g. List<int>, Str
9
                          // Mark and sweep, but don't trace children
10
const int FixedSize = 2;  // Consult field_mask for children
11
const int Scanned = 3;    // Scan a contiguous range of children
12
};                        // namespace HeapTag
13
14
// These tags are mainly for debugging.  Oils is a statically typed program, so
15
// we don't need runtime types in general.
16
// This "enum" starts from the end of the valid type_tag range.
17
// asdl/gen_cpp.py starts from 1 for variants, or 64 for shared variants.
18
namespace TypeTag {
19
const int OtherClass = 127;  // non-ASDL class
20
const int Str = 126;         // asserted in dynamic StrFormat()
21
const int Slab = 125;
22
const int Tuple = 124;
23
const int List = 123;
24
const int Dict = 122;
25
};  // namespace TypeTag
26
27
const int kNotInPool = 0;
28
const int kInPool = 1;
29
30
const unsigned kZeroMask = 0;  // for types with no pointers
31
32
const int kMaxObjId = (1 << 28) - 1;  // 28 bits means 512 Mi objects per pool
33
const int kIsGlobal = kMaxObjId;      // for debugging, not strictly needed
34
35
const int kUndefinedId = 0;  // Uninitialized object ID
36
37
// Every GC-managed object is preceded in memory by an ObjHeader.
38
// TODO: ./configure could detect endian-ness, and reorder the fields in
39
// ObjHeader.  See mycpp/demo/gc_header.cc.
40
struct ObjHeader {
41
  unsigned type_tag : 8;  // TypeTag, ASDL variant / shared variant
42
  // Depending on heap_tag, up to 24 fields or 2**24 = 16 Mi pointers to scan
43
  unsigned u_mask_npointers : 24;
44
45
  unsigned heap_tag : 2;  // HeapTag::Opaque, etc.
46
  unsigned pool_id : 2;   // 0 for malloc(), or 1 2 3 for fixed sized pools
47
  unsigned obj_id : 28;   // 1 Gi unique objects
48
49
  // Returns the address of the GC managed object associated with this header.
50
  // Note: this relies on there being no padding between the header and the
51
  // object. See Alloc<T>() and GcGlobal<T> for relevant static_assert()s.
52
8.20k
  void* ObjectAddress() {
53
8.20k
    return reinterpret_cast<void*>(reinterpret_cast<char*>(this) +
54
8.20k
                                   sizeof(ObjHeader));
55
8.20k
  }
56
57
  // Returns the header for the given GC managed object.
58
  // Note: this relies on there being no padding between the header and the
59
  // object. See Alloc<T>() and GcGlobal<T> for relevant static_assert()s.
60
708
  static ObjHeader* FromObject(const void* obj) {
61
708
    return reinterpret_cast<ObjHeader*>(
62
708
        static_cast<char*>(const_cast<void*>(obj)) - sizeof(ObjHeader));
63
708
  }
64
65
  // Used by hand-written and generated classes
66
1.30k
  static constexpr ObjHeader ClassFixed(uint32_t field_mask, uint32_t obj_len) {
67
1.30k
    return {TypeTag::OtherClass, field_mask, HeapTag::FixedSize, kNotInPool,
68
1.30k
            kUndefinedId};
69
1.30k
  }
70
71
  // Classes with no inheritance (e.g. used by mycpp)
72
  static constexpr ObjHeader ClassScanned(uint32_t num_pointers,
73
65
                                          uint32_t obj_len) {
74
65
    return {TypeTag::OtherClass, num_pointers, HeapTag::Scanned, kNotInPool,
75
65
            kUndefinedId};
76
65
  }
77
78
  // Used by frontend/flag_gen.py.  TODO: Sort fields and use GC_CLASS_SCANNED
79
  static constexpr ObjHeader Class(uint8_t heap_tag, uint32_t field_mask,
80
0
                                   uint32_t obj_len) {
81
0
    return {TypeTag::OtherClass, field_mask, heap_tag, kNotInPool,
82
0
            kUndefinedId};
83
0
  }
84
85
  // Used by ASDL.
86
  static constexpr ObjHeader AsdlClass(uint8_t type_tag,
87
205
                                       uint32_t num_pointers) {
88
205
    return {type_tag, num_pointers, HeapTag::Scanned, kNotInPool, kUndefinedId};
89
205
  }
90
91
5.28k
  static constexpr ObjHeader Str() {
92
5.28k
    return {TypeTag::Str, kZeroMask, HeapTag::Opaque, kNotInPool, kUndefinedId};
93
5.28k
  }
94
95
1.19k
  static constexpr ObjHeader Slab(uint8_t heap_tag, uint32_t num_pointers) {
96
1.19k
    return {TypeTag::Slab, num_pointers, heap_tag, kNotInPool, kUndefinedId};
97
1.19k
  }
98
99
89
  static constexpr ObjHeader Tuple(uint32_t field_mask, uint32_t obj_len) {
100
89
    return {TypeTag::Tuple, field_mask, HeapTag::FixedSize, kNotInPool,
101
89
            kUndefinedId};
102
89
  }
103
104
  // Used by GLOBAL_STR, GLOBAL_LIST, GLOBAL_DICT
105
147
  static constexpr ObjHeader Global(uint8_t type_tag) {
106
147
    return {type_tag, kZeroMask, HeapTag::Global, kNotInPool, kIsGlobal};
107
147
  }
108
};
109
110
58
#define FIELD_MASK(header) (header).u_mask_npointers
111
10
#define NUM_POINTERS(header) (header).u_mask_npointers
112
113
// A RawObject* is like a void*. We use it to represent GC managed objects.
114
struct RawObject;
115
116
//
117
// Compile-time computation of GC field masks.
118
//
119
120
// maskbit(field_offset) returns a bit in mask that you can bitwise-or (|) with
121
// other bits.
122
//
123
// - Note that we only call maskbit() on offsets of pointer fields, which must
124
//   be POINTER-ALIGNED.
125
126
1.48k
constexpr int maskbit(size_t offset) {
127
1.48k
  return 1 << (offset / sizeof(void*));
128
1.48k
}
129
130
// A wrapper for a GC object and its header. For creating global GC objects,
131
// like GlobalStr.
132
// TODO: Make this more ergonomic by automatically initializing header
133
// with T::obj_header() and providing a forwarding constructor for obj.
134
template <typename T>
135
class GcGlobalImpl {
136
 public:
137
  ObjHeader header;
138
  T obj;
139
140
  // This class only exists to write the static_assert. If you try to put the
141
  // static_assert directly in the outer class you get a compiler error that
142
  // taking the offsets is an 'invalid use of incomplete type'. Doing it this
143
  // way means the type gets completed before the assert.
144
  struct Internal {
145
    using type = GcGlobalImpl<T>;
146
    static_assert(offsetof(type, obj) - sizeof(ObjHeader) ==
147
                      offsetof(type, header),
148
                  "ObjHeader doesn't fit");
149
  };
150
151
  DISALLOW_COPY_AND_ASSIGN(GcGlobalImpl);
152
};
153
154
// Refer to `Internal::type` to force Internal to be instantiated.
155
template <typename T>
156
using GcGlobal = typename GcGlobalImpl<T>::Internal::type;
157
158
// The "homogeneous" layout of objects with HeapTag::FixedSize.  LayoutFixed is
159
// for casting; it isn't a real type.
160
161
// TODO: we could determine the max of all objects statically!
162
const int kFieldMaskBits = 24;
163
164
struct LayoutFixed {
165
  // only the entries denoted in field_mask will be valid
166
  RawObject* children_[kFieldMaskBits];
167
};
168
169
#endif  // MYCPP_GC_OBJ_H