google / leveldb

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.
BSD 3-Clause "New" or "Revised" License
35.71k stars 7.72k forks source link

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

Open TDAkory opened 5 months ago

TDAkory commented 5 months ago

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.

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?