heap_allocation_interface.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
  5. #define CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
  6. #include "common/error.h"
  7. #include "explorer/ast/address.h"
  8. #include "explorer/ast/value_node.h"
  9. #include "explorer/base/arena.h"
  10. #include "explorer/base/nonnull.h"
  11. #include "explorer/base/source_location.h"
  12. namespace Carbon {
  13. class Value;
  14. // The allocation interface for Heap, factored out as an interface in order to
  15. // resolve a layering issue. No other class should derive from this.
  16. class HeapAllocationInterface {
  17. public:
  18. HeapAllocationInterface(const HeapAllocationInterface&) = delete;
  19. auto operator=(const HeapAllocationInterface&)
  20. -> HeapAllocationInterface& = delete;
  21. // Returns the value at the given address in the heap after
  22. // checking that it is alive.
  23. virtual auto Read(const Address& a, SourceLocation source_loc) const
  24. -> ErrorOr<Nonnull<const Value*>> = 0;
  25. // Writes the given value at the address in the heap after
  26. // checking that the address is alive.
  27. virtual auto Write(const Address& a, Nonnull<const Value*> v,
  28. SourceLocation source_loc) -> ErrorOr<Success> = 0;
  29. // Put the given value on the heap and mark it as alive.
  30. virtual auto AllocateValue(Nonnull<const Value*> v) -> AllocationId = 0;
  31. // Marks this allocation, and all of its sub-objects, as dead.
  32. virtual auto Deallocate(AllocationId allocation) -> ErrorOr<Success> = 0;
  33. // Returns the arena used to allocate the values in this heap.
  34. virtual auto arena() const -> Arena& = 0;
  35. // Binds a value node to a reference, and manages its lifetime.
  36. virtual void BindValueToReference(const ValueNodeView& node,
  37. const Address& a) = 0;
  38. // Returns whether the value bound at the given node is still alive.
  39. virtual auto is_bound_value_alive(const ValueNodeView& node,
  40. const Address& a) const -> bool = 0;
  41. protected:
  42. HeapAllocationInterface() = default;
  43. virtual ~HeapAllocationInterface() = default;
  44. };
  45. } // namespace Carbon
  46. #endif // CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_