• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/port_platform.h>
18 #include <stddef.h>
19 
20 #include "absl/strings/string_view.h"
21 
22 namespace grpc_event_engine {
23 namespace experimental {
24 
25 /// Reservation request - how much memory do we want to allocate?
26 class MemoryRequest {
27  public:
28   /// Request a fixed amount of memory.
29   // NOLINTNEXTLINE(google-explicit-constructor)
MemoryRequest(size_t n)30   MemoryRequest(size_t n) : min_(n), max_(n) {}
31   /// Request a range of memory.
32   /// Requires: \a min <= \a max.
33   /// Requires: \a max <= max_size()
MemoryRequest(size_t min,size_t max)34   MemoryRequest(size_t min, size_t max) : min_(min), max_(max) {}
35 
36   /// Maximum allowable request size - hard coded to 1GB.
max_allowed_size()37   static constexpr size_t max_allowed_size() { return 1024 * 1024 * 1024; }
38 
39   /// Increase the size by \a amount.
40   /// Undefined behavior if min() + amount or max() + amount overflows.
Increase(size_t amount)41   MemoryRequest Increase(size_t amount) const {
42     return MemoryRequest(min_ + amount, max_ + amount);
43   }
44 
min()45   size_t min() const { return min_; }
max()46   size_t max() const { return max_; }
47 
48   bool operator==(const MemoryRequest& other) const {
49     return min_ == other.min_ && max_ == other.max_;
50   }
51   bool operator!=(const MemoryRequest& other) const {
52     return !(*this == other);
53   }
54 
55   template <typename Sink>
AbslStringify(Sink & s,const MemoryRequest & r)56   friend void AbslStringify(Sink& s, const MemoryRequest& r) {
57     if (r.min_ == r.max_) {
58       s.Append(r.min_);
59     } else {
60       s.Append(r.min_);
61       s.Append("..");
62       s.Append(r.max_);
63     }
64   }
65 
66  private:
67   size_t min_;
68   size_t max_;
69 };
70 
71 }  // namespace experimental
72 }  // namespace grpc_event_engine
73 
74 #endif  // GRPC_EVENT_ENGINE_MEMORY_REQUEST_H
75