Dupsetup

I was wondering what the purpose of the DUPSETUP() macro is in ruby.h. I didn't find anything about it using google.
What is it intended to be used for?
-Charlie

Charles Mills wrote:

I was wondering what the purpose of the DUPSETUP() macro is in ruby.h.
I didn't find anything about it using google.
What is it intended to be used for?
-Charlie

I guess it may, eventually, be replaced by rb_obj_init_copy
(Object#initialize_copy) referred to in the #dup ri entry [1],
but I'll have a shot at describing what it _was_ for - using
the last remaining example from the source distribution in
array.c:

VALUE
rb_ary_dup(ary)
    VALUE ary;
{
    VALUE dup = rb_ary_new2(RARRAY(ary)->len);

    DUPSETUP(dup, ary);
    MEMCPY(RARRAY(dup)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
    RARRAY(dup)->len = RARRAY(ary)->len;
    return dup;
}

"dup" (an empty array with same length) is created (with default state
for a new array and no associated instance variables).

DUPSETUP copies some state flags and any associated instance variables.

MEMCPY copies the object pointers (contents), then the length update
seems to be redundant ( unchanged since rb_ary_new2() ??).

:daz

[1]
------------------------------------------------------------- Object#dup
     obj.dup -> an_object

···

------------------------------------------------------------------------
     Produces a shallow copy of _obj_---the instance variables of _obj_
     are copied, but not the objects they reference. +dup+ copies the
     tainted state of _obj_. See also the discussion under
     +Object#clone+. In general, +clone+ and +dup+ may have different
     semantics in descendent classes. While +clone+ is used to duplicate
     an object, including its internal state, +dup+ typically uses the
     class of the descendent object to create the new instance.

     This method may have class-specific behavior. If so, that behavior
     will be documented under the #+initialize_copy+ method of the
     class.

------------------------------------------------------------------------

I wrote:

    RARRAY(dup)->len = RARRAY(ary)->len;

[...] then the length update seems to be redundant [...]

Sorry, to save someone correcting me ...

   rb_ary_new2(length)

.... creates an array with capacity of "length" but with
current_length == 0. So the real current_length must be
inserted (happens to be the same as capacity, so any added
entries will force reallocation).

:daz

Charles Mills wrote:

I was wondering what the purpose of the DUPSETUP() macro is in ruby.h.
I didn't find anything about it using google.
What is it intended to be used for?
-Charlie

I guess it may, eventually, be replaced by rb_obj_init_copy
(Object#initialize_copy) referred to in the #dup ri entry [1],
but I'll have a shot at describing what it _was_ for - using
the last remaining example from the source distribution in
array.c:

VALUE
rb_ary_dup(ary)
    VALUE ary;
{
    VALUE dup = rb_ary_new2(RARRAY(ary)->len);

    DUPSETUP(dup, ary);
    MEMCPY(RARRAY(dup)->ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
    RARRAY(dup)->len = RARRAY(ary)->len;
    return dup;
}

"dup" (an empty array with same length) is created (with default state
for a new array and no associated instance variables).

DUPSETUP copies some state flags and any associated instance variables.

Thanks for the response.
Can DUPSETUP() be used for objects with type T_DATA?
-Charlie

···

On Jul 18, 2004, at 12:02 AM, daz wrote:

MEMCPY copies the object pointers (contents), then the length update
seems to be redundant ( unchanged since rb_ary_new2() ??).

:daz

[1]
------------------------------------------------------------- Object#dup
     obj.dup -> an_object
------------------------------------------------------------------------
     Produces a shallow copy of _obj_---the instance variables of _obj_
     are copied, but not the objects they reference. +dup+ copies the
     tainted state of _obj_. See also the discussion under
     +Object#clone+. In general, +clone+ and +dup+ may have different
     semantics in descendent classes. While +clone+ is used to duplicate
     an object, including its internal state, +dup+ typically uses the
     class of the descendent object to create the new instance.

     This method may have class-specific behavior. If so, that behavior
     will be documented under the #+initialize_copy+ method of the
     class.

------------------------------------------------------------------------

Charles Mills wrote:

Can DUPSETUP() be used for objects with type T_DATA?

Yes, I think, because it's a part of constructing an empty object
and not concerned with content.

From Ruby 1.8, classes need not define their own #dup (or #clone)
methods. The default methods (in Object via Kernel) embody the
functionality of DUPSETUP() / CLONESETUP(), then #initialize_copy
is invoked; here, you would copy across the specific T_DATA content.

Any recommendation of what to use to be backward/forward compatible
would have to come from those who know what's going on.
Some detail of your usage requirement might be useful to them.

:daz