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 | 10 |
11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 public: | 308 public: |
308 LocalPersistentMemoryAllocator(size_t size, uint64_t id, | 309 LocalPersistentMemoryAllocator(size_t size, uint64_t id, |
309 base::StringPiece name); | 310 base::StringPiece name); |
310 ~LocalPersistentMemoryAllocator() override; | 311 ~LocalPersistentMemoryAllocator() override; |
311 | 312 |
312 private: | 313 private: |
313 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); | 314 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); |
314 }; | 315 }; |
315 | 316 |
316 | 317 |
| 318 // This allocator takes a shared-memory object and performs allocation from |
| 319 // it. The memory must be previously mapped via Map() or MapAt(). The allocator |
| 320 // takes ownership of the memory object. |
| 321 class BASE_EXPORT SharedPersistentMemoryAllocator |
| 322 : public PersistentMemoryAllocator { |
| 323 public: |
| 324 SharedPersistentMemoryAllocator(scoped_ptr<SharedMemory> memory, uint64_t id, |
| 325 base::StringPiece name, bool read_only); |
| 326 ~SharedPersistentMemoryAllocator() override; |
| 327 |
| 328 SharedMemory* shared_memory() { return shared_memory_.get(); } |
| 329 |
| 330 // Ensure that the memory isn't so invalid that it won't crash when passing it |
| 331 // to the allocator. This doesn't guarantee the data is valid, just that it |
| 332 // won't cause the program to abort. The existing IsCorrupt() call will handle |
| 333 // the rest. |
| 334 static bool IsSharedMemoryAcceptable(const SharedMemory& memory); |
| 335 |
| 336 private: |
| 337 scoped_ptr<SharedMemory> shared_memory_; |
| 338 |
| 339 DISALLOW_COPY_AND_ASSIGN(SharedPersistentMemoryAllocator); |
| 340 }; |
| 341 |
| 342 |
317 // This allocator takes a memory-mapped file object and performs allocation | 343 // This allocator takes a memory-mapped file object and performs allocation |
318 // from it. The allocator takes ownership of the file object. Only read access | 344 // from it. The allocator takes ownership of the file object. Only read access |
319 // is provided due to limitions of the MemoryMappedFile class. | 345 // is provided due to limitions of the MemoryMappedFile class. |
320 class BASE_EXPORT FilePersistentMemoryAllocator | 346 class BASE_EXPORT FilePersistentMemoryAllocator |
321 : public PersistentMemoryAllocator { | 347 : public PersistentMemoryAllocator { |
322 public: | 348 public: |
323 FilePersistentMemoryAllocator(MemoryMappedFile* file, uint64_t id, | 349 FilePersistentMemoryAllocator(scoped_ptr<MemoryMappedFile> file, uint64_t id, |
324 base::StringPiece name); | 350 base::StringPiece name); |
325 ~FilePersistentMemoryAllocator() override; | 351 ~FilePersistentMemoryAllocator() override; |
326 | 352 |
327 // Ensure that the file isn't so invalid that it won't crash when passing it | 353 // Ensure that the file isn't so invalid that it won't crash when passing it |
328 // to the allocator. This doesn't guarantee the file is valid, just that it | 354 // to the allocator. This doesn't guarantee the file is valid, just that it |
329 // won't cause program to abort. The existing IsCorrupt() call will handle | 355 // won't cause the program to abort. The existing IsCorrupt() call will handle |
330 // the rest. | 356 // the rest. |
331 static bool IsFileAcceptable(const MemoryMappedFile& file); | 357 static bool IsFileAcceptable(const MemoryMappedFile& file); |
332 | 358 |
333 private: | 359 private: |
334 scoped_ptr<MemoryMappedFile> mapped_file_; | 360 scoped_ptr<MemoryMappedFile> mapped_file_; |
| 361 |
| 362 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
335 }; | 363 }; |
336 | 364 |
337 } // namespace base | 365 } // namespace base |
338 | 366 |
339 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 367 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |