diff options
author | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-13 19:59:07 +0000 |
---|---|---|
committer | avi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-13 19:59:07 +0000 |
commit | cba5d987abca6016a534dce83136141869c18aac (patch) | |
tree | d675ac5cc34388c16988adf2795c4172cdbec041 /base | |
parent | 9f8303cb3bd19db39f626b1e838310b645da4ad6 (diff) | |
download | chromium_src-cba5d987abca6016a534dce83136141869c18aac.zip chromium_src-cba5d987abca6016a534dce83136141869c18aac.tar.gz chromium_src-cba5d987abca6016a534dce83136141869c18aac.tar.bz2 |
Revving the shared memory to the comments on the list.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@824 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/shared_memory.h | 3 | ||||
-rw-r--r-- | base/shared_memory_posix.cc | 56 |
2 files changed, 32 insertions, 27 deletions
diff --git a/base/shared_memory.h b/base/shared_memory.h index 74888e8..0723a57 100644 --- a/base/shared_memory.h +++ b/base/shared_memory.h @@ -136,6 +136,9 @@ class SharedMemory { void Unlock(); private: +#if defined(OS_POSIX) + bool CreateOrOpen(const std::wstring &name, int posix_flags); +#endif bool ShareToProcessCommon(ProcessHandle process, SharedMemoryHandle *new_handle, bool close_self); diff --git a/base/shared_memory_posix.cc b/base/shared_memory_posix.cc index 51a06a6..9b09b1b 100644 --- a/base/shared_memory_posix.cc +++ b/base/shared_memory_posix.cc @@ -36,9 +36,9 @@ #include "base/string_util.h" namespace { - // Paranoia. Semaphores and shared memory segments should live in different - // namespaces, but who knows what's out there. - const char kSemaphoreSuffix[] = "-sem"; +// Paranoia. Semaphores and shared memory segments should live in different +// namespaces, but who knows what's out there. +const char kSemaphoreSuffix[] = "-sem"; } SharedMemory::SharedMemory() @@ -77,22 +77,41 @@ SharedMemory::~SharedMemory() { bool SharedMemory::Create(const std::wstring &name, bool read_only, bool open_existing, size_t size) { - DCHECK(mapped_file_ == -1); - - name_ = L"/" + name; read_only_ = read_only; int posix_flags = 0; posix_flags |= read_only ? O_RDONLY : O_RDWR; - posix_flags |= O_CREAT; + if (!open_existing) + posix_flags |= O_CREAT; + + if (CreateOrOpen(name, posix_flags)) { + ftruncate(mapped_file_, size); + max_size_ = size; + return true; + } + + return false; +} + +bool SharedMemory::Open(const std::wstring &name, bool read_only) { + read_only_ = read_only; + + int posix_flags = 0; + posix_flags |= read_only ? O_RDONLY : O_RDWR; + + return CreateOrOpen(name, posix_flags); +} + +bool SharedMemory::CreateOrOpen(const std::wstring &name, int posix_flags) { + DCHECK(mapped_file_ == -1); + + name_ = L"/" + name; int posix_mode = 0600; // owner read/write std::string posix_name(WideToUTF8(name_)); mapped_file_ = shm_open(posix_name.c_str(), posix_flags, posix_mode); - if (mapped_file_ < 0) return false; - ftruncate(mapped_file_, size); posix_name += kSemaphoreSuffix; lock_ = sem_open(posix_name.c_str(), O_CREAT, posix_mode, 1); @@ -103,25 +122,8 @@ bool SharedMemory::Create(const std::wstring &name, bool read_only, lock_ = NULL; return false; } - - max_size_ = size; - return true; -} - -bool SharedMemory::Open(const std::wstring &name, bool read_only) { - DCHECK(mapped_file_ == -1); - - name_ = name; - read_only_ = read_only; - int posix_flags = 0; - posix_flags |= read_only ? O_RDONLY : O_RDWR; - - int posix_mode = 0600; // owner read/write - std::string posix_name(WideToUTF8(name_)); - mapped_file_ = shm_open(posix_name.c_str(), posix_flags, posix_mode); - - return (mapped_file_ >= 0); + return true; } bool SharedMemory::Map(size_t bytes) { |