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