(2010/07/09 19:22), Nikolai Weibull wrote:
> On Fri, Jul 9, 2010 at 11:58, Urabe Shyouhei <shyouhei / ruby-lang.org> wrote:
>> (2010/07/09 18:28), Nikolai Weibull wrote:
>>> I own result, so after creating rbresult I currently have to
>>> immediately free it.
> 
>> The problem is the ruby's GC engine cannot know if you own result.  When GC
>> reclaims its memory space, any shared memory regions shall not be freed, and
>> given it cannot say if your variable is for your own or not, the only safe way
>> for the collector is to copy the memory for its own.
> 
> (What variable are we talking about here, Ruby or C?)
> 
> But how is the call to ALLOC_N() in str_new in string.c any different?
>  I mean, ALLOC_N calls vm_xmalloc, which calls malloc.  I see no fancy
> handling of the memory here, more than updating malloc_increase
> (unless CALC_EXACT_MALLOC_SIZE is on).

Ruby Genesis 1:1 In the beginning Matz created the Ruby String, so that at
most one Ruby String object can point a specific address of memory at one
time.  So the answer to your question is: a C string reachable from a Ruby
String is, by definition, owned by that Ruby String.  Strictly speaking this
is no longer true because Ruby Strings can now be CoW, but at least Ruby
Strings are still "closed" in a sense of memory spaces.

> I°«m sorry for being so slow to understand, but I still don°«t see why
> Ruby can°«t use the char * that I°«ve malloced myself.  I want to be
> able to tell Ruby that °…here, take this char *, I own it, no one else
> knows about it, guaranteed, and I°«m now giving it to you for you to
> own, I don°«t want it anymore°….

No, sorry Ruby can't trust you.  Not even myself.  When a machine can trust a
human, there is absolutely zero reason for a GC to exist.  Ruby GC's base line
is that C Strings are owned by Ruby Strings, and that line should not be
broken just because you are sure by a heuristic approach.