From b758b29f5842d60933d98c1c292f00f73fedf24b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 2 Sep 2010 14:23:09 -0700 Subject: 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. --- README | 7 +++---- ext/rpatricia/extconf.rb | 1 + ext/rpatricia/patricia.c | 8 +++++++- 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 +# define calloc xcalloc +# define Delete xfree +#else +# define Delete free +#endif /* { from prefix.c */ -- cgit v1.2.3-24-ge0c7