On Mon, Jun 16, 2003 at 09:32:32AM +0900, Josef 'Jupp' Schugt wrote:
> Saluton!
> 
> I am presently using 'Programming Ruby' but I have problems with the
> over-minimalistic description of the C function's behavior.
> 
> I did google (both on WWW and Usenet) but that didn't reveal anything
> useful. I for example need some more detailed information on how
> REALLOC_N works to avoid core dumps which I presently face :-<

#define REALLOC_N(var,type,n) (var)=(type*)xrealloc((char*)(var),sizeof(type)*(n))
#define xrealloc ruby_xrealloc

void *
ruby_xrealloc(ptr, size)
    void *ptr;
    long size;
{
    void *mem;

    if (size < 0) {
        rb_raise(rb_eArgError, "negative re-allocation size");
    }
    if (!ptr) return xmalloc(size);
    if (size == 0) size = 1;
    malloc_increase += size;
    RUBY_CRITICAL(mem = realloc(ptr, size));
    if (!mem) {
        rb_gc();
        RUBY_CRITICAL(mem = realloc(ptr, size));
        if (!mem) {
            rb_memerror();
        }
    }

    return mem;
}

REALLOC_N is passed a pointer, the corresponding type and the number
of objects to (re)allocate place for. (Re)allocation is tried first and
in case of failure a GC run is launched. If it fails twice an exception
(NoMemError) is raised.

In case of success, newly allocated memory (past the block that was
given the REALLOC_N) is uninitialized.

> The descriptions in 'Programming Ruby' are insufficient for old C
> hands ...

Fortunately the code is always up-to-date, and "it even documents
the bugs" :-)

-- 
 _           _                             
| |__   __ _| |_ ___ _ __ ___   __ _ _ __  
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ 
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
	Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

panic("Foooooooood fight!");
	-- In the kernel source aha1542.c, after detecting a bad segment list