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/gprpp/arena.h"
22 
23 #include <string.h>
24 #include <new>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/atm.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/sync.h>
30 
31 #include "src/core/lib/gpr/alloc.h"
32 #include "src/core/lib/gprpp/memory.h"
33 
34 namespace {
35 
ArenaStorage(size_t initial_size)36 void* ArenaStorage(size_t initial_size) {
37   static constexpr size_t base_size =
38       GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(grpc_core::Arena));
39   initial_size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(initial_size);
40   size_t alloc_size = base_size + initial_size;
41   static constexpr size_t alignment =
42       (GPR_CACHELINE_SIZE > GPR_MAX_ALIGNMENT &&
43        GPR_CACHELINE_SIZE % GPR_MAX_ALIGNMENT == 0)
44           ? GPR_CACHELINE_SIZE
45           : GPR_MAX_ALIGNMENT;
46   return gpr_malloc_aligned(alloc_size, alignment);
47 }
48 
49 }  // namespace
50 
51 namespace grpc_core {
52 
~Arena()53 Arena::~Arena() {
54   Zone* z = last_zone_;
55   while (z) {
56     Zone* prev_z = z->prev;
57     z->~Zone();
58     gpr_free_aligned(z);
59     z = prev_z;
60   }
61 }
62 
Create(size_t initial_size)63 Arena* Arena::Create(size_t initial_size) {
64   return new (ArenaStorage(initial_size)) Arena(initial_size);
65 }
66 
CreateWithAlloc(size_t initial_size,size_t alloc_size)67 std::pair<Arena*, void*> Arena::CreateWithAlloc(size_t initial_size,
68                                                 size_t alloc_size) {
69   static constexpr size_t base_size =
70       GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena));
71   auto* new_arena =
72       new (ArenaStorage(initial_size)) Arena(initial_size, alloc_size);
73   void* first_alloc = reinterpret_cast<char*>(new_arena) + base_size;
74   return std::make_pair(new_arena, first_alloc);
75 }
76 
Destroy()77 size_t Arena::Destroy() {
78   size_t size = total_used_.Load(MemoryOrder::RELAXED);
79   this->~Arena();
80   gpr_free_aligned(this);
81   return size;
82 }
83 
AllocZone(size_t size)84 void* Arena::AllocZone(size_t size) {
85   // If the allocation isn't able to end in the initial zone, create a new
86   // zone for this allocation, and any unused space in the initial zone is
87   // wasted. This overflowing and wasting is uncommon because of our arena
88   // sizing hysteresis (that is, most calls should have a large enough initial
89   // zone and will not need to grow the arena).
90   static constexpr size_t zone_base_size =
91       GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Zone));
92   size_t alloc_size = zone_base_size + size;
93   Zone* z = new (gpr_malloc_aligned(alloc_size, GPR_MAX_ALIGNMENT)) Zone();
94   {
95     gpr_spinlock_lock(&arena_growth_spinlock_);
96     z->prev = last_zone_;
97     last_zone_ = z;
98     gpr_spinlock_unlock(&arena_growth_spinlock_);
99   }
100   return reinterpret_cast<char*>(z) + zone_base_size;
101 }
102 
103 }  // namespace grpc_core
104