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);
class V8_EXPORT_PRIVATE VirtualMemory final {
public:
// Empty VirtualMemory object, controlling no reserved memory.
VirtualMemory()
: page_allocator_(nullptr), address_(kNullAddress), size_(0) {}
VirtualMemory() = default;
// Reserves virtual memory containing an area of the given size that is
// aligned per alignment. This may not be at the position returned by
......@@ -175,6 +174,15 @@ class V8_EXPORT_PRIVATE VirtualMemory final {
// object.
~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.
bool IsReserved() const { return address_ != kNullAddress; }
......@@ -224,9 +232,11 @@ class V8_EXPORT_PRIVATE VirtualMemory final {
private:
// Page allocator that controls the virtual memory.
v8::PageAllocator* page_allocator_;
Address address_; // Start address of the virtual memory.
size_t size_; // Size of the virtual memory.
v8::PageAllocator* page_allocator_ = nullptr;
Address address_ = kNullAddress; // Start address 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,
......
......@@ -639,7 +639,7 @@ void MemoryChunk::SetReadAndWritable() {
MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size,
Address area_start, Address area_end,
Executability executable, Space* owner,
VirtualMemory* reservation) {
VirtualMemory&& reservation) {
MemoryChunk* chunk = FromAddress(base);
DCHECK(base == chunk->address());
......@@ -709,9 +709,7 @@ MemoryChunk* MemoryChunk::Initialize(Heap* heap, Address base, size_t size,
}
}
if (reservation != nullptr) {
chunk->reservation_.TakeControl(reservation);
}
chunk->reservation_ = std::move(reservation);
return chunk;
}
......@@ -948,7 +946,7 @@ MemoryChunk* MemoryAllocator::AllocateChunk(size_t reserve_area_size,
MemoryChunk* chunk =
MemoryChunk::Initialize(heap, base, chunk_size, area_start, area_end,
executable, owner, &reservation);
executable, owner, std::move(reservation));
if (chunk->executable()) RegisterExecutableMemoryChunk(chunk);
return chunk;
......@@ -1230,7 +1228,7 @@ MemoryChunk* MemoryAllocator::AllocatePagePooled(SpaceType* owner) {
}
VirtualMemory reservation(data_page_allocator(), start, size);
MemoryChunk::Initialize(isolate_->heap(), start, size, area_start, area_end,
NOT_EXECUTABLE, owner, &reservation);
NOT_EXECUTABLE, owner, std::move(reservation));
size_ += size;
return chunk;
}
......
......@@ -652,7 +652,7 @@ class MemoryChunk {
static MemoryChunk* Initialize(Heap* heap, Address base, size_t size,
Address area_start, Address area_end,
Executability executable, Space* owner,
VirtualMemory* reservation);
VirtualMemory&& reservation);
// Should be called when memory chunk is about to be freed.
void ReleaseAllocatedMemory();
......
......@@ -309,22 +309,20 @@ WasmCode::~WasmCode() {
}
NativeModule::NativeModule(Isolate* isolate, const WasmFeatures& enabled,
bool can_request_more, VirtualMemory* code_space,
bool can_request_more, VirtualMemory&& code_space,
WasmCodeManager* code_manager,
std::shared_ptr<const WasmModule> module,
const ModuleEnv& env)
: enabled_features_(enabled),
module_(std::move(module)),
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),
can_request_more_memory_(can_request_more),
use_trap_handler_(env.use_trap_handler) {
DCHECK_EQ(module_.get(), env.module);
DCHECK_NOT_NULL(module_);
VirtualMemory my_mem;
owned_code_space_.push_back(my_mem);
owned_code_space_.back().TakeControl(code_space);
owned_code_space_.emplace_back(std::move(code_space));
owned_code_.reserve(num_functions());
uint32_t num_wasm_functions = module_->num_declared_functions;
......@@ -649,8 +647,8 @@ Address NativeModule::AllocateForCode(size_t size) {
Address hint = owned_code_space_.empty() ? kNullAddress
: 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();
wasm_code_manager_->TryAllocate(size, &new_mem,
reinterpret_cast<void*>(hint));
......@@ -895,8 +893,8 @@ std::unique_ptr<NativeModule> WasmCodeManager::NewNativeModule(
size_t size = mem.size();
Address end = mem.end();
std::unique_ptr<NativeModule> ret(
new NativeModule(isolate, enabled, can_request_more, &mem, this,
std::move(module), env));
new NativeModule(isolate, enabled, can_request_more, std::move(mem),
this, std::move(module), env));
TRACE_HEAP("New NativeModule %p: Mem: %" PRIuPTR ",+%zu\n", this, start,
size);
base::LockGuard<base::Mutex> lock(&native_modules_mutex_);
......
......@@ -345,7 +345,7 @@ class V8_EXPORT_PRIVATE NativeModule final {
friend class NativeModuleModificationScope;
NativeModule(Isolate* isolate, const WasmFeatures& enabled_features,
bool can_request_more, VirtualMemory* code_space,
bool can_request_more, VirtualMemory&& code_space,
WasmCodeManager* code_manager,
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