memory_allocator.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <stdint.h>
18 #include <stdlib.h>
19 
20 #include <memory>
21 #include <new>
22 #include <utility>
23 
26 #include <grpc/slice.h>
27 
29 
30 namespace grpc_event_engine {
31 namespace experimental {
32 
33 namespace {
34 
35 // Reference count for a slice allocated by MemoryAllocator::MakeSlice.
36 // Takes care of releasing memory back when the slice is destroyed.
37 class SliceRefCount : public grpc_slice_refcount {
38  public:
39  SliceRefCount(std::shared_ptr<internal::MemoryAllocatorImpl> allocator,
40  size_t size)
42  allocator_(std::move(allocator)),
43  size_(size) {
44  // Nothing to do here.
45  }
46  ~SliceRefCount() { allocator_->Release(size_); }
47 
48  private:
49  static void Destroy(grpc_slice_refcount* p) {
50  auto* rc = static_cast<SliceRefCount*>(p);
51  rc->~SliceRefCount();
52  free(rc);
53  }
54 
55  std::shared_ptr<internal::MemoryAllocatorImpl> allocator_;
56  size_t size_;
57 };
58 
59 } // namespace
60 
62  auto size = Reserve(request.Increase(sizeof(SliceRefCount)));
63  void* p = malloc(size);
64  new (p) SliceRefCount(allocator_, size);
66  slice.refcount = static_cast<SliceRefCount*>(p);
68  static_cast<uint8_t*>(p) + sizeof(SliceRefCount);
69  slice.data.refcounted.length = size - sizeof(SliceRefCount);
70  return slice;
71 }
72 
73 } // namespace experimental
74 } // namespace grpc_event_engine
grpc_event_engine::experimental::MemoryAllocator::Reserve
size_t Reserve(MemoryRequest request)
Definition: memory_allocator.h:66
grpc_slice::refcount
struct grpc_slice_refcount * refcount
Definition: include/grpc/impl/codegen/slice.h:66
slice.h
grpc_slice::grpc_slice_data::grpc_slice_refcounted::length
size_t length
Definition: include/grpc/impl/codegen/slice.h:69
benchmark.request
request
Definition: benchmark.py:77
allocator_
std::shared_ptr< internal::MemoryAllocatorImpl > allocator_
Definition: memory_allocator.cc:55
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
slice_refcount_base.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_event_engine::experimental::MemoryAllocator::MakeSlice
grpc_slice MakeSlice(MemoryRequest request)
Definition: memory_allocator.cc:61
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc_slice_refcount
Definition: slice_refcount_base.h:25
grpc_event_engine::experimental::MemoryAllocator::allocator_
std::shared_ptr< internal::MemoryAllocatorImpl > allocator_
Definition: memory_allocator.h:183
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
memory_request.h
stdint.h
grpc_slice::grpc_slice_data::refcounted
struct grpc_slice::grpc_slice_data::grpc_slice_refcounted refcounted
phony_transport::Destroy
void Destroy(grpc_transport *)
Definition: bm_call_create.cc:443
size_
size_t size_
Definition: memory_allocator.cc:56
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_event_engine
Definition: endpoint_config.h:24
grpc_slice::data
union grpc_slice::grpc_slice_data data
grpc_slice::grpc_slice_data::grpc_slice_refcounted::bytes
uint8_t * bytes
Definition: include/grpc/impl/codegen/slice.h:70
memory_allocator.h
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc_event_engine::experimental::MemoryRequest
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:27
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:35