GRPC C++  1.78.1
memory_request.h
Go to the documentation of this file.
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 
18 #include <stddef.h>
19 
20 #include <string>
21 
22 #include "absl/strings/string_view.h"
23 
24 namespace grpc_event_engine {
25 namespace experimental {
26 
29  public:
31  // NOLINTNEXTLINE(google-explicit-constructor)
32  MemoryRequest(size_t n) : min_(n), max_(n) {}
36  MemoryRequest(size_t min, size_t max) : min_(min), max_(max) {}
37 
39  static constexpr size_t max_allowed_size() { return 1024 * 1024 * 1024; }
40 
43  MemoryRequest Increase(size_t amount) const {
44  return MemoryRequest(min_ + amount, max_ + amount);
45  }
46 
47  size_t min() const { return min_; }
48  size_t max() const { return max_; }
49 
50  bool operator==(const MemoryRequest& other) const {
51  return min_ == other.min_ && max_ == other.max_;
52  }
53  bool operator!=(const MemoryRequest& other) const {
54  return !(*this == other);
55  }
56 
57  template <typename Sink>
58  friend void AbslStringify(Sink& s, const MemoryRequest& r) {
59  if (r.min_ == r.max_) {
60  s.Append(std::to_string(r.min_));
61  } else {
62  s.Append(std::to_string(r.min_));
63  s.Append("..");
64  s.Append(std::to_string(r.max_));
65  }
66  }
67 
68  private:
69  size_t min_;
70  size_t max_;
71 };
72 
73 } // namespace experimental
74 } // namespace grpc_event_engine
75 
76 #endif // GRPC_EVENT_ENGINE_MEMORY_REQUEST_H
grpc_event_engine::experimental::MemoryRequest::max
size_t max() const
Definition: memory_request.h:48
grpc_event_engine::experimental::MemoryRequest::MemoryRequest
MemoryRequest(size_t min, size_t max)
Request a range of memory.
Definition: memory_request.h:36
grpc_event_engine::experimental::MemoryRequest::operator!=
bool operator!=(const MemoryRequest &other) const
Definition: memory_request.h:53
grpc_event_engine::experimental::MemoryRequest
Reservation request - how much memory do we want to allocate?
Definition: memory_request.h:28
grpc_event_engine::experimental::MemoryRequest::Increase
MemoryRequest Increase(size_t amount) const
Increase the size by amount.
Definition: memory_request.h:43
grpc_event_engine::experimental::MemoryRequest::AbslStringify
friend void AbslStringify(Sink &s, const MemoryRequest &r)
Definition: memory_request.h:58
grpc_event_engine::experimental::MemoryRequest::operator==
bool operator==(const MemoryRequest &other) const
Definition: memory_request.h:50
grpc_event_engine::experimental::MemoryRequest::min
size_t min() const
Definition: memory_request.h:47
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::MemoryRequest::MemoryRequest
MemoryRequest(size_t n)
Request a fixed amount of memory.
Definition: memory_request.h:32
grpc_event_engine::experimental::MemoryRequest::max_allowed_size
static constexpr size_t max_allowed_size()
Maximum allowable request size - hard coded to 1GB.
Definition: memory_request.h:39
port_platform.h