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