Skip to content

Commit

Permalink
options/posix: Implement tdestroy
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci committed Feb 24, 2024
1 parent 7e92eae commit 94f44e8
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions options/posix/generic/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,23 @@ void twalk(const void *, void (*action)(const void *, VISIT, int)) {
__builtin_unreachable();
}

void tdestroy(void *, void (*free_node)(void *)) {
(void)free_node;
__ensure(!"Not implemented");
__builtin_unreachable();
void tdestroy(void *root, void (*free_node)(void *)) {
auto *n = static_cast<node *>(root);
frg::stack<node *, MemoryAllocator> nodes(getAllocator());

while (n || !nodes.empty()) {
if (n == nullptr) {
n = nodes.top();
nodes.pop();
free_node(const_cast<void *>(n->key));
auto *next = static_cast<node *>(n->a[1]);
free(n);
n = next;
} else {
nodes.push(n);
n = static_cast<node *>(n->a[0]);
}
}
}

void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
Expand Down

0 comments on commit 94f44e8

Please sign in to comment.