forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena.h
58 lines (46 loc) · 1.57 KB
/
arena.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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 CARBON_EXPLORER_COMMON_ARENA_H_
#define CARBON_EXPLORER_COMMON_ARENA_H_
#include <memory>
#include <type_traits>
#include <vector>
#include "explorer/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<std::is_constructible_v<T, Args...>>* = nullptr>
auto New(Args&&... args) -> Nonnull<T*> {
auto smart_ptr =
std::make_unique<ArenaEntryTyped<T>>(std::forward<Args>(args)...);
Nonnull<T*> 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 <typename T>
class ArenaEntryTyped : public ArenaEntry {
public:
template <typename... Args>
explicit ArenaEntryTyped(Args&&... args)
: instance_(std::forward<Args>(args)...) {}
auto Instance() -> Nonnull<T*> { return Nonnull<T*>(&instance_); }
private:
T instance_;
};
// Manages allocations in an arena for destruction at shutdown.
std::vector<std::unique_ptr<ArenaEntry>> arena_;
};
} // namespace Carbon
#endif // CARBON_EXPLORER_COMMON_ARENA_H_