You may sometimes need to allocate memory in an extension that
won't be used for object storage---perhaps you've got a giant bitmap
for a Bloom filter, or an image, or a whole bunch of little structures
that Ruby doesn't use directly.
In order to work correctly with the garbage collector, you should use
the following memory allocation routines. These routines do a little
bit more work than the standard
malloc
. For instance, if
ALLOC_N
determines that it cannot allocate the desired amount of
memory, it will invoke the garbage collector to try to reclaim some
space. It will raise a
NoMemError
if it can't or if the requested amount of memory is
invalid.
Memory Allocation |
type *�
|
ALLOC_N(c-type, n")
|
� |
Allocates n c-type objects, where c-type is
the literal name of the C type, not a variable of that type. |
type *�
|
ALLOC(c-type")
|
� |
Allocates a c-type and casts the result to a pointer of
that type. |
�
|
REALLOC_N(var, c-type, n")
|
� |
Reallocates n c-types and assigns the result to var,
a pointer to a c-type. |
type *�
|
ALLOCA_N(c-type, n")
|
� |
Allocates memory for n objects of c-type on the
stack---this memory will be automatically freed when the function
that invokes ALLOCA_N returns. |