cpp

Coverage Report

Created: 2022-11-10 11:34

/home/andy/git/oilshell/oil/mycpp/gc_slab.h
Line
Count
Source
1
#ifndef GC_SLAB_H
2
#define GC_SLAB_H
3
4
// Return the size of a resizeable allocation.  For now we just round up by
5
// powers of 2. This could be optimized later.  CPython has an interesting
6
// policy in listobject.c.
7
//
8
// https://stackoverflow.com/questions/466204/rounding-up-to-next-power-of-2
9
1.05k
inline int RoundUp(int n) {
10
  // minimum size
11
1.05k
  if (n < 8) {
12
482
    return 8;
13
482
  }
14
15
  // TODO: what if int isn't 32 bits?
16
572
  n--;
17
572
  n |= n >> 1;
18
572
  n |= n >> 2;
19
572
  n |= n >> 4;
20
572
  n |= n >> 8;
21
572
  n |= n >> 16;
22
572
  n++;
23
572
  return n;
24
1.05k
}
25
26
// don't include items_[1]
27
const int kSlabHeaderSize = sizeof(Obj);
28
29
// Opaque slab, e.g. for List<int>
30
template <typename T>
31
class Slab : public Obj {
32
 public:
33
  // slabs of pointers are scanned; slabs of ints/bools are opaque.
34
  explicit Slab(int obj_len)
35
      : Obj(std::is_pointer<T>() ? Tag::Scanned : Tag::Opaque, kZeroMask,
36
541
            obj_len) {
37
541
  }
_ZN4SlabIP3StrEC2Ei
Line
Count
Source
36
118
            obj_len) {
37
118
  }
_ZN4SlabIiEC2Ei
Line
Count
Source
36
381
            obj_len) {
37
381
  }
_ZN4SlabIbEC2Ei
Line
Count
Source
36
3
            obj_len) {
37
3
  }
_ZN4SlabIdEC2Ei
Line
Count
Source
36
2
            obj_len) {
37
2
  }
_ZN4SlabIP6Tuple2IP3StriEEC2Ei
Line
Count
Source
36
1
            obj_len) {
37
1
  }
_ZN4SlabIP6Tuple2IiP3StrEEC2Ei
Line
Count
Source
36
2
            obj_len) {
37
2
  }
_ZN4SlabIPN10hnode_asdl5fieldEEC2Ei
Line
Count
Source
36
34
            obj_len) {
37
34
  }
Unexecuted instantiation: _ZN4SlabIPN10hnode_asdl7hnode_tEEC2Ei
Unexecuted instantiation: _ZN4SlabIPN12runtime_asdl7value_tEEC2Ei
Unexecuted instantiation: _ZN4SlabIPN4args7_ActionEEC2Ei
38
  T items_[1];  // variable length
39
};
40
41
template <typename T, int N>
42
class GlobalSlab {
43
  // A template type with the same layout as Str with length N-1 (which needs a
44
  // buffer of size N).  For initializing global constant instances.
45
 public:
46
  OBJ_HEADER()
47
48
  T items_[N];
49
50
  DISALLOW_COPY_AND_ASSIGN(GlobalSlab)
51
};
52
53
// Note: entries will be zero'd because the Heap is zero'd.
54
template <typename T>
55
541
inline Slab<T>* NewSlab(int len) {
56
541
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
541
  void* place = gHeap.Allocate(obj_len);
58
541
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
541
  return slab;
60
541
}
_Z7NewSlabIP3StrEP4SlabIT_Ei
Line
Count
Source
55
118
inline Slab<T>* NewSlab(int len) {
56
118
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
118
  void* place = gHeap.Allocate(obj_len);
58
118
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
118
  return slab;
60
118
}
_Z7NewSlabIiEP4SlabIT_Ei
Line
Count
Source
55
381
inline Slab<T>* NewSlab(int len) {
56
381
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
381
  void* place = gHeap.Allocate(obj_len);
58
381
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
381
  return slab;
60
381
}
_Z7NewSlabIbEP4SlabIT_Ei
Line
Count
Source
55
3
inline Slab<T>* NewSlab(int len) {
56
3
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
3
  void* place = gHeap.Allocate(obj_len);
58
3
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
3
  return slab;
60
3
}
_Z7NewSlabIdEP4SlabIT_Ei
Line
Count
Source
55
2
inline Slab<T>* NewSlab(int len) {
56
2
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
2
  void* place = gHeap.Allocate(obj_len);
58
2
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
2
  return slab;
60
2
}
_Z7NewSlabIP6Tuple2IP3StriEEP4SlabIT_Ei
Line
Count
Source
55
1
inline Slab<T>* NewSlab(int len) {
56
1
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
1
  void* place = gHeap.Allocate(obj_len);
58
1
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
1
  return slab;
60
1
}
_Z7NewSlabIP6Tuple2IiP3StrEEP4SlabIT_Ei
Line
Count
Source
55
2
inline Slab<T>* NewSlab(int len) {
56
2
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
2
  void* place = gHeap.Allocate(obj_len);
58
2
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
2
  return slab;
60
2
}
_Z7NewSlabIPN10hnode_asdl5fieldEEP4SlabIT_Ei
Line
Count
Source
55
34
inline Slab<T>* NewSlab(int len) {
56
34
  int obj_len = RoundUp(kSlabHeaderSize + len * sizeof(T));
57
34
  void* place = gHeap.Allocate(obj_len);
58
34
  auto slab = new (place) Slab<T>(obj_len);  // placement new
59
34
  return slab;
60
34
}
Unexecuted instantiation: _Z7NewSlabIPN10hnode_asdl7hnode_tEEP4SlabIT_Ei
Unexecuted instantiation: _Z7NewSlabIPN12runtime_asdl7value_tEEP4SlabIT_Ei
Unexecuted instantiation: _Z7NewSlabIPN4args7_ActionEEP4SlabIT_Ei
61
62
#endif  // GC_SLAB_H