3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
25 #ifndef GRPC_CORE_LIB_GPRPP_ARENA_H
26 #define GRPC_CORE_LIB_GPRPP_ARENA_H
28 #include <grpc/support/port_platform.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/sync.h>
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"
47 // Create an arena, with \a initial_size bytes in the first allocated buffer.
48 static Arena* Create(size_t initial_size);
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
53 static Pair<Arena*, void*> CreateWithAlloc(size_t initial_size,
56 // Destroy an arena, returning the total number of bytes allocated.
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;
67 return AllocZone(size);
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
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)...);
87 // Initialize an arena.
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.
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
99 explicit Arena(size_t initial_size, size_t initial_alloc = 0)
100 : total_used_(initial_alloc), initial_zone_size_(initial_size) {}
104 void* AllocZone(size_t size);
106 // Keep track of the total used size. We use this in our call sizing
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;
119 } // namespace grpc_core
121 #endif /* GRPC_CORE_LIB_GPRPP_ARENA_H */