summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-10-03 07:19:19 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-10-03 07:19:19 +0000
commit1fe40b7cc5e92105f636d670d77b059fe4a4c50b (patch)
tree02dfc7bab198fc494d9d4f1f3bf1072d292fed66 /array.c
parentd902111a57dfcf3c9b017b0ebd1b49f19142168c (diff)
* marshal.c (r_object): better allocation type check for
TYPE_UCLASS. usage of allocation framework is disabled for now. * variable.c (rb_class_path): Module may have subclass. * string.c (rb_str_update): should maintain original negative offset. * string.c (rb_str_subpat_set): ditto * string.c (rb_str_aset): ditto. * re.c (rb_reg_nth_match): should check negative nth. * re.c (rb_reg_nth_defined): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1764 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c59
1 files changed, 26 insertions, 33 deletions
diff --git a/array.c b/array.c
index 81b09cd..9b81bd5 100644
--- a/array.c
+++ b/array.c
@@ -71,12 +71,25 @@ rb_ary_frozen_p(ary)
return Qfalse;
}
+static VALUE
+rb_ary_s_alloc(klass)
+ VALUE klass;
+{
+ NEWOBJ(ary, struct RArray);
+ OBJSETUP(ary, rb_cArray, T_ARRAY);
+
+ ary->len = 0;
+ ary->capa = 0;
+ ary->ptr = 0;
+
+ return (VALUE)ary;
+}
+
VALUE
rb_ary_new2(len)
long len;
{
- NEWOBJ(ary, struct RArray);
- OBJSETUP(ary, rb_cArray, T_ARRAY);
+ VALUE ary = rb_obj_alloc(rb_cArray);
if (len < 0) {
rb_raise(rb_eArgError, "negative array size (or size too big)");
@@ -84,13 +97,11 @@ rb_ary_new2(len)
if (len > 0 && len*sizeof(VALUE) <= 0) {
rb_raise(rb_eArgError, "array size too big");
}
- ary->len = 0;
- ary->capa = len;
- ary->ptr = 0;
if (len == 0) len++;
- ary->ptr = ALLOC_N(VALUE, len);
+ RARRAY(ary)->ptr = ALLOC_N(VALUE, len);
+ RARRAY(ary)->capa = len;
- return (VALUE)ary;
+ return ary;
}
VALUE
@@ -143,7 +154,7 @@ rb_ary_new4(n, elts)
VALUE ary;
ary = rb_ary_new2(n);
- if (elts) {
+ if (n > 0 && elts) {
MEMCPY(RARRAY(ary)->ptr, elts, VALUE, n);
}
RARRAY(ary)->len = n;
@@ -166,19 +177,6 @@ rb_assoc_new(car, cdr)
}
static VALUE
-rb_ary_s_new(argc, argv, klass)
- int argc;
- VALUE *argv;
- VALUE klass;
-{
- VALUE ary = rb_ary_new();
- OBJSETUP(ary, klass, T_ARRAY);
- rb_obj_call_init(ary, argc, argv);
-
- return ary;
-}
-
-static VALUE
to_ary(ary)
VALUE ary;
{
@@ -237,20 +235,15 @@ rb_ary_s_create(argc, argv, klass)
VALUE *argv;
VALUE klass;
{
- NEWOBJ(ary, struct RArray);
- OBJSETUP(ary, klass, T_ARRAY);
+ VALUE ary = rb_obj_alloc(klass);
- ary->len = ary->capa = 0;
- if (argc == 0) {
- ary->ptr = 0;
- }
- else {
- ary->ptr = ALLOC_N(VALUE, argc);
- MEMCPY(ary->ptr, argv, VALUE, argc);
+ if (argc != 0) {
+ RARRAY(ary)->ptr = ALLOC_N(VALUE, argc);
+ MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
}
- ary->len = ary->capa = argc;
+ RARRAY(ary)->len = RARRAY(ary)->capa = argc;
- return (VALUE)ary;
+ return ary;
}
void
@@ -1715,7 +1708,7 @@ Init_Array()
rb_cArray = rb_define_class("Array", rb_cObject);
rb_include_module(rb_cArray, rb_mEnumerable);
- rb_define_singleton_method(rb_cArray, "new", rb_ary_s_new, -1);
+ rb_define_singleton_method(rb_cArray, "allocate", rb_ary_s_alloc, 0);
rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);
rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1);
rb_define_method(rb_cArray, "to_s", rb_ary_to_s, 0);