b73ee3344a
- Eliminates OS::ReleaseRegion, replacing with calls to OS::Free. - Adds bool return value to OS::Free. - Cleans up types of flags, protection on Windows and Cygwin. Bug: chromium:756050 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: I6a642374e33876966a5552fb0cdf552dc6d79aaa Reviewed-on: https://chromium-review.googlesource.com/762345 Commit-Queue: Bill Budge <bbudge@chromium.org> Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Cr-Commit-Position: refs/heads/master@{#49305}
227 lines
6.0 KiB
C++
227 lines
6.0 KiB
C++
// Copyright 2012 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/allocation.h"
|
|
|
|
#include <stdlib.h> // For free, malloc.
|
|
#include "src/base/bits.h"
|
|
#include "src/base/logging.h"
|
|
#include "src/base/platform/platform.h"
|
|
#include "src/utils.h"
|
|
#include "src/v8.h"
|
|
|
|
#if V8_LIBC_BIONIC
|
|
#include <malloc.h> // NOLINT
|
|
#endif
|
|
|
|
#if defined(LEAK_SANITIZER)
|
|
#include <sanitizer/lsan_interface.h>
|
|
#endif
|
|
|
|
namespace v8 {
|
|
namespace internal {
|
|
|
|
namespace {
|
|
|
|
void* AlignedAllocInternal(size_t size, size_t alignment) {
|
|
void* ptr;
|
|
#if V8_OS_WIN
|
|
ptr = _aligned_malloc(size, alignment);
|
|
#elif V8_LIBC_BIONIC
|
|
// posix_memalign is not exposed in some Android versions, so we fall back to
|
|
// memalign. See http://code.google.com/p/android/issues/detail?id=35391.
|
|
ptr = memalign(alignment, size);
|
|
#else
|
|
if (posix_memalign(&ptr, alignment, size)) ptr = nullptr;
|
|
#endif
|
|
return ptr;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void* Malloced::New(size_t size) {
|
|
void* result = malloc(size);
|
|
if (result == nullptr) {
|
|
V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
|
|
result = malloc(size);
|
|
if (result == nullptr) {
|
|
V8::FatalProcessOutOfMemory("Malloced operator new");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
void Malloced::Delete(void* p) {
|
|
free(p);
|
|
}
|
|
|
|
|
|
char* StrDup(const char* str) {
|
|
int length = StrLength(str);
|
|
char* result = NewArray<char>(length + 1);
|
|
MemCopy(result, str, length);
|
|
result[length] = '\0';
|
|
return result;
|
|
}
|
|
|
|
|
|
char* StrNDup(const char* str, int n) {
|
|
int length = StrLength(str);
|
|
if (n < length) length = n;
|
|
char* result = NewArray<char>(length + 1);
|
|
MemCopy(result, str, length);
|
|
result[length] = '\0';
|
|
return result;
|
|
}
|
|
|
|
|
|
void* AlignedAlloc(size_t size, size_t alignment) {
|
|
DCHECK_LE(V8_ALIGNOF(void*), alignment);
|
|
DCHECK(base::bits::IsPowerOfTwo(alignment));
|
|
void* ptr = AlignedAllocInternal(size, alignment);
|
|
if (ptr == nullptr) {
|
|
V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
|
|
ptr = AlignedAllocInternal(size, alignment);
|
|
if (ptr == nullptr) {
|
|
V8::FatalProcessOutOfMemory("AlignedAlloc");
|
|
}
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
|
|
void AlignedFree(void *ptr) {
|
|
#if V8_OS_WIN
|
|
_aligned_free(ptr);
|
|
#elif V8_LIBC_BIONIC
|
|
// Using free is not correct in general, but for V8_LIBC_BIONIC it is.
|
|
free(ptr);
|
|
#else
|
|
free(ptr);
|
|
#endif
|
|
}
|
|
|
|
byte* AllocateSystemPage(void* address, size_t* allocated) {
|
|
size_t page_size = base::OS::AllocatePageSize();
|
|
void* result = base::OS::Allocate(address, page_size, page_size,
|
|
base::OS::MemoryPermission::kReadWrite);
|
|
if (result != nullptr) *allocated = page_size;
|
|
return static_cast<byte*>(result);
|
|
}
|
|
|
|
VirtualMemory::VirtualMemory() : address_(nullptr), size_(0) {}
|
|
|
|
VirtualMemory::VirtualMemory(size_t size, void* hint, size_t alignment)
|
|
: address_(nullptr), size_(0) {
|
|
size_t page_size = base::OS::AllocatePageSize();
|
|
size_t alloc_size = RoundUp(size, page_size);
|
|
address_ = base::OS::Allocate(hint, alloc_size, alignment,
|
|
base::OS::MemoryPermission::kNoAccess);
|
|
if (address_ != nullptr) {
|
|
size_ = alloc_size;
|
|
#if defined(LEAK_SANITIZER)
|
|
__lsan_register_root_region(address_, size_);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
VirtualMemory::~VirtualMemory() {
|
|
if (IsReserved()) {
|
|
Release();
|
|
}
|
|
}
|
|
|
|
void VirtualMemory::Reset() {
|
|
address_ = nullptr;
|
|
size_ = 0;
|
|
}
|
|
|
|
bool VirtualMemory::Commit(void* address, size_t size, bool is_executable) {
|
|
CHECK(InVM(address, size));
|
|
return base::OS::CommitRegion(address, size, is_executable);
|
|
}
|
|
|
|
bool VirtualMemory::Uncommit(void* address, size_t size) {
|
|
CHECK(InVM(address, size));
|
|
return base::OS::UncommitRegion(address, size);
|
|
}
|
|
|
|
bool VirtualMemory::Guard(void* address) {
|
|
CHECK(InVM(address, base::OS::CommitPageSize()));
|
|
base::OS::Guard(address, base::OS::CommitPageSize());
|
|
return true;
|
|
}
|
|
|
|
size_t VirtualMemory::ReleasePartial(void* free_start) {
|
|
DCHECK(IsReserved());
|
|
// Notice: Order is important here. The VirtualMemory object might live
|
|
// inside the allocated region.
|
|
const size_t free_size = size_ - (reinterpret_cast<size_t>(free_start) -
|
|
reinterpret_cast<size_t>(address_));
|
|
CHECK(InVM(free_start, free_size));
|
|
DCHECK_LT(address_, free_start);
|
|
DCHECK_LT(free_start, reinterpret_cast<void*>(
|
|
reinterpret_cast<size_t>(address_) + size_));
|
|
#if defined(LEAK_SANITIZER)
|
|
__lsan_unregister_root_region(address_, size_);
|
|
__lsan_register_root_region(address_, size_ - free_size);
|
|
#endif
|
|
const bool result = base::OS::ReleasePartialRegion(free_start, free_size);
|
|
USE(result);
|
|
DCHECK(result);
|
|
size_ -= free_size;
|
|
return free_size;
|
|
}
|
|
|
|
void VirtualMemory::Release() {
|
|
DCHECK(IsReserved());
|
|
// Notice: Order is important here. The VirtualMemory object might live
|
|
// inside the allocated region.
|
|
void* address = address_;
|
|
size_t size = size_;
|
|
CHECK(InVM(address, size));
|
|
Reset();
|
|
bool result = base::OS::Free(address, size);
|
|
USE(result);
|
|
DCHECK(result);
|
|
}
|
|
|
|
void VirtualMemory::TakeControl(VirtualMemory* from) {
|
|
DCHECK(!IsReserved());
|
|
address_ = from->address_;
|
|
size_ = from->size_;
|
|
from->Reset();
|
|
}
|
|
|
|
bool AllocVirtualMemory(size_t size, void* hint, VirtualMemory* result) {
|
|
VirtualMemory first_try(size, hint);
|
|
if (first_try.IsReserved()) {
|
|
result->TakeControl(&first_try);
|
|
return true;
|
|
}
|
|
|
|
V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
|
|
VirtualMemory second_try(size, hint);
|
|
result->TakeControl(&second_try);
|
|
return result->IsReserved();
|
|
}
|
|
|
|
bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint,
|
|
VirtualMemory* result) {
|
|
VirtualMemory first_try(size, hint, alignment);
|
|
if (first_try.IsReserved()) {
|
|
result->TakeControl(&first_try);
|
|
return true;
|
|
}
|
|
|
|
V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
|
|
VirtualMemory second_try(size, hint, alignment);
|
|
result->TakeControl(&second_try);
|
|
return result->IsReserved();
|
|
}
|
|
|
|
} // namespace internal
|
|
} // namespace v8
|