Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / src / core / lib / gprpp / arena.h
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 // \file Arena based allocator
20 // Allows very fast allocation of memory, but that memory cannot be freed until
21 // the arena as a whole is freed
22 // Tracks the total memory allocated against it, so that future arenas can
23 // pre-allocate the right amount of memory
24
25 #ifndef GRPC_CORE_LIB_GPRPP_ARENA_H
26 #define GRPC_CORE_LIB_GPRPP_ARENA_H
27
28 #include <grpc/support/port_platform.h>
29
30 #include <new>
31 #include <utility>
32
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/sync.h>
35
36 #include "src/core/lib/gpr/alloc.h"
37 #include "src/core/lib/gpr/spinlock.h"
38 #include "src/core/lib/gprpp/atomic.h"
39 #include "src/core/lib/gprpp/pair.h"
40
41 #include <stddef.h>
42
43 namespace grpc_core {
44
45 class Arena {
46  public:
47   // Create an arena, with \a initial_size bytes in the first allocated buffer.
48   static Arena* Create(size_t initial_size);
49
50   // Create an arena, with \a initial_size bytes in the first allocated buffer,
51   // and return both a void pointer to the returned arena and a void* with the
52   // first allocation.
53   static Pair<Arena*, void*> CreateWithAlloc(size_t initial_size,
54                                              size_t alloc_size);
55
56   // Destroy an arena, returning the total number of bytes allocated.
57   size_t Destroy();
58   // Allocate \a size bytes from the arena.
59   void* Alloc(size_t size) {
60     static constexpr size_t base_size =
61         GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena));
62     size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(size);
63     size_t begin = total_used_.FetchAdd(size, MemoryOrder::RELAXED);
64     if (begin + size <= initial_zone_size_) {
65       return reinterpret_cast<char*>(this) + base_size + begin;
66     } else {
67       return AllocZone(size);
68     }
69   }
70
71   // TODO(roth): We currently assume that all callers need alignment of 16
72   // bytes, which may be wrong in some cases. When we have time, we should
73   // change this to instead use the alignment of the type being allocated by
74   // this method.
75   template <typename T, typename... Args>
76   T* New(Args&&... args) {
77     T* t = static_cast<T*>(Alloc(sizeof(T)));
78     new (t) T(std::forward<Args>(args)...);
79     return t;
80   }
81
82  private:
83   struct Zone {
84     Zone* prev;
85   };
86
87   // Initialize an arena.
88   // Parameters:
89   //   initial_size: The initial size of the whole arena in bytes. These bytes
90   //   are contained within 'zone 0'. If the arena user ends up requiring more
91   //   memory than the arena contains in zone 0, subsequent zones are allocated
92   //   on demand and maintained in a tail-linked list.
93   //
94   //   initial_alloc: Optionally, construct the arena as though a call to
95   //   Alloc() had already been made for initial_alloc bytes. This provides a
96   //   quick optimization (avoiding an atomic fetch-add) for the common case
97   //   where we wish to create an arena and then perform an immediate
98   //   allocation.
99   explicit Arena(size_t initial_size, size_t initial_alloc = 0)
100       : total_used_(initial_alloc), initial_zone_size_(initial_size) {}
101
102   ~Arena();
103
104   void* AllocZone(size_t size);
105
106   // Keep track of the total used size. We use this in our call sizing
107   // hysteresis.
108   Atomic<size_t> total_used_;
109   size_t initial_zone_size_;
110   gpr_spinlock arena_growth_spinlock_ = GPR_SPINLOCK_STATIC_INITIALIZER;
111   // If the initial arena allocation wasn't enough, we allocate additional zones
112   // in a reverse linked list. Each additional zone consists of (1) a pointer to
113   // the zone added before this zone (null if this is the first additional zone)
114   // and (2) the allocated memory. The arena itself maintains a pointer to the
115   // last zone; the zone list is reverse-walked during arena destruction only.
116   Zone* last_zone_ = nullptr;
117 };
118
119 }  // namespace grpc_core
120
121 #endif /* GRPC_CORE_LIB_GPRPP_ARENA_H */