|
| 1 | +/* |
| 2 | +** allocf.c - default memory allocation function |
| 3 | +** |
| 4 | +** See Copyright Notice in mruby.h |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include "mruby.h" |
| 9 | + |
| 10 | +/* This function serves as the default memory allocation function and accepts four arguments: |
| 11 | + * |
| 12 | + * - `mrb`: An instance of `mrb_state`. It's important to note that for the initial allocation (used to allocate the `mrb_state` itself), `mrb` is set to NULL. |
| 13 | + * - `p`: The previous pointer to the memory region. For memory allocation, this parameter is NULL. |
| 14 | + * - `size`: The new size of the memory region to be returned. |
| 15 | + * - `ud`: User data, represented as a `void*`, which is passed to the `mrb_state`. |
| 16 | + */ |
| 17 | + |
| 18 | +void* |
| 19 | +mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud) |
| 20 | +{ |
| 21 | +if (size == 0) { |
| 22 | +/* `free(NULL)` should be no-op */ |
| 23 | +free(p); |
| 24 | +return NULL; |
| 25 | + } |
| 26 | +else { |
| 27 | +/* `ralloc(NULL, size)` works as `malloc(size)` */ |
| 28 | +return realloc(p, size); |
| 29 | + } |
| 30 | +} |