Commit 32438e0b authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[cleanup] Disallow copy constructor and assign operator on VirtualMemory

... and add move constructor and move assignment operator.
Also define some VirtualMemory parameters as r-values to explicitly express
that the ownership is passed from the caller to callee.

Bug: v8:8015
Change-Id: Iee27ddc844556dc7465177656b339cd1f83a3b56
Reviewed-on: https://chromium-review.googlesource.com/1213062Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55726}
parent e56b6d24
...@@ -155,8 +155,7 @@ V8_EXPORT_PRIVATE bool OnCriticalMemoryPressure(size_t length); ...@@ -155,8 +155,7 @@ V8_EXPORT_PRIVATE bool OnCriticalMemoryPressure(size_t length);
class V8_EXPORT_PRIVATE VirtualMemory final { class V8_EXPORT_PRIVATE VirtualMemory final {
public: public:
// Empty VirtualMemory object, controlling no reserved memory. // Empty VirtualMemory object, controlling no reserved memory.
VirtualMemory() VirtualMemory() = default;
: page_allocator_(nullptr), address_(kNullAddress), size_(0) {}
// Reserves virtual memory containing an area of the given size that is // Reserves virtual memory containing an area of the given size that is
// aligned per alignment. This may not be at the position returned by // aligned per alignment. This may not be at the position returned by
...@@ -175,6 +174,15 @@ class V8_EXPORT_PRIVATE VirtualMemory final { ...@@ -175,6 +174,15 @@ class V8_EXPORT_PRIVATE VirtualMemory final {
// object. // object.
~VirtualMemory(); ~VirtualMemory();
// Move constructor.
VirtualMemory(VirtualMemory&& other) V8_NOEXCEPT { TakeControl(&other); }
// Move assignment operator.
VirtualMemory& operator=(VirtualMemory&& other) V8_NOEXCEPT {
TakeControl(&other);
return *this;
}
// Returns whether the memory has been reserved. // Returns whether the memory has been reserved.
bool IsReserved() const { return address_ != kNullAddress; } bool IsReserved() const { return address_ != kNullAddress; }
...@@ -224,9 +232,11 @@ class V8_EXPORT_PRIVATE VirtualMemory final { ...@@ -224,9 +232,11 @@ class V8_EXPORT_PRIVATE VirtualMemory final {
private: private:
// Page allocator that controls the virtual memory. // Page allocator that controls the virtual memory.
v8::PageAllocator* page_allocator_; v8::PageAllocator* page_allocator_ = nullptr;
Address address_; // Start address of the virtual memory. Address address_ = kNullAddress; // Start address of the virtual memory.
size_t size_; // Size of the virtual memory. size_t size_ = 0; // Size of the virtual memory.
DISALLOW_COPY_AND_ASSIGN(VirtualMemory);
}; };
bool AllocVirtualMemory(v8::PageAllocator* page_allocator, size_t size, bool AllocVirtualMemory(v8::PageAllocator* page_allocator, size_t size,
......
...@@ -639,7 +639,7 @@ void MemoryChunk::SetReadAndWritable() { ...@@ -639,7 +639,7 @@ void MemoryChunk::SetReadAndWritable() {
MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size, MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size,
Address area_start, Address area_end, Address area_start, Address area_end,
Executability executable, Space* owner, Executability executable, Space* owner,
VirtualMemory* reservation) { VirtualMemory&& reservation) {
MemoryChunk* chunk = FromAddress(base); MemoryChunk* chunk = FromAddress(base);
DCHECK(base == chunk->address()); DCHECK(base == chunk->address());
...@@ -709,9 +709,7 @@ MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size, ...@@ -709,9 +709,7 @@ MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size,
} }
} }
if (reservation != nullptr) { chunk->reservation_ = std::move(reservation);
chunk->reservation_.TakeControl(reservation);
}
return chunk; return chunk;
} }
...@@ -948,7 +946,7 @@ MemoryChunk* MemoryAllocator::AllocateChunk(size_t reserve_area_size, ...@@ -948,7 +946,7 @@ MemoryChunk* MemoryAllocator::AllocateChunk(size_t reserve_area_size,
MemoryChunk* chunk = MemoryChunk* chunk =
MemoryChunk::Initialize(heap, base, chunk_size, area_start, area_end, MemoryChunk::Initialize(heap, base, chunk_size, area_start, area_end,
executable, owner, &reservation); executable, owner, std::move(reservation));
if (chunk->executable()) RegisterExecutableMemoryChunk(chunk); if (chunk->executable()) RegisterExecutableMemoryChunk(chunk);
return chunk; return chunk;
...@@ -1230,7 +1228,7 @@ MemoryChunk* MemoryAllocator::AllocatePagePooled(SpaceType* owner) { ...@@ -1230,7 +1228,7 @@ MemoryChunk* MemoryAllocator::AllocatePagePooled(SpaceType* owner) {
} }
VirtualMemory reservation(data_page_allocator(), start, size); VirtualMemory reservation(data_page_allocator(), start, size);
MemoryChunk::Initialize(isolate_->heap(), start, size, area_start, area_end, MemoryChunk::Initialize(isolate_->heap(), start, size, area_start, area_end,
NOT_EXECUTABLE, owner, &reservation); NOT_EXECUTABLE, owner, std::move(reservation));
size_ += size; size_ += size;
return chunk; return chunk;
} }
......
...@@ -652,7 +652,7 @@ class MemoryChunk { ...@@ -652,7 +652,7 @@ class MemoryChunk {
static MemoryChunk* Initialize(Heap* heap, Address base, size_t size, static MemoryChunk* Initialize(Heap* heap, Address base, size_t size,
Address area_start, Address area_end, Address area_start, Address area_end,
Executability executable, Space* owner, Executability executable, Space* owner,
VirtualMemory* reservation); VirtualMemory&& reservation);
// Should be called when memory chunk is about to be freed. // Should be called when memory chunk is about to be freed.
void ReleaseAllocatedMemory(); void ReleaseAllocatedMemory();
......
...@@ -309,22 +309,20 @@ WasmCode::~WasmCode() { ...@@ -309,22 +309,20 @@ WasmCode::~WasmCode() {
} }
NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled, NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
bool can_request_more, VirtualMemory* code_space, bool can_request_more, VirtualMemory&& code_space,
WasmCodeManager* code_manager, WasmCodeManager* code_manager,
std::shared_ptr<const WasmModule> module, std::shared_ptr<const WasmModule> module,
const ModuleEnv& env) const ModuleEnv& env)
: enabled_features_(enabled), : enabled_features_(enabled),
module_(std::move(module)), module_(std::move(module)),
compilation_state_(NewCompilationState(isolate, env)), compilation_state_(NewCompilationState(isolate, env)),
free_code_space_({code_space->address(), code_space->end()}), free_code_space_({code_space.address(), code_space.end()}),
wasm_code_manager_(code_manager), wasm_code_manager_(code_manager),
can_request_more_memory_(can_request_more), can_request_more_memory_(can_request_more),
use_trap_handler_(env.use_trap_handler) { use_trap_handler_(env.use_trap_handler) {
DCHECK_EQ(module_.get(), env.module); DCHECK_EQ(module_.get(), env.module);
DCHECK_NOT_NULL(module_); DCHECK_NOT_NULL(module_);
VirtualMemory my_mem; owned_code_space_.emplace_back(std::move(code_space));
owned_code_space_.push_back(my_mem);
owned_code_space_.back().TakeControl(code_space);
owned_code_.reserve(num_functions()); owned_code_.reserve(num_functions());
uint32_t num_wasm_functions = module_->num_declared_functions; uint32_t num_wasm_functions = module_->num_declared_functions;
...@@ -649,8 +647,8 @@ Address NativeModule::AllocateForCode(size_t size) { ...@@ -649,8 +647,8 @@ Address NativeModule::AllocateForCode(size_t size) {
Address hint = owned_code_space_.empty() ? kNullAddress Address hint = owned_code_space_.empty() ? kNullAddress
: owned_code_space_.back().end(); : owned_code_space_.back().end();
VirtualMemory empty_mem;
owned_code_space_.push_back(empty_mem); owned_code_space_.emplace_back();
VirtualMemory& new_mem = owned_code_space_.back(); VirtualMemory& new_mem = owned_code_space_.back();
wasm_code_manager_->TryAllocate(size, &new_mem, wasm_code_manager_->TryAllocate(size, &new_mem,
reinterpret_cast<void*>(hint)); reinterpret_cast<void*>(hint));
...@@ -895,8 +893,8 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule( ...@@ -895,8 +893,8 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule(
size_t size = mem.size(); size_t size = mem.size();
Address end = mem.end(); Address end = mem.end();
std::unique_ptr<NativeModule> ret( std::unique_ptr<NativeModule> ret(
new NativeModule(isolate, enabled, can_request_more, &mem, this, new NativeModule(isolate, enabled, can_request_more, std::move(mem),
std::move(module), env)); this, std::move(module), env));
TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start, TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start,
size); size);
base::LockGuard<base::Mutex> lock(&native_modules_mutex_); base::LockGuard<base::Mutex> lock(&native_modules_mutex_);
......
...@@ -345,7 +345,7 @@ class V8_EXPORT_PRIVATE NativeModule final { ...@@ -345,7 +345,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
friend class NativeModuleModificationScope; friend class NativeModuleModificationScope;
NativeModule(Isolate* isolate, const WasmFeatures& enabled_features, NativeModule(Isolate* isolate, const WasmFeatures& enabled_features,
bool can_request_more, VirtualMemory* code_space, bool can_request_more, VirtualMemory&& code_space,
WasmCodeManager* code_manager, WasmCodeManager* code_manager,
std::shared_ptr<const WasmModule> module, const ModuleEnv& env); std::shared_ptr<const WasmModule> module, const ModuleEnv& env);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment