Commit 6c3ad8f9 authored by Alex Ilin's avatar Alex Ilin Committed by Commit Bot

Remove base::SharedMemory from media::UnalignedSharedMemory

This CL removes all usages of base::SharedMemory and
base::SharedMemoryHandle from media::UnalignedSharedMemory since
there are no more callers of the constructor using these classes.

UnalignedSharedMemory itself is deprecated and should be eventually
removed as part of shared memory refactoring in future CLs.

Bug: 795291
Change-Id: I808c9fff08923f3414643e6258667dba2365f621
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1917376
Commit-Queue: Alex Ilin <alexilin@chromium.org>
Reviewed-by: default avatarThomas Guilbert <tguilbert@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715854}
parent f675e316
......@@ -46,11 +46,6 @@ bool CalculateMisalignmentAndOffset(size_t size,
}
} // namespace
UnalignedSharedMemory::UnalignedSharedMemory(
const base::SharedMemoryHandle& handle,
size_t size,
bool read_only)
: shm_(handle, read_only), read_only_(read_only), size_(size) {}
UnalignedSharedMemory::UnalignedSharedMemory(
base::subtle::PlatformSharedMemoryRegion region,
......@@ -74,36 +69,27 @@ bool UnalignedSharedMemory::MapAt(off_t offset, size_t size) {
return false;
}
if (region_.IsValid()) {
if (read_only_) {
auto shm =
base::ReadOnlySharedMemoryRegion::Deserialize(std::move(region_));
read_only_mapping_ = shm.MapAt(adjusted_offset, size + misalignment);
if (!read_only_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory";
return false;
}
// TODO(crbug.com/849207): this ugly const cast will go away when uses of
// UnalignedSharedMemory are converted to
// {Writable,ReadOnly}UnalignedMapping.
mapping_ptr_ = const_cast<uint8_t*>(
static_cast<const uint8_t*>(read_only_mapping_.memory()));
} else {
auto shm =
base::UnsafeSharedMemoryRegion::Deserialize(std::move(region_));
writable_mapping_ = shm.MapAt(adjusted_offset, size + misalignment);
if (!writable_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory";
return false;
}
mapping_ptr_ = static_cast<uint8_t*>(writable_mapping_.memory());
if (read_only_) {
auto shm =
base::ReadOnlySharedMemoryRegion::Deserialize(std::move(region_));
read_only_mapping_ = shm.MapAt(adjusted_offset, size + misalignment);
if (!read_only_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory";
return false;
}
// TODO(crbug.com/849207): this ugly const cast will go away when uses of
// UnalignedSharedMemory are converted to
// {Writable,ReadOnly}UnalignedMapping.
mapping_ptr_ = const_cast<uint8_t*>(
static_cast<const uint8_t*>(read_only_mapping_.memory()));
} else {
if (!shm_.MapAt(adjusted_offset, size + misalignment)) {
auto shm = base::UnsafeSharedMemoryRegion::Deserialize(std::move(region_));
writable_mapping_ = shm.MapAt(adjusted_offset, size + misalignment);
if (!writable_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory";
return false;
}
mapping_ptr_ = static_cast<uint8_t*>(shm_.memory());
mapping_ptr_ = static_cast<uint8_t*>(writable_mapping_.memory());
}
DCHECK(mapping_ptr_);
......
......@@ -10,25 +10,20 @@
#include "base/macros.h"
#include "base/memory/platform_shared_memory_region.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/shared_memory.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "media/base/media_export.h"
namespace media {
// Wrapper over base::SharedMemory that can be mapped at unaligned offsets.
// Wrapper over base::PlatformSharedMemoryRegion that can be mapped at unaligned
// offsets.
// DEPRECATED! See https://crbug.com/795291.
class MEDIA_EXPORT UnalignedSharedMemory {
public:
// Creates an |UnalignedSharedMemory| instance from a
// |SharedMemoryHandle|. |size| sets the maximum size that may be mapped. This
// instance will own the handle.
UnalignedSharedMemory(const base::SharedMemoryHandle& handle,
size_t size,
bool read_only);
// As above, but from a PlatformSharedMemoryRegion.
// |PlatformSharedMemoryRegion|. |size| sets the maximum size that may be
// mapped. This instance will own the handle.
UnalignedSharedMemory(base::subtle::PlatformSharedMemoryRegion region,
size_t size,
bool read_only);
......@@ -42,11 +37,9 @@ class MEDIA_EXPORT UnalignedSharedMemory {
void* memory() const { return mapping_ptr_; }
private:
// Either |shm_| or the set |region_| and one of the mappings are active,
// depending on which constructor was used and the value of read_only_. These
// variables are held to keep the shared memory mapping valid for the lifetime
// of this instance.
base::SharedMemory shm_;
// Only one of the mappings is active, depending on the value of |read_only_|.
// These variables are held to keep the shared memory mapping valid for the
// lifetime of this instance.
base::subtle::PlatformSharedMemoryRegion region_;
base::WritableSharedMemoryMapping writable_mapping_;
base::ReadOnlySharedMemoryMapping read_only_mapping_;
......@@ -54,7 +47,7 @@ class MEDIA_EXPORT UnalignedSharedMemory {
// If the mapping should be made read-only.
bool read_only_;
// The size of the region associated with |shm_|.
// The size of the region associated with |region_|.
size_t size_;
// Pointer to the unaligned data in the shared memory mapping.
......
......@@ -10,7 +10,6 @@
#include <limits>
#include "base/logging.h"
#include "base/memory/shared_memory.h"
#include "base/stl_util.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -25,13 +24,6 @@ const off_t kUnalignedOffset = 3;
const uint8_t kData[] = "hello";
const size_t kDataSize = base::size(kData);
base::SharedMemoryHandle CreateHandle(const uint8_t* data, size_t size) {
base::SharedMemory shm;
EXPECT_TRUE(shm.CreateAndMapAnonymous(size));
memcpy(shm.memory(), data, size);
return shm.TakeHandle();
}
base::UnsafeSharedMemoryRegion CreateRegion(const uint8_t* data, size_t size) {
auto region = base::UnsafeSharedMemoryRegion::Create(size);
auto mapping = region.Map();
......@@ -49,11 +41,6 @@ base::ReadOnlySharedMemoryRegion CreateReadOnlyRegion(const uint8_t* data,
}
} // namespace
TEST(UnalignedSharedMemoryTest, CreateAndDestroy) {
auto handle = CreateHandle(kData, kDataSize);
UnalignedSharedMemory shm(handle, kDataSize, true);
}
TEST(UnalignedSharedMemoryTest, CreateAndDestroyRegion) {
auto region = CreateRegion(kData, kDataSize);
UnalignedSharedMemory shm(
......@@ -70,23 +57,11 @@ TEST(UnalignedSharedMemoryTest, CreateAndDestroyReadOnlyRegion) {
kDataSize, true);
}
TEST(UnalignedSharedMemoryTest, CreateAndDestroy_InvalidHandle) {
base::SharedMemoryHandle handle;
UnalignedSharedMemory shm(handle, kDataSize, true);
}
TEST(UnalignedSharedMemoryTest, CreateAndDestroy_InvalidRegion) {
UnalignedSharedMemory shm(base::subtle::PlatformSharedMemoryRegion(),
kDataSize, false);
}
TEST(UnalignedSharedMemoryTest, Map) {
auto handle = CreateHandle(kData, kDataSize);
UnalignedSharedMemory shm(handle, kDataSize, true);
ASSERT_TRUE(shm.MapAt(0, kDataSize));
EXPECT_EQ(0, memcmp(shm.memory(), kData, kDataSize));
}
TEST(UnalignedSharedMemoryTest, MapRegion) {
auto region = CreateRegion(kData, kDataSize);
UnalignedSharedMemory shm(
......@@ -107,13 +82,6 @@ TEST(UnalignedSharedMemoryTest, MapReadOnlyRegion) {
EXPECT_EQ(0, memcmp(shm.memory(), kData, kDataSize));
}
TEST(UnalignedSharedMemoryTest, Map_Unaligned) {
auto handle = CreateHandle(kUnalignedData, kUnalignedDataSize);
UnalignedSharedMemory shm(handle, kUnalignedDataSize, true);
ASSERT_TRUE(shm.MapAt(kUnalignedOffset, kDataSize));
EXPECT_EQ(0, memcmp(shm.memory(), kData, kDataSize));
}
TEST(UnalignedSharedMemoryTest, Map_UnalignedRegion) {
auto region = CreateRegion(kUnalignedData, kUnalignedDataSize);
UnalignedSharedMemory shm(
......@@ -134,13 +102,6 @@ TEST(UnalignedSharedMemoryTest, Map_UnalignedReadOnlyRegion) {
EXPECT_EQ(0, memcmp(shm.memory(), kData, kDataSize));
}
TEST(UnalignedSharedMemoryTest, Map_InvalidHandle) {
base::SharedMemoryHandle handle;
UnalignedSharedMemory shm(handle, kDataSize, true);
ASSERT_FALSE(shm.MapAt(1, kDataSize));
EXPECT_EQ(shm.memory(), nullptr);
}
TEST(UnalignedSharedMemoryTest, Map_InvalidRegion) {
UnalignedSharedMemory shm(base::subtle::PlatformSharedMemoryRegion(),
kDataSize, true);
......@@ -148,12 +109,6 @@ TEST(UnalignedSharedMemoryTest, Map_InvalidRegion) {
EXPECT_EQ(shm.memory(), nullptr);
}
TEST(UnalignedSharedMemoryTest, Map_NegativeOffset) {
auto handle = CreateHandle(kData, kDataSize);
UnalignedSharedMemory shm(handle, kDataSize, true);
ASSERT_FALSE(shm.MapAt(-1, kDataSize));
}
TEST(UnalignedSharedMemoryTest, Map_NegativeOffsetRegion) {
auto region = CreateRegion(kData, kDataSize);
UnalignedSharedMemory shm(
......@@ -172,12 +127,6 @@ TEST(UnalignedSharedMemoryTest, Map_NegativeOffsetReadOnlyRegion) {
ASSERT_FALSE(shm.MapAt(-1, kDataSize));
}
TEST(UnalignedSharedMemoryTest, Map_SizeOverflow) {
auto handle = CreateHandle(kData, kDataSize);
UnalignedSharedMemory shm(handle, kDataSize, true);
ASSERT_FALSE(shm.MapAt(1, std::numeric_limits<size_t>::max()));
}
TEST(UnalignedSharedMemoryTest, Map_SizeOverflowRegion) {
auto region = CreateRegion(kData, kDataSize);
UnalignedSharedMemory shm(
......@@ -196,12 +145,6 @@ TEST(UnalignedSharedMemoryTest, Map_SizeOverflowReadOnlyRegion) {
ASSERT_FALSE(shm.MapAt(1, std::numeric_limits<size_t>::max()));
}
TEST(UnalignedSharedMemoryTest, UnmappedIsNullptr) {
auto handle = CreateHandle(kData, kDataSize);
UnalignedSharedMemory shm(handle, kDataSize, true);
ASSERT_EQ(shm.memory(), nullptr);
}
TEST(UnalignedSharedMemoryTest, UnmappedRegionIsNullptr) {
auto region = CreateRegion(kData, kDataSize);
UnalignedSharedMemory shm(
......
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