Commit 62f41ddd authored by take_cheeze's avatar take_cheeze

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.
parent 0042e586
......@@ -59,6 +59,12 @@
/* fixed size GC arena */
//#define MRB_GC_FIXED_ARENA
/* state atexit stack size */
//#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
/* fixed size state atexit stack */
//#define MRB_FIXED_STATE_ATEXIT_STACK
/* -DDISABLE_XXXX to drop following features */
//#define DISABLE_STDIO /* use of stdio */
......
......@@ -52,6 +52,10 @@ typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
#define MRB_GC_ARENA_SIZE 100
#endif
#ifndef MRB_FIXED_STATE_ATEXIT_STACK_SIZE
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE 5
#endif
typedef struct {
mrb_sym mid;
struct RProc *proc;
......@@ -173,7 +177,11 @@ typedef struct mrb_state {
void *ud; /* auxiliary data */
#ifdef MRB_FIXED_STATE_ATEXIT_STACK
mrb_atexit_func atexit_stack[MRB_FIXED_STATE_ATEXIT_STACK_SIZE];
#else
mrb_atexit_func *atexit_stack;
#endif
mrb_int atexit_stack_len;
} mrb_state;
......
......@@ -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;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment