From 29879f9d582416f205555260fbe9ac8a3d9013ba Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Mon, 1 Apr 2013 08:35:53 +0900 Subject: Add out_of_memory field to mrb_state. --- include/mruby.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/mruby.h b/include/mruby.h index e204820a3..5272d0400 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -125,6 +125,7 @@ typedef struct mrb_state { mrb_bool gc_disabled:1; mrb_bool gc_full:1; mrb_bool is_generational_gc_mode:1; + mrb_bool out_of_memory:1; size_t majorgc_old_threshold; struct alloca_header *mems; -- cgit v1.2.3 From c7ff1bd88150c8f61f7483ea857f198ca1992fc7 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Mon, 1 Apr 2013 08:40:05 +0900 Subject: Add the care for malloc failed. At the first time, mrb_realloc raise RuntimeError. At the time fails twice in a row, it causes panic. --- src/gc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/gc.c b/src/gc.c index c90e00c89..07bc23b12 100644 --- a/src/gc.c +++ b/src/gc.c @@ -157,6 +157,20 @@ mrb_realloc(mrb_state *mrb, void *p, size_t len) mrb_garbage_collect(mrb); p2 = (mrb->allocf)(mrb, p, len, mrb->ud); } + + if (!p2 && len) { + if (mrb->out_of_memory) { + /* mrb_panic(mrb); */ + } + else { + mrb->out_of_memory = 1; + mrb_raise(mrb, E_RUNTIME_ERROR, "Out of memory"); + } + } + else { + mrb->out_of_memory = 0; + } + return p2; } -- cgit v1.2.3