summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/heap/page_pool.cc
blob: 100a7803614b31e00c444aa7b44cfa32fdb45de5 (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
// Copyright 2015 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.

#include "third_party/blink/renderer/platform/heap/page_pool.h"

#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/heap/page_memory.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"

namespace blink {

PagePool::PagePool() {
  for (int i = 0; i < BlinkGC::kNumberOfArenas; ++i) {
    pool_[i] = nullptr;
  }
}

PagePool::~PagePool() {
  for (int index = 0; index < BlinkGC::kNumberOfArenas; ++index) {
    while (PoolEntry* entry = pool_[index]) {
      pool_[index] = entry->next;
      PageMemory* memory = entry->data;
      DCHECK(memory);
      delete memory;
      delete entry;
    }
  }
}

void PagePool::Add(int index, PageMemory* memory) {
  // When adding a page to the pool we decommit it to ensure it is unused
  // while in the pool.  This also allows the physical memory, backing the
  // page, to be given back to the OS.
  memory->Decommit();
  PoolEntry* entry = new PoolEntry(memory, pool_[index]);
  pool_[index] = entry;
}

PageMemory* PagePool::Take(int index) {
  while (PoolEntry* entry = pool_[index]) {
    pool_[index] = entry->next;
    PageMemory* memory = entry->data;
    DCHECK(memory);
    delete entry;
    if (memory->Commit())
      return memory;

    // We got some memory, but failed to commit it, try again.
    delete memory;
  }
  return nullptr;
}

}  // namespace blink