InterpBlock.cpp 1.9 KB

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