bounded-page-allocator.cc 7.06 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 the V8 project 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 "src/base/bounded-page-allocator.h"

namespace v8 {
namespace base {

10 11 12
BoundedPageAllocator::BoundedPageAllocator(
    v8::PageAllocator* page_allocator, Address start, size_t size,
    size_t allocate_page_size, PageInitializationMode page_initialization_mode)
13 14 15
    : allocate_page_size_(allocate_page_size),
      commit_page_size_(page_allocator->CommitPageSize()),
      page_allocator_(page_allocator),
16 17
      region_allocator_(start, size, allocate_page_size_),
      page_initialization_mode_(page_initialization_mode) {
18 19 20
  DCHECK_NOT_NULL(page_allocator);
  DCHECK(IsAligned(allocate_page_size, page_allocator->AllocatePageSize()));
  DCHECK(IsAligned(allocate_page_size_, commit_page_size_));
21 22
}

23 24 25 26 27 28
BoundedPageAllocator::Address BoundedPageAllocator::begin() const {
  return region_allocator_.begin();
}

size_t BoundedPageAllocator::size() const { return region_allocator_.size(); }

29 30 31
void* BoundedPageAllocator::AllocatePages(void* hint, size_t size,
                                          size_t alignment,
                                          PageAllocator::Permission access) {
32
  MutexGuard guard(&mutex_);
33
  DCHECK(IsAligned(alignment, region_allocator_.page_size()));
34 35
  DCHECK(IsAligned(alignment, allocate_page_size_));

36 37 38 39 40 41 42 43
  Address address = RegionAllocator::kAllocationFailure;

  Address hint_address = reinterpret_cast<Address>(hint);
  if (hint_address && IsAligned(hint_address, alignment) &&
      region_allocator_.contains(hint_address, size)) {
    if (region_allocator_.AllocateRegionAt(hint_address, size)) {
      address = hint_address;
    }
44
  }
45 46 47 48 49 50 51 52 53 54

  if (address == RegionAllocator::kAllocationFailure) {
    if (alignment <= allocate_page_size_) {
      // TODO(ishell): Consider using randomized version here.
      address = region_allocator_.AllocateRegion(size);
    } else {
      address = region_allocator_.AllocateAlignedRegion(size, alignment);
    }
  }

55 56 57
  if (address == RegionAllocator::kAllocationFailure) {
    return nullptr;
  }
58 59 60 61 62 63 64 65 66

  void* ptr = reinterpret_cast<void*>(address);
  if (!page_allocator_->SetPermissions(ptr, size, access)) {
    // This most likely means that we ran out of memory.
    CHECK_EQ(region_allocator_.FreeRegion(address), size);
    return nullptr;
  }

  return ptr;
67 68
}

69 70
bool BoundedPageAllocator::AllocatePagesAt(Address address, size_t size,
                                           PageAllocator::Permission access) {
71 72
  DCHECK(IsAligned(address, allocate_page_size_));
  DCHECK(IsAligned(size, allocate_page_size_));
73

74 75
  {
    MutexGuard guard(&mutex_);
76
    DCHECK(region_allocator_.contains(address, size));
77 78 79 80

    if (!region_allocator_.AllocateRegionAt(address, size)) {
      return false;
    }
81
  }
82

83 84 85 86 87 88 89
  void* ptr = reinterpret_cast<void*>(address);
  if (!page_allocator_->SetPermissions(ptr, size, access)) {
    // This most likely means that we ran out of memory.
    CHECK_EQ(region_allocator_.FreeRegion(address), size);
    return false;
  }

90 91 92
  return true;
}

93 94 95
bool BoundedPageAllocator::ReserveForSharedMemoryMapping(void* ptr,
                                                         size_t size) {
  Address address = reinterpret_cast<Address>(ptr);
96 97
  DCHECK(IsAligned(address, allocate_page_size_));
  DCHECK(IsAligned(size, commit_page_size_));
98 99 100

  {
    MutexGuard guard(&mutex_);
101
    DCHECK(region_allocator_.contains(address, size));
102 103 104 105 106 107 108 109

    // Region allocator requires page size rather than commit size so just over-
    // allocate there since any extra space couldn't be used anyway.
    size_t region_size = RoundUp(size, allocate_page_size_);
    if (!region_allocator_.AllocateRegionAt(
            address, region_size, RegionAllocator::RegionState::kExcluded)) {
      return false;
    }
110 111 112 113 114 115 116
  }

  CHECK(page_allocator_->SetPermissions(ptr, size,
                                        PageAllocator::Permission::kNoAccess));
  return true;
}

117
bool BoundedPageAllocator::FreePages(void* raw_address, size_t size) {
118
  MutexGuard guard(&mutex_);
119 120 121 122

  Address address = reinterpret_cast<Address>(raw_address);
  size_t freed_size = region_allocator_.FreeRegion(address);
  if (freed_size != size) return false;
123 124 125 126 127 128 129 130 131 132 133
  if (page_initialization_mode_ ==
      PageInitializationMode::kAllocatedPagesMustBeZeroInitialized) {
    // When we are required to return zero-initialized pages, we decommit the
    // pages here, which will cause any wired pages to be removed by the OS.
    CHECK(page_allocator_->DecommitPages(raw_address, size));
  } else {
    DCHECK_EQ(page_initialization_mode_,
              PageInitializationMode::kAllocatedPagesCanBeUninitialized);
    CHECK(page_allocator_->SetPermissions(raw_address, size,
                                          PageAllocator::kNoAccess));
  }
134 135 136 137 138 139
  return true;
}

bool BoundedPageAllocator::ReleasePages(void* raw_address, size_t size,
                                        size_t new_size) {
  Address address = reinterpret_cast<Address>(raw_address);
140
  DCHECK(IsAligned(address, allocate_page_size_));
141 142 143 144

  DCHECK_LT(new_size, size);
  DCHECK(IsAligned(size - new_size, commit_page_size_));

145 146 147
  // This must be held until the page permissions are updated.
  MutexGuard guard(&mutex_);

148 149 150 151
  // Check if we freed any allocatable pages by this release.
  size_t allocated_size = RoundUp(size, allocate_page_size_);
  size_t new_allocated_size = RoundUp(new_size, allocate_page_size_);

152 153 154 155
#ifdef DEBUG
  {
    // There must be an allocated region at given |address| of a size not
    // smaller than |size|.
156
    DCHECK_EQ(allocated_size, region_allocator_.CheckRegion(address));
157 158
  }
#endif
159 160 161 162 163

  if (new_allocated_size < allocated_size) {
    region_allocator_.TrimRegion(address, new_allocated_size);
  }

164 165 166
  // Keep the region in "used" state just uncommit some pages.
  Address free_address = address + new_size;
  size_t free_size = size - new_size;
167 168 169 170 171 172 173 174 175 176 177 178
  if (page_initialization_mode_ ==
      PageInitializationMode::kAllocatedPagesMustBeZeroInitialized) {
    // See comment in FreePages().
    return page_allocator_->DecommitPages(reinterpret_cast<void*>(free_address),
                                          free_size);
  } else {
    DCHECK_EQ(page_initialization_mode_,
              PageInitializationMode::kAllocatedPagesCanBeUninitialized);
    return page_allocator_->SetPermissions(
        reinterpret_cast<void*>(free_address), free_size,
        PageAllocator::kNoAccess);
  }
179 180 181 182 183 184 185 186 187 188
}

bool BoundedPageAllocator::SetPermissions(void* address, size_t size,
                                          PageAllocator::Permission access) {
  DCHECK(IsAligned(reinterpret_cast<Address>(address), commit_page_size_));
  DCHECK(IsAligned(size, commit_page_size_));
  DCHECK(region_allocator_.contains(reinterpret_cast<Address>(address), size));
  return page_allocator_->SetPermissions(address, size, access);
}

189 190 191 192
bool BoundedPageAllocator::DiscardSystemPages(void* address, size_t size) {
  return page_allocator_->DiscardSystemPages(address, size);
}

193 194 195 196
bool BoundedPageAllocator::DecommitPages(void* address, size_t size) {
  return page_allocator_->DecommitPages(address, size);
}

197 198
}  // namespace base
}  // namespace v8