memory_request.h 1.8 KB

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