On Wed, Aug 21, 2002 at 12:30:24AM +0900, Bill Tj wrote:
> Suppose we assume that the marking functions are really proper, i.e.,
> they always check the state of each memory before rb_gc_mark is called
> such as:
> 
>     if (ptr) rb_gc_mark (...);
> 
> Suppose also we have a struct such as
> 
>     typedef struct
>     {
>         struct_A *data1;
>         struct_B *data2;
>     } struct_C;
> 
> which is involved in some rb_gc_mark() because struct_C is part of
> (but not direct) internal data of some class.  

If I understand it correctly, you've got a pointer to a struct_C value
inside the struct that is actually Data_Wrap_Struct()'ed.

It seems to me that either
 * the struct_C value is ALLOCated or malloc()ed and there's no need
   to mark any VALUE whatsoever (as it isn't wrapped), so it won't attempt
   to mark data1 and data2 before these parts of the struct are properly
   initialized
 * you use Data_Make_Struct and the struct is memset to 0, so nothing
  bad happens if the mark function is called by rb_gc_mark(). 

> Now, the following code
> may fail:
> 
>     ....
>     ptr = ALLOC (struct_C);
>     ptr->data1 = ALLOC (struct_A);    /* may fail here because of
> rb_gc() */
>     ptr->data2 = ALLOC (struct_B);

Why? There's no marking involved if you only use ALLOC, AFAIK.
No way ptr->data1 and ptr->data2 can be used before initialized.

> where SAFE_MALLOC is just a wrapper of malloc() that deals with
> out-of-memory.  The only way to solve the ALLOC problem is to do
> "double-initialization":
> 
>     ....
>     ptr = ALLOC (struct_C);
>     ptr->data1 = NULL;
>     ptr->data2 = NULL;    /* or just use one memset when it is proper
> */
>     ptr->data1 = ALLOC (struct_A);
>     ....

I can't see the point of this, but wouldn't SAFE_ALLOC (a wrapper to
ALLOC which memsets to 0) be a better solution?


I don't really see the problem in the code above; maybe if you gave a
little more context. I think it's a non-issue as no marking function is
involved to mark the fields of struct_C, AFAIK.

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
  
Linux: the operating system with a CLUE... Command Line User Environment.
	-- seen in a posting in comp.software.testing