From c3cc559dfcc74e45ea284f26f107bb623f49d136 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 9 May 2025 19:17:13 +0900 Subject: [PATCH] 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. --- include/mruby.h | 11 +++++------ .../mruby-bin-strip/tools/mruby-strip/mruby-strip.c | 2 +- mrbgems/mruby-test/mrbgem.rake | 2 +- src/allocf.c | 3 +-- src/gc.c | 6 +++--- src/state.c | 6 +++--- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/mruby.h b/include/mruby.h index da4ba3cc5..8f9acbf6a 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -164,9 +164,9 @@ struct mrb_state; * - If ptr is NULL it must allocate new space. * - If size is zero, ptr must be freed. * - * See @see mrb_default_allocf for the default implementation. + * See mrb_basic_alloc_func in src/allocf.c for the default implementation. */ -typedef void* (*mrb_allocf) (struct mrb_state *mrb, void *ptr, size_t size, void *ud); +typedef void* (*mrb_allocf) (void *ptr, size_t size, void *ud); #ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE #define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5 @@ -1255,7 +1255,7 @@ MRB_API mrb_state* mrb_open_allocf(mrb_allocf f, void *ud); * * @param f * Reference to the allocation function. - * Use mrb_default_allocf for the default + * Use mrb_basic_alloc_func for the default * @param ud * User data will be passed to custom allocator f. * If user data isn't required just pass NULL. @@ -1273,11 +1273,10 @@ MRB_API mrb_state* mrb_open_core(mrb_allocf f, void *ud); MRB_API void mrb_close(mrb_state *mrb); /** - * The default allocation function. + * The memory allocation function. You can redefine this function for your own allocator. * - * @see mrb_allocf */ -MRB_API void* mrb_default_allocf(mrb_state*, void*, size_t, void*); +MRB_API void* mrb_basic_alloc_func(void*, size_t, void*); MRB_API mrb_value mrb_top_self(mrb_state *mrb); diff --git a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c index 5c7d67180..47009ee0c 100644 --- a/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c +++ b/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c @@ -126,7 +126,7 @@ main(int argc, char **argv) print_usage(argv[0]); return EXIT_FAILURE; } - mrb = mrb_open_core(mrb_default_allocf, NULL); + mrb = mrb_open_core(mrb_basic_alloc_func, NULL); if (mrb == NULL) { fputs("Invalid mrb_state, exiting mruby-strip\n", stderr); return EXIT_FAILURE; diff --git a/mrbgems/mruby-test/mrbgem.rake b/mrbgems/mruby-test/mrbgem.rake index db20755b2..ac78fc811 100644 --- a/mrbgems/mruby-test/mrbgem.rake +++ b/mrbgems/mruby-test/mrbgem.rake @@ -69,7 +69,7 @@ MRuby::Gem::Specification.new('mruby-test') do |spec| unless g.test_args.empty? f.puts %Q[ mrb_value test_args_hash;] end - f.puts %Q[ mrb_state *mrb2 = mrb_open_core(mrb_default_allocf, NULL);] + f.puts %Q[ mrb_state *mrb2 = mrb_open_core(mrb_basic_alloc_func, NULL);] f.puts %Q[ if (mrb2 == NULL) {] f.puts %Q[ fprintf(stderr, "Invalid mrb_state, exiting \%s", __func__);] f.puts %Q[ exit(EXIT_FAILURE);] diff --git a/src/allocf.c b/src/allocf.c index b5c7d4b03..ae7df7997 100644 --- a/src/allocf.c +++ b/src/allocf.c @@ -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 */ diff --git a/src/gc.c b/src/gc.c index e44bfcb9e..d8f0b5df5 100644 --- a/src/gc.c +++ b/src/gc.c @@ -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* diff --git a/src/state.c b/src/state.c index f28ef046d..0a98a6019 100644 --- a/src/state.c +++ b/src/state.c @@ -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; }