Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (91 sloc) 2.145 kb
8354436 @mimaki add file header
mimaki authored
1 /*
e27cc40 @matz replace RiteVM in comments by mruby
matz authored
2 ** state.c - mrb_state open/close functions
4ec6d41 rm whitespace
roco authored
3 **
8354436 @mimaki add file header
mimaki authored
4 ** See Copyright Notice in mruby.h
5 */
6
e0d6430 @mimaki add mruby sources
mimaki authored
7 #include "mruby.h"
8b36709 @matz move header files {irep,dump,cdump,ritehash}.h to /include/mruby
matz authored
8 #include "mruby/irep.h"
a37c1da @matz call mrb_gc_free_gc from mrb_close
matz authored
9 #include "mruby/variable.h"
e0d6430 @mimaki add mruby sources
mimaki authored
10 #include <string.h>
11
12 void mrb_init_heap(mrb_state*);
13 void mrb_init_core(mrb_state*);
14 void mrb_init_ext(mrb_state*);
15
16 mrb_state*
b8fc484 @matz allocf should take ud; https://twitter.com/junjis0203/status/23694997646...
matz authored
17 mrb_open_allocf(mrb_allocf f, void *ud)
e0d6430 @mimaki add mruby sources
mimaki authored
18 {
b8fc484 @matz allocf should take ud; https://twitter.com/junjis0203/status/23694997646...
matz authored
19 mrb_state *mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state), ud);
49aa42f @jonforums Verify that memory for mrb_state was allocated
jonforums authored
20 if (mrb == NULL) return NULL;
e0d6430 @mimaki add mruby sources
mimaki authored
21
22 memset(mrb, 0, sizeof(mrb_state));
147aa04 @matz mrb_open_allocf() should keep ud in mrb_state
matz authored
23 mrb->ud = ud;
e0d6430 @mimaki add mruby sources
mimaki authored
24 mrb->allocf = f;
25 mrb->current_white_part = MRB_GC_WHITE_A;
26
27 mrb_init_heap(mrb);
28 mrb_init_core(mrb);
29 mrb_init_ext(mrb);
30 return mrb;
31 }
32
33 static void*
b8fc484 @matz allocf should take ud; https://twitter.com/junjis0203/status/23694997646...
matz authored
34 allocf(mrb_state *mrb, void *p, size_t size, void *ud)
e0d6430 @mimaki add mruby sources
mimaki authored
35 {
36 if (size == 0) {
37 free(p);
38 return NULL;
39 }
40 else {
41 return realloc(p, size);
42 }
43 }
44
45 mrb_state*
46 mrb_open()
47 {
b8fc484 @matz allocf should take ud; https://twitter.com/junjis0203/status/23694997646...
matz authored
48 mrb_state *mrb = mrb_open_allocf(allocf, NULL);
e0d6430 @mimaki add mruby sources
mimaki authored
49
50 return mrb;
51 }
52
ac789ae @matz free symbol table from mrb_close()
matz authored
53 void mrb_free_symtbl(mrb_state *mrb);
d04a7ec @matz free heap pages from mrb_close()
matz authored
54 void mrb_free_heap(mrb_state *mrb);
ac789ae @matz free symbol table from mrb_close()
matz authored
55
e0d6430 @mimaki add mruby sources
mimaki authored
56 void
57 mrb_close(mrb_state *mrb)
58 {
59 int i;
60
61 /* free */
a37c1da @matz call mrb_gc_free_gc from mrb_close
matz authored
62 mrb_gc_free_gv(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
63 mrb_free(mrb, mrb->stbase);
64 mrb_free(mrb, mrb->cibase);
65 for (i=0; i<mrb->irep_len; i++) {
66 if (mrb->irep[i]->flags & MRB_IREP_NOFREE) continue;
67 if ((mrb->irep[i]->flags & MRB_ISEQ_NOFREE) == 0) {
68 mrb_free(mrb, mrb->irep[i]->iseq);
69 }
70 mrb_free(mrb, mrb->irep[i]->pool);
71 mrb_free(mrb, mrb->irep[i]->syms);
72 mrb_free(mrb, mrb->irep[i]);
73 }
74 mrb_free(mrb, mrb->irep);
ac789ae @matz free symbol table from mrb_close()
matz authored
75 mrb_free_symtbl(mrb);
d04a7ec @matz free heap pages from mrb_close()
matz authored
76 mrb_free_heap(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
77 mrb_free(mrb, mrb);
78 }
79
80 void
81 mrb_add_irep(mrb_state *mrb, int idx)
82 {
83 if (!mrb->irep) {
84 int max = 256;
85
86 if (idx > max) max = idx+1;
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
87 mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
e0d6430 @mimaki add mruby sources
mimaki authored
88 mrb->irep_capa = max;
89 }
1685d24 @carsonmcdonald idx should be less than the cap, if it is equal irep needs to grow.
carsonmcdonald authored
90 else if (mrb->irep_capa <= idx) {
a851bcd @masamitsu-murase Refer to irep_capa as well as irep_len when irep is marked by GC.
masamitsu-murase authored
91 size_t old_capa = mrb->irep_capa;
1685d24 @carsonmcdonald idx should be less than the cap, if it is equal irep needs to grow.
carsonmcdonald authored
92 while (mrb->irep_capa <= idx) {
e0d6430 @mimaki add mruby sources
mimaki authored
93 mrb->irep_capa *= 2;
94 }
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
95 mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
a851bcd @masamitsu-murase Refer to irep_capa as well as irep_len when irep is marked by GC.
masamitsu-murase authored
96 memset(mrb->irep + old_capa, 0, sizeof(mrb_irep*) * (mrb->irep_capa - old_capa));
e0d6430 @mimaki add mruby sources
mimaki authored
97 }
98 }
99
100 mrb_value
101 mrb_top_self(mrb_state *mrb)
102 {
103 // for now
104 return mrb_nil_value();
105 }
Something went wrong with that request. Please try again.