summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authoragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 18:59:20 +0000
committeragl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-11 18:59:20 +0000
commit5fe733dee6afd3ab897cafbfcdcc1450264409b0 (patch)
tree64d39f452e1aee1076356da52d836c902e8b92eb /base
parent072f6f57560ab3616915b0aa9f61331deb2cf260 (diff)
downloadchromium_src-5fe733dee6afd3ab897cafbfcdcc1450264409b0.zip
chromium_src-5fe733dee6afd3ab897cafbfcdcc1450264409b0.tar.gz
chromium_src-5fe733dee6afd3ab897cafbfcdcc1450264409b0.tar.bz2
POSIX: Transfer network data using shared memory
This patch adds the long planned support for sharing memory on POSIX by transporting file descriptors. It largely builds on the shared memory cleanup work by jrg. We move FileDescriptor out of chrome/common/file_descriptor_posix.h and into base/file_descriptor_posix.h. Since all that's left in the chrome/common verion is the DescriptorSet, those files are renamed to descriptor_set.[h|cc]. The SharedMemoryHandle on POSIX then becomes a typedef to a FileDescriptor and thus can be serialised over IPC. After that, it's mostly a case of cleaning up those snippets of code which considered SharedMemoryHandles to be scaler values. Review URL: http://codereview.chromium.org/21208 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9580 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_descriptor_posix.h32
-rw-r--r--base/shared_memory.h15
-rw-r--r--base/shared_memory_posix.cc26
-rw-r--r--base/shared_memory_win.cc9
4 files changed, 73 insertions, 9 deletions
diff --git a/base/file_descriptor_posix.h b/base/file_descriptor_posix.h
new file mode 100644
index 0000000..8ebc5be
--- /dev/null
+++ b/base/file_descriptor_posix.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_FILE_DESCRIPTOR_POSIX_H_
+#define BASE_FILE_DESCRIPTOR_POSIX_H_
+
+namespace base {
+
+// -----------------------------------------------------------------------------
+// We introduct a special structure for file descriptors in order that we are
+// able to use template specialisation to special-case their handling.
+// -----------------------------------------------------------------------------
+struct FileDescriptor {
+ FileDescriptor()
+ : fd(-1),
+ auto_close(false) { }
+
+ FileDescriptor(int ifd, bool iauto_close)
+ : fd(ifd),
+ auto_close(iauto_close) { }
+
+ int fd;
+ // If true, this file descriptor should be closed after it has been used. For
+ // example an IPC system might interpret this flag as indicating that the
+ // file descriptor it has been given should be closed after use.
+ bool auto_close;
+};
+
+} // namespace base
+
+#endif // BASE_FILE_DESCRIPTOR_POSIX_H_
diff --git a/base/shared_memory.h b/base/shared_memory.h
index 3bfa003..b44367a 100644
--- a/base/shared_memory.h
+++ b/base/shared_memory.h
@@ -9,6 +9,7 @@
#if defined(OS_POSIX)
#include <semaphore.h>
+#include "base/file_descriptor_posix.h"
#endif
#include <string>
@@ -23,7 +24,7 @@ namespace base {
typedef HANDLE SharedMemoryHandle;
typedef HANDLE SharedMemoryLock;
#elif defined(OS_POSIX)
-typedef int SharedMemoryHandle;
+typedef FileDescriptor SharedMemoryHandle;
// On POSIX, the lock is implemented as a lockf() on the mapped file,
// so no additional member (or definition of SharedMemoryLock) is
// needed.
@@ -49,6 +50,10 @@ class SharedMemory {
// Destructor. Will close any open files.
~SharedMemory();
+ // Return true iff the given handle is valid (i.e. not the distingished
+ // invalid value; NULL for a HANDLE and -1 for a file descriptor)
+ static bool IsHandleValid(const SharedMemoryHandle& handle);
+
// Creates or opens a shared memory segment based on a name.
// If read_only is true, opens the memory as read-only.
// If open_existing is true, and the shared memory already exists,
@@ -92,7 +97,7 @@ class SharedMemory {
// Get access to the underlying OS handle for this segment.
// Use of this handle for anything other than an opaque
// identifier is not portable.
- SharedMemoryHandle handle() const { return mapped_file_; }
+ SharedMemoryHandle handle() const;
// Closes the open shared memory segment.
// It is safe to call Close repeatedly.
@@ -147,7 +152,11 @@ class SharedMemory {
bool close_self);
std::wstring name_;
- SharedMemoryHandle mapped_file_;
+#if defined(OS_WIN)
+ HANDLE mapped_file_;
+#elif defined(OS_POSIX)
+ int mapped_file_;
+#endif
void* memory_;
bool read_only_;
size_t max_size_;
diff --git a/base/shared_memory_posix.cc b/base/shared_memory_posix.cc
index bd33cda..e6f81c5 100644
--- a/base/shared_memory_posix.cc
+++ b/base/shared_memory_posix.cc
@@ -29,7 +29,7 @@ SharedMemory::SharedMemory()
}
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
- : mapped_file_(handle),
+ : mapped_file_(handle.fd),
memory_(NULL),
read_only_(read_only),
max_size_(0) {
@@ -37,7 +37,7 @@ SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
ProcessHandle process)
- : mapped_file_(handle),
+ : mapped_file_(handle.fd),
memory_(NULL),
read_only_(read_only),
max_size_(0) {
@@ -50,6 +50,11 @@ SharedMemory::~SharedMemory() {
Close();
}
+// static
+bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
+ return handle.fd >= 0;
+}
+
bool SharedMemory::Create(const std::wstring &name, bool read_only,
bool open_existing, size_t size) {
read_only_ = read_only;
@@ -232,10 +237,15 @@ bool SharedMemory::Unmap() {
bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
SharedMemoryHandle *new_handle,
bool close_self) {
- *new_handle = 0;
- // TODO(awalker): figure out if we need this, and do the appropriate
- // VM magic if so.
- return false;
+ const int new_fd = dup(mapped_file_);
+ DCHECK(new_fd >= -1);
+ new_handle->fd = new_fd;
+ new_handle->auto_close = true;
+
+ if (close_self)
+ Close();
+
+ return true;
}
@@ -277,4 +287,8 @@ void SharedMemory::Unlock() {
LockOrUnlockCommon(F_ULOCK);
}
+SharedMemoryHandle SharedMemory::handle() const {
+ return FileDescriptor(mapped_file_, false);
+}
+
} // namespace base
diff --git a/base/shared_memory_win.cc b/base/shared_memory_win.cc
index 4ae4752..3822fc9 100644
--- a/base/shared_memory_win.cc
+++ b/base/shared_memory_win.cc
@@ -45,6 +45,11 @@ SharedMemory::~SharedMemory() {
CloseHandle(lock_);
}
+// static
+bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
+ return handle != NULL;
+}
+
bool SharedMemory::Create(const std::wstring &name, bool read_only,
bool open_existing, size_t size) {
DCHECK(mapped_file_ == NULL);
@@ -168,4 +173,8 @@ void SharedMemory::Unlock() {
ReleaseMutex(lock_);
}
+SharedMemoryHandle SharedMemory::handle() const {
+ return mapped_file_;
+}
+
} // namespace base