memory_resource_impl.cc 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include "components/reporting/resources/memory_resource_impl.h"
  5. #include <atomic>
  6. #include <cstdint>
  7. #include "base/check_op.h"
  8. namespace reporting {
  9. MemoryResourceImpl::MemoryResourceImpl(uint64_t total_size)
  10. : total_(total_size) {}
  11. MemoryResourceImpl::~MemoryResourceImpl() = default;
  12. bool MemoryResourceImpl::Reserve(uint64_t size) {
  13. uint64_t old_used = used_.fetch_add(size);
  14. if (old_used + size > total_) {
  15. used_.fetch_sub(size);
  16. return false;
  17. }
  18. return true;
  19. }
  20. void MemoryResourceImpl::Discard(uint64_t size) {
  21. DCHECK_LE(size, used_.load());
  22. used_.fetch_sub(size);
  23. }
  24. uint64_t MemoryResourceImpl::GetTotal() const {
  25. return total_;
  26. }
  27. uint64_t MemoryResourceImpl::GetUsed() const {
  28. return used_.load();
  29. }
  30. void MemoryResourceImpl::Test_SetTotal(uint64_t test_total) {
  31. total_ = test_total;
  32. }
  33. } // namespace reporting