google / google/leveldb

[Question] Why Cleanup Functions in Iterator are registered in a wired way ?

Open
#1,167 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.4k
Forks
8.2k
PR merge metrics
No merged PRs in 30d

Description

I've read some source code of Iterator and having this confusion about `Iterator::RegisterCleanup`

```
void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
assert(func != nullptr);
CleanupNode* node;
if (cleanup_head_.IsEmpty()) {
node = &cleanup_head_;
} else {
node = new CleanupNode();
node->next = cleanup_head_.next;
cleanup_head_.next = node;
}
node->function = func;
node->arg1 = arg1;
node->arg2 = arg2;
}
```

Let's say I want to register multiple cleanup functions in an Iterator.

* The first function will be in cleanup_head_, the first place of linked list
* The second function will be in cleanup_head_.next, the second place of linked list, which is fine by now
* But the third function will be in cleanup_head_.next, the second place of linked list, so far and so forth, why do insertion always happens in the second place of linked list?

If I'm understanding it right: If we registered 4 cleanup functions like [A,B,C,D] , it will called in a sequence like [A, D, C, B]
I demonstrated it here :https://godbolt.org/z/Kf6YYfnKn

what's the design thoughts here?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.