diff --git a/src/allocf.c b/src/allocf.c new file mode 100644 index 000000000..aa9ca646f --- /dev/null +++ b/src/allocf.c @@ -0,0 +1,30 @@ +/* +** allocf.c - default memory allocation function +** +** See Copyright Notice in mruby.h +*/ + +#include +#include "mruby.h" + +/* This function serves as the default memory allocation function and accepts four arguments: + * + * - `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. + * - `p`: The previous pointer to the memory region. For memory allocation, this parameter is NULL. + * - `size`: The new size of the memory region to be returned. + * - `ud`: User data, represented as a `void*`, which is passed to the `mrb_state`. + */ + +void* +mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud) +{ + if (size == 0) { + /* `free(NULL)` should be no-op */ + free(p); + return NULL; + } + else { + /* `ralloc(NULL, size)` works as `malloc(size)` */ + return realloc(p, size); + } +} diff --git a/src/state.c b/src/state.c index 3f48756e0..ced96cb88 100644 --- a/src/state.c +++ b/src/state.c @@ -58,18 +58,6 @@ mrb_open_core(mrb_allocf f, void *ud) return mrb; } -void* -mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud) -{ - if (size == 0) { - free(p); - return NULL; - } - else { - return realloc(p, size); - } -} - MRB_API mrb_state* mrb_open(void) {