From dea277d8e1f776ac1f1740be9347409c0d62453c Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Thu, 28 Oct 2021 10:01:24 -0700 Subject: [PATCH] Factor allocation interface out of `Heap` (#918) This will enable `Action` to deallocate from the heap without creating a dependency cycle. --- executable_semantics/interpreter/BUILD | 12 +++++++ executable_semantics/interpreter/heap.h | 10 +++--- .../interpreter/heap_allocation_interface.h | 36 +++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 executable_semantics/interpreter/heap_allocation_interface.h diff --git a/executable_semantics/interpreter/BUILD b/executable_semantics/interpreter/BUILD index f9d8ba44a2b2..0a760e301a61 100644 --- a/executable_semantics/interpreter/BUILD +++ b/executable_semantics/interpreter/BUILD @@ -75,11 +75,23 @@ cc_library( deps = [ ":action_and_value", ":address", + ":heap_allocation_interface", "//common:ostream", + "//executable_semantics/ast:source_location", + "//executable_semantics/common:nonnull", "@llvm-project//llvm:Support", ], ) +cc_library( + name = "heap_allocation_interface", + hdrs = ["heap_allocation_interface.h"], + deps = [ + ":address", + "//executable_semantics/common:nonnull", + ], +) + cc_library( name = "interpreter", srcs = [ diff --git a/executable_semantics/interpreter/heap.h b/executable_semantics/interpreter/heap.h index 43a863b21074..c7eb111a2482 100644 --- a/executable_semantics/interpreter/heap.h +++ b/executable_semantics/interpreter/heap.h @@ -8,14 +8,16 @@ #include #include "common/ostream.h" +#include "executable_semantics/ast/source_location.h" +#include "executable_semantics/common/nonnull.h" #include "executable_semantics/interpreter/address.h" +#include "executable_semantics/interpreter/heap_allocation_interface.h" #include "executable_semantics/interpreter/value.h" -#include "llvm/Support/Compiler.h" namespace Carbon { // A Heap represents the abstract machine's dynamically allocated memory. -class Heap { +class Heap : public HeapAllocationInterface { public: // Constructs an empty Heap. explicit Heap(Nonnull arena) : arena_(arena){}; @@ -34,10 +36,10 @@ class Heap { SourceLocation source_loc); // Put the given value on the heap and mark it as alive. - auto AllocateValue(Nonnull v) -> AllocationId; + auto AllocateValue(Nonnull v) -> AllocationId override; // Marks this allocation, and all of its sub-objects, as dead. - void Deallocate(AllocationId allocation); + void Deallocate(AllocationId allocation) override; // Print the value at the given allocation to the stream `out`. void PrintAllocation(AllocationId allocation, llvm::raw_ostream& out) const; diff --git a/executable_semantics/interpreter/heap_allocation_interface.h b/executable_semantics/interpreter/heap_allocation_interface.h new file mode 100644 index 000000000000..dfa849fcbffe --- /dev/null +++ b/executable_semantics/interpreter/heap_allocation_interface.h @@ -0,0 +1,36 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef EXECUTABLE_SEMANTICS_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_ +#define EXECUTABLE_SEMANTICS_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_ + +#include "executable_semantics/common/nonnull.h" +#include "executable_semantics/interpreter/address.h" + +namespace Carbon { + +class Value; + +// The allocation interface for Heap, factored out as an interface in order to +// resolve a layering issue. No other class should derive from this. +class HeapAllocationInterface { + public: + HeapAllocationInterface(const HeapAllocationInterface&) = delete; + auto operator=(const HeapAllocationInterface&) + -> HeapAllocationInterface& = delete; + + // Put the given value on the heap and mark it as alive. + virtual auto AllocateValue(Nonnull v) -> AllocationId = 0; + + // Marks this allocation, and all of its sub-objects, as dead. + virtual void Deallocate(AllocationId allocation) = 0; + + protected: + HeapAllocationInterface() = default; + virtual ~HeapAllocationInterface() = default; +}; + +} // namespace Carbon + +#endif // EXECUTABLE_SEMANTICS_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_