heap.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_
  6. #include <vector>
  7. #include "common/ostream.h"
  8. #include "executable_semantics/interpreter/address.h"
  9. #include "executable_semantics/interpreter/value.h"
  10. namespace Carbon {
  11. // A Heap represents the abstract machine's dynamically allocated memory.
  12. class Heap {
  13. public:
  14. // Constructs an empty Heap.
  15. Heap() = default;
  16. Heap(const Heap&) = delete;
  17. Heap& operator=(const Heap&) = delete;
  18. // Returns the value at the given address in the heap after
  19. // checking that it is alive.
  20. auto Read(const Address& a, int line_num) -> const Value*;
  21. // Writes the given value at the address in the heap after
  22. // checking that the address is alive.
  23. void Write(const Address& a, const Value* v, int line_num);
  24. // Put the given value on the heap and mark it as alive.
  25. auto AllocateValue(const Value* v) -> Address;
  26. // Marks the object at this address, and all of its sub-objects, as dead.
  27. void Deallocate(const Address& address);
  28. // Print the value at the given address to the stream `out`.
  29. void PrintAddress(const Address& a, llvm::raw_ostream& out) const;
  30. // Print all the values on the heap to the stream `out`.
  31. void Print(llvm::raw_ostream& out) const;
  32. private:
  33. // Signal an error if the address is no longer alive.
  34. void CheckAlive(const Address& address, int line_num);
  35. std::vector<const Value*> values_;
  36. std::vector<bool> alive_;
  37. };
  38. } // namespace Carbon
  39. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_MEMORY_H_