@@ -1221,29 +1221,44 @@ int raxRemove(rax *rax, unsigned char *s, size_t len, void **old) {
1221
1221
1222
1222
/* This is the core of raxFree(): performs a depth-first scan of the
1223
1223
* tree and releases all the nodes found. */
1224
- void raxRecursiveFree (rax * rax , raxNode * n , void (* free_callback )(void * ) ) {
1224
+ void raxRecursiveFree (rax * rax , raxNode * n , void (* free_callback )(void * , void * ), void * argument ) {
1225
1225
debugnode ("free traversing" ,n );
1226
1226
int numchildren = n -> iscompr ? 1 : n -> size ;
1227
1227
raxNode * * cp = raxNodeLastChildPtr (n );
1228
1228
while (numchildren -- ) {
1229
1229
raxNode * child ;
1230
1230
memcpy (& child ,cp ,sizeof (child ));
1231
- raxRecursiveFree (rax ,child ,free_callback );
1231
+ raxRecursiveFree (rax ,child ,free_callback , argument );
1232
1232
cp -- ;
1233
1233
}
1234
1234
debugnode ("free depth-first" ,n );
1235
1235
if (free_callback && n -> iskey && !n -> isnull )
1236
- free_callback (raxGetData (n ));
1236
+ free_callback (raxGetData (n ), argument );
1237
1237
rax_free (n );
1238
1238
rax -> numnodes -- ;
1239
1239
}
1240
1240
1241
+ /* Free the entire radix tree, invoking a free_callback function for each key's data.
1242
+ * An additional argument is passed to the free_callback function.*/
1243
+ void raxFreeWithCallbackAndArgument (rax * rax , void (* free_callback )(void * , void * ), void * argument ) {
1244
+ raxRecursiveFree (rax ,rax -> head ,free_callback ,argument );
1245
+ assert (rax -> numnodes == 0 );
1246
+ rax_free (rax );
1247
+ }
1248
+
1249
+ /* Wrapper for the callback to adapt it for the context */
1250
+ void freeCallbackWrapper (void * data , void * argument ) {
1251
+ if (!argument ) {
1252
+ return ;
1253
+ }
1254
+ void (* free_callback )(void * ) = (void (* )(void * ))argument ;
1255
+ free_callback (data );
1256
+ }
1257
+
1241
1258
/* Free a whole radix tree, calling the specified callback in order to
1242
1259
* free the auxiliary data. */
1243
1260
void raxFreeWithCallback (rax * rax , void (* free_callback )(void * )) {
1244
- raxRecursiveFree (rax ,rax -> head ,free_callback );
1245
- assert (rax -> numnodes == 0 );
1246
- rax_free (rax );
1261
+ raxFreeWithCallbackAndArgument (rax , freeCallbackWrapper , (void * )free_callback );
1247
1262
}
1248
1263
1249
1264
/* Free a whole radix tree. */
0 commit comments