| 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 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 // when the last field is actually variable length. | 463 // when the last field is actually variable length. |
| 464 template <typename T> | 464 template <typename T> |
| 465 T* New(size_t size) { | 465 T* New(size_t size) { |
| 466 if (size < sizeof(T)) | 466 if (size < sizeof(T)) |
| 467 size = sizeof(T); | 467 size = sizeof(T); |
| 468 Reference ref = Allocate(size, T::kPersistentTypeId); | 468 Reference ref = Allocate(size, T::kPersistentTypeId); |
| 469 void* mem = | 469 void* mem = |
| 470 const_cast<void*>(GetBlockData(ref, T::kPersistentTypeId, size)); | 470 const_cast<void*>(GetBlockData(ref, T::kPersistentTypeId, size)); |
| 471 if (!mem) | 471 if (!mem) |
| 472 return nullptr; | 472 return nullptr; |
| 473 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (ALIGNOF(T) - 1)); | 473 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (alignof(T) - 1)); |
| 474 return new (mem) T(); | 474 return new (mem) T(); |
| 475 } | 475 } |
| 476 template <typename T> | 476 template <typename T> |
| 477 T* New() { | 477 T* New() { |
| 478 return New<T>(sizeof(T)); | 478 return New<T>(sizeof(T)); |
| 479 } | 479 } |
| 480 | 480 |
| 481 // Similar to New, above, but construct the object out of an existing memory | 481 // Similar to New, above, but construct the object out of an existing memory |
| 482 // block and of an expected type. If |clear| is true, memory will be zeroed | 482 // block and of an expected type. If |clear| is true, memory will be zeroed |
| 483 // before construction. Though this is not standard object behavior, it | 483 // before construction. Though this is not standard object behavior, it |
| 484 // is present to match with new allocations that always come from zeroed | 484 // is present to match with new allocations that always come from zeroed |
| 485 // memory. Anything previously present simply ceases to exist; no destructor | 485 // memory. Anything previously present simply ceases to exist; no destructor |
| 486 // is called for it so explicitly Delete() the old object first if need be. | 486 // is called for it so explicitly Delete() the old object first if need be. |
| 487 // Calling this will not invalidate existing pointers to the object, either | 487 // Calling this will not invalidate existing pointers to the object, either |
| 488 // in this process or others, so changing the object could have unpredictable | 488 // in this process or others, so changing the object could have unpredictable |
| 489 // results. USE WITH CARE! | 489 // results. USE WITH CARE! |
| 490 template <typename T> | 490 template <typename T> |
| 491 T* New(Reference ref, uint32_t from_type_id, bool clear) { | 491 T* New(Reference ref, uint32_t from_type_id, bool clear) { |
| 492 DCHECK_LE(sizeof(T), GetAllocSize(ref)) << "alloc not big enough for obj"; | 492 DCHECK_LE(sizeof(T), GetAllocSize(ref)) << "alloc not big enough for obj"; |
| 493 // Make sure the memory is appropriate. This won't be used until after | 493 // Make sure the memory is appropriate. This won't be used until after |
| 494 // the type is changed but checking first avoids the possibility of having | 494 // the type is changed but checking first avoids the possibility of having |
| 495 // to change the type back. | 495 // to change the type back. |
| 496 void* mem = const_cast<void*>(GetBlockData(ref, 0, sizeof(T))); | 496 void* mem = const_cast<void*>(GetBlockData(ref, 0, sizeof(T))); |
| 497 if (!mem) | 497 if (!mem) |
| 498 return nullptr; | 498 return nullptr; |
| 499 // Ensure the allocator's internal alignment is sufficient for this object. | 499 // Ensure the allocator's internal alignment is sufficient for this object. |
| 500 // This protects against coding errors in the allocator. | 500 // This protects against coding errors in the allocator. |
| 501 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (ALIGNOF(T) - 1)); | 501 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(mem) & (alignof(T) - 1)); |
| 502 // Change the type, clearing the memory if so desired. The new type is | 502 // Change the type, clearing the memory if so desired. The new type is |
| 503 // "transitioning" so that there is no race condition with the construction | 503 // "transitioning" so that there is no race condition with the construction |
| 504 // of the object should another thread be simultaneously iterating over | 504 // of the object should another thread be simultaneously iterating over |
| 505 // data. This will "acquire" the memory so no changes get reordered before | 505 // data. This will "acquire" the memory so no changes get reordered before |
| 506 // it. | 506 // it. |
| 507 if (!ChangeType(ref, kTypeIdTransitioning, from_type_id, clear)) | 507 if (!ChangeType(ref, kTypeIdTransitioning, from_type_id, clear)) |
| 508 return nullptr; | 508 return nullptr; |
| 509 // Construct an object of the desired type on this memory, just as if | 509 // Construct an object of the desired type on this memory, just as if |
| 510 // New() had been called to create it. | 510 // New() had been called to create it. |
| 511 T* obj = new (mem) T(); | 511 T* obj = new (mem) T(); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 private: | 714 private: |
| 715 std::unique_ptr<MemoryMappedFile> mapped_file_; | 715 std::unique_ptr<MemoryMappedFile> mapped_file_; |
| 716 | 716 |
| 717 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | 717 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
| 718 }; | 718 }; |
| 719 #endif // !defined(OS_NACL) | 719 #endif // !defined(OS_NACL) |
| 720 | 720 |
| 721 } // namespace base | 721 } // namespace base |
| 722 | 722 |
| 723 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 723 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
| OLD | NEW |