From 6caf73ad9dae0ee91873bcb39554537b85163770 Mon Sep 17 00:00:00 2001 From: costan Date: Mon, 4 Jun 2018 12:29:13 -0700 Subject: Clean up Iterator. This CL renames the private struct Iterator::Cleanup -> Iterator::CleanupNode, to better reflect that it's a linked list node, and extracts duplicated code from its user in IsEmpty() and Run() methods. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=199175058 --- include/leveldb/iterator.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/leveldb/iterator.h b/include/leveldb/iterator.h index 436508a..6c1d91b 100644 --- a/include/leveldb/iterator.h +++ b/include/leveldb/iterator.h @@ -77,17 +77,25 @@ class LEVELDB_EXPORT Iterator { // // Note that unlike all of the preceding methods, this method is // not abstract and therefore clients should not override it. - typedef void (*CleanupFunction)(void* arg1, void* arg2); + using CleanupFunction = void (*)(void* arg1, void* arg2); void RegisterCleanup(CleanupFunction function, void* arg1, void* arg2); private: - struct Cleanup { + // Cleanup functions are stored in a single-linked list. + // The list's head node is inlined in the iterator. + struct CleanupNode { + // The head node is used if the function pointer is not null. CleanupFunction function; void* arg1; void* arg2; - Cleanup* next; + CleanupNode* next; + + // True if the node is not used. Only head nodes might be unused. + bool IsEmpty() const { return function == nullptr; } + // Invokes the cleanup function. + void Run() { assert(function != nullptr); (*function)(arg1, arg2); } }; - Cleanup cleanup_; + CleanupNode cleanup_head_; }; // Return an empty iterator (yields nothing). -- cgit v1.2.1