summaryrefslogtreecommitdiffhomepage
path: root/src/state.c
diff options
context:
space:
mode:
authortake_cheeze <[email protected]>2014-06-16 16:03:35 +0900
committertake_cheeze <[email protected]>2014-06-16 16:03:35 +0900
commit62f41ddd3bf8a1afa3d7ed94081835f8996ecbc4 (patch)
tree75942827a4f3092792834d6ff0d5bad4db3ca2ec /src/state.c
parent0042e586db875fca2631c243cb41599e9782b05a (diff)
downloadmruby-62f41ddd3bf8a1afa3d7ed94081835f8996ecbc4.tar.gz
mruby-62f41ddd3bf8a1afa3d7ed94081835f8996ecbc4.zip
Add fixed state atexit stack feature.
Adds following macros: * MRB_FIXED_STATE_ATEXIT_STACK (not defined by default) * When defined enables fixed state atexit stack. * MRB_FIXED_STATE_ATEXIT_STACK_SIZE (default value: 5) * This macro will be ignored when `MRB_FIXED_STATE_ATEXIT_STACK` isn't defined. * When `mrb_state_atexit` is called more than this value it will raise runtime error.
Diffstat (limited to 'src/state.c')
-rw-r--r--src/state.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/state.c b/src/state.c
index c0a9c14c2..3e82a159d 100644
--- a/src/state.c
+++ b/src/state.c
@@ -226,7 +226,9 @@ mrb_close(mrb_state *mrb)
for (i = mrb->atexit_stack_len; i > 0; --i) {
mrb->atexit_stack[i - 1](mrb);
}
+#ifndef MRB_FIXED_STATE_ATEXIT_STACK
mrb_free(mrb, mrb->atexit_stack);
+#endif
}
/* free */
@@ -268,6 +270,11 @@ mrb_top_self(mrb_state *mrb)
void
mrb_state_atexit(mrb_state *mrb, mrb_atexit_func f)
{
+#ifdef MRB_FIXED_STATE_ATEXIT_STACK
+ if (mrb->atexit_stack_len + 1 > MRB_FIXED_STATE_ATEXIT_STACK_SIZE) {
+ mrb_raise(mrb, E_RUNTIME_ERROR, "exceeded fixed state atexit stack limit");
+ }
+#else
size_t stack_size;
stack_size = sizeof(mrb_atexit_func) * (mrb->atexit_stack_len + 1);
@@ -276,6 +283,7 @@ mrb_state_atexit(mrb_state *mrb, mrb_atexit_func f)
} else {
mrb->atexit_stack = (mrb_atexit_func*)mrb_realloc(mrb, mrb->atexit_stack, stack_size);
}
+#endif
mrb->atexit_stack[mrb->atexit_stack_len++] = f;
}