OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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> |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return SharedMemoryHandle(); | 91 return SharedMemoryHandle(); |
92 } | 92 } |
93 | 93 |
94 // static | 94 // static |
95 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 95 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
96 DCHECK_GE(handle.fd, 0); | 96 DCHECK_GE(handle.fd, 0); |
97 if (HANDLE_EINTR(close(handle.fd)) < 0) | 97 if (HANDLE_EINTR(close(handle.fd)) < 0) |
98 DPLOG(ERROR) << "close"; | 98 DPLOG(ERROR) << "close"; |
99 } | 99 } |
100 | 100 |
101 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { | 101 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
102 return CreateAnonymous(size) && Map(size); | 102 return CreateAnonymous(size) && Map(size); |
103 } | 103 } |
104 | 104 |
105 #if !defined(OS_ANDROID) | 105 #if !defined(OS_ANDROID) |
106 // Chromium mostly only uses the unique/private shmem as specified by | 106 // Chromium mostly only uses the unique/private shmem as specified by |
107 // "name == L"". The exception is in the StatsTable. | 107 // "name == L"". The exception is in the StatsTable. |
108 // 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 |
109 // 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.) |
110 // 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 |
111 // of mem_filename after FilePathForMemoryName(). | 111 // of mem_filename after FilePathForMemoryName(). |
112 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 112 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
113 DCHECK_EQ(-1, mapped_file_); | 113 DCHECK_EQ(-1, mapped_file_); |
114 if (options.size == 0) return false; | 114 if (options.size == 0) return false; |
115 | 115 |
| 116 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 117 return false; |
| 118 |
116 // This function theoretically can block on the disk, but realistically | 119 // This function theoretically can block on the disk, but realistically |
117 // the temporary files we create will just go into the buffer cache | 120 // the temporary files we create will just go into the buffer cache |
118 // and be deleted before they ever make it out to disk. | 121 // and be deleted before they ever make it out to disk. |
119 base::ThreadRestrictions::ScopedAllowIO allow_io; | 122 base::ThreadRestrictions::ScopedAllowIO allow_io; |
120 | 123 |
121 FILE *fp; | 124 FILE *fp; |
122 bool fix_size = true; | 125 bool fix_size = true; |
123 | 126 |
124 FilePath path; | 127 FilePath path; |
125 if (options.name == NULL || options.name->empty()) { | 128 if (options.name == NULL || options.name->empty()) { |
(...skipping 20 matching lines...) Expand all Loading... |
146 fix_size = false; | 149 fix_size = false; |
147 } | 150 } |
148 } | 151 } |
149 if (fp && fix_size) { | 152 if (fp && fix_size) { |
150 // Get current size. | 153 // Get current size. |
151 struct stat stat; | 154 struct stat stat; |
152 if (fstat(fileno(fp), &stat) != 0) { | 155 if (fstat(fileno(fp), &stat) != 0) { |
153 file_util::CloseFile(fp); | 156 file_util::CloseFile(fp); |
154 return false; | 157 return false; |
155 } | 158 } |
156 const uint32 current_size = stat.st_size; | 159 const size_t current_size = stat.st_size; |
157 if (current_size != options.size) { | 160 if (current_size != options.size) { |
158 if (HANDLE_EINTR(ftruncate(fileno(fp), options.size)) != 0) { | 161 if (HANDLE_EINTR(ftruncate(fileno(fp), options.size)) != 0) { |
159 file_util::CloseFile(fp); | 162 file_util::CloseFile(fp); |
160 return false; | 163 return false; |
161 } | 164 } |
162 if (fseeko(fp, options.size, SEEK_SET) != 0) { | 165 if (fseeko(fp, options.size, SEEK_SET) != 0) { |
163 file_util::CloseFile(fp); | 166 file_util::CloseFile(fp); |
164 return false; | 167 return false; |
165 } | 168 } |
166 } | 169 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 212 |
210 read_only_ = read_only; | 213 read_only_ = read_only; |
211 | 214 |
212 const char *mode = read_only ? "r" : "r+"; | 215 const char *mode = read_only ? "r" : "r+"; |
213 FILE *fp = file_util::OpenFile(path, mode); | 216 FILE *fp = file_util::OpenFile(path, mode); |
214 return PrepareMapFile(fp); | 217 return PrepareMapFile(fp); |
215 } | 218 } |
216 | 219 |
217 #endif // !defined(OS_ANDROID) | 220 #endif // !defined(OS_ANDROID) |
218 | 221 |
219 bool SharedMemory::Map(uint32 bytes) { | 222 bool SharedMemory::Map(size_t bytes) { |
220 if (mapped_file_ == -1) | 223 if (mapped_file_ == -1) |
221 return false; | 224 return false; |
222 | 225 |
| 226 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 227 return false; |
| 228 |
223 #if defined(OS_ANDROID) | 229 #if defined(OS_ANDROID) |
224 if (bytes == 0) { | 230 if (bytes == 0) { |
225 int ashmem_bytes = ashmem_get_size_region(mapped_file_); | 231 int ashmem_bytes = ashmem_get_size_region(mapped_file_); |
226 if (ashmem_bytes < 0) | 232 if (ashmem_bytes < 0) |
227 return false; | 233 return false; |
228 | 234 |
229 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); | 235 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); |
230 // The caller wants to determine the map region size from ashmem. | 236 // The caller wants to determine the map region size from ashmem. |
231 bytes = ashmem_bytes; | 237 bytes = ashmem_bytes; |
232 // TODO(port): we set the created size here so that it is available in | 238 // TODO(port): we set the created size here so that it is available in |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 new_handle->fd = new_fd; | 380 new_handle->fd = new_fd; |
375 new_handle->auto_close = true; | 381 new_handle->auto_close = true; |
376 | 382 |
377 if (close_self) | 383 if (close_self) |
378 Close(); | 384 Close(); |
379 | 385 |
380 return true; | 386 return true; |
381 } | 387 } |
382 | 388 |
383 } // namespace base | 389 } // namespace base |
OLD | NEW |