Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
|
Declares ruby_xmalloc(). More...
#include "ruby/internal/config.h"
#include "ruby/internal/attr/alloc_size.h"
#include "ruby/internal/attr/nodiscard.h"
#include "ruby/internal/attr/noexcept.h"
#include "ruby/internal/attr/restrict.h"
#include "ruby/internal/attr/returns_nonnull.h"
#include "ruby/internal/dllexport.h"
Go to the source code of this file.
Macros | |
#define | USE_GC_MALLOC_OBJ_INFO_DETAILS 0 |
#define | xmalloc ruby_xmalloc |
#define | xmalloc2 ruby_xmalloc2 |
#define | xcalloc ruby_xcalloc |
#define | xrealloc ruby_xrealloc |
#define | xrealloc2 ruby_xrealloc2 |
#define | xfree ruby_xfree |
Functions | |
void * | ruby_xmalloc (size_t size) |
Allocates a storage instance. More... | |
void * | ruby_xmalloc2 (size_t nelems, size_t elemsiz) |
Identical to ruby_xmalloc(), except it allocates nelems * elemsiz bytes. More... | |
void * | ruby_xcalloc (size_t nelems, size_t elemsiz) |
Identical to ruby_xmalloc2(), except it zero-fills the region before it returns. More... | |
void * | ruby_xrealloc (void *ptr, size_t newsiz) |
Resize the storage instance. More... | |
void * | ruby_xrealloc2 (void *ptr, size_t newelems, size_t newsiz) |
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems * newsiz bytes. More... | |
void | ruby_xfree (void *ptr) |
Deallocates a storage instance. More... | |
Declares ruby_xmalloc().
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. __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 xmalloc.h.
#define xcalloc ruby_xcalloc |
#define xfree ruby_xfree |
#define xmalloc ruby_xmalloc |
#define xmalloc2 ruby_xmalloc2 |
#define xrealloc ruby_xrealloc |
#define xrealloc2 ruby_xrealloc2 |
void * ruby_xcalloc | ( | size_t | nelems, |
size_t | elemsiz | ||
) |
Identical to ruby_xmalloc2(), except it zero-fills the region before it returns.
This could also be seen as a routine identical to ruby_xmalloc(), except it calls calloc() instead of malloc() internally.
[in] | nelems | Number of elements. |
[in] | elemsiz | Size of an element. |
rb_eNoMemError | No space left for allocation. |
rb_eArgError | `nelems` * `elemsiz` would overflow. |
nelems
* elemsiz
bytes width, with appropriate alignment detected by the underlying calloc() routine. nelems
or elemsiz
or both are zero. Definition at line 12815 of file gc.c.
References ruby_xcalloc_body().
Referenced by getifaddrs(), rb_iseq_defined_string(), and rb_xcalloc_mul_add_mul().
void ruby_xfree | ( | void * | ptr | ) |
Deallocates a storage instance.
[out] | ptr | Either NULL, or a valid pointer previously returned from one of ruby_xmalloc(), ruby_xmalloc2(), ruby_xcalloc(), ruby_xrealloc(), or ruby_xrealloc2(). |
Definition at line 10914 of file gc.c.
References ruby_sized_xfree.
Referenced by freeifaddrs(), getifaddrs(), Init_fiddle(), rb_fiddle_free(), rb_free_tmp_buffer(), rb_iseq_free(), rb_iseq_insns_info_encode_positions(), rb_ractor_finish_marking(), and rb_str_resize().
void * ruby_xmalloc | ( | size_t | size | ) |
Allocates a storage instance.
It is largely the same as system malloc(), except:
ENOMEM
it tries to GC to make some room.[in] | size | Requested amount of memory. |
rb_eNoMemError | No space left for `size` bytes allocation. |
size
bytes width, with appropriate alignment detected by the underlying malloc() routine. size
is equal to zero. Definition at line 12795 of file gc.c.
References ruby_xmalloc_body().
Referenced by getifaddrs(), rb_xmalloc_mul_add(), and rb_xmalloc_mul_add_mul().
void * ruby_xmalloc2 | ( | size_t | nelems, |
size_t | elemsiz | ||
) |
Identical to ruby_xmalloc(), except it allocates nelems
* elemsiz
bytes.
This is needed because the multiplication could integer overflow. On such situations Ruby does not try to allocate at all but raises Ruby level exceptions instead. If there is no integer overflow the behaviour is exactly the same as ruby_xmalloc(nelems*elemsiz)
.
[in] | nelems | Number of elements. |
[in] | elemsiz | Size of an element. |
rb_eNoMemError | No space left for allocation. |
rb_eArgError | `nelems` * `elemsiz` would overflow. |
nelems
* elemsiz
bytes width, with appropriate alignment detected by the underlying malloc() routine. nelems
or elemsiz
or both are zero. Definition at line 12805 of file gc.c.
References ruby_xmalloc2_body().
void * ruby_xrealloc | ( | void * | ptr, |
size_t | newsiz | ||
) |
Resize the storage instance.
[in] | ptr | A valid pointer to a storage instance that was previously returned from either ruby_xmalloc(), ruby_xmalloc2(), ruby_xcalloc(), ruby_xrealloc(), or ruby_xrealloc2(). |
[in] | newsiz | Requested new amount of memory. |
rb_eNoMemError | No space left for `newsiz` bytes allocation. |
ptr | In case the function returns the passed pointer as-is, the storage instance that the pointer holds is either grown or shrunken to have at least newsiz bytes. |
otherwise | A valid pointer to a newly allocated storage instance which has at least newsiz bytes width, and holds previous contents of ptr . In this case ptr is invalidated as if it was passed to ruby_xfree(). |
elemsiz
is not the same as calling ruby_xfree(), because this function never returns NULL. Something meaningful still returns then. Definition at line 12825 of file gc.c.
References ptr, and ruby_xrealloc_body().
Referenced by rb_xrealloc_mul_add().
void * ruby_xrealloc2 | ( | void * | ptr, |
size_t | newelems, | ||
size_t | newsiz | ||
) |
Identical to ruby_xrealloc(), except it resizes the given storage instance to newelems
* newsiz
bytes.
This is needed because the multiplication could integer overflow. On such situations Ruby does not try to touch the contents of argument pointer at all but raises Ruby level exceptions instead. If there is no integer overflow the behaviour is exactly the same as ruby_xrealloc(ptr,nelems*elemsiz)
.
This is roughly the same as reallocarray() function that OpenBSD etc. provides, but also interacts with our GC.
[in] | ptr | A valid pointer to a storage instance that was previously returned from either ruby_xmalloc(), ruby_xmalloc2(), ruby_xcalloc(), ruby_xrealloc(), or ruby_xrealloc2(). |
[in] | newelems | Requested new number of elements. |
[in] | newsiz | Requested new size of each element. |
rb_eNoMemError | No space left for allocation. |
rb_eArgError | `newelems` * `newsiz` would overflow. |
ptr | In case the function returns the passed pointer as-is, the storage instance that the pointer holds is either grown or shrunken to have at least newelems * newsiz bytes. |
otherwise | A valid pointer to a newly allocated storage instance which has at least newelems * newsiz bytes width, and holds previous contents of ptr . In this case ptr is invalidated as if it was passed to ruby_xfree(). |
newelems
or elemsiz
are not the same as calling ruby_xfree(), because this function never returns NULL. Something meaningful still returns then. Definition at line 12835 of file gc.c.
References ptr, and ruby_xrealloc2_body().