ALLOCA_N() isn't good because stacks are not meant to be huge and often have
reasonable resource limits imposed on them to prevent programs from running
away in infinite loops until they use all the machine's memory. alloca()
would be an elegant solution to the necessity of temporary storage -- so
maybe ALLOCA_N() should just allocate a new "InternalData" object or
something and return its data pointer?
Ruby should be using its internal GC for all memory allocations, I believe.
Here's an example of something using ALLOCA_N that was easily corrected to
use the proper Ruby type instead (and it's more readable). I'd like to see
this kind of change made for another reason, too: it makes Ruby more
suitable for an embedded interpreter in small devices.
--- re.c Fri Sep 1 05:18:11 2000
+++ /home/green/ruby/re.c Mon Sep 25 13:42:47 2000
@@ -1035,7 +1035,7 @@
int argc;
VALUE *argv;
{
- VALUE str, kcode;
+ VALUE str, kcode, newstr;
int kcode_saved = reg_kcode;
char *s, *send, *t;
char *tmp;
@@ -1049,7 +1049,8 @@
}
s = rb_str2cstr(str, &len);
send = s + len;
- tmp = ALLOCA_N(char, len*2);
+ newstr = rb_str_new(0, len * 2);
+ tmp = RSTRING(newstr)->ptr;
t = tmp;
for (; s < send; s++) {
@@ -1072,9 +1073,10 @@
}
*t++ = *s;
}
+ rb_str_resize(newstr, t - tmp);
kcode_reset_option();
- return rb_str_new(tmp, t - tmp);
+ return newstr;
}
int
--
Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! /
green / FreeBSD.org `------------------------------'