Advertise a minimum alignment for SharedMemory::Map().

No real changes, just adds a DCHECK() and unit test for a minimum
alignment that callers can count on from Map().  Map() is already
returning addresses which are aligned to the platform page size.

Necessary for a media use case in which a wrapped shared memory
object is passed around and is expected to have a certain alignment.

BUG=none
TEST=unittests, try bots.


Review URL: https://chromiumcodereview.appspot.com/10827400

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152218 0039d316-1c4b-4281-b951-d872f2087c98
parent 258082ab
......@@ -142,8 +142,10 @@ class BASE_EXPORT SharedMemory {
// Maps the shared memory into the caller's address space.
// Returns true on success, false otherwise. The memory address
// is accessed via the memory() accessor.
// is accessed via the memory() accessor. The mapped address is guaranteed to
// have an alignment of at least MAP_MINIMUM_ALIGNMENT.
bool Map(uint32 bytes);
enum { MAP_MINIMUM_ALIGNMENT = 32 };
// Unmaps the shared memory from the caller's address space.
// Returns true if successful; returns false on error or if the
......
......@@ -90,10 +90,13 @@ bool SharedMemory::Map(uint32 bytes) {
MAP_SHARED, mapped_file_, 0);
bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL;
if (mmap_succeeded)
if (mmap_succeeded) {
mapped_size_ = bytes;
else
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
} else {
memory_ = NULL;
}
return mmap_succeeded;
}
......
......@@ -240,10 +240,13 @@ bool SharedMemory::Map(uint32 bytes) {
MAP_SHARED, mapped_file_, 0);
bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL;
if (mmap_succeeded)
if (mmap_succeeded) {
mapped_size_ = bytes;
else
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
} else {
memory_ = NULL;
}
return mmap_succeeded;
}
......
......@@ -361,6 +361,19 @@ TEST(SharedMemoryTest, AnonymousExecutable) {
}
#endif
// Map() will return addresses which are aligned to the platform page size, this
// varies from platform to platform though. Since we'd like to advertise a
// minimum alignment that callers can count on, test for it here.
TEST(SharedMemoryTest, MapMinimumAlignment) {
static const int kDataSize = 8192;
SharedMemory shared_memory;
ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize));
EXPECT_EQ(0U, reinterpret_cast<uintptr_t>(
shared_memory.memory()) & (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
shared_memory.Close();
}
#if !defined(OS_IOS) // iOS does not allow multiple processes.
// On POSIX it is especially important we test shmem across processes,
......
......@@ -136,6 +136,8 @@ bool SharedMemory::Map(uint32 bytes) {
memory_ = MapViewOfFile(mapped_file_,
read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes);
if (memory_ != NULL) {
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
return true;
}
return false;
......
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