• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/heap/cppgc/virtual-memory.h"
6 
7 #include "include/cppgc/platform.h"
8 #include "src/base/macros.h"
9 
10 namespace cppgc {
11 namespace internal {
12 
VirtualMemory(PageAllocator * page_allocator,size_t size,size_t alignment,void * hint)13 VirtualMemory::VirtualMemory(PageAllocator* page_allocator, size_t size,
14                              size_t alignment, void* hint)
15     : page_allocator_(page_allocator) {
16   DCHECK_NOT_NULL(page_allocator);
17   DCHECK(IsAligned(size, page_allocator->CommitPageSize()));
18 
19   const size_t page_size = page_allocator_->AllocatePageSize();
20   start_ = page_allocator->AllocatePages(hint, RoundUp(size, page_size),
21                                          RoundUp(alignment, page_size),
22                                          PageAllocator::kNoAccess);
23   if (start_) {
24     size_ = RoundUp(size, page_size);
25   }
26 }
27 
~VirtualMemory()28 VirtualMemory::~VirtualMemory() V8_NOEXCEPT {
29   if (IsReserved()) {
30     page_allocator_->FreePages(start_, size_);
31   }
32 }
33 
VirtualMemory(VirtualMemory && other)34 VirtualMemory::VirtualMemory(VirtualMemory&& other) V8_NOEXCEPT
35     : page_allocator_(std::move(other.page_allocator_)),
36       start_(std::move(other.start_)),
37       size_(std::move(other.size_)) {
38   other.Reset();
39 }
40 
operator =(VirtualMemory && other)41 VirtualMemory& VirtualMemory::operator=(VirtualMemory&& other) V8_NOEXCEPT {
42   DCHECK(!IsReserved());
43   page_allocator_ = std::move(other.page_allocator_);
44   start_ = std::move(other.start_);
45   size_ = std::move(other.size_);
46   other.Reset();
47   return *this;
48 }
49 
Reset()50 void VirtualMemory::Reset() {
51   start_ = nullptr;
52   size_ = 0;
53 }
54 
55 }  // namespace internal
56 }  // namespace cppgc
57