@@ -78,7 +78,7 @@ ffi_cdef[[
7878 int radix_tree_pcre(void *it, const unsigned char *buf, size_t len);
7979 int radix_tree_stop(void *it);
8080
81- void *radix_tree_new_it();
81+ void *radix_tree_new_it(void *t );
8282]]
8383
8484
@@ -290,8 +290,15 @@ function _M.new(routes)
290290
291291 local route_n = # routes
292292
293+ local tree = radix .radix_tree_new ()
294+ local tree_it = radix .radix_tree_new_it (tree )
295+ if tree_it == nil then
296+ error (" failed to new radixtree iterator" )
297+ end
298+
293299 local self = setmt__gc ({
294- tree = radix .radix_tree_new (),
300+ tree = tree ,
301+ tree_it = tree_it ,
295302 match_data_index = 0 ,
296303 match_data = new_tab (# routes , 0 ),
297304 hash_path = new_tab (0 , # routes ),
@@ -318,12 +325,18 @@ end -- do
318325
319326
320327function _M .free (self )
321- if not self .tree then
322- return
328+ local it = self .tree_it
329+ if it then
330+ radix .radix_tree_stop (it )
331+ ffi .C .free (it )
332+ self .tree_it = nil
333+ end
334+
335+ if self .tree then
336+ radix .radix_tree_destroy (self .tree )
337+ self .tree = nil
323338 end
324339
325- radix .radix_tree_destroy (self .tree )
326- self .tree = nil
327340 return
328341end
329342
@@ -481,13 +494,6 @@ local function match_route_opts(route, opts)
481494 return true
482495end
483496
484- local radix_it = radix .radix_tree_new_it ()
485- if radix_it == nil then
486- error (" failed to new radixtree it" )
487- end
488- -- use gc to free
489- ffi .gc (radix_it , ffi .C .free )
490-
491497local function _match_from_routes (routes , path , opts )
492498 for _ , route in ipairs (routes ) do
493499 if route .path_op == " =" then
@@ -516,7 +522,7 @@ local function match_route(self, path, opts)
516522 end
517523 end
518524
519- local it = radix .radix_tree_search (self .tree , radix_it , path , # path )
525+ local it = radix .radix_tree_search (self .tree , self . tree_it , path , # path )
520526 if not it then
521527 return nil , " failed to match"
522528 end
@@ -531,13 +537,11 @@ local function match_route(self, path, opts)
531537 if routes then
532538 local route = _match_from_routes (routes , path , opts )
533539 if route then
534- radix .radix_tree_stop (it )
535540 return route
536541 end
537542 end
538543 end
539544
540- radix .radix_tree_stop (it )
541545 return nil
542546end
543547
0 commit comments