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 | 9 |
10 #include <atomic> | 10 #include <atomic> |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 | 280 |
281 // Flag set if an allocation has failed because the memory segment was full. | 281 // Flag set if an allocation has failed because the memory segment was full. |
282 bool IsFull() const; | 282 bool IsFull() const; |
283 | 283 |
284 // Update those "tracking" histograms which do not get updates during regular | 284 // Update those "tracking" histograms which do not get updates during regular |
285 // operation, such as how much memory is currently used. This should be | 285 // operation, such as how much memory is currently used. This should be |
286 // called before such information is to be displayed or uploaded. | 286 // called before such information is to be displayed or uploaded. |
287 void UpdateTrackingHistograms(); | 287 void UpdateTrackingHistograms(); |
288 | 288 |
289 protected: | 289 protected: |
| 290 enum MemoryType { |
| 291 MEM_EXTERNAL, |
| 292 MEM_MALLOC, |
| 293 MEM_VIRTUAL, |
| 294 MEM_SHARED, |
| 295 MEM_FILE, |
| 296 }; |
| 297 |
| 298 struct Memory { |
| 299 Memory(void* b, MemoryType t) : base(b), type(t) {} |
| 300 |
| 301 void* base; |
| 302 MemoryType type; |
| 303 }; |
| 304 |
| 305 // Constructs the allocator. Everything is the same as the public allocator |
| 306 // except |memory| which is a structure with additional information besides |
| 307 // the base address. |
| 308 PersistentMemoryAllocator(Memory memory, size_t size, size_t page_size, |
| 309 uint64_t id, base::StringPiece name, |
| 310 bool readonly); |
| 311 |
290 volatile char* const mem_base_; // Memory base. (char so sizeof guaranteed 1) | 312 volatile char* const mem_base_; // Memory base. (char so sizeof guaranteed 1) |
| 313 const MemoryType mem_type_; // Type of memory allocation. |
291 const uint32_t mem_size_; // Size of entire memory segment. | 314 const uint32_t mem_size_; // Size of entire memory segment. |
292 const uint32_t mem_page_; // Page size allocations shouldn't cross. | 315 const uint32_t mem_page_; // Page size allocations shouldn't cross. |
293 | 316 |
294 private: | 317 private: |
295 struct SharedMetadata; | 318 struct SharedMetadata; |
296 struct BlockHeader; | 319 struct BlockHeader; |
297 static const uint32_t kAllocAlignment; | 320 static const uint32_t kAllocAlignment; |
298 static const Reference kReferenceQueue; | 321 static const Reference kReferenceQueue; |
299 | 322 |
300 // The shared metadata is always located at the top of the memory segment. | 323 // The shared metadata is always located at the top of the memory segment. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 : public PersistentMemoryAllocator { | 375 : public PersistentMemoryAllocator { |
353 public: | 376 public: |
354 LocalPersistentMemoryAllocator(size_t size, uint64_t id, | 377 LocalPersistentMemoryAllocator(size_t size, uint64_t id, |
355 base::StringPiece name); | 378 base::StringPiece name); |
356 ~LocalPersistentMemoryAllocator() override; | 379 ~LocalPersistentMemoryAllocator() override; |
357 | 380 |
358 private: | 381 private: |
359 // Allocates a block of local memory of the specified |size|, ensuring that | 382 // Allocates a block of local memory of the specified |size|, ensuring that |
360 // the memory will not be physically allocated until accessed and will read | 383 // the memory will not be physically allocated until accessed and will read |
361 // as zero when that happens. | 384 // as zero when that happens. |
362 static void* AllocateLocalMemory(size_t size); | 385 static Memory AllocateLocalMemory(size_t size); |
363 | 386 |
364 // Deallocates a block of local |memory| of the specified |size|. | 387 // Deallocates a block of local |memory| of the specified |size|. |
365 static void DeallocateLocalMemory(void* memory, size_t size); | 388 static void DeallocateLocalMemory(void* memory, size_t size, MemoryType type); |
366 | 389 |
367 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); | 390 DISALLOW_COPY_AND_ASSIGN(LocalPersistentMemoryAllocator); |
368 }; | 391 }; |
369 | 392 |
370 | 393 |
371 // This allocator takes a shared-memory object and performs allocation from | 394 // This allocator takes a shared-memory object and performs allocation from |
372 // it. The memory must be previously mapped via Map() or MapAt(). The allocator | 395 // it. The memory must be previously mapped via Map() or MapAt(). The allocator |
373 // takes ownership of the memory object. | 396 // takes ownership of the memory object. |
374 class BASE_EXPORT SharedPersistentMemoryAllocator | 397 class BASE_EXPORT SharedPersistentMemoryAllocator |
375 : public PersistentMemoryAllocator { | 398 : public PersistentMemoryAllocator { |
376 public: | 399 public: |
377 SharedPersistentMemoryAllocator(std::unique_ptr<SharedMemory> memory, | 400 SharedPersistentMemoryAllocator(std::unique_ptr<SharedMemory> memory, |
378 uint64_t id, | 401 uint64_t id, |
379 base::StringPiece name, | 402 base::StringPiece name, |
380 bool read_only); | 403 bool read_only); |
381 ~SharedPersistentMemoryAllocator() override; | 404 ~SharedPersistentMemoryAllocator() override; |
382 | 405 |
383 SharedMemory* shared_memory() { return shared_memory_.get(); } | 406 SharedMemory* shared_memory() { return shared_memory_.get(); } |
384 | 407 |
385 // Ensure that the memory isn't so invalid that it won't crash when passing it | 408 // Ensure that the memory isn't so invalid that it would crash when passing it |
386 // to the allocator. This doesn't guarantee the data is valid, just that it | 409 // to the allocator. This doesn't guarantee the data is valid, just that it |
387 // won't cause the program to abort. The existing IsCorrupt() call will handle | 410 // won't cause the program to abort. The existing IsCorrupt() call will handle |
388 // the rest. | 411 // the rest. |
389 static bool IsSharedMemoryAcceptable(const SharedMemory& memory); | 412 static bool IsSharedMemoryAcceptable(const SharedMemory& memory); |
390 | 413 |
391 private: | 414 private: |
392 std::unique_ptr<SharedMemory> shared_memory_; | 415 std::unique_ptr<SharedMemory> shared_memory_; |
393 | 416 |
394 DISALLOW_COPY_AND_ASSIGN(SharedPersistentMemoryAllocator); | 417 DISALLOW_COPY_AND_ASSIGN(SharedPersistentMemoryAllocator); |
395 }; | 418 }; |
396 | 419 |
397 | 420 |
398 #if !defined(OS_NACL) // NACL doesn't support any kind of file access in build. | 421 #if !defined(OS_NACL) // NACL doesn't support any kind of file access in build. |
399 // This allocator takes a memory-mapped file object and performs allocation | 422 // This allocator takes a memory-mapped file object and performs allocation |
400 // from it. The allocator takes ownership of the file object. | 423 // from it. The allocator takes ownership of the file object. |
401 class BASE_EXPORT FilePersistentMemoryAllocator | 424 class BASE_EXPORT FilePersistentMemoryAllocator |
402 : public PersistentMemoryAllocator { | 425 : public PersistentMemoryAllocator { |
403 public: | 426 public: |
404 // A |max_size| of zero will use the length of the file as the maximum | 427 // A |max_size| of zero will use the length of the file as the maximum |
405 // size. The |file| object must have been already created with sufficient | 428 // size. The |file| object must have been already created with sufficient |
406 // permissions (read, read/write, or read/write/extend). | 429 // permissions (read, read/write, or read/write/extend). |
407 FilePersistentMemoryAllocator(std::unique_ptr<MemoryMappedFile> file, | 430 FilePersistentMemoryAllocator(std::unique_ptr<MemoryMappedFile> file, |
408 size_t max_size, | 431 size_t max_size, |
409 uint64_t id, | 432 uint64_t id, |
410 base::StringPiece name, | 433 base::StringPiece name, |
411 bool read_only); | 434 bool read_only); |
412 ~FilePersistentMemoryAllocator() override; | 435 ~FilePersistentMemoryAllocator() override; |
413 | 436 |
414 // Ensure that the file isn't so invalid that it won't crash when passing it | 437 // Ensure that the file isn't so invalid that it would crash when passing it |
415 // to the allocator. This doesn't guarantee the file is valid, just that it | 438 // to the allocator. This doesn't guarantee the file is valid, just that it |
416 // won't cause the program to abort. The existing IsCorrupt() call will handle | 439 // won't cause the program to abort. The existing IsCorrupt() call will handle |
417 // the rest. | 440 // the rest. |
418 static bool IsFileAcceptable(const MemoryMappedFile& file, bool read_only); | 441 static bool IsFileAcceptable(const MemoryMappedFile& file, bool read_only); |
419 | 442 |
420 private: | 443 private: |
421 std::unique_ptr<MemoryMappedFile> mapped_file_; | 444 std::unique_ptr<MemoryMappedFile> mapped_file_; |
422 | 445 |
423 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | 446 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
424 }; | 447 }; |
425 #endif // !defined(OS_NACL) | 448 #endif // !defined(OS_NACL) |
426 | 449 |
427 } // namespace base | 450 } // namespace base |
428 | 451 |
429 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 452 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |