about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-09-02 13:38:01 -0700
committerEric Wong <normalperson@yhbt.net>2010-09-02 13:38:01 -0700
commit531c7ee4fade42ff115ba9df9ca1ca7d7ad631d6 (patch)
tree221db6480087f2231ad3622d8fb1995f9c8c1511
parent916b7be928c1a7c6b4aa8ab2f58987487a78777e (diff)
downloadrpatricia-531c7ee4fade42ff115ba9df9ca1ca7d7ad631d6.tar.gz
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.
-rw-r--r--ext/rpatricia/rpatricia.c10
-rw-r--r--test/test_gc.rb13
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