Skip to content
Newer
Older
100644 95 lines (81 sloc) 1.69 KB
8354436 add file header
mimaki authored Apr 23, 2012
1 /*
2 ** state.c - RiteVM open/close functions
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
e0d6430 add mruby sources
mimaki authored Apr 20, 2012
7 #include "mruby.h"
8b36709 @matz move header files {irep,dump,cdump,ritehash}.h to /include/mruby
matz authored Apr 24, 2012
8 #include "mruby/irep.h"
e0d6430 add mruby sources
mimaki authored Apr 20, 2012
9 #include <string.h>
10
11 void mrb_init_heap(mrb_state*);
12 void mrb_init_core(mrb_state*);
13 void mrb_init_ext(mrb_state*);
14
15 mrb_state*
16 mrb_open_allocf(mrb_allocf f)
17 {
18 mrb_state *mrb = (f)(NULL, NULL, sizeof(mrb_state));
19
20 memset(mrb, 0, sizeof(mrb_state));
21 mrb->allocf = f;
22 mrb->current_white_part = MRB_GC_WHITE_A;
23
24 mrb_init_heap(mrb);
25 mrb_init_core(mrb);
26 mrb_init_ext(mrb);
27 return mrb;
28 }
29
30 static void*
31 allocf(mrb_state *mrb, void *p, size_t size)
32 {
33 if (size == 0) {
34 free(p);
35 return NULL;
36 }
37 else {
38 return realloc(p, size);
39 }
40 }
41
42 mrb_state*
43 mrb_open()
44 {
45 mrb_state *mrb = mrb_open_allocf(allocf);
46
47 return mrb;
48 }
49
50 void
51 mrb_close(mrb_state *mrb)
52 {
53 int i;
54
55 /* free */
56 mrb_free(mrb, mrb->stbase);
57 mrb_free(mrb, mrb->cibase);
58 for (i=0; i<mrb->irep_len; i++) {
59 if (mrb->irep[i]->flags & MRB_IREP_NOFREE) continue;
60 if ((mrb->irep[i]->flags & MRB_ISEQ_NOFREE) == 0) {
61 mrb_free(mrb, mrb->irep[i]->iseq);
62 }
63 mrb_free(mrb, mrb->irep[i]->pool);
64 mrb_free(mrb, mrb->irep[i]->syms);
65 mrb_free(mrb, mrb->irep[i]);
66 }
67 mrb_free(mrb, mrb->irep);
68 mrb_free(mrb, mrb);
69 }
70
71 void
72 mrb_add_irep(mrb_state *mrb, int idx)
73 {
74 if (!mrb->irep) {
75 int max = 256;
76
77 if (idx > max) max = idx+1;
78 mrb->irep = mrb_malloc(mrb, sizeof(mrb_irep*)*max);
79 mrb->irep_capa = max;
80 }
81 else if (mrb->irep_capa < idx) {
82 while (mrb->irep_capa < idx) {
83 mrb->irep_capa *= 2;
84 }
85 mrb->irep = mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep)*mrb->irep_capa);
86 }
87 }
88
89 mrb_value
90 mrb_top_self(mrb_state *mrb)
91 {
92 // for now
93 return mrb_nil_value();
94 }
Something went wrong with that request. Please try again.