accounting-allocator.cc 3.72 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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/zone/accounting-allocator.h"

7 8 9
#include <memory>

#include "src/base/bounded-page-allocator.h"
10
#include "src/base/logging.h"
11
#include "src/base/macros.h"
12
#include "src/utils/allocation.h"
13
#include "src/zone/zone-fwd.h"
14
#include "src/zone/zone-segment.h"
15

16 17 18
namespace v8 {
namespace internal {

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
namespace {

static constexpr size_t kZonePageSize = 256 * KB;

VirtualMemory ReserveAddressSpace(v8::PageAllocator* platform_allocator) {
  DCHECK(
      IsAligned(kZoneReservationSize, platform_allocator->AllocatePageSize()));

  void* hint = reinterpret_cast<void*>(RoundDown(
      reinterpret_cast<uintptr_t>(platform_allocator->GetRandomMmapAddr()),
      kZoneReservationAlignment));

  VirtualMemory memory(platform_allocator, kZoneReservationSize, hint,
                       kZoneReservationAlignment);
  if (memory.IsReserved()) {
    CHECK(IsAligned(memory.address(), kZoneReservationAlignment));
    return memory;
  }

  FATAL(
      "Fatal process out of memory: Failed to reserve memory for compressed "
      "zones");
  UNREACHABLE();
}

std::unique_ptr<v8::base::BoundedPageAllocator> CreateBoundedAllocator(
    v8::PageAllocator* platform_allocator, Address reservation_start) {
  DCHECK(reservation_start);

  auto allocator = std::make_unique<v8::base::BoundedPageAllocator>(
      platform_allocator, reservation_start, kZoneReservationSize,
      kZonePageSize);

  // Exclude first page from allocation to ensure that accesses through
  // decompressed null pointer will seg-fault.
  allocator->AllocatePagesAt(reservation_start, kZonePageSize,
                             v8::PageAllocator::kNoAccess);
  return allocator;
}

}  // namespace

AccountingAllocator::AccountingAllocator() {
  if (COMPRESS_ZONES_BOOL) {
    v8::PageAllocator* platform_page_allocator = GetPlatformPageAllocator();
    VirtualMemory memory = ReserveAddressSpace(platform_page_allocator);
    reserved_area_ = std::make_unique<VirtualMemory>(std::move(memory));
    bounded_page_allocator_ = CreateBoundedAllocator(platform_page_allocator,
                                                     reserved_area_->address());
  }
}

71
AccountingAllocator::~AccountingAllocator() = default;
72

73 74 75 76 77 78 79 80 81 82 83
Segment* AccountingAllocator::AllocateSegment(size_t bytes,
                                              bool supports_compression) {
  void* memory;
  if (COMPRESS_ZONES_BOOL && supports_compression) {
    bytes = RoundUp(bytes, kZonePageSize);
    memory = AllocatePages(bounded_page_allocator_.get(), nullptr, bytes,
                           kZonePageSize, PageAllocator::kReadWrite);

  } else {
    memory = AllocWithRetry(bytes);
  }
84 85 86
  if (memory == nullptr) return nullptr;

  size_t current =
87
      current_memory_usage_.fetch_add(bytes, std::memory_order_relaxed) + bytes;
88 89 90 91
  size_t max = max_memory_usage_.load(std::memory_order_relaxed);
  while (current > max && !max_memory_usage_.compare_exchange_weak(
                              max, current, std::memory_order_relaxed)) {
    // {max} was updated by {compare_exchange_weak}; retry.
92
  }
93 94
  DCHECK_LE(sizeof(Segment), bytes);
  return new (memory) Segment(bytes);
95 96
}

97 98
void AccountingAllocator::ReturnSegment(Segment* segment,
                                        bool supports_compression) {
99
  segment->ZapContents();
100 101
  size_t segment_size = segment->total_size();
  current_memory_usage_.fetch_sub(segment_size, std::memory_order_relaxed);
102
  segment->ZapHeader();
103 104 105 106 107 108
  if (COMPRESS_ZONES_BOOL && supports_compression) {
    CHECK(FreePages(bounded_page_allocator_.get(), segment, segment_size));

  } else {
    free(segment);
  }
109 110
}

111 112
}  // namespace internal
}  // namespace v8