Debian Codegraph @

src/core/lib/resource_quota/arena.cc

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/resource_quota/arena.h"
22
23 #include <atomic>
24 #include <new>
25
26 #include <grpc/support/alloc.h>
27
28 #include "src/core/lib/gpr/alloc.h"
29
30 namespace {
31
32 void* ArenaStorage(size_t initial_size) {
33 static constexpr size_t base_size =
34 GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_core::Arena));
35 initial_size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(initial_size);
36 size_t alloc_size = base_size + initial_size;
37 static constexpr size_t alignment =
38 (GPR_CACHELINE_SIZE > GPR_MAX_ALIGNMENT &&
39 GPR_CACHELINE_SIZE % GPR_MAX_ALIGNMENT == 0)
40 ? GPR_CACHELINE_SIZE
41 : GPR_MAX_ALIGNMENT;
42 return gpr_malloc_aligned(alloc_size, alignment);
43 }
44
45 } // namespace
46
47 namespace grpc_core {
48
49 Arena::~Arena() {
50 Zone* z = last_zone_;
51 while (z) {
52 Zone* prev_z = z->prev;
53 Destruct(z);
54 gpr_free_aligned(z);
55 z = prev_z;
56 }
57 }
58
59 Arena* Arena::Create(size_t initial_size, MemoryAllocator* memory_allocator) {
60 return new (ArenaStorage(initial_size))
61 Arena(initial_size, 0, memory_allocator);
62 }
63
64 std::pair<Arena*, void*> Arena::CreateWithAlloc(
65 size_t initial_size, size_t alloc_size, MemoryAllocator* memory_allocator) {
66 static constexpr size_t base_size =
67 GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena));
68 auto* new_arena = new (ArenaStorage(initial_size))
69 Arena(initial_size, alloc_size, memory_allocator);
70 void* first_alloc = reinterpret_cast<char*>(new_arena) + base_size;
71 return std::make_pair(new_arena, first_alloc);
72 }
73
74 size_t Arena::Destroy() {
75 ManagedNewObject* p;
76 // Outer loop: clear the managed new object list.
77 // We do this repeatedly in case a destructor ends up allocating something.
78 while ((p = managed_new_head_.exchange(nullptr, std::memory_order_relaxed)) !=
79 nullptr) {
80 // Inner loop: destruct a batch of objects.
81 while (p != nullptr) {
82 Destruct(std::exchange(p, p->next));
83 }
84 }
85 size_t size = total_used_.load(std::memory_order_relaxed);
86 memory_allocator_->Release(total_allocated_.load(std::memory_order_relaxed));
87 this->~Arena();
88 gpr_free_aligned(this);
89 return size;
90 }
91
92 void* Arena::AllocZone(size_t size) {
93 // If the allocation isn't able to end in the initial zone, create a new
94 // zone for this allocation, and any unused space in the initial zone is
95 // wasted. This overflowing and wasting is uncommon because of our arena
96 // sizing hysteresis (that is, most calls should have a large enough initial
97 // zone and will not need to grow the arena).
98 static constexpr size_t zone_base_size =
99 GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Zone));
100 size_t alloc_size = zone_base_size + size;
101 memory_allocator_->Reserve(alloc_size);
102 total_allocated_.fetch_add(alloc_size, std::memory_order_relaxed);
103 Zone* z = new (gpr_malloc_aligned(alloc_size, GPR_MAX_ALIGNMENT)) Zone();
104 auto* prev = last_zone_.load(std::memory_order_relaxed);
105 do {
106 z->prev = prev;
107 } while (!last_zone_.compare_exchange_weak(prev, z, std::memory_order_relaxed,
108 std::memory_order_relaxed));
109 return reinterpret_cast<char*>(z) + zone_base_size;
110 }
111
112 void Arena::ManagedNewObject::Link(std::atomic<ManagedNewObject*>* head) {
113 next = head->load(std::memory_order_relaxed);
114 while (!head->compare_exchange_weak(next, this, std::memory_order_acq_rel,
115 std::memory_order_relaxed)) {
116 }
117 }
118
119 void* Arena::AllocPooled(size_t alloc_size, std::atomic<FreePoolNode*>* head) {
120 FreePoolNode* p = head->load(std::memory_order_acquire);
121 while (p != nullptr) {
122 if (head->compare_exchange_weak(p, p->next, std::memory_order_acq_rel,
123 std::memory_order_relaxed)) {
124 return p;
125 }
126 }
127 return Alloc(alloc_size);
128 }
129
130 void Arena::FreePooled(void* p, std::atomic<FreePoolNode*>* head) {
131 FreePoolNode* node = static_cast<FreePoolNode*>(p);
132 node->next = head->load(std::memory_order_acquire);
133 while (!head->compare_exchange_weak(
134 node->next, node, std::memory_order_acq_rel, std::memory_order_relaxed)) {
135 }
136 }
137
138 } // namespace grpc_core