Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 98 lines (84 sloc) 1.851 kb
8354436 @mimaki add file header
mimaki authored
1 /*
2 ** state.c - RiteVM 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*
17 mrb_open_allocf(mrb_allocf f)
18 {
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
19 mrb_state *mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state));
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));
23 mrb->allocf = f;
24 mrb->current_white_part = MRB_GC_WHITE_A;
25
26 mrb_init_heap(mrb);
27 mrb_init_core(mrb);
28 mrb_init_ext(mrb);
29 return mrb;
30 }
31
32 static void*
33 allocf(mrb_state *mrb, void *p, size_t size)
34 {
35 if (size == 0) {
36 free(p);
37 return NULL;
38 }
39 else {
40 return realloc(p, size);
41 }
42 }
43
44 mrb_state*
45 mrb_open()
46 {
47 mrb_state *mrb = mrb_open_allocf(allocf);
48
49 return mrb;
50 }
51
52 void
53 mrb_close(mrb_state *mrb)
54 {
55 int i;
56
57 /* free */
a37c1da @matz call mrb_gc_free_gc from mrb_close
matz authored
58 mrb_gc_free_gv(mrb);
e0d6430 @mimaki add mruby sources
mimaki authored
59 mrb_free(mrb, mrb->stbase);
60 mrb_free(mrb, mrb->cibase);
61 for (i=0; i<mrb->irep_len; i++) {
62 if (mrb->irep[i]->flags & MRB_IREP_NOFREE) continue;
63 if ((mrb->irep[i]->flags & MRB_ISEQ_NOFREE) == 0) {
64 mrb_free(mrb, mrb->irep[i]->iseq);
65 }
66 mrb_free(mrb, mrb->irep[i]->pool);
67 mrb_free(mrb, mrb->irep[i]->syms);
68 mrb_free(mrb, mrb->irep[i]);
69 }
70 mrb_free(mrb, mrb->irep);
71 mrb_free(mrb, mrb);
72 }
73
74 void
75 mrb_add_irep(mrb_state *mrb, int idx)
76 {
77 if (!mrb->irep) {
78 int max = 256;
79
80 if (idx > max) max = idx+1;
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
81 mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
e0d6430 @mimaki add mruby sources
mimaki authored
82 mrb->irep_capa = max;
83 }
1685d24 @carsonmcdonald idx should be less than the cap, if it is equal irep needs to grow.
carsonmcdonald authored
84 else if (mrb->irep_capa <= idx) {
85 while (mrb->irep_capa <= idx) {
e0d6430 @mimaki add mruby sources
mimaki authored
86 mrb->irep_capa *= 2;
87 }
5a4beee @silverhammermba Make all(?) void casts explicit for C++
silverhammermba authored
88 mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
e0d6430 @mimaki add mruby sources
mimaki authored
89 }
90 }
91
92 mrb_value
93 mrb_top_self(mrb_state *mrb)
94 {
95 // for now
96 return mrb_nil_value();
97 }
Something went wrong with that request. Please try again.