blob: 56fd3bce99fe551056e0f866b5dabcc788525b44 [file] [log] [blame]
Leonid Baraz3e8af4b2020-11-23 19:04:471// 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
Leonid Barazea0c6dc2021-11-02 20:38:055#ifndef COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_
6#define COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_
Leonid Baraz3e8af4b2020-11-23 19:04:477
8#include <cstdint>
9
Leonid Baraz960ea8f2022-05-17 01:01:0610#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_refptr.h"
Anton Bikineev1156b5f2021-05-15 22:35:3612#include "third_party/abseil-cpp/absl/types/optional.h"
Leonid Baraz3e8af4b2020-11-23 19:04:4713
14namespace reporting {
15
16// Interface to resources management by Storage module.
17// Must be implemented by the caller base on the platform limitations.
18// All APIs are non-blocking.
Leonid Baraz960ea8f2022-05-17 01:01:0619class ResourceInterface : public base::RefCountedThreadSafe<ResourceInterface> {
Leonid Baraz3e8af4b2020-11-23 19:04:4720 public:
Leonid Baraz3e8af4b2020-11-23 19:04:4721 // Needs to be called before attempting to allocate specified size.
22 // Returns true if requested amount can be allocated.
23 // After that the caller can actually allocate it or must call
24 // |Discard| if decided not to allocate.
Zach Trudof1bf7ce2020-12-22 21:46:1525 virtual bool Reserve(uint64_t size) = 0;
Leonid Baraz3e8af4b2020-11-23 19:04:4726
27 // Reverts reservation.
28 // Must be called after the specified amount is released.
Zach Trudof1bf7ce2020-12-22 21:46:1529 virtual void Discard(uint64_t size) = 0;
Leonid Baraz3e8af4b2020-11-23 19:04:4730
31 // Returns total amount.
Hong Xuf8e3c1302022-06-01 01:49:2132 virtual uint64_t GetTotal() const = 0;
Leonid Baraz3e8af4b2020-11-23 19:04:4733
34 // Returns current used amount.
Hong Xuf8e3c1302022-06-01 01:49:2135 virtual uint64_t GetUsed() const = 0;
Leonid Baraz3e8af4b2020-11-23 19:04:4736
37 // Test only: Sets non-default usage limit.
Zach Trudof1bf7ce2020-12-22 21:46:1538 virtual void Test_SetTotal(uint64_t test_total) = 0;
Leonid Baraz3e8af4b2020-11-23 19:04:4739
40 protected:
Leonid Baraz960ea8f2022-05-17 01:01:0641 friend class base::RefCountedThreadSafe<ResourceInterface>;
42
Leonid Baraz3e8af4b2020-11-23 19:04:4743 ResourceInterface() = default;
Leonid Baraz960ea8f2022-05-17 01:01:0644 virtual ~ResourceInterface() = default;
Leonid Baraz3e8af4b2020-11-23 19:04:4745};
46
47// Moveable RAII class used for scoped Reserve-Discard.
48//
49// Usage:
50// {
Leonid Baraz960ea8f2022-05-17 01:01:0651// ScopedReservation reservation(1024u, options.memory_resource());
Leonid Baraz3e8af4b2020-11-23 19:04:4752// if (!reservation.reserved()) {
53// // Allocation failed.
54// return;
55// }
56// // Allocation succeeded.
57// ...
58// } // Automatically discarded.
59//
60// Can be handed over to another owner.
61class ScopedReservation {
62 public:
Leonid Barazaf0a319f2022-06-15 02:17:4763 ScopedReservation(
64 uint64_t size,
65 scoped_refptr<ResourceInterface> resource_interface) noexcept;
66 ScopedReservation(ScopedReservation&& other) noexcept;
Leonid Baraz3e8af4b2020-11-23 19:04:4767 ScopedReservation(const ScopedReservation& other) = delete;
Leonid Barazaf0a319f2022-06-15 02:17:4768 ScopedReservation& operator=(ScopedReservation&& other) = delete;
Leonid Baraz3e8af4b2020-11-23 19:04:4769 ScopedReservation& operator=(const ScopedReservation& other) = delete;
70 ~ScopedReservation();
71
72 bool reserved() const;
Leonid Barazaf0a319f2022-06-15 02:17:4773
74 // Reduces reservation to |new_size|.
Santiago Castano Moreno150ba5f2021-11-12 21:34:5275 bool Reduce(uint64_t new_size);
Leonid Baraz3e8af4b2020-11-23 19:04:4776
Leonid Barazaf0a319f2022-06-15 02:17:4777 // Adds |other| to |this| without assigning or releasing any reservation.
78 // Used for seamless transition from one reservation to another (more generic
79 // than std::move). Resets |other| to non-reserved state upon return from this
80 // method.
81 void HandOver(ScopedReservation& other);
82
Leonid Baraz3e8af4b2020-11-23 19:04:4783 private:
Leonid Baraz960ea8f2022-05-17 01:01:0684 const scoped_refptr<ResourceInterface> resource_interface_;
Anton Bikineev1156b5f2021-05-15 22:35:3685 absl::optional<uint64_t> size_;
Leonid Baraz3e8af4b2020-11-23 19:04:4786};
87
Leonid Baraz3e8af4b2020-11-23 19:04:4788} // namespace reporting
89
Leonid Barazea0c6dc2021-11-02 20:38:0590#endif // COMPONENTS_REPORTING_RESOURCES_RESOURCE_INTERFACE_H_