Added memory allocation hook to mruby core.
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
46ccf53bab
commit
b792c9b695
46
src/main.c
46
src/main.c
|
@ -15,6 +15,10 @@
|
|||
/* App */
|
||||
#include "app_syscalls.h"
|
||||
|
||||
#ifndef APP_LOG_SUCCESSFUL_ALLOC
|
||||
#define APP_LOG_SUCCESSFUL_ALLOC false
|
||||
#endif
|
||||
|
||||
static void app_mrb_runtime_task(void *parameters);
|
||||
|
||||
int main(void) {
|
||||
|
@ -26,7 +30,7 @@ int main(void) {
|
|||
goto dead_loop;
|
||||
}
|
||||
|
||||
if (xTaskCreate(app_mrb_runtime_task, "MRB_RT", 1536, NULL, 2, NULL) != pdPASS) {
|
||||
if (xTaskCreate(app_mrb_runtime_task, "MRB_RT", 1536, NULL, 2, NULL) != pdPASS) {
|
||||
goto dead_loop;
|
||||
}
|
||||
|
||||
|
@ -38,8 +42,46 @@ dead_loop:
|
|||
}
|
||||
}
|
||||
|
||||
static void app_mrb_alloc_fail_hook(void) {
|
||||
for (;;) {
|
||||
__WFI();
|
||||
}
|
||||
}
|
||||
|
||||
static void *app_mrb_allocf(mrb_state *mrb, void *ptr, size_t len, void *user) {
|
||||
void *res = realloc(ptr, len);
|
||||
|
||||
if (ptr == NULL) {
|
||||
if (res == NULL) {
|
||||
printf("[ALLOC] malloc() failed, size: %d\n", len);
|
||||
app_mrb_alloc_fail_hook();
|
||||
} else {
|
||||
#if APP_LOG_SUCCESSFUL_ALLOC
|
||||
printf("[ALLOC] malloced %d bytes to ptr %p\n", len, res);
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
if (len == 0) {
|
||||
#if APP_LOG_SUCCESSFUL_ALLOC
|
||||
printf("[ALLOC] freed ptr %p\n", ptr);
|
||||
#endif
|
||||
} else {
|
||||
if (res == NULL) {
|
||||
printf("[ALLOC] malloc-copy-free failed, orig ptr: %p, new size: %d\n", ptr, len);
|
||||
app_mrb_alloc_fail_hook();
|
||||
} else {
|
||||
#if APP_LOG_SUCCESSFUL_ALLOC
|
||||
printf("[ALLOC] malloc-copy-free done, orig ptr: %p, new size: %d, new ptr: %p\n", ptr, len, res);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void app_mrb_runtime_task(void *parameters) {
|
||||
mrb_state *mrb = mrb_open();
|
||||
mrb_state *mrb = mrb_open_allocf(app_mrb_allocf, NULL);
|
||||
mrbc_context *cxt = mrbc_context_new(mrb);
|
||||
|
||||
mrb_show_version(mrb);
|
||||
|
|
Loading…
Reference in New Issue