blob: 5fc93eb39f4eaa5eaa9ecc50318760d4cec9b1f9 [file] [log] [blame]
Nandor Licker950b70d2019-09-13 09:46:16 +00001//===--- 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 "Block.h"
14#include "Pointer.h"
15
16using namespace clang;
17using namespace clang::interp;
18
19
20
21void Block::addPointer(Pointer *P) {
22 if (IsStatic)
23 return;
24 if (Pointers)
25 Pointers->Prev = P;
26 P->Next = Pointers;
27 P->Prev = nullptr;
28 Pointers = P;
29}
30
31void Block::removePointer(Pointer *P) {
32 if (IsStatic)
33 return;
34 if (Pointers == P)
35 Pointers = P->Next;
36 if (P->Prev)
37 P->Prev->Next = P->Next;
38 if (P->Next)
39 P->Next->Prev = P->Prev;
40}
41
42void Block::cleanup() {
43 if (Pointers == nullptr && IsDead)
44 (reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
45}
46
47void Block::movePointer(Pointer *From, Pointer *To) {
48 if (IsStatic)
49 return;
50 To->Prev = From->Prev;
51 if (To->Prev)
52 To->Prev->Next = To;
53 To->Next = From->Next;
54 if (To->Next)
55 To->Next->Prev = To;
56 if (Pointers == From)
57 Pointers = To;
58
59 From->Prev = nullptr;
60 From->Next = nullptr;
61}
62
63DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
64 : Root(Root), B(Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {
65 // Add the block to the chain of dead blocks.
66 if (Root)
67 Root->Prev = this;
68
69 Next = Root;
70 Prev = nullptr;
71 Root = this;
72
73 // Transfer pointers.
74 B.Pointers = Blk->Pointers;
75 for (Pointer *P = Blk->Pointers; P; P = P->Next)
76 P->Pointee = &B;
77}
78
79void DeadBlock::free() {
80 if (Prev)
81 Prev->Next = Next;
82 if (Next)
83 Next->Prev = Prev;
84 if (Root == this)
85 Root = Next;
86 ::free(this);
87}