Fix some nits in some base/ shared_memory* files.
Also clarify whether iOS builds with shared_memory_mac.cc or shared_memory_posix.cc. Change-Id: Ic257e6f77fec818f00af58a9eb59a6b5d8687e1f Reviewed-on: https://chromium-review.googlesource.com/797674 Reviewed-by: Eugene But <eugenebut@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org> Cr-Commit-Position: refs/heads/master@{#520411}
This commit is contained in:
@ -25,7 +25,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
|
||||
// "name" is just a label in ashmem. It is visible in /proc/pid/maps.
|
||||
int fd = ashmem_create_region(
|
||||
options.name_deprecated == NULL ? "" : options.name_deprecated->c_str(),
|
||||
options.name_deprecated ? options.name_deprecated->c_str() : "",
|
||||
options.size);
|
||||
shm_ = SharedMemoryHandle::ImportHandle(fd, options.size);
|
||||
if (!shm_.IsValid()) {
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/files/scoped_file.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/mac/foundation_util.h"
|
||||
#include "base/mac/mac_util.h"
|
||||
#include "base/mac/scoped_mach_vm.h"
|
||||
#include "base/memory/shared_memory_helper.h"
|
||||
@ -29,9 +30,9 @@
|
||||
#include "base/unguessable_token.h"
|
||||
#include "build/build_config.h"
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
#include "base/mac/foundation_util.h"
|
||||
#endif // OS_MACOSX
|
||||
#if defined(OS_IOS)
|
||||
#error "MacOS only - iOS uses shared_memory_posix.cc"
|
||||
#endif
|
||||
|
||||
namespace base {
|
||||
|
||||
@ -143,7 +144,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
// This function theoretically can block on the disk. Both profiling of real
|
||||
// users and local instrumentation shows that this is a real problem.
|
||||
// https://code.google.com/p/chromium/issues/detail?id=466437
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
|
||||
ScopedFD fd;
|
||||
ScopedFD readonly_fd;
|
||||
@ -195,14 +196,14 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) {
|
||||
mapped_id_ = shm_.GetGUID();
|
||||
SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
|
||||
} else {
|
||||
memory_ = NULL;
|
||||
memory_ = nullptr;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool SharedMemory::Unmap() {
|
||||
if (memory_ == NULL)
|
||||
if (!memory_)
|
||||
return false;
|
||||
|
||||
SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
|
||||
@ -216,7 +217,7 @@ bool SharedMemory::Unmap() {
|
||||
mapped_size_);
|
||||
break;
|
||||
}
|
||||
memory_ = NULL;
|
||||
memory_ = nullptr;
|
||||
mapped_size_ = 0;
|
||||
mapped_id_ = UnguessableToken();
|
||||
return true;
|
||||
@ -252,7 +253,7 @@ SharedMemoryHandle SharedMemory::GetReadOnlyHandle() {
|
||||
}
|
||||
|
||||
DCHECK(shm_.IsValid());
|
||||
base::SharedMemoryHandle new_handle;
|
||||
SharedMemoryHandle new_handle;
|
||||
bool success = MakeMachSharedMemoryHandleReadOnly(&new_handle, shm_, memory_);
|
||||
if (success)
|
||||
new_handle.SetOwnershipPassesToIPC(true);
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/files/scoped_file.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/shared_memory_helper.h"
|
||||
#include "base/memory/shared_memory_tracker.h"
|
||||
#include "base/posix/eintr_wrapper.h"
|
||||
@ -35,6 +36,10 @@
|
||||
#include "third_party/ashmem/ashmem.h"
|
||||
#endif
|
||||
|
||||
#if defined(OS_MACOSX) && !defined(OS_IOS)
|
||||
#error "MacOS uses shared_memory_mac.cc"
|
||||
#endif
|
||||
|
||||
namespace base {
|
||||
|
||||
SharedMemory::SharedMemory() = default;
|
||||
@ -60,7 +65,7 @@ void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
|
||||
|
||||
// static
|
||||
size_t SharedMemory::GetHandleLimit() {
|
||||
return base::GetMaxFds();
|
||||
return GetMaxFds();
|
||||
}
|
||||
|
||||
// static
|
||||
@ -132,13 +137,13 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
// This function theoretically can block on the disk, but realistically
|
||||
// the temporary files we create will just go into the buffer cache
|
||||
// and be deleted before they ever make it out to disk.
|
||||
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
ThreadRestrictions::ScopedAllowIO allow_io;
|
||||
|
||||
bool fix_size = true;
|
||||
ScopedFD fd;
|
||||
ScopedFD readonly_fd;
|
||||
FilePath path;
|
||||
if (options.name_deprecated == nullptr || options.name_deprecated->empty()) {
|
||||
if (!options.name_deprecated || options.name_deprecated->empty()) {
|
||||
bool result = false;
|
||||
#if defined(__NR_memfd_create)
|
||||
result = CreateMemFDSharedMemory(options, &fd, &readonly_fd, &path);
|
||||
@ -240,10 +245,10 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
|
||||
bool result = PrepareMapFile(std::move(fd), std::move(readonly_fd),
|
||||
&mapped_file, &readonly_mapped_file);
|
||||
shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false),
|
||||
options.size, UnguessableToken::Create());
|
||||
shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), options.size,
|
||||
UnguessableToken::Create());
|
||||
readonly_shm_ =
|
||||
SharedMemoryHandle(base::FileDescriptor(readonly_mapped_file, false),
|
||||
SharedMemoryHandle(FileDescriptor(readonly_mapped_file, false),
|
||||
options.size, shm_.GetGUID());
|
||||
return result;
|
||||
}
|
||||
@ -257,7 +262,7 @@ bool SharedMemory::Delete(const std::string& name) {
|
||||
return false;
|
||||
|
||||
if (PathExists(path))
|
||||
return base::DeleteFile(path, false);
|
||||
return DeleteFile(path, false);
|
||||
|
||||
// Doesn't exist, so success.
|
||||
return true;
|
||||
@ -291,10 +296,10 @@ bool SharedMemory::Open(const std::string& name, bool read_only) {
|
||||
// single version of the service process.
|
||||
// We pass the size |0|, which is a dummy size and wrong, but otherwise
|
||||
// harmless.
|
||||
shm_ = SharedMemoryHandle(base::FileDescriptor(mapped_file, false), 0u,
|
||||
shm_ = SharedMemoryHandle(FileDescriptor(mapped_file, false), 0u,
|
||||
UnguessableToken::Create());
|
||||
readonly_shm_ = SharedMemoryHandle(
|
||||
base::FileDescriptor(readonly_mapped_file, false), 0, shm_.GetGUID());
|
||||
FileDescriptor(readonly_mapped_file, false), 0, shm_.GetGUID());
|
||||
return result;
|
||||
}
|
||||
#endif // !defined(OS_ANDROID)
|
||||
@ -324,7 +329,7 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) {
|
||||
memory_ = mmap(nullptr, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
|
||||
MAP_SHARED, shm_.GetHandle(), offset);
|
||||
|
||||
bool mmap_succeeded = memory_ != (void*)-1 && memory_ != nullptr;
|
||||
bool mmap_succeeded = memory_ && memory_ != reinterpret_cast<void*>(-1);
|
||||
if (mmap_succeeded) {
|
||||
mapped_size_ = bytes;
|
||||
mapped_id_ = shm_.GetGUID();
|
||||
@ -340,7 +345,7 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) {
|
||||
}
|
||||
|
||||
bool SharedMemory::Unmap() {
|
||||
if (memory_ == nullptr)
|
||||
if (!memory_)
|
||||
return false;
|
||||
|
||||
SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
|
||||
@ -391,11 +396,12 @@ bool SharedMemory::FilePathForMemoryName(const std::string& mem_name,
|
||||
return false;
|
||||
|
||||
#if defined(GOOGLE_CHROME_BUILD)
|
||||
std::string name_base = std::string("com.google.Chrome");
|
||||
static const char kShmem[] = "com.google.Chrome.shmem.";
|
||||
#else
|
||||
std::string name_base = std::string("org.chromium.Chromium");
|
||||
static const char kShmem[] = "org.chromium.Chromium.shmem.";
|
||||
#endif
|
||||
*path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name);
|
||||
CR_DEFINE_STATIC_LOCAL(const std::string, name_base, (kShmem));
|
||||
*path = temp_dir.AppendASCII(name_base + mem_name);
|
||||
return true;
|
||||
}
|
||||
#endif // !defined(OS_ANDROID)
|
||||
|
@ -177,8 +177,8 @@ bool SharedMemory::CreateAndMapAnonymous(size_t size) {
|
||||
}
|
||||
|
||||
bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
// TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
|
||||
// wasting 32k per mapping on average.
|
||||
// TODO(crbug.com/210609): NaCl forces us to round up 64k here, wasting 32k
|
||||
// per mapping on average.
|
||||
static const size_t kSectionMask = 65536 - 1;
|
||||
DCHECK(!options.executable);
|
||||
DCHECK(!shm_.IsValid());
|
||||
@ -197,7 +197,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
|
||||
size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
|
||||
name_ = options.name_deprecated ?
|
||||
ASCIIToUTF16(*options.name_deprecated) : L"";
|
||||
SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, FALSE };
|
||||
SECURITY_ATTRIBUTES sa = {sizeof(sa), nullptr, FALSE};
|
||||
SECURITY_DESCRIPTOR sd;
|
||||
ACL dacl;
|
||||
|
||||
@ -314,25 +314,26 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) {
|
||||
shm_.GetHandle(),
|
||||
read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
|
||||
static_cast<uint64_t>(offset) >> 32, static_cast<DWORD>(offset), bytes);
|
||||
if (memory_ != NULL) {
|
||||
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
|
||||
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
|
||||
mapped_size_ = GetMemorySectionSize(memory_);
|
||||
mapped_id_ = shm_.GetGUID();
|
||||
SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
|
||||
return true;
|
||||
if (!memory_) {
|
||||
DPLOG(ERROR) << "Failed executing MapViewOfFile";
|
||||
return false;
|
||||
}
|
||||
DPLOG(ERROR) << "Failed executing MapViewOfFile";
|
||||
return false;
|
||||
|
||||
DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
|
||||
(SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
|
||||
mapped_size_ = GetMemorySectionSize(memory_);
|
||||
mapped_id_ = shm_.GetGUID();
|
||||
SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SharedMemory::Unmap() {
|
||||
if (memory_ == NULL)
|
||||
if (!memory_)
|
||||
return false;
|
||||
|
||||
SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
|
||||
UnmapViewOfFile(memory_);
|
||||
memory_ = NULL;
|
||||
memory_ = nullptr;
|
||||
mapped_id_ = UnguessableToken();
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user