Commit 1d32273a authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

[base] Align the address hint in VirtualMemory.

BUG=chromium:739644

Change-Id: I6c7d0f48c959826dd2a8587d7a321be4387ef39f
Reviewed-on: https://chromium-review.googlesource.com/586529Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46936}
parent 37680d65
......@@ -321,4 +321,11 @@ constexpr inline T RoundUp(T x) {
return RoundDown<m, T>(static_cast<T>(x + m - 1));
}
inline void* AlignedAddress(void* address, size_t alignment) {
// The alignment must be a power of two.
DCHECK_EQ(alignment & (alignment - 1), 0u);
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) &
~static_cast<uintptr_t>(alignment - 1));
}
#endif // V8_BASE_MACROS_H_
......@@ -138,6 +138,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size =
RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation = mmap(hint, request_size, PROT_NONE,
......
......@@ -161,6 +161,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
hint = AlignedAddress(hint, alignment);
DCHECK((alignment % OS::AllocateAlignment()) == 0);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
......
......@@ -116,6 +116,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
hint = AlignedAddress(hint, alignment);
DCHECK((alignment % OS::AllocateAlignment()) == 0);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
......
......@@ -202,6 +202,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size =
RoundUp(size + alignment, static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation =
......
......@@ -105,6 +105,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation =
......
......@@ -147,6 +147,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation =
......
......@@ -173,6 +173,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation =
......
......@@ -92,6 +92,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
void* reservation =
......
......@@ -1214,6 +1214,7 @@ VirtualMemory::VirtualMemory(size_t size, void* hint)
VirtualMemory::VirtualMemory(size_t size, size_t alignment, void* hint)
: address_(NULL), size_(0) {
DCHECK((alignment % OS::AllocateAlignment()) == 0);
hint = AlignedAddress(hint, alignment);
size_t request_size = RoundUp(size + alignment,
static_cast<intptr_t>(OS::AllocateAlignment()));
void* address = ReserveRegion(request_size, hint);
......
......@@ -29,6 +29,7 @@ v8_executable("unittests") {
"base/ieee754-unittest.cc",
"base/iterator-unittest.cc",
"base/logging-unittest.cc",
"base/macros-unittest.cc",
"base/ostreams-unittest.cc",
"base/platform/condition-variable-unittest.cc",
"base/platform/mutex-unittest.cc",
......
// Copyright 2017 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/base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace base {
TEST(AlignedAddressTest, AlignedAddress) {
EXPECT_EQ(reinterpret_cast<void*>(0xFFFF0),
AlignedAddress(reinterpret_cast<void*>(0xFFFF0), 16));
EXPECT_EQ(reinterpret_cast<void*>(0xFFFF0),
AlignedAddress(reinterpret_cast<void*>(0xFFFF2), 16));
EXPECT_EQ(reinterpret_cast<void*>(0xFFFF0),
AlignedAddress(reinterpret_cast<void*>(0xFFFF2), 16));
EXPECT_EQ(reinterpret_cast<void*>(0xFFFF0),
AlignedAddress(reinterpret_cast<void*>(0xFFFFF), 16));
EXPECT_EQ(reinterpret_cast<void*>(0x0),
AlignedAddress(reinterpret_cast<void*>(0xFFFFF), 0x100000));
}
} // namespace base
} // namespace v8
......@@ -25,6 +25,7 @@
'base/functional-unittest.cc',
'base/ieee754-unittest.cc',
'base/logging-unittest.cc',
'base/macros-unittest.cc',
'base/iterator-unittest.cc',
'base/ostreams-unittest.cc',
'base/platform/condition-variable-unittest.cc',
......
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