Good guide to writing Ruby extensions in C?

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 :-<

The descriptions in ‘Programming Ruby’ are insufficient for old C
hands …

Gis,

Josef ‘Jupp’ Schugt

Agree… not much documentation.

I have just created a RubyApi site which hopefully will grow and
cover whats the purpose with each call in the “ruby.h” and
“intern.h” files.

add your missing/undocumented methods :slight_smile:

···

On Mon, 16 Jun 2003 10:32:32 +0900, Josef ‘Jupp’ Schugt wrote:

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 :-<

The descriptions in ‘Programming Ruby’ are insufficient for old C
hands …


Simon Strandgaard

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” :slight_smile:

···

On Mon, Jun 16, 2003 at 09:32:32AM +0900, Josef ‘Jupp’ Schugt wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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

Hello,

I’m having problems with memory allocation too. How does this actually work:
If I reserve memory with e.g. ALLOC_N, do I need to register it to gc
somehow? If I put the memory to a C variable, say:

 int a = ALLOC_N(int, 2);

Does this memory get freed by gc or not and if yes, when? If it doesn’t,
I guess I must use ‘free’ and then what is the point of using ALLOC_N?
I don’t want/need to put it into a Ruby object.

Also, if I use plain malloc, is it possible that gc frees it?

It seems that ALLOCA_N uses alloca which is not recommended? And is it
so that alloca might fail for very large requests where malloc works?

Could someone give an example of correct memory allocation? I could not
really get the idea by reading the sources. E.g. in the post below, I
don’t see where the registration to gc might occur. Documentation on
this is badly needed.
Thanks.
m.

Mauricio Fernández wrote:

···

#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.