11cb0ef41Sopenharmony_ci// Copyright 2020 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/heap/cppgc/virtual-memory.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "include/cppgc/platform.h"
81cb0ef41Sopenharmony_ci#include "src/base/macros.h"
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cinamespace cppgc {
111cb0ef41Sopenharmony_cinamespace internal {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciVirtualMemory::VirtualMemory(PageAllocator* page_allocator, size_t size,
141cb0ef41Sopenharmony_ci                             size_t alignment, void* hint)
151cb0ef41Sopenharmony_ci    : page_allocator_(page_allocator) {
161cb0ef41Sopenharmony_ci  DCHECK_NOT_NULL(page_allocator);
171cb0ef41Sopenharmony_ci  DCHECK(IsAligned(size, page_allocator->CommitPageSize()));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  const size_t page_size = page_allocator_->AllocatePageSize();
201cb0ef41Sopenharmony_ci  start_ = page_allocator->AllocatePages(hint, RoundUp(size, page_size),
211cb0ef41Sopenharmony_ci                                         RoundUp(alignment, page_size),
221cb0ef41Sopenharmony_ci                                         PageAllocator::kNoAccess);
231cb0ef41Sopenharmony_ci  if (start_) {
241cb0ef41Sopenharmony_ci    size_ = RoundUp(size, page_size);
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciVirtualMemory::~VirtualMemory() V8_NOEXCEPT {
291cb0ef41Sopenharmony_ci  if (IsReserved()) {
301cb0ef41Sopenharmony_ci    page_allocator_->FreePages(start_, size_);
311cb0ef41Sopenharmony_ci  }
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciVirtualMemory::VirtualMemory(VirtualMemory&& other) V8_NOEXCEPT
351cb0ef41Sopenharmony_ci    : page_allocator_(std::move(other.page_allocator_)),
361cb0ef41Sopenharmony_ci      start_(std::move(other.start_)),
371cb0ef41Sopenharmony_ci      size_(std::move(other.size_)) {
381cb0ef41Sopenharmony_ci  other.Reset();
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciVirtualMemory& VirtualMemory::operator=(VirtualMemory&& other) V8_NOEXCEPT {
421cb0ef41Sopenharmony_ci  DCHECK(!IsReserved());
431cb0ef41Sopenharmony_ci  page_allocator_ = std::move(other.page_allocator_);
441cb0ef41Sopenharmony_ci  start_ = std::move(other.start_);
451cb0ef41Sopenharmony_ci  size_ = std::move(other.size_);
461cb0ef41Sopenharmony_ci  other.Reset();
471cb0ef41Sopenharmony_ci  return *this;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_civoid VirtualMemory::Reset() {
511cb0ef41Sopenharmony_ci  start_ = nullptr;
521cb0ef41Sopenharmony_ci  size_ = 0;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci}  // namespace internal
561cb0ef41Sopenharmony_ci}  // namespace cppgc
57