summaryrefslogtreecommitdiffstats
path: root/blimp/client/app/blimp_discardable_memory_allocator.h
blob: 382b09bd7e5ece94688818d52e2df42e0699034e (plain)
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BLIMP_CLIENT_APP_BLIMP_DISCARDABLE_MEMORY_ALLOCATOR_H_
#define BLIMP_CLIENT_APP_BLIMP_DISCARDABLE_MEMORY_ALLOCATOR_H_

#include <list>

#include "base/macros.h"
#include "base/memory/discardable_memory_allocator.h"
#include "base/synchronization/lock.h"

namespace blimp {
namespace client {

// TODO(khushalsagar): See if we can share this implementation with html_viewer.
// A discardable memory allocator which will unallocate chunks on new
// allocations or as old chunks are unlocked.
class BlimpDiscardableMemoryAllocator :
    public base::DiscardableMemoryAllocator {
 public:
  BlimpDiscardableMemoryAllocator();
  explicit BlimpDiscardableMemoryAllocator(size_t desired_max_memory);
  ~BlimpDiscardableMemoryAllocator() override;

  // Overridden from DiscardableMemoryAllocator:
  scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
      size_t size) override;

 private:
  class DiscardableMemoryChunkImpl;
  friend class DiscardableMemoryChunkImpl;

  typedef std::list<DiscardableMemoryChunkImpl*> MemoryChunkList;

  // Called by DiscardableMemoryChunkImpl when they are unlocked. This puts them
  // at the end of the live_unlocked_chunks_ list and passes an iterator to
  // their position in the unlocked chunk list.
  MemoryChunkList::iterator NotifyUnlocked(
      DiscardableMemoryChunkImpl* chunk);

  // Called by DiscardableMemoryChunkImpl when they are locked. This removes the
  // passed in unlocked chunk list.
  void NotifyLocked(MemoryChunkList::iterator it);

  // Called by DiscardableMemoryChunkImpl when it's destructed. It must be
  // unlocked, by definition.
  void NotifyDestructed(MemoryChunkList::iterator it);

  // Unallocate unlocked chunks if the total live memory is higher than the
  // desired max memory.
  void FreeUnlockedChunksIfNeeded();

  // The amount of memory we can allocate before we try to free unlocked
  // chunks. We can go over this amount if all callers keep their discardable
  // chunks locked.
  const size_t desired_max_memory_;

  // Protects all members below, since this class can be called on the main
  // thread and impl side painting raster threads.
  base::Lock lock_;

  // A count of the sum of memory. Used to trigger discarding the oldest
  // memory.
  size_t total_live_memory_;

  // The number of locked chunks.
  int locked_chunks_;

  // A linked list of unlocked allocated chunks so that the tail is most
  // recently accessed chunks.
  MemoryChunkList live_unlocked_chunks_;

  DISALLOW_COPY_AND_ASSIGN(BlimpDiscardableMemoryAllocator);
};

}  // namespace client
}  // namespace blimp

#endif  // BLIMP_CLIENT_APP_BLIMP_DISCARDABLE_MEMORY_ALLOCATOR_H_