allocf.c: rename mrb_default_alloc to mrb_basic_alloc_func

Along with removing mrb_state first argument from the function. From
mruby 3.2, this function is *not* the default function, but the entry
point that can be redefined for the application. The function in
`src/allocf.c` is the default *implementation* (using malloc / realloc /
free) of the function.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-05-09 19:17:13 +09:00
parent 0bdd529430
commit c3cc559dfc
6 changed files with 14 additions and 16 deletions
+1 -2
View File
@@ -8,14 +8,13 @@
/* 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)
mrb_basic_alloc_func(void *p, size_t size, void *ud)
{
if (size == 0) {
/* `free(NULL)` should be no-op */
+3 -3
View File
@@ -194,10 +194,10 @@ mrb_realloc_simple(mrb_state *mrb, void *p, size_t len)
mrb_full_gc(mrb);
}
#endif
p2 = mrb_default_allocf(mrb, p, len, NULL);
p2 = mrb_basic_alloc_func(p, len, NULL);
if (!p2 && len > 0 && mrb->gc.heaps && mrb->gc.state != MRB_GC_STATE_SWEEP) {
mrb_full_gc(mrb);
p2 = mrb_default_allocf(mrb, p, len, NULL);
p2 = mrb_basic_alloc_func(p, len, NULL);
}
return p2;
@@ -256,7 +256,7 @@ mrb_calloc(mrb_state *mrb, size_t nelem, size_t len)
MRB_API void
mrb_free(mrb_state *mrb, void *p)
{
mrb_default_allocf(mrb, p, 0, NULL);
mrb_basic_alloc_func(p, 0, NULL);
}
MRB_API void*
+3 -3
View File
@@ -41,8 +41,8 @@ mrb_open_core(mrb_allocf f, void *ud)
static const mrb_state mrb_state_zero = { 0 };
mrb_state *mrb;
if (f == NULL) f = mrb_default_allocf;
mrb = (mrb_state*)(f)(NULL, NULL, sizeof(mrb_state), ud);
if (f == NULL) f = mrb_basic_alloc_func;
mrb = (mrb_state*)(f)(NULL, sizeof(mrb_state), ud);
if (mrb == NULL) return NULL;
*mrb = mrb_state_zero;
@@ -59,7 +59,7 @@ mrb_open_core(mrb_allocf f, void *ud)
MRB_API mrb_state*
mrb_open(void)
{
mrb_state *mrb = mrb_open_allocf(mrb_default_allocf, NULL);
mrb_state *mrb = mrb_open_allocf(mrb_basic_alloc_func, NULL);
return mrb;
}