xref: /aosp_15_r20/external/grpc-grpc/include/grpc/event_engine/memory_request.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2021 The 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 #ifndef GRPC_EVENT_ENGINE_MEMORY_REQUEST_H
15 #define GRPC_EVENT_ENGINE_MEMORY_REQUEST_H
16 
17 #include <stddef.h>
18 
19 #include "absl/strings/string_view.h"
20 
21 #include <grpc/support/port_platform.h>
22 
23 namespace grpc_event_engine {
24 namespace experimental {
25 
26 /// Reservation request - how much memory do we want to allocate?
27 class MemoryRequest {
28  public:
29   /// Request a fixed amount of memory.
30   // NOLINTNEXTLINE(google-explicit-constructor)
MemoryRequest(size_t n)31   MemoryRequest(size_t n) : min_(n), max_(n) {}
32   /// Request a range of memory.
33   /// Requires: \a min <= \a max.
34   /// Requires: \a max <= max_size()
MemoryRequest(size_t min,size_t max)35   MemoryRequest(size_t min, size_t max) : min_(min), max_(max) {}
36 
37   /// Maximum allowable request size - hard coded to 1GB.
max_allowed_size()38   static constexpr size_t max_allowed_size() { return 1024 * 1024 * 1024; }
39 
40   /// Increase the size by \a amount.
41   /// Undefined behavior if min() + amount or max() + amount overflows.
Increase(size_t amount)42   MemoryRequest Increase(size_t amount) const {
43     return MemoryRequest(min_ + amount, max_ + amount);
44   }
45 
min()46   size_t min() const { return min_; }
max()47   size_t max() const { return max_; }
48 
49  private:
50   size_t min_;
51   size_t max_;
52 };
53 
54 }  // namespace experimental
55 }  // namespace grpc_event_engine
56 
57 #endif  // GRPC_EVENT_ENGINE_MEMORY_REQUEST_H
58