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 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 // PersistentMemoryAllocator: | 760 // PersistentMemoryAllocator: |
761 void FlushPartial(size_t length, bool sync) override; | 761 void FlushPartial(size_t length, bool sync) override; |
762 | 762 |
763 private: | 763 private: |
764 std::unique_ptr<MemoryMappedFile> mapped_file_; | 764 std::unique_ptr<MemoryMappedFile> mapped_file_; |
765 | 765 |
766 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); | 766 DISALLOW_COPY_AND_ASSIGN(FilePersistentMemoryAllocator); |
767 }; | 767 }; |
768 #endif // !defined(OS_NACL) | 768 #endif // !defined(OS_NACL) |
769 | 769 |
| 770 // An allocation that is defined but not executed until required at a later |
| 771 // time. This allows for potential users of an allocation to be decoupled |
| 772 // from the logic that defines it. In addition, there can be multiple users |
| 773 // of the same allocation or any region thereof that are guaranteed to always |
| 774 // use the same space. It's okay to copy/move these objects. |
| 775 // |
| 776 // This is a top-level class instead of an inner class of the PMA so that it |
| 777 // can be forward-declared in other header files without the need to include |
| 778 // the full contents of this file. |
| 779 class BASE_EXPORT DelayedPersistentAllocation { |
| 780 public: |
| 781 using Reference = PersistentMemoryAllocator::Reference; |
| 782 |
| 783 // Creates a delayed allocation using the specified |allocator|. When |
| 784 // needed, the memory will be allocated using the specified |type| and |
| 785 // |size|. If |offset| is given, the returned pointer will be at that |
| 786 // offset into the segment; this allows combining allocations into a |
| 787 // single persistent segment to reduce overhead and means an "all or |
| 788 // nothing" request. Note that |size| is always the total memory size |
| 789 // and |offset| is just indicating the start of a block within it. If |
| 790 // |make_iterable| was true, the allocation will made iterable when it |
| 791 // is created; already existing allocations are not changed. |
| 792 // |
| 793 // Once allocated, a reference to the segment will be stored at |ref|. |
| 794 // This shared location must be initialized to zero (0); it is checked |
| 795 // with every Get() request to see if the allocation has already been |
| 796 // done. |
| 797 // |
| 798 // For convenience, methods taking both Atomic32 and std::atomic<Reference> |
| 799 // are defined. |
| 800 DelayedPersistentAllocation(PersistentMemoryAllocator* allocator, |
| 801 subtle::Atomic32* ref, |
| 802 uint32_t type, |
| 803 size_t size, |
| 804 bool make_iterable); |
| 805 DelayedPersistentAllocation(PersistentMemoryAllocator* allocator, |
| 806 subtle::Atomic32* ref, |
| 807 uint32_t type, |
| 808 size_t size, |
| 809 size_t offset, |
| 810 bool make_iterable); |
| 811 DelayedPersistentAllocation(PersistentMemoryAllocator* allocator, |
| 812 std::atomic<Reference>* ref, |
| 813 uint32_t type, |
| 814 size_t size, |
| 815 bool make_iterable); |
| 816 DelayedPersistentAllocation(PersistentMemoryAllocator* allocator, |
| 817 std::atomic<Reference>* ref, |
| 818 uint32_t type, |
| 819 size_t size, |
| 820 size_t offset, |
| 821 bool make_iterable); |
| 822 ~DelayedPersistentAllocation(); |
| 823 |
| 824 // Gets a pointer to the defined allocation. This will realize the request |
| 825 // and update the reference provided during construction. The memory will |
| 826 // be zeroed the first time it is returned, after that it is shared with |
| 827 // all other Get() requests and so shows any changes made to it elsewhere. |
| 828 // |
| 829 // If the allocation fails for any reason, null will be returned. This works |
| 830 // even on "const" objects because the allocation is already defined, just |
| 831 // delayed. |
| 832 void* Get() const; |
| 833 |
| 834 // Gets the internal reference value. If this returns a non-zero value then |
| 835 // a subsequent call to Get() will do nothing but convert that reference into |
| 836 // a memory location -- useful for accessing an existing allocation without |
| 837 // creating one unnecessarily. |
| 838 Reference reference() const { |
| 839 return reference_->load(std::memory_order_relaxed); |
| 840 } |
| 841 |
| 842 private: |
| 843 // The underlying object that does the actual allocation of memory. Its |
| 844 // lifetime must exceed that of all DelayedPersistentAllocation objects |
| 845 // that use it. |
| 846 PersistentMemoryAllocator* const allocator_; |
| 847 |
| 848 // The desired type and size of the allocated segment plus the offset |
| 849 // within it for the defined request. |
| 850 const uint32_t type_; |
| 851 const size_t size_; |
| 852 const size_t offset_; |
| 853 |
| 854 // Flag indicating if allocation should be made iterable when done. |
| 855 const bool make_iterable_; |
| 856 |
| 857 // The location at which a reference to the allocated segment is to be |
| 858 // stored once the allocation is complete. If multiple delayed allocations |
| 859 // share the same pointer then an allocation on one will amount to an |
| 860 // allocation for all. |
| 861 volatile std::atomic<Reference>* const reference_; |
| 862 |
| 863 // No DISALLOW_COPY_AND_ASSIGN as it's okay to copy/move these objects. |
| 864 }; |
| 865 |
770 } // namespace base | 866 } // namespace base |
771 | 867 |
772 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ | 868 #endif // BASE_METRICS_PERSISTENT_MEMORY_ALLOCATOR_H_ |
OLD | NEW |