|
#define | RUBY_ALLOCV_LIMIT 1024 |
| Maximum possible number of bytes that RB_ALLOCV can allocate using alloca . More...
|
|
#define | RB_GC_GUARD(v) |
| Prevents premature destruction of local objects. More...
|
|
#define | RB_ALLOC_N(type, n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type))) |
| Convenient macro that allocates an array of n elements. More...
|
|
#define | RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type))) |
| Shorthand of RB_ALLOC_N with n=1 . More...
|
|
#define | RB_ZALLOC_N(type, n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type))) |
| Identical to RB_ALLOC_N() but also nullifies the allocated region before returning. More...
|
|
#define | RB_ZALLOC(type) (RB_ZALLOC_N(type, 1)) |
| Shorthand of RB_ZALLOC_N with n=1 . More...
|
|
#define | RB_REALLOC_N(var, type, n) ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type)))) |
| Convenient macro that reallocates an array with a new size. More...
|
|
#define | ALLOCA_N(type, n) RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n)))) |
|
#define | RB_ALLOCV(v, n) |
| Identical to RB_ALLOCV_N(), except that it allocates a number of bytes and returns a void* . More...
|
|
#define | RB_ALLOCV_N(type, v, n) |
| Allocates a memory region, possibly on stack. More...
|
|
#define | RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v)) |
| Polite way to declare that the given array is not used any longer. More...
|
|
#define | MEMZERO(p, type, n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n))) |
| Handy macro to erase a region of memory. More...
|
|
#define | MEMCPY(p1, p2, type, n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n))) |
| Handy macro to call memcpy. More...
|
|
#define | MEMMOVE(p1, p2, type, n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n))) |
| Handy macro to call memmove. More...
|
|
#define | MEMCMP(p1, p2, type, n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n))) |
| Handy macro to call memcmp. More...
|
|
#define | ALLOC_N RB_ALLOC_N |
| Old name of RB_ALLOC_N. More...
|
|
#define | ALLOC RB_ALLOC |
| Old name of RB_ALLOC. More...
|
|
#define | ZALLOC_N RB_ZALLOC_N |
| Old name of RB_ZALLOC_N. More...
|
|
#define | ZALLOC RB_ZALLOC |
| Old name of RB_ZALLOC. More...
|
|
#define | REALLOC_N RB_REALLOC_N |
| Old name of RB_REALLOC_N. More...
|
|
#define | ALLOCV RB_ALLOCV |
| Old name of RB_ALLOCV. More...
|
|
#define | ALLOCV_N RB_ALLOCV_N |
| Old name of RB_ALLOCV_N. More...
|
|
#define | ALLOCV_END RB_ALLOCV_END |
| Old name of RB_ALLOCV_END. More...
|
|
Memory management stuff.
- Author
- Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org
- Copyright
- This file is a part of the programming language Ruby. Permission is hereby granted, to either redistribute and/or modify this file, provided that the conditions mentioned in the file COPYING are met. Consult the file for details.
- Warning
- Symbols prefixed with either
RBIMPL
or rbimpl
are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will.
- Note
- To ruby-core: remember that this header can be possibly recursively included from extension libraries written in C++. Do not expect for instance
__VA_ARGS__
is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98.
Definition in file memory.h.
Value: (*__extension__ ({ \
volatile
VALUE *rb_gc_guarded_ptr = &(v); \
__asm__("" : : "m"(rb_gc_guarded_ptr)); \
rb_gc_guarded_ptr; \
}))
Prevents premature destruction of local objects.
Ruby's garbage collector is conservative; it scans the C level machine stack as well. Possible in- use Ruby objects must remain visible on stack, to be properly marked as such. However contemporary C compilers do not interface well with this. Consider the following example:
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Here, without the RB_GC_GUARD, the last use of s
is before the last use of sptr
. Compilers could thus think s
and t
are allowed to overlap. That would eliminate s
from the stack, while sptr
is still in use. If our GC ran at that very moment, s
gets swept out, which also destroys sptr
. Boom! You got a SEGV.
In order to prevent this scenario RB_GC_GUARD must be placed after the last use of sptr
. Placing RB_GC_GUARD before dereferencing sptr
would be of no use.
RB_GC_GUARD would not be necessary at all in the above example if non- inlined function calls are made on the s
variable after sptr
is dereferenced. Thus, in the above example, calling any un-inlined function on s
such as rb_str_modify(s);
will ensure s
stays on the stack or register to prevent a GC invocation from prematurely freeing it.
Using the RB_GC_GUARD macro is preferable to using the volatile
keyword in C. RB_GC_GUARD has the following advantages:
- the intent of the macro use is clear.
- RB_GC_GUARD only affects its call site. OTOH
volatile
generates some extra code every time the variable is used, hurting optimisation.
volatile
implementations may be buggy/inconsistent in some compilers and architectures. RB_GC_GUARD is customisable for broken systems/compilers without negatively affecting other systems.
- C++ since C++20 deprecates
volatile
. If you write your extension library in that language there is no escape but to use this macro.
- Parameters
-
v | A variable of VALUE type. |
- Postcondition
v
is still alive.
Definition at line 162 of file memory.h.