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 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 T* ChangeObject(Reference ref, uint32_t from_type_id) { | 420 T* ChangeObject(Reference ref, uint32_t from_type_id) { |
421 DCHECK_LE(sizeof(T), GetAllocSize(ref)) << "alloc not big enough for obj"; | 421 DCHECK_LE(sizeof(T), GetAllocSize(ref)) << "alloc not big enough for obj"; |
422 // Make sure the memory is appropriate. This won't be used until after | 422 // Make sure the memory is appropriate. This won't be used until after |
423 // the type is changed but checking first avoids the possibility of having | 423 // the type is changed but checking first avoids the possibility of having |
424 // to change the type back. | 424 // to change the type back. |
425 void* mem = const_cast<void*>(GetBlockData(ref, 0, sizeof(T))); | 425 void* mem = const_cast<void*>(GetBlockData(ref, 0, sizeof(T))); |
426 if (!mem) | 426 if (!mem) |
427 return nullptr; | 427 return nullptr; |
428 // Ensure the allocator's internal alignment is sufficient for this object. | 428 // Ensure the allocator's internal alignment is sufficient for this object. |
429 // This protects against coding errors in the allocator. | 429 // This protects against coding errors in the allocator. |
430 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (ALIGNOF(T) - 1)); | 430 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (alignof(T) - 1)); |
431 // First change the type to "transitioning" so that there is no race | 431 // First change the type to "transitioning" so that there is no race |
432 // condition with the clearing and construction of the object should | 432 // condition with the clearing and construction of the object should |
433 // another thread be simultaneously iterating over data. This will | 433 // another thread be simultaneously iterating over data. This will |
434 // "acquire" the memory so no changes get reordered before it. | 434 // "acquire" the memory so no changes get reordered before it. |
435 if (!ChangeType(ref, kReferenceTransitioning, from_type_id)) | 435 if (!ChangeType(ref, kReferenceTransitioning, from_type_id)) |
436 return nullptr; | 436 return nullptr; |
437 // Clear the memory so that the property of all memory being zero after an | 437 // Clear the memory so that the property of all memory being zero after an |
438 // allocation also applies here. | 438 // allocation also applies here. |
439 memset(mem, 0, GetAllocSize(ref)); | 439 memset(mem, 0, GetAllocSize(ref)); |
440 // Construct an object of the desired type on this memory, just as if | 440 // Construct an object of the desired type on this memory, just as if |
(...skipping 21 matching lines...) Expand all Loading... |
462 // when the last field is actually variable length. | 462 // when the last field is actually variable length. |
463 template <typename T> | 463 template <typename T> |
464 T* AllocateObject(size_t size) { | 464 T* AllocateObject(size_t size) { |
465 if (size < sizeof(T)) | 465 if (size < sizeof(T)) |
466 size = sizeof(T); | 466 size = sizeof(T); |
467 Reference ref = Allocate(size, T::kPersistentTypeId); | 467 Reference ref = Allocate(size, T::kPersistentTypeId); |
468 void* mem = | 468 void* mem = |
469 const_cast<void*>(GetBlockData(ref, T::kPersistentTypeId, size)); | 469 const_cast<void*>(GetBlockData(ref, T::kPersistentTypeId, size)); |
470 if (!mem) | 470 if (!mem) |
471 return nullptr; | 471 return nullptr; |
472 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (ALIGNOF(T) - 1)); | 472 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (alignof(T) - 1)); |
473 return new (mem) T(); | 473 return new (mem) T(); |
474 } | 474 } |
475 template <typename T> | 475 template <typename T> |
476 T* AllocateObject() { | 476 T* AllocateObject() { |
477 return AllocateObject<T>(sizeof(T)); | 477 return AllocateObject<T>(sizeof(T)); |
478 } | 478 } |
479 | 479 |
480 // Deletes an object by destructing it and then changing the type to a | 480 // Deletes an object by destructing it and then changing the type to a |
481 // different value (default 0). | 481 // different value (default 0). |
482 template <typename T> | 482 template <typename T> |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 private: | 702 private: |
703 std::unique_ptr<MemoryMappedFile> mapped_file_; | 703 std::unique_ptr<MemoryMappedFile> mapped_file_; |
704 | 704 |
705 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | 705 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
706 }; | 706 }; |
707 #endif // !defined(OS_NACL) | 707 #endif // !defined(OS_NACL) |
708 | 708 |
709 } // namespace base | 709 } // namespace base |
710 | 710 |
711 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 711 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |