Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(375)

Unified Diff: base/metrics/persistent_memory_allocator.h

Issue 1803253002: Improved iterator for persistent memory allocator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-hp
Patch Set: rebased Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/metrics/persistent_memory_allocator.h
diff --git a/base/metrics/persistent_memory_allocator.h b/base/metrics/persistent_memory_allocator.h
index 8c849b61d13c0ab5bf4f2ec558e810174e6e94b5..45777fe4477f77777f32942caad4ac9ddf230eba 100644
--- a/base/metrics/persistent_memory_allocator.h
+++ b/base/metrics/persistent_memory_allocator.h
@@ -50,22 +50,66 @@ class BASE_EXPORT PersistentMemoryAllocator {
public:
typedef uint32_t Reference;
- // Internal state information when iterating over memory allocations.
- class Iterator {
+ // Iterator for going through all iterable memory records in an allocator.
+ // Like the allocator itself, iterators are lock-free and thread-secure.
+ // That means that multiple threads can share an iterator and the same
+ // reference will not be returned twice.
+ //
+ // Iteration, in general, is tolerant of corrupted memory. It will return
+ // what it can and stop only when corruption forces it to. Bad corruption
+ // could cause the same object to be returned many times but it will
+ // eventually quit.
+ class BASE_EXPORT Iterator {
public:
- Iterator() : last(0) {}
+ // Constructs an iterator on a given |allocator|, starting at the beginning.
+ // The allocator must live beyond the lifetime of the iterator. This class
+ // has read-only access to the allocator (hence "const") but the returned
+ // references can be used on a read/write version, too.
+ explicit Iterator(const PersistentMemoryAllocator* allocator);
+
+ // As above but resuming from the |starting_after| reference. The first call
+ // to GetNext() will return the next object found after that reference. The
+ // reference must be to an "iterable" object; references to non-iterable
+ // objects (those that never had MakeIterable() called for them) will cause
+ // a run-time error.
+ Iterator(const PersistentMemoryAllocator* allocator,
+ Reference starting_after);
+
+ // Gets the next iterable, storing that type in |type_return|. The actual
+ // return value is a reference to the allocation inside the allocator or
+ // zero if there are no more. GetNext() may still be called again at a
+ // later time to retrieve any new allocations that have been added.
+ Reference GetNext(uint32_t* type_return);
+
+ // Similar to above but gets the next iterable of a specific |type_match|.
+ // This should not be mixed with calls to GetNext() because any allocations
+ // skipped here due to a type mis-match will never be returned by later
+ // calls to GetNext() meaning it's possible to completely miss entries.
+ Reference GetNextOfType(uint32_t type_match);
+
+ // Converts references to objects. This is a convenience method so that
+ // users of the iterator don't need to also have their own pointer to the
+ // allocator over which the iterator runs in order to retrieve objects.
+ // Because the iterator is not read/write, only "const" objects can be
+ // fetched. Non-const objects can be fetched using the reference on a
+ // non-const (external) pointer to the same allocator (or use const_cast
+ // to remove the qualifier).
+ template <typename T>
+ const T* GetAsObject(Reference ref, uint32_t type_id) const {
+ return allocator_->GetAsObject<T>(ref, type_id);
+ }
- bool operator==(const Iterator& rhs) const { return last == rhs.last; }
- bool operator!=(const Iterator& rhs) const { return last != rhs.last; }
+ private:
+ // Weak-pointer to memory allocator being iterated over.
+ const PersistentMemoryAllocator* allocator_;
- void clear() { last = 0; }
- bool is_clear() const { return last == 0; }
+ // The last record that was returned.
+ std::atomic<Reference> last_record_;
- private:
- friend class PersistentMemoryAllocator;
+ // The number of records found; used for detecting loops.
+ std::atomic<uint32_t> record_count_;
- Reference last;
- uint32_t niter;
+ DISALLOW_COPY_AND_ASSIGN(Iterator);
};
// Returned information about the internal state of the heap.
@@ -212,17 +256,6 @@ class BASE_EXPORT PersistentMemoryAllocator {
// also make the true amount less than what is reported.
void GetMemoryInfo(MemoryInfo* meminfo) const;
- // Iterating uses a |state| structure (initialized by CreateIterator) and
- // returns both the reference to the object as well as the |type_id| of
- // that object. A zero return value indicates there are currently no more
- // objects to be found but future attempts can be made without having to
- // reset the iterator to "first". Creating an iterator |starting_after|
- // a known iterable object allows "resume" from that point with the next
- // call to GetNextIterable returning the object after it.
- void CreateIterator(Iterator* state) const { CreateIterator(state, 0); };
- void CreateIterator(Iterator* state, Reference starting_after) const;
- Reference GetNextIterable(Iterator* state, uint32_t* type_id) const;
-
// If there is some indication that the memory has become corrupted,
// calling this will attempt to prevent further damage by indicating to
// all processes that something is not as expected.
« no previous file with comments | « base/metrics/persistent_histogram_allocator_unittest.cc ('k') | base/metrics/persistent_memory_allocator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698