Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Unified Diff: base/memory/shared_memory_win.cc

Issue 2854833004: Use SharedMemoryHandle instead ScopedHandle as ivar for SharedMemory (Closed)
Patch Set: close should be idempotent. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/shared_memory_win.cc
diff --git a/base/memory/shared_memory_win.cc b/base/memory/shared_memory_win.cc
index 1dfa10fbe58b54b689d69c7ccde9e03015f9e758..daa700da6a373cd491ce56228f9b58037517c23f 100644
--- a/base/memory/shared_memory_win.cc
+++ b/base/memory/shared_memory_win.cc
@@ -152,12 +152,11 @@ SharedMemory::SharedMemory(const std::wstring& name)
SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only)
: external_section_(true),
+ shm_(handle),
mapped_size_(0),
memory_(NULL),
read_only_(read_only),
- requested_size_(0) {
- mapped_file_.Set(handle.GetHandle());
-}
+ requested_size_(0) {}
SharedMemory::~SharedMemory() {
Unmap();
@@ -196,7 +195,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
// wasting 32k per mapping on average.
static const size_t kSectionMask = 65536 - 1;
DCHECK(!options.executable);
- DCHECK(!mapped_file_.Get());
+ DCHECK(!shm_.IsValid());
if (options.size == 0) {
LogError(SIZE_ZERO, 0);
return false;
@@ -241,9 +240,9 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
rand_values[2], rand_values[3]);
}
DCHECK(!name_.empty());
- mapped_file_.Set(CreateFileMappingWithReducedPermissions(&sa, rounded_size,
- name_.c_str()));
- if (!mapped_file_.IsValid()) {
+ shm_ = SharedMemoryHandle(CreateFileMappingWithReducedPermissions(
+ &sa, rounded_size, name_.c_str()));
+ if (!shm_.IsValid()) {
// The error is logged within CreateFileMappingWithReducedPermissions().
return false;
}
@@ -274,15 +273,15 @@ bool SharedMemory::Delete(const std::string& name) {
}
bool SharedMemory::Open(const std::string& name, bool read_only) {
- DCHECK(!mapped_file_.Get());
+ DCHECK(!shm_.IsValid());
DWORD access = FILE_MAP_READ | SECTION_QUERY;
if (!read_only)
access |= FILE_MAP_WRITE;
name_ = ASCIIToUTF16(name);
read_only_ = read_only;
- mapped_file_.Set(
+ shm_ = SharedMemoryHandle(
OpenFileMapping(access, false, name_.empty() ? nullptr : name_.c_str()));
- if (!mapped_file_.IsValid())
+ if (!shm_.IsValid())
return false;
// If a name specified assume it's an external section.
if (!name_.empty())
@@ -292,7 +291,7 @@ bool SharedMemory::Open(const std::string& name, bool read_only) {
}
bool SharedMemory::MapAt(off_t offset, size_t bytes) {
- if (!mapped_file_.Get())
+ if (!shm_.IsValid())
return false;
if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
@@ -301,11 +300,11 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) {
if (memory_)
return false;
- if (external_section_ && !IsSectionSafeToMap(mapped_file_.Get()))
+ if (external_section_ && !IsSectionSafeToMap(shm_.GetHandle()))
return false;
memory_ = MapViewOfFile(
- mapped_file_.Get(),
+ 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) {
@@ -329,7 +328,7 @@ bool SharedMemory::Unmap() {
SharedMemoryHandle SharedMemory::GetReadOnlyHandle() {
HANDLE result;
ProcessHandle process = GetCurrentProcess();
- if (!::DuplicateHandle(process, mapped_file_.Get(), process, &result,
+ if (!::DuplicateHandle(process, shm_.GetHandle(), process, &result,
FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) {
return SharedMemoryHandle();
}
@@ -339,16 +338,20 @@ SharedMemoryHandle SharedMemory::GetReadOnlyHandle() {
}
void SharedMemory::Close() {
- mapped_file_.Close();
+ if (shm_.IsValid()) {
+ shm_.Close();
+ shm_ = SharedMemoryHandle();
+ }
}
SharedMemoryHandle SharedMemory::handle() const {
- return SharedMemoryHandle(mapped_file_.Get());
+ return shm_;
}
SharedMemoryHandle SharedMemory::TakeHandle() {
- SharedMemoryHandle handle(mapped_file_.Take());
+ SharedMemoryHandle handle(shm_);
handle.SetOwnershipPassesToIPC(true);
+ shm_ = SharedMemoryHandle();
memory_ = nullptr;
mapped_size_ = 0;
return handle;
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698