allof.c: mrb_default_allocf() definition in the separate file

So that the user can define their own version of mrb_default_alloc
function to override memory allocation of mruby.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-12-21 15:28:04 +09:00
parent 899a09152b
commit 34c5d96e86
2 changed files with 30 additions and 12 deletions
+30
View File
@@ -0,0 +1,30 @@
/*
** allocf.c - default memory allocation function
**
** See Copyright Notice in mruby.h
*/
#include <stdlib.h>
#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);
}
}
-12
View File
@@ -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)
{