Ask a Question related to Ruby, Design and Development.
-
m #1
Re: Good guide to writing Ruby extensions in C?
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.m Guest
-
good guide for setting up mysql
I am trying to find a good guide for setting up mysql on a stand alone linux box just to play around with but I can't find any good one do you have... -
Good MIME::Parser reference guide?
I can't seem to find a decent reference for the MIME::Parser Anybody out there know of a good book or site that explains this module well? Thank... -
Tips on a good POSIX guide?
Greg P. wrote: http://www.gnu.org/manual/manual.html http://www.gnu.org/manual/glibc-2.2.5/libc.html -
A Quick Guide to SQLite and Ruby
-Talkers: I've been truly impressed by SQLite, a small database with support for much of SQL-92. This is a swift, ACIDic, customizable database... -
Ruby Developer's Guide - hurt book sale
I have just recived my book , and it just have a little imperfection(almost invisible). for just 30 dll (20 ship) , I think I did a good buy.. ...



Reply With Quote

