From 531c7ee4fade42ff115ba9df9ca1ca7d7ad631d6 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 2 Sep 2010 13:38:01 -0700 Subject: Patricia#{clear,destroy} clears the tree While the GC can take care of this, this is still useful if one wishes to reuse the same object. --- ext/rpatricia/rpatricia.c | 10 ++++++---- test/test_gc.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/ext/rpatricia/rpatricia.c b/ext/rpatricia/rpatricia.c index f9564d2..866572c 100644 --- a/ext/rpatricia/rpatricia.c +++ b/ext/rpatricia/rpatricia.c @@ -12,13 +12,15 @@ static VALUE cPatricia, cNode; static void dummy(void) {} -/* - * this method only exists for backwards compatibility, we now rely - * on the GC to do all the dirty work of freeing node data for us - */ static VALUE p_destroy (VALUE self) { + patricia_tree_t *tree; + + Data_Get_Struct(self, patricia_tree_t, tree); + Clear_Patricia(tree, dummy); + tree->head = NULL; /* Clear_Patricia() should do this, actually */ + return Qtrue; } diff --git a/test/test_gc.rb b/test/test_gc.rb index 316d355..2ea6df1 100644 --- a/test/test_gc.rb +++ b/test/test_gc.rb @@ -15,6 +15,19 @@ class TestGc < Test::Unit::TestCase @strings.add('127.0.0.0/24', "localhost") end + def test_clear + 1_000_000.times do + @strings.clear + assert_equal 0, @strings.num_nodes + @strings.add('10.0.0.0/8', "big lan") + assert_equal 1, @strings.num_nodes + @strings.add('127.0.0.0/8', "localhost") + assert_equal 2, @strings.num_nodes + @strings.add('192.168.1.0/24', "home") + assert_equal 3, @strings.num_nodes + end + end + def test_gc_dup 100000.times do tmp = @strings.dup -- cgit v1.2.3-24-ge0c7