mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2024-12-14 11:58:02 +00:00
chore(rax_tree): Introduce raxFreeWithCallback call in RaxTreeMap destructor
Signed-off-by: Stepan Bagritsevich <stefan@dragonflydb.io>
This commit is contained in:
parent
81079df0e1
commit
2f13e11975
3 changed files with 32 additions and 12 deletions
|
@ -109,12 +109,16 @@ template <typename V> struct RaxTreeMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
~RaxTreeMap() {
|
~RaxTreeMap() {
|
||||||
for (auto it = begin(); it != end(); ++it) {
|
using Allocator = decltype(alloc_);
|
||||||
V* ptr = &(*it).second;
|
|
||||||
std::allocator_traits<decltype(alloc_)>::destroy(alloc_, ptr);
|
auto free_callback = [](void* data, void* context) {
|
||||||
alloc_.deallocate(ptr, 1);
|
Allocator* allocator = static_cast<Allocator*>(context);
|
||||||
}
|
V* ptr = static_cast<V*>(data);
|
||||||
raxFree(tree_);
|
std::allocator_traits<Allocator>::destroy(*allocator, ptr);
|
||||||
|
allocator->deallocate(ptr, 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
raxFreeWithCallbackAndArgument(tree_, free_callback, &alloc_);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
size_t size() const {
|
||||||
|
|
|
@ -1221,29 +1221,44 @@ int raxRemove(rax *rax, unsigned char *s, size_t len, void **old) {
|
||||||
|
|
||||||
/* This is the core of raxFree(): performs a depth-first scan of the
|
/* This is the core of raxFree(): performs a depth-first scan of the
|
||||||
* tree and releases all the nodes found. */
|
* tree and releases all the nodes found. */
|
||||||
void raxRecursiveFree(rax *rax, raxNode *n, void (*free_callback)(void*)) {
|
void raxRecursiveFree(rax *rax, raxNode *n, void (*free_callback)(void*, void*), void* argument) {
|
||||||
debugnode("free traversing",n);
|
debugnode("free traversing",n);
|
||||||
int numchildren = n->iscompr ? 1 : n->size;
|
int numchildren = n->iscompr ? 1 : n->size;
|
||||||
raxNode **cp = raxNodeLastChildPtr(n);
|
raxNode **cp = raxNodeLastChildPtr(n);
|
||||||
while(numchildren--) {
|
while(numchildren--) {
|
||||||
raxNode *child;
|
raxNode *child;
|
||||||
memcpy(&child,cp,sizeof(child));
|
memcpy(&child,cp,sizeof(child));
|
||||||
raxRecursiveFree(rax,child,free_callback);
|
raxRecursiveFree(rax,child,free_callback,argument);
|
||||||
cp--;
|
cp--;
|
||||||
}
|
}
|
||||||
debugnode("free depth-first",n);
|
debugnode("free depth-first",n);
|
||||||
if (free_callback && n->iskey && !n->isnull)
|
if (free_callback && n->iskey && !n->isnull)
|
||||||
free_callback(raxGetData(n));
|
free_callback(raxGetData(n),argument);
|
||||||
rax_free(n);
|
rax_free(n);
|
||||||
rax->numnodes--;
|
rax->numnodes--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Free the entire radix tree, invoking a free_callback function for each key's data.
|
||||||
|
* An additional argument is passed to the free_callback function.*/
|
||||||
|
void raxFreeWithCallbackAndArgument(rax *rax, void (*free_callback)(void*, void*), void* argument) {
|
||||||
|
raxRecursiveFree(rax,rax->head,free_callback,argument);
|
||||||
|
assert(rax->numnodes == 0);
|
||||||
|
rax_free(rax);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Wrapper for the callback to adapt it for the context */
|
||||||
|
void freeCallbackWrapper(void* data, void* argument) {
|
||||||
|
if (!argument) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void (*free_callback)(void*) = (void (*)(void*))argument;
|
||||||
|
free_callback(data);
|
||||||
|
}
|
||||||
|
|
||||||
/* Free a whole radix tree, calling the specified callback in order to
|
/* Free a whole radix tree, calling the specified callback in order to
|
||||||
* free the auxiliary data. */
|
* free the auxiliary data. */
|
||||||
void raxFreeWithCallback(rax *rax, void (*free_callback)(void*)) {
|
void raxFreeWithCallback(rax *rax, void (*free_callback)(void*)) {
|
||||||
raxRecursiveFree(rax,rax->head,free_callback);
|
raxFreeWithCallbackAndArgument(rax, freeCallbackWrapper, (void*)free_callback);
|
||||||
assert(rax->numnodes == 0);
|
|
||||||
rax_free(rax);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free a whole radix tree. */
|
/* Free a whole radix tree. */
|
||||||
|
|
|
@ -195,6 +195,7 @@ int raxRemove(rax *rax, unsigned char *s, size_t len, void **old);
|
||||||
void *raxFind(rax *rax, unsigned char *s, size_t len);
|
void *raxFind(rax *rax, unsigned char *s, size_t len);
|
||||||
void raxFree(rax *rax);
|
void raxFree(rax *rax);
|
||||||
void raxFreeWithCallback(rax *rax, void (*free_callback)(void*));
|
void raxFreeWithCallback(rax *rax, void (*free_callback)(void*));
|
||||||
|
void raxFreeWithCallbackAndArgument(rax *rax, void (*free_callback)(void*, void*), void* argument);
|
||||||
void raxStart(raxIterator *it, rax *rt);
|
void raxStart(raxIterator *it, rax *rt);
|
||||||
int raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len);
|
int raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len);
|
||||||
int raxNext(raxIterator *it);
|
int raxNext(raxIterator *it);
|
||||||
|
|
Loading…
Reference in a new issue