clang  19.0.0git
InterpBlock.cpp
Go to the documentation of this file.
1 //===--- Block.cpp - Allocated blocks for the interpreter -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the classes describing allocated blocks.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "InterpBlock.h"
14 #include "Pointer.h"
15 
16 using namespace clang;
17 using namespace clang::interp;
18 
20  assert(P);
21  if (IsStatic) {
22  assert(!Pointers);
23  return;
24  }
25 
26 #ifndef NDEBUG
27  assert(!hasPointer(P));
28 #endif
29  if (Pointers)
30  Pointers->Prev = P;
31  P->Next = Pointers;
32  P->Prev = nullptr;
33  Pointers = P;
34 }
35 
37  assert(P);
38  if (IsStatic) {
39  assert(!Pointers);
40  return;
41  }
42 
43 #ifndef NDEBUG
44  assert(hasPointer(P));
45 #endif
46 
47  if (Pointers == P)
48  Pointers = P->Next;
49 
50  if (P->Prev)
51  P->Prev->Next = P->Next;
52  if (P->Next)
53  P->Next->Prev = P->Prev;
54 }
55 
57  if (Pointers == nullptr && IsDead)
58  (reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
59 }
60 
62  assert(Old);
63  assert(New);
64  if (IsStatic) {
65  assert(!Pointers);
66  return;
67  }
68 
69 #ifndef NDEBUG
70  assert(hasPointer(Old));
71 #endif
72 
73  removePointer(Old);
74  addPointer(New);
75 
76  Old->PointeeStorage.BS.Pointee = nullptr;
77 
78 #ifndef NDEBUG
79  assert(!hasPointer(Old));
80  assert(hasPointer(New));
81 #endif
82 }
83 
84 #ifndef NDEBUG
85 bool Block::hasPointer(const Pointer *P) const {
86  for (const Pointer *C = Pointers; C; C = C->Next) {
87  if (C == P)
88  return true;
89  }
90  return false;
91 }
92 #endif
93 
95  : Root(Root), B(Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {
96  // Add the block to the chain of dead blocks.
97  if (Root)
98  Root->Prev = this;
99 
100  Next = Root;
101  Prev = nullptr;
102  Root = this;
103 
104  // Transfer pointers.
105  B.Pointers = Blk->Pointers;
106  for (Pointer *P = Blk->Pointers; P; P = P->Next)
107  P->PointeeStorage.BS.Pointee = &B;
108 }
109 
110 void DeadBlock::free() {
111  if (Prev)
112  Prev->Next = Next;
113  if (Next)
114  Next->Prev = Prev;
115  if (Root == this)
116  Root = Next;
117  std::free(this);
118 }
StringRef P
bool IsStatic
Definition: Format.cpp:2988
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
void addPointer(Pointer *P)
Pointer chain management.
Definition: InterpBlock.cpp:19
void replacePointer(Pointer *Old, Pointer *New)
Definition: InterpBlock.cpp:61
bool hasPointer(const Pointer *P) const
Definition: InterpBlock.cpp:85
void removePointer(Pointer *P)
Definition: InterpBlock.cpp:36
void cleanup()
Deletes a dead block at the end of its lifetime.
Definition: InterpBlock.cpp:56
bool IsStatic
Flag indicating if the block has static storage duration.
Definition: InterpBlock.h:152
Pointer * Pointers
Start of the chain of pointers.
Definition: InterpBlock.h:148
bool IsDead
Flag indicating if the pointer is dead.
Definition: InterpBlock.h:157
Descriptor for a dead block.
Definition: InterpBlock.h:169
DeadBlock(DeadBlock *&Root, Block *Blk)
Copies the block.
Definition: InterpBlock.cpp:94
A pointer to a memory block, live or dead.
Definition: Pointer.h:80
BlockPointer BS
Definition: Pointer.h:659
The JSON file list parser is used to communicate input to InstallAPI.
#define true
Definition: stdbool.h:25
Block * Pointee
The block the pointer is pointing to.
Definition: Pointer.h:39