memory_allocator_impl.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_INTERNAL_MEMORY_ALLOCATOR_IMPL_H
  15. #define GRPC_EVENT_ENGINE_INTERNAL_MEMORY_ALLOCATOR_IMPL_H
  16. #include <grpc/impl/codegen/port_platform.h>
  17. #include <algorithm>
  18. #include <memory>
  19. #include <type_traits>
  20. #include <vector>
  21. #include <grpc/event_engine/memory_request.h>
  22. #include <grpc/slice.h>
  23. namespace grpc_event_engine {
  24. namespace experimental {
  25. namespace internal {
  26. /// Underlying memory allocation interface.
  27. /// This is an internal interface, not intended to be used by users.
  28. /// Its interface is subject to change at any time.
  29. class MemoryAllocatorImpl
  30. : public std::enable_shared_from_this<MemoryAllocatorImpl> {
  31. public:
  32. MemoryAllocatorImpl() {}
  33. virtual ~MemoryAllocatorImpl() {}
  34. MemoryAllocatorImpl(const MemoryAllocatorImpl&) = delete;
  35. MemoryAllocatorImpl& operator=(const MemoryAllocatorImpl&) = delete;
  36. /// Reserve bytes from the quota.
  37. /// If we enter overcommit, reclamation will begin concurrently.
  38. /// Returns the number of bytes reserved.
  39. /// If MemoryRequest is invalid, this function will abort.
  40. /// If MemoryRequest is valid, this function is infallible, and will always
  41. /// succeed at reserving the some number of bytes between request.min() and
  42. /// request.max() inclusively.
  43. virtual size_t Reserve(MemoryRequest request) = 0;
  44. /// Release some bytes that were previously reserved.
  45. /// If more bytes are released than were reserved, we will have undefined
  46. /// behavior.
  47. virtual void Release(size_t n) = 0;
  48. /// Shutdown this allocator.
  49. /// Further usage of Reserve() is undefined behavior.
  50. virtual void Shutdown() = 0;
  51. };
  52. } // namespace internal
  53. } // namespace experimental
  54. } // namespace grpc_event_engine
  55. #endif // GRPC_EVENT_ENGINE_INTERNAL_MEMORY_ALLOCATOR_IMPL_H