Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TDAkory opened this issue Feb 4, 2024 · 0 comments

Comments

@TDAkory
Copy link

TDAkory commented Feb 4, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant