// 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_COMMON_ARENA_H_ #define EXECUTABLE_SEMANTICS_COMMON_ARENA_H_ #include #include #include #include "executable_semantics/common/nonnull.h" namespace Carbon { class Arena { public: // Allocates an object in the arena, returning a pointer to it. template < typename T, typename... Args, typename std::enable_if_t>* = nullptr> auto New(Args&&... args) -> Nonnull { auto smart_ptr = std::make_unique>(std::forward(args)...); Nonnull ptr = smart_ptr->Instance(); arena_.push_back(std::move(smart_ptr)); return ptr; } private: // Virtualizes arena entries so that a single vector can contain many types, // avoiding templated statics. class ArenaEntry { public: virtual ~ArenaEntry() = default; }; // Templated destruction of a pointer. template class ArenaEntryTyped : public ArenaEntry { public: template explicit ArenaEntryTyped(Args&&... args) : instance_(std::forward(args)...) {} auto Instance() -> Nonnull { return Nonnull(&instance_); } private: T instance_; }; // Manages allocations in an arena for destruction at shutdown. std::vector> arena_; }; } // namespace Carbon #endif // EXECUTABLE_SEMANTICS_COMMON_ARENA_H_