[base] Align the address hint in VirtualMemory.
BUG=chromium:739644 Change-Id: I6c7d0f48c959826dd2a8587d7a321be4387ef39f Reviewed-on: https://chromium-review.googlesource.com/586529 Reviewed-by: Michael Lippautz <mlippautz@chromium.org> Commit-Queue: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#46936}
This commit is contained in:
parent
37680d6563
commit
1d32273a49
@ -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",
|
||||
|
25
test/unittests/base/macros-unittest.cc
Normal file
25
test/unittests/base/macros-unittest.cc
Normal file
@ -0,0 +1,25 @@
|
||||
// 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',
|
||||
|
Loading…
Reference in New Issue
Block a user