Make SharedMemory::TakeHandle() unmap memory.

TakeHandle() was resetting |memory_| without unmapping the shared
memory. This leaks resources because it's no longer possible to unmap
after that point.

Bug: 804399
Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: I42397ec8dc768ec6d9b2affcaee829f6c8a8ea00
Reviewed-on: https://chromium-review.googlesource.com/897969
Commit-Queue: kylechar <kylechar@chromium.org>
Reviewed-by: Erik Chen <erikchen@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#534109}(cherry picked from commit 58fd01a7e6537738c1f6a7063cb54bdc33d1e9a8)
Reviewed-on: https://chromium-review.googlesource.com/929741
Reviewed-by: Siddhartha S <ssid@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#542}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/base/memory/shared_memory.h b/base/memory/shared_memory.h
index 2ab7870..2eb9ee2 100644
--- a/base/memory/shared_memory.h
+++ b/base/memory/shared_memory.h
@@ -189,11 +189,10 @@
   // identifier is not portable.
   SharedMemoryHandle handle() const;
 
-  // Returns the underlying OS handle for this segment. The caller also gets
-  // ownership of the handle. This is logically equivalent to:
-  //   SharedMemoryHandle dup = DuplicateHandle(handle());
-  //   Close();
-  //   return dup;
+  // Returns the underlying OS handle for this segment. The caller takes
+  // ownership of the handle and memory is unmapped. This is equivalent to
+  // duplicating the handle and then calling Unmap() and Close() on this object,
+  // without the overhead of duplicating the handle.
   SharedMemoryHandle TakeHandle();
 
   // Closes the open shared memory segment. The memory will remain mapped if
diff --git a/base/memory/shared_memory_fuchsia.cc b/base/memory/shared_memory_fuchsia.cc
index a76f4f1..4036bf6 100644
--- a/base/memory/shared_memory_fuchsia.cc
+++ b/base/memory/shared_memory_fuchsia.cc
@@ -138,15 +138,8 @@
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle handle(shm_);
   handle.SetOwnershipPassesToIPC(true);
+  Unmap();
   shm_ = SharedMemoryHandle();
-  // TODO(ssid): Find some way to track the shared memory in this case
-  // https://crbug/804399.
-  if (memory_) {
-    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
-    mapped_id_ = UnguessableToken();
-  }
-  memory_ = nullptr;
-  mapped_size_ = 0;
   return handle;
 }
 
diff --git a/base/memory/shared_memory_mac.cc b/base/memory/shared_memory_mac.cc
index e2735f7..0a233e5 100644
--- a/base/memory/shared_memory_mac.cc
+++ b/base/memory/shared_memory_mac.cc
@@ -229,6 +229,7 @@
 
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle dup = DuplicateHandle(handle());
+  Unmap();
   Close();
   return dup;
 }
diff --git a/base/memory/shared_memory_nacl.cc b/base/memory/shared_memory_nacl.cc
index 6ac0b3d..4bcbb54 100644
--- a/base/memory/shared_memory_nacl.cc
+++ b/base/memory/shared_memory_nacl.cc
@@ -117,15 +117,8 @@
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle handle_copy = shm_;
   handle_copy.SetOwnershipPassesToIPC(true);
+  Unmap();
   shm_ = SharedMemoryHandle();
-  // TODO(ssid): Find some way to track the shared memory in this case
-  // https://crbug/804399.
-  if (memory_) {
-    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
-    mapped_id_ = UnguessableToken();
-  }
-  memory_ = nullptr;
-  mapped_size_ = 0;
   return handle_copy;
 }
 
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_posix.cc
index d9e03a0..d3163e5 100644
--- a/base/memory/shared_memory_posix.cc
+++ b/base/memory/shared_memory_posix.cc
@@ -336,15 +336,8 @@
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle handle_copy = shm_;
   handle_copy.SetOwnershipPassesToIPC(true);
-  // TODO(ssid): Find some way to track the shared memory in this case
-  // https://crbug/804399.
-  if (memory_) {
-    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
-    mapped_id_ = UnguessableToken();
-  }
+  Unmap();
   shm_ = SharedMemoryHandle();
-  memory_ = nullptr;
-  mapped_size_ = 0;
   return handle_copy;
 }
 
diff --git a/base/memory/shared_memory_win.cc b/base/memory/shared_memory_win.cc
index 2130218..cf06dd3 100644
--- a/base/memory/shared_memory_win.cc
+++ b/base/memory/shared_memory_win.cc
@@ -373,15 +373,8 @@
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle handle(shm_);
   handle.SetOwnershipPassesToIPC(true);
+  Unmap();
   shm_ = SharedMemoryHandle();
-  // TODO(ssid): Find some way to track the shared memory in this case
-  // https://crbug/804399.
-  if (memory_) {
-    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
-    mapped_id_ = UnguessableToken();
-  }
-  memory_ = nullptr;
-  mapped_size_ = 0;
   return handle;
 }