Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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