Skip to content

Commit 34c5d96

Browse files
committed
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.
1 parent 899a091 commit 34c5d96

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/allocf.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

src/state.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ mrb_open_core(mrb_allocf f, void *ud)
5858
return mrb;
5959
}
6060

61-
void*
62-
mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud)
63-
{
64-
if (size == 0) {
65-
free(p);
66-
return NULL;
67-
}
68-
else {
69-
return realloc(p, size);
70-
}
71-
}
72-
7361
MRB_API mrb_state*
7462
mrb_open(void)
7563
{

0 commit comments

Comments
 (0)