Skip to content
This repository
Newer
Older
100644 138 lines (117 sloc) 2.645 kb
83544361 »
2012-04-23 add file header
1 /*
e27cc402 »
2012-08-19 replace RiteVM in comments by mruby
2 ** state.c - mrb_state open/close functions
4ec6d41f »
2012-04-30 rm whitespace
3 **
83544361 »
2012-04-23 add file header
4 ** See Copyright Notice in mruby.h
5 */
6
e0d6430f »
2012-04-20 add mruby sources
7 #include "mruby.h"
8b367098 »
2012-04-24 move header files {irep,dump,cdump,ritehash}.h to /include/mruby
8 #include "mruby/irep.h"
a37c1daf »
2012-08-12 call mrb_gc_free_gc from mrb_close
9 #include "mruby/variable.h"
e0d6430f »
2012-04-20 add mruby sources
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*
b8fc484d »
2012-08-19 allocf should take ud; https://twitter.com/junjis0203/status/23694997…
17 mrb_open_allocf(mrb_allocf f, void *ud)
e0d6430f »
2012-04-20 add mruby sources
18 {
b8fc484d »
2012-08-19 allocf should take ud; https://twitter.com/junjis0203/status/23694997…
19 mrb_state *mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state), ud);
49aa42f3 »
2012-06-03 Verify that memory for mrb_state was allocated
20 if (mrb == NULL) return NULL;
e0d6430f »
2012-04-20 add mruby sources
21
22 memset(mrb, 0, sizeof(mrb_state));
147aa040 »
2012-08-19 mrb_open_allocf() should keep ud in mrb_state
23 mrb->ud = ud;
e0d6430f »
2012-04-20 add mruby sources
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*
b8fc484d »
2012-08-19 allocf should take ud; https://twitter.com/junjis0203/status/23694997…
34 allocf(mrb_state *mrb, void *p, size_t size, void *ud)
e0d6430f »
2012-04-20 add mruby sources
35 {
36 if (size == 0) {
37 free(p);
38 return NULL;
39 }
40 else {
41 return realloc(p, size);
42 }
43 }
44
4f7a1a16 »
2012-09-03 remove memleaks using linked allocator
45 struct alloca_header {
46 struct alloca_header *next;
47 char buf[0];
48 };
49
50 void*
51 mrb_alloca(mrb_state *mrb, size_t size)
52 {
53 struct alloca_header *p;
54
55 p = mrb_malloc(mrb, sizeof(struct alloca_header)+size);
56 p->next = mrb->mems;
57 mrb->mems = p;
58 return (void*)p->buf;
59 }
60
61 static void
62 mrb_alloca_free(mrb_state *mrb)
63 {
64 struct alloca_header *p = mrb->mems;
65 struct alloca_header *tmp;
66
67 while (p) {
68 tmp = p;
69 p = p->next;
70 mrb_free(mrb, tmp);
71 }
72 }
73
e0d6430f »
2012-04-20 add mruby sources
74 mrb_state*
75 mrb_open()
76 {
b8fc484d »
2012-08-19 allocf should take ud; https://twitter.com/junjis0203/status/23694997…
77 mrb_state *mrb = mrb_open_allocf(allocf, NULL);
e0d6430f »
2012-04-20 add mruby sources
78
79 return mrb;
80 }
81
ac789ae6 »
2012-08-28 free symbol table from mrb_close()
82 void mrb_free_symtbl(mrb_state *mrb);
d04a7ec6 »
2012-08-28 free heap pages from mrb_close()
83 void mrb_free_heap(mrb_state *mrb);
ac789ae6 »
2012-08-28 free symbol table from mrb_close()
84
e0d6430f »
2012-04-20 add mruby sources
85 void
86 mrb_close(mrb_state *mrb)
87 {
88 int i;
89
90 /* free */
a37c1daf »
2012-08-12 call mrb_gc_free_gc from mrb_close
91 mrb_gc_free_gv(mrb);
e0d6430f »
2012-04-20 add mruby sources
92 mrb_free(mrb, mrb->stbase);
93 mrb_free(mrb, mrb->cibase);
94 for (i=0; i<mrb->irep_len; i++) {
950204bc »
2012-08-29 remove flags from irep
95 mrb_free(mrb, mrb->irep[i]->iseq);
e0d6430f »
2012-04-20 add mruby sources
96 mrb_free(mrb, mrb->irep[i]->pool);
97 mrb_free(mrb, mrb->irep[i]->syms);
4f7a1a16 »
2012-09-03 remove memleaks using linked allocator
98 mrb_free(mrb, mrb->irep[i]->lines);
e0d6430f »
2012-04-20 add mruby sources
99 mrb_free(mrb, mrb->irep[i]);
100 }
101 mrb_free(mrb, mrb->irep);
ab5a5813 »
2012-09-05 Free mrb->rescue and mrb->ensure.
102 mrb_free(mrb, mrb->rescue);
103 mrb_free(mrb, mrb->ensure);
ac789ae6 »
2012-08-28 free symbol table from mrb_close()
104 mrb_free_symtbl(mrb);
d04a7ec6 »
2012-08-28 free heap pages from mrb_close()
105 mrb_free_heap(mrb);
4f7a1a16 »
2012-09-03 remove memleaks using linked allocator
106 mrb_alloca_free(mrb);
e0d6430f »
2012-04-20 add mruby sources
107 mrb_free(mrb, mrb);
108 }
109
110 void
111 mrb_add_irep(mrb_state *mrb, int idx)
112 {
113 if (!mrb->irep) {
114 int max = 256;
115
116 if (idx > max) max = idx+1;
5a4beeed »
2012-07-29 Make all(?) void casts explicit for C++
117 mrb->irep = (mrb_irep **)mrb_calloc(mrb, max, sizeof(mrb_irep*));
e0d6430f »
2012-04-20 add mruby sources
118 mrb->irep_capa = max;
119 }
1685d241 »
2012-05-17 idx should be less than the cap, if it is equal irep needs to grow.
120 else if (mrb->irep_capa <= idx) {
a851bcd6 »
2012-08-21 Refer to irep_capa as well as irep_len when irep is marked by GC.
121 size_t old_capa = mrb->irep_capa;
1685d241 »
2012-05-17 idx should be less than the cap, if it is equal irep needs to grow.
122 while (mrb->irep_capa <= idx) {
e0d6430f »
2012-04-20 add mruby sources
123 mrb->irep_capa *= 2;
124 }
5a4beeed »
2012-07-29 Make all(?) void casts explicit for C++
125 mrb->irep = (mrb_irep **)mrb_realloc(mrb, mrb->irep, sizeof(mrb_irep*)*mrb->irep_capa);
a851bcd6 »
2012-08-21 Refer to irep_capa as well as irep_len when irep is marked by GC.
126 memset(mrb->irep + old_capa, 0, sizeof(mrb_irep*) * (mrb->irep_capa - old_capa));
e0d6430f »
2012-04-20 add mruby sources
127 }
128 }
129
130 mrb_value
131 mrb_top_self(mrb_state *mrb)
132 {
fd3af6ae »
2012-09-03 introduce toplevel object main; close #327
133 mrb_value v;
134
135 MRB_SET_VALUE(v, MRB_TT_MAIN, value.i, 0);
136 return v;
e0d6430f »
2012-04-20 add mruby sources
137 }
Something went wrong with that request. Please try again.