OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/shared_memory.h" | 5 #include "base/shared_memory.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
16 #include "base/safe_strerror_posix.h" | 16 #include "base/safe_strerror_posix.h" |
17 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 | 19 |
20 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
21 #include "base/mac/foundation_util.h" | 21 #include "base/mac/foundation_util.h" |
22 #endif // OS_MACOSX | 22 #endif // OS_MACOSX |
23 | 23 |
24 #if defined(OS_ANDROID) | |
25 #include "base/os_compat_android.h" | |
26 #endif // defined(OS_ANDROID) | |
27 | |
24 namespace base { | 28 namespace base { |
25 | 29 |
26 namespace { | 30 namespace { |
31 | |
27 // Paranoia. Semaphores and shared memory segments should live in different | 32 // Paranoia. Semaphores and shared memory segments should live in different |
28 // namespaces, but who knows what's out there. | 33 // namespaces, but who knows what's out there. |
29 const char kSemaphoreSuffix[] = "-sem"; | 34 const char kSemaphoreSuffix[] = "-sem"; |
30 } | 35 |
36 } // namespace. | |
darin (slow to review)
2011/06/21 18:44:02
nit: no trailing comment
michaelbai
2011/06/21 20:17:31
Done.
| |
31 | 37 |
32 SharedMemory::SharedMemory() | 38 SharedMemory::SharedMemory() |
33 : mapped_file_(-1), | 39 : mapped_file_(-1), |
34 mapped_size_(0), | 40 mapped_size_(0), |
35 inode_(0), | 41 inode_(0), |
36 memory_(NULL), | 42 memory_(NULL), |
37 read_only_(false), | 43 read_only_(false), |
38 created_size_(0) { | 44 created_size_(0) { |
39 } | 45 } |
40 | 46 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 } | 94 } |
89 | 95 |
90 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { | 96 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { |
91 return CreateAnonymous(size) && Map(size); | 97 return CreateAnonymous(size) && Map(size); |
92 } | 98 } |
93 | 99 |
94 bool SharedMemory::CreateAnonymous(uint32 size) { | 100 bool SharedMemory::CreateAnonymous(uint32 size) { |
95 return CreateNamed("", false, size); | 101 return CreateNamed("", false, size); |
96 } | 102 } |
97 | 103 |
104 #if !defined(OS_ANDROID) | |
98 // Chromium mostly only uses the unique/private shmem as specified by | 105 // Chromium mostly only uses the unique/private shmem as specified by |
99 // "name == L"". The exception is in the StatsTable. | 106 // "name == L"". The exception is in the StatsTable. |
100 // TODO(jrg): there is no way to "clean up" all unused named shmem if | 107 // TODO(jrg): there is no way to "clean up" all unused named shmem if |
101 // we restart from a crash. (That isn't a new problem, but it is a problem.) | 108 // we restart from a crash. (That isn't a new problem, but it is a problem.) |
102 // In case we want to delete it later, it may be useful to save the value | 109 // In case we want to delete it later, it may be useful to save the value |
103 // of mem_filename after FilePathForMemoryName(). | 110 // of mem_filename after FilePathForMemoryName(). |
104 bool SharedMemory::CreateNamed(const std::string& name, | 111 bool SharedMemory::CreateNamed(const std::string& name, |
105 bool open_existing, uint32 size) { | 112 bool open_existing, uint32 size) { |
106 DCHECK_EQ(-1, mapped_file_); | 113 DCHECK_EQ(-1, mapped_file_); |
107 if (size == 0) return false; | 114 if (size == 0) return false; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 if (!FilePathForMemoryName(name, &path)) | 201 if (!FilePathForMemoryName(name, &path)) |
195 return false; | 202 return false; |
196 | 203 |
197 read_only_ = read_only; | 204 read_only_ = read_only; |
198 | 205 |
199 const char *mode = read_only ? "r" : "r+"; | 206 const char *mode = read_only ? "r" : "r+"; |
200 FILE *fp = file_util::OpenFile(path, mode); | 207 FILE *fp = file_util::OpenFile(path, mode); |
201 return PrepareMapFile(fp); | 208 return PrepareMapFile(fp); |
202 } | 209 } |
203 | 210 |
211 #endif // !defined(OS_ANDROID) | |
212 | |
204 bool SharedMemory::Map(uint32 bytes) { | 213 bool SharedMemory::Map(uint32 bytes) { |
205 if (mapped_file_ == -1) | 214 if (mapped_file_ == -1) |
206 return false; | 215 return false; |
207 | 216 |
217 #if defined(OS_ANDROID) | |
218 if (bytes == 0) { | |
219 int ashmem_bytes = GetAshmenSizeRegion(); | |
220 if (ashmem_bytes < 0) | |
221 return false; | |
222 | |
223 DCHECK_GE((uint32)ashmem_bytes, bytes); | |
224 // The caller wants to determine the map region size from ashmem. | |
225 bytes = ashmem_bytes; | |
226 // TODO(port): we set the created size here so that it is available in | |
227 // transport_dib_android.cc. We should use | |
228 // SharedMemory::GetAshmenSizeRegion() in TransportDIB::Map(). | |
229 created_size_ = bytes; | |
230 } | |
231 #endif | |
232 | |
208 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 233 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
209 MAP_SHARED, mapped_file_, 0); | 234 MAP_SHARED, mapped_file_, 0); |
210 | 235 |
211 if (memory_) | 236 if (memory_) |
212 mapped_size_ = bytes; | 237 mapped_size_ = bytes; |
213 | 238 |
214 bool mmap_succeeded = (memory_ != (void*)-1); | 239 bool mmap_succeeded = (memory_ != (void*)-1); |
215 DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; | 240 DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; |
216 return mmap_succeeded; | 241 return mmap_succeeded; |
217 } | 242 } |
(...skipping 23 matching lines...) Expand all Loading... | |
241 } | 266 } |
242 | 267 |
243 void SharedMemory::Lock() { | 268 void SharedMemory::Lock() { |
244 LockOrUnlockCommon(F_LOCK); | 269 LockOrUnlockCommon(F_LOCK); |
245 } | 270 } |
246 | 271 |
247 void SharedMemory::Unlock() { | 272 void SharedMemory::Unlock() { |
248 LockOrUnlockCommon(F_ULOCK); | 273 LockOrUnlockCommon(F_ULOCK); |
249 } | 274 } |
250 | 275 |
276 #if !defined(OS_ANDROID) | |
251 bool SharedMemory::PrepareMapFile(FILE *fp) { | 277 bool SharedMemory::PrepareMapFile(FILE *fp) { |
252 DCHECK_EQ(-1, mapped_file_); | 278 DCHECK_EQ(-1, mapped_file_); |
253 if (fp == NULL) return false; | 279 if (fp == NULL) return false; |
254 | 280 |
255 // This function theoretically can block on the disk, but realistically | 281 // This function theoretically can block on the disk, but realistically |
256 // the temporary files we create will just go into the buffer cache | 282 // the temporary files we create will just go into the buffer cache |
257 // and be deleted before they ever make it out to disk. | 283 // and be deleted before they ever make it out to disk. |
258 base::ThreadRestrictions::ScopedAllowIO allow_io; | 284 base::ThreadRestrictions::ScopedAllowIO allow_io; |
259 | 285 |
260 file_util::ScopedFILE file_closer(fp); | 286 file_util::ScopedFILE file_closer(fp); |
261 | 287 |
262 mapped_file_ = dup(fileno(fp)); | 288 mapped_file_ = dup(fileno(fp)); |
263 if (mapped_file_ == -1) { | 289 if (mapped_file_ == -1) { |
264 if (errno == EMFILE) { | 290 if (errno == EMFILE) { |
265 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; | 291 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; |
266 return false; | 292 return false; |
267 } else { | 293 } else { |
268 NOTREACHED() << "Call to dup failed, errno=" << errno; | 294 NOTREACHED() << "Call to dup failed, errno=" << errno; |
269 } | 295 } |
270 } | 296 } |
271 | 297 |
272 struct stat st; | 298 struct stat st; |
273 if (fstat(mapped_file_, &st)) | 299 if (fstat(mapped_file_, &st)) |
274 NOTREACHED(); | 300 NOTREACHED(); |
275 inode_ = st.st_ino; | 301 inode_ = st.st_ino; |
276 | 302 |
277 return true; | 303 return true; |
278 } | 304 } |
305 #endif | |
279 | 306 |
280 // For the given shmem named |mem_name|, return a filename to mmap() | 307 // For the given shmem named |mem_name|, return a filename to mmap() |
281 // (and possibly create). Modifies |filename|. Return false on | 308 // (and possibly create). Modifies |filename|. Return false on |
282 // error, or true of we are happy. | 309 // error, or true of we are happy. |
283 bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, | 310 bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
284 FilePath* path) { | 311 FilePath* path) { |
285 // mem_name will be used for a filename; make sure it doesn't | 312 // mem_name will be used for a filename; make sure it doesn't |
286 // contain anything which will confuse us. | 313 // contain anything which will confuse us. |
287 DCHECK_EQ(std::string::npos, mem_name.find('/')); | 314 DCHECK_EQ(std::string::npos, mem_name.find('/')); |
288 DCHECK_EQ(std::string::npos, mem_name.find('\0')); | 315 DCHECK_EQ(std::string::npos, mem_name.find('\0')); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
331 new_handle->fd = new_fd; | 358 new_handle->fd = new_fd; |
332 new_handle->auto_close = true; | 359 new_handle->auto_close = true; |
333 | 360 |
334 if (close_self) | 361 if (close_self) |
335 Close(); | 362 Close(); |
336 | 363 |
337 return true; | 364 return true; |
338 } | 365 } |
339 | 366 |
340 } // namespace base | 367 } // namespace base |
OLD | NEW |