w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

state.c
Go to the documentation of this file.
1 /*
2 ** state.c - mrb_state open/close functions
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <mruby.h>
10 #include <mruby/irep.h>
11 #include <mruby/variable.h>
12 #include <mruby/debug.h>
13 #include <mruby/string.h>
14 #include <mruby/class.h>
15 
18 
21 
22 int mrb_core_init_protect(mrb_state *mrb, void (*body)(mrb_state *, void *), void *opaque);
23 
24 static void
25 init_gc_and_core(mrb_state *mrb, void *opaque)
26 {
27  static const struct mrb_context mrb_context_zero = { 0 };
28 
29  mrb_gc_init(mrb, &mrb->gc);
30  mrb->c = (struct mrb_context*)mrb_malloc(mrb, sizeof(struct mrb_context));
31  *mrb->c = mrb_context_zero;
32  mrb->root_c = mrb->c;
33 
34  mrb_init_core(mrb);
35 }
36 
39 {
40  static const mrb_state mrb_state_zero = { 0 };
41  mrb_state *mrb;
42 
43  if (f == NULL) f = mrb_default_allocf;
44  mrb = (mrb_state *)(f)(NULL, NULL, sizeof(mrb_state), ud);
45  if (mrb == NULL) return NULL;
46 
47  *mrb = mrb_state_zero;
48  mrb->allocf_ud = ud;
49  mrb->allocf = f;
50  mrb->atexit_stack_len = 0;
51 
53  mrb_close(mrb);
54  return NULL;
55  }
56 
57  return mrb;
58 }
59 
60 void*
61 mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud)
62 {
63  if (size == 0) {
64  free(p);
65  return NULL;
66  }
67  else {
68  return realloc(p, size);
69  }
70 }
71 
73 mrb_open(void)
74 {
76 
77  return mrb;
78 }
79 
80 #ifndef MRB_NO_GEMS
81 static void
82 init_mrbgems(mrb_state *mrb, void *opaque)
83 {
84  mrb_init_mrbgems(mrb);
85 }
86 #endif
87 
90 {
91  mrb_state *mrb = mrb_open_core(f, ud);
92 
93  if (mrb == NULL) {
94  return NULL;
95  }
96 
97 #ifndef MRB_NO_GEMS
99  mrb_close(mrb);
100  return NULL;
101  }
102  mrb_gc_arena_restore(mrb, 0);
103 #endif
104  return mrb;
105 }
106 
107 void mrb_free_symtbl(mrb_state *mrb);
108 
109 void
111 {
112  if (irep->flags & MRB_IREP_NO_FREE) return;
113  irep->refcnt++;
114 }
115 
116 void
118 {
119  if (irep->flags & MRB_IREP_NO_FREE) return;
120  irep->refcnt--;
121  if (irep->refcnt == 0) {
122  mrb_irep_free(mrb, irep);
123  }
124 }
125 
126 void
128 {
129  mrb_irep **reps;
130  int i;
131 
132  if (irep->flags & MRB_IREP_NO_FREE) return;
133  reps = (mrb_irep**)irep->reps;
134  for (i=0; i<irep->rlen; i++) {
135  mrb_irep *tmp = reps[i];
136  reps[i] = NULL;
137  if (tmp) mrb_irep_decref(mrb, tmp);
138  }
139 }
140 
141 void
143 {
144  int i;
145 
146  if (irep->flags & MRB_IREP_NO_FREE) return;
147  if (!(irep->flags & MRB_ISEQ_NO_FREE))
148  mrb_free(mrb, (void*)irep->iseq);
149  if (irep->pool) {
150  for (i=0; i<irep->plen; i++) {
151  if ((irep->pool[i].tt & 3) == IREP_TT_STR) {
152  mrb_free(mrb, (void*)irep->pool[i].u.str);
153  }
154  }
155  mrb_free(mrb, (void*)irep->pool);
156  }
157  mrb_free(mrb, (void*)irep->syms);
158  if (irep->reps) {
159  for (i=0; i<irep->rlen; i++) {
160  if (irep->reps[i])
161  mrb_irep_decref(mrb, (mrb_irep*)irep->reps[i]);
162  }
163  mrb_free(mrb, (void*)irep->reps);
164  }
165  mrb_free(mrb, (void*)irep->lv);
166  mrb_debug_info_free(mrb, irep->debug_info);
167  mrb_free(mrb, irep);
168 }
169 
170 MRB_API void
172 {
173  if (!c) return;
174  mrb_free(mrb, c->stbase);
175  mrb_free(mrb, c->cibase);
176  mrb_free(mrb, c);
177 }
178 
179 int mrb_protect_atexit(mrb_state *mrb);
180 
181  MRB_API void
183 {
184  if (!mrb) return;
185  mrb_protect_atexit(mrb);
186 
187  /* free */
188  mrb_gc_destroy(mrb, &mrb->gc);
189  mrb_free_context(mrb, mrb->root_c);
190  mrb_gc_free_gv(mrb);
191  mrb_free_symtbl(mrb);
192  mrb_free(mrb, mrb);
193 }
194 
197 {
198  static const mrb_irep mrb_irep_zero = { 0 };
199  mrb_irep *irep;
200 
201  irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
202  *irep = mrb_irep_zero;
203  irep->refcnt = 1;
204 
205  return irep;
206 }
207 
210 {
211  return mrb_obj_value(mrb->top_self);
212 }
213 
214 MRB_API void
216 {
217 #ifdef MRB_FIXED_STATE_ATEXIT_STACK
219  mrb_raise(mrb, E_RUNTIME_ERROR, "exceeded fixed state atexit stack limit");
220  }
221 #else
222  size_t stack_size;
223 
224  stack_size = sizeof(mrb_atexit_func) * (mrb->atexit_stack_len + 1);
225  if (mrb->atexit_stack_len == 0) {
227  }
228  else {
230  }
231 #endif
232 
233  mrb->atexit_stack[mrb->atexit_stack_len++] = f;
234 }
#define free(a)
Definition: decNumber.cpp:310
mpz_t * f
Definition: gen-fib.c:34
#define c(n)
Definition: gpos-common.c:150
#define MRB_ISEQ_NO_FREE
Definition: irep.h:82
#define MRB_IREP_NO_FREE
Definition: irep.h:83
@ IREP_TT_STR
Definition: irep.h:19
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p
Definition: afcover.h:72
small capitals from c petite p scientific i
Definition: afcover.h:80
for(n=0;n< outline->n_points;n++)
Definition: ftbbox.c:494
static const void * body(MD5_CTX *ctx, const void *data, unsigned long size)
Definition: md5.c:100
#define stack_size
Definition: ctangleboot.c:103
#define realloc
Definition: glob.c:206
void * mrb_realloc(mrb_state *, void *, size_t)
Definition: gc.c:238
static void mrb_gc_arena_restore(mrb_state *mrb, int idx)
Definition: mruby.h:1239
#define MRB_FIXED_STATE_ATEXIT_STACK_SIZE
Definition: mruby.h:148
void mrb_raise(mrb_state *mrb, struct RClass *c, const char *msg)
Definition: error.c:214
struct mrb_state mrb_state
void mrb_free(mrb_state *, void *)
Definition: gc.c:288
#define E_RUNTIME_ERROR
Definition: mruby.h:1307
void(* mrb_atexit_func)(struct mrb_state *)
Definition: mruby.h:227
void *(* mrb_allocf)(struct mrb_state *mrb, void *, size_t, void *ud)
Definition: mruby.h:145
void * mrb_malloc(mrb_state *, size_t)
Definition: gc.c:256
static int size
Definition: ppmlabel.c:24
#define MRB_API
Definition: common.h:73
void mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d)
Definition: debug.c:201
void mrb_gc_free_gv(mrb_state *)
Definition: variable.c:255
void mrb_irep_cutref(mrb_state *mrb, mrb_irep *irep)
Definition: state.c:127
mrb_state * mrb_open(void)
Definition: state.c:73
void * mrb_default_allocf(mrb_state *mrb, void *p, size_t size, void *ud)
Definition: state.c:61
void mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
Definition: state.c:142
static void init_gc_and_core(mrb_state *mrb, void *opaque)
Definition: state.c:25
void mrb_gc_destroy(mrb_state *, mrb_gc *gc)
Definition: gc.c:442
mrb_state * mrb_open_allocf(mrb_allocf f, void *ud)
Definition: state.c:89
void mrb_init_mrbgems(mrb_state *)
Definition: mrbc.c:349
void mrb_gc_init(mrb_state *, mrb_gc *gc)
Definition: gc.c:398
void mrb_close(mrb_state *mrb)
Definition: state.c:182
void mrb_free_symtbl(mrb_state *mrb)
Definition: symbol.c:346
void mrb_state_atexit(mrb_state *mrb, mrb_atexit_func f)
Definition: state.c:215
mrb_value mrb_top_self(mrb_state *mrb)
Definition: state.c:209
int mrb_core_init_protect(mrb_state *mrb, void(*body)(mrb_state *, void *), void *opaque)
Definition: error.c:562
static void init_mrbgems(mrb_state *mrb, void *opaque)
Definition: state.c:82
int mrb_protect_atexit(mrb_state *mrb)
Definition: error.c:595
mrb_irep * mrb_add_irep(mrb_state *mrb)
Definition: state.c:196
void mrb_free_context(mrb_state *mrb, struct mrb_context *c)
Definition: state.c:171
void mrb_irep_incref(mrb_state *mrb, mrb_irep *irep)
Definition: state.c:110
void mrb_irep_decref(mrb_state *mrb, mrb_irep *irep)
Definition: state.c:117
mrb_state * mrb_open_core(mrb_allocf f, void *ud)
Definition: state.c:38
void mrb_init_core(mrb_state *)
Definition: init.c:30
Definition: gc.h:58
Definition: irep.h:56
uint16_t plen
Definition: irep.h:77
uint8_t flags
Definition: irep.h:60
const mrb_code * iseq
Definition: irep.h:62
struct mrb_irep_debug_info * debug_info
Definition: irep.h:74
uint16_t refcnt
Definition: irep.h:79
const mrb_sym * lv
Definition: irep.h:72
const mrb_sym * syms
Definition: irep.h:69
uint16_t rlen
Definition: irep.h:78
const mrb_pool_value * pool
Definition: irep.h:68
const struct mrb_irep *const * reps
Definition: irep.h:70
union mrb_pool_value::@1660 u
const char * str
Definition: irep.h:32
uint32_t tt
Definition: irep.h:30
mrb_atexit_func * atexit_stack
Definition: mruby.h:297
mrb_gc gc
Definition: mruby.h:261
struct mrb_context * c
Definition: mruby.h:235
void * allocf_ud
Definition: mruby.h:233
uint16_t atexit_stack_len
Definition: mruby.h:299
struct RObject * top_self
Definition: mruby.h:241
mrb_allocf allocf
Definition: mruby.h:232
struct mrb_context * root_c
Definition: mruby.h:236
static mrb_value mrb_obj_value(void *p)
Definition: value.h:317