resource_interface.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_
  5. #define COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_
  6. #include <cstdint>
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace reporting {
  11. // Interface to resources management by Storage module.
  12. // Must be implemented by the caller base on the platform limitations.
  13. // All APIs are non-blocking.
  14. class ResourceInterface : public base::RefCountedThreadSafe<ResourceInterface> {
  15. public:
  16. // Needs to be called before attempting to allocate specified size.
  17. // Returns true if requested amount can be allocated.
  18. // After that the caller can actually allocate it or must call
  19. // |Discard| if decided not to allocate.
  20. virtual bool Reserve(uint64_t size) = 0;
  21. // Reverts reservation.
  22. // Must be called after the specified amount is released.
  23. virtual void Discard(uint64_t size) = 0;
  24. // Returns total amount.
  25. virtual uint64_t GetTotal() const = 0;
  26. // Returns current used amount.
  27. virtual uint64_t GetUsed() const = 0;
  28. // Test only: Sets non-default usage limit.
  29. virtual void Test_SetTotal(uint64_t test_total) = 0;
  30. protected:
  31. friend class base::RefCountedThreadSafe<ResourceInterface>;
  32. ResourceInterface() = default;
  33. virtual ~ResourceInterface() = default;
  34. };
  35. // Moveable RAII class used for scoped Reserve-Discard.
  36. //
  37. // Usage:
  38. // {
  39. // ScopedReservation reservation(1024u, options.memory_resource());
  40. // if (!reservation.reserved()) {
  41. // // Allocation failed.
  42. // return;
  43. // }
  44. // // Allocation succeeded.
  45. // ...
  46. // } // Automatically discarded.
  47. //
  48. // Can be handed over to another owner by move-constructor or using HandOver
  49. // method:
  50. // {
  51. // ScopedReservation summary;
  52. // for (const uint64_t size : sizes) {
  53. // ScopedReservation single_reservation(size, resource);
  54. // ...
  55. // summary.HandOver(single_reservation);
  56. // }
  57. // }
  58. class ScopedReservation {
  59. public:
  60. // Zero-size reservation with no resource interface attached.
  61. // reserved() returns false.
  62. ScopedReservation() noexcept;
  63. // Specified reservation, must have resource interface attached.
  64. ScopedReservation(
  65. uint64_t size,
  66. scoped_refptr<ResourceInterface> resource_interface) noexcept;
  67. // New reservation on the same resource interface as |other_reservation|.
  68. ScopedReservation(uint64_t size,
  69. const ScopedReservation& other_reservation) noexcept;
  70. // Move constructor.
  71. ScopedReservation(ScopedReservation&& other) noexcept;
  72. ScopedReservation(const ScopedReservation& other) = delete;
  73. ScopedReservation& operator=(ScopedReservation&& other) = delete;
  74. ScopedReservation& operator=(const ScopedReservation& other) = delete;
  75. ~ScopedReservation();
  76. bool reserved() const;
  77. // Reduces reservation to |new_size|.
  78. bool Reduce(uint64_t new_size);
  79. // Adds |other| to |this| without assigning or releasing any reservation.
  80. // Used for seamless transition from one reservation to another (more generic
  81. // than std::move). Resets |other| to non-reserved state upon return from this
  82. // method.
  83. void HandOver(ScopedReservation& other);
  84. private:
  85. scoped_refptr<ResourceInterface> resource_interface_;
  86. absl::optional<uint64_t> size_;
  87. };
  88. } // namespace reporting
  89. #endif // COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_