Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 99 lines (85 sloc) 1.9 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/23694997…
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/23694997…
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/23694997…
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/23694997…
matz authored
48 mrb_state *mrb = mrb_open_allocf(allocf, NULL);
e0d6430 @mimaki add mruby sources
mimaki authored
49
50 return mrb;
51 }
52
53 void
54 mrb_close(mrb_state *mrb)
55 {
56 int i;
57
58 /* free */
a37c1da @matz call mrb_gc_free_gc from mrb_close
matz authored
59 mrb_gc_free_gv(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
60 mrb_free(mrb, mrb->stbase);
61 mrb_free(mrb, mrb->cibase);
62 for (i=0; i<mrb->irep_len; i++) {
63 if (mrb->irep[i]->flags & MRB_IREP_NOFREE) continue;
64 if ((mrb->irep[i]->flags & MRB_ISEQ_NOFREE) == 0) {
65 mrb_free(mrb, mrb->irep[i]->iseq);
66 }
67 mrb_free(mrb, mrb->irep[i]->pool);
68 mrb_free(mrb, mrb->irep[i]->syms);
69 mrb_free(mrb, mrb->irep[i]);
70 }
71 mrb_free(mrb, mrb->irep);
72 mrb_free(mrb, mrb);
73 }
74
75 void
76 mrb_add_irep(mrb_state *mrb, int idx)
77 {
78 if (!mrb->irep) {
79 int max = 256;
80
81 if (idx > max) max = idx+1;
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
82 mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
e0d6430 @mimaki add mruby sources
mimaki authored
83 mrb->irep_capa = max;
84 }
1685d24 @carsonmcdonald idx should be less than the cap, if it is equal irep needs to grow.
carsonmcdonald authored
85 else if (mrb->irep_capa <= idx) {
86 while (mrb->irep_capa <= idx) {
e0d6430 @mimaki add mruby sources
mimaki authored
87 mrb->irep_capa *= 2;
88 }
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
89 mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
e0d6430 @mimaki add mruby sources
mimaki authored
90 }
91 }
92
93 mrb_value
94 mrb_top_self(mrb_state *mrb)
95 {
96 // for now
97 return mrb_nil_value();
98 }
Something went wrong with that request. Please try again.