File: memory_cache.h

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (96 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_SERVICE_MEMORY_CACHE_H_
#define GPU_COMMAND_BUFFER_SERVICE_MEMORY_CACHE_H_

#include "base/containers/flat_set.h"
#include "base/containers/heap_array.h"
#include "base/containers/linked_list.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/memory_dump_provider.h"
#include "gpu/gpu_gles2_export.h"

namespace gpu {
// MemoryCacheEntry class for LRU tracking and holding key/value pair.
class GPU_GLES2_EXPORT MemoryCacheEntry
    : public base::LinkNode<MemoryCacheEntry>,
      public base::RefCountedThreadSafe<MemoryCacheEntry> {
 public:
  MemoryCacheEntry(std::string_view key, base::span<const uint8_t> data);
  MemoryCacheEntry(std::string_view key, base::HeapArray<uint8_t> data);

  std::string_view Key() const;

  size_t TotalSize() const;
  size_t DataSize() const;

  base::span<const uint8_t> Data() const;
  size_t ReadData(void* value_out, size_t value_size) const;

 private:
  friend class base::RefCountedThreadSafe<MemoryCacheEntry>;
  ~MemoryCacheEntry();

  const std::string key_;
  const base::HeapArray<uint8_t> data_;
};

class GPU_GLES2_EXPORT MemoryCache : public base::RefCounted<MemoryCache> {
 public:
  explicit MemoryCache(size_t max_size,
                       std::string_view cache_hit_trace_event = "");

  scoped_refptr<MemoryCacheEntry> Store(std::string_view key,
                                        base::span<const uint8_t> data);
  scoped_refptr<MemoryCacheEntry> Store(std::string_view key,
                                        base::HeapArray<uint8_t> data);
  scoped_refptr<MemoryCacheEntry> Find(std::string_view key);

  void PurgeMemory(base::MemoryPressureLevel memory_pressure_level);

  void OnMemoryDump(const std::string& dump_name,
                    base::trace_event::ProcessMemoryDump* pmd);
  template <typename Fn>
  void ForEach(Fn fn) {
    base::AutoLock lock(mutex_);

    for (auto* node = lru_.head(); node != lru_.end(); node = node->next()) {
      fn(node->value());
    }
  }

 private:
  // Overrides for transparent flat_set lookups using a string.
  friend bool operator<(const scoped_refptr<MemoryCacheEntry>& lhs,
                        const scoped_refptr<MemoryCacheEntry>& rhs);
  friend bool operator<(const scoped_refptr<MemoryCacheEntry>& lhs,
                        std::string_view rhs);
  friend bool operator<(std::string_view lhs,
                        const scoped_refptr<MemoryCacheEntry>& rhs);

  friend class base::RefCounted<MemoryCache>;
  ~MemoryCache();

  void EvictEntry(std::string_view key) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  void EvictEntry(MemoryCacheEntry* entry) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  void InsertEntry(scoped_refptr<MemoryCacheEntry> entry)
      EXCLUSIVE_LOCKS_REQUIRED(mutex_);

  bool CanFitMemoryCacheEntry(size_t data_size) const;

  base::Lock mutex_;
  base::flat_set<scoped_refptr<MemoryCacheEntry>> entries_ GUARDED_BY(mutex_);
  base::LinkedList<MemoryCacheEntry> lru_ GUARDED_BY(mutex_);

  const size_t max_size_;
  size_t current_size_ GUARDED_BY(mutex_) = 0;

  const std::string cache_hit_trace_event_;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_MEMORY_CACHE_H_