OLD | NEW |
---|---|
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 #ifndef BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 5 #ifndef BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
6 #define BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 6 #define BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <atomic> | 9 #include <atomic> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 class HistogramBase; | 20 class HistogramBase; |
21 class MemoryMappedFile; | 21 class MemoryMappedFile; |
22 class SharedMemory; | |
22 | 23 |
23 // Simple allocator for pieces of a memory block that may be persistent | 24 // Simple allocator for pieces of a memory block that may be persistent |
24 // to some storage or shared across multiple processes. This class resides | 25 // to some storage or shared across multiple processes. This class resides |
25 // under base/metrics because it was written for that purpose. It is, | 26 // under base/metrics because it was written for that purpose. It is, |
26 // however, fully general-purpose and can be freely moved to base/memory | 27 // however, fully general-purpose and can be freely moved to base/memory |
27 // if other uses are found. | 28 // if other uses are found. |
28 // | 29 // |
29 // This class provides for thread-secure (i.e. safe against other threads | 30 // This class provides for thread-secure (i.e. safe against other threads |
30 // or processes that may be compromised and thus have malicious intent) | 31 // or processes that may be compromised and thus have malicious intent) |
31 // allocation of memory within a designated block and also a mechanism by | 32 // allocation of memory within a designated block and also a mechanism by |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 public: | 302 public: |
302 LocalPersistentMemoryAllocator(size_t size, uint64_t id, | 303 LocalPersistentMemoryAllocator(size_t size, uint64_t id, |
303 const std::string& name); | 304 const std::string& name); |
304 ~LocalPersistentMemoryAllocator() override; | 305 ~LocalPersistentMemoryAllocator() override; |
305 | 306 |
306 private: | 307 private: |
307 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); | 308 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); |
308 }; | 309 }; |
309 | 310 |
310 | 311 |
312 // This allocator takes a shared-memory object and performs allocation from | |
313 // it. The memory must be previously mapped via Map() or MapAt(). The allocator | |
314 // takes ownership of the memory object. | |
315 class BASE_EXPORT SharedPersistentMemoryAllocator | |
316 : public PersistentMemoryAllocator { | |
317 public: | |
318 SharedPersistentMemoryAllocator(scoped_ptr<SharedMemory> memory, uint64_t id, | |
319 const std::string& name, bool read_only); | |
320 ~SharedPersistentMemoryAllocator() override; | |
321 | |
322 SharedMemory* shared_memory() { return shared_memory_.get(); } | |
323 | |
324 // Ensure that the memory isn't so invalid that it won't crash when passing it | |
325 // to the allocator. This doesn't guarantee the data is valid, just that it | |
326 // won't cause the program to abort. The existing IsCorrupt() call will handle | |
327 // the rest. | |
328 static bool IsSharedMemoryAcceptable(const SharedMemory& memory); | |
Alexei Svitkine (slow)
2016/02/09 19:58:44
I understand why you don't want to use IsValid() b
bcwhite
2016/02/10 16:43:53
Acknowledged.
| |
329 | |
330 private: | |
331 scoped_ptr<SharedMemory> shared_memory_; | |
332 DISALLOW_COPY_AND_ASSIGN(SharedPersistentMemoryAllocator); | |
333 }; | |
334 | |
335 | |
311 // This allocator takes a memory-mapped file object and performs allocation | 336 // This allocator takes a memory-mapped file object and performs allocation |
312 // from it. The allocator takes ownership of the file object. Only read access | 337 // from it. The allocator takes ownership of the file object. Only read access |
313 // is provided due to limitions of the MemoryMappedFile class. | 338 // is provided due to limitions of the MemoryMappedFile class. |
314 class BASE_EXPORT FilePersistentMemoryAllocator | 339 class BASE_EXPORT FilePersistentMemoryAllocator |
315 : public PersistentMemoryAllocator { | 340 : public PersistentMemoryAllocator { |
316 public: | 341 public: |
317 FilePersistentMemoryAllocator(MemoryMappedFile* file, uint64_t id, | 342 FilePersistentMemoryAllocator(scoped_ptr<MemoryMappedFile> file, uint64_t id, |
318 const std::string& name); | 343 const std::string& name); |
319 ~FilePersistentMemoryAllocator() override; | 344 ~FilePersistentMemoryAllocator() override; |
320 | 345 |
321 // Ensure that the file isn't so invalid that it won't crash when passing it | 346 // Ensure that the file isn't so invalid that it won't crash when passing it |
322 // to the allocator. This doesn't guarantee the file is valid, just that it | 347 // to the allocator. This doesn't guarantee the file is valid, just that it |
323 // won't cause program to abort. The existing IsCorrupt() call will handle | 348 // won't cause the program to abort. The existing IsCorrupt() call will handle |
324 // the rest. | 349 // the rest. |
325 static bool IsFileAcceptable(const MemoryMappedFile& file); | 350 static bool IsFileAcceptable(const MemoryMappedFile& file); |
326 | 351 |
327 private: | 352 private: |
328 scoped_ptr<MemoryMappedFile> mapped_file_; | 353 scoped_ptr<MemoryMappedFile> mapped_file_; |
354 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | |
Alexei Svitkine (slow)
2016/02/09 19:58:44
Nit: Put an empty line above these lines.
bcwhite
2016/02/10 16:43:53
Done.
| |
329 }; | 355 }; |
330 | 356 |
331 } // namespace base | 357 } // namespace base |
332 | 358 |
333 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 359 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |