about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-09-02 14:23:09 -0700
committerEric Wong <normalperson@yhbt.net>2010-09-02 14:23:09 -0700
commitb758b29f5842d60933d98c1c292f00f73fedf24b (patch)
tree6ecc1de7e6a947b907fa20f9c4931a7c0954d384
parent03daddf53a88bf1a3b7890d02577dd8921d70b76 (diff)
downloadrpatricia-b758b29f5842d60933d98c1c292f00f73fedf24b.tar.gz
make allocations go through xcalloc in Ruby
Ruby's xcalloc() has built-in failure handling and will force
GC to run if memory is needed.  Patricia.new will no longer
fail with anything other than NoMemoryError, which rare.
-rw-r--r--README7
-rwxr-xr-xext/rpatricia/extconf.rb1
-rw-r--r--ext/rpatricia/patricia.c8
3 files changed, 11 insertions, 5 deletions
diff --git a/README b/README
index 0f9c012..029599a 100644
--- a/README
+++ b/README
@@ -60,10 +60,9 @@ new:
 
       pt = Patricia.new
 
-    This is the class' constructor - it returns a Patricia object upon
-    success or nil on failure. For now, the constructor takes no
-    arguments, and defaults to creating a tree which uses AF_INET IPv4
-    address and mask values as keys.
+    This is the class' constructor - it returns a Patricia object.  For
+    now, the constructor takes no arguments, and defaults to creating a
+    tree which uses AF_INET IPv4 address and mask values as keys.
 
     The Patricia object will be destroyed automatically when there are
     no longer any references to it.
diff --git a/ext/rpatricia/extconf.rb b/ext/rpatricia/extconf.rb
index 18bb7a7..f8753af 100755
--- a/ext/rpatricia/extconf.rb
+++ b/ext/rpatricia/extconf.rb
@@ -1,5 +1,6 @@
 #!/usr/bin/env ruby
 require "mkmf"
 
+have_func("ruby_xcalloc")
 have_func("rb_str_set_len", "ruby.h")
 create_makefile("rpatricia")
diff --git a/ext/rpatricia/patricia.c b/ext/rpatricia/patricia.c
index d211a34..7fc3d27 100644
--- a/ext/rpatricia/patricia.c
+++ b/ext/rpatricia/patricia.c
@@ -31,7 +31,13 @@ static char copyright[] =
 
 #include "patricia.h"
 
-#define Delete free
+#if defined(HAVE_RUBY_XCALLOC)
+#  include <ruby.h>
+#  define calloc xcalloc
+#  define Delete xfree
+#else
+#  define Delete free
+#endif
 
 /* { from prefix.c */