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)  

mruby_objectspace.c
Go to the documentation of this file.
1 #include <mruby.h>
2 #include <mruby/gc.h>
3 #include <mruby/hash.h>
4 #include <mruby/class.h>
5 #include <mruby/object.h>
6 #include <mruby/numeric.h>
7 #include <mruby/string.h>
8 #include <mruby/array.h>
9 #include <mruby/variable.h>
10 #include <mruby/proc.h>
11 #include <mruby/value.h>
12 #include <mruby/range.h>
13 #include <mruby/presym.h>
14 
19 };
20 
21 static int
22 os_count_object_type(mrb_state *mrb, struct RBasic *obj, void *data)
23 {
24  struct os_count_struct *obj_count;
25  obj_count = (struct os_count_struct*)data;
26 
27  obj_count->total++;
28 
29  if (mrb_object_dead_p(mrb, obj)) {
30  obj_count->freed++;
31  }
32  else {
33  obj_count->counts[obj->tt]++;
34  }
35  return MRB_EACH_OBJ_OK;
36 }
37 
38 /*
39  * call-seq:
40  * ObjectSpace.count_objects([result_hash]) -> hash
41  *
42  * Counts objects for each type.
43  *
44  * It returns a hash, such as:
45  * {
46  * :TOTAL=>10000,
47  * :FREE=>3011,
48  * :T_OBJECT=>6,
49  * :T_CLASS=>404,
50  * # ...
51  * }
52  *
53  * If the optional argument +result_hash+ is given,
54  * it is overwritten and returned. This is intended to avoid probe effect.
55  *
56  */
57 
58 static mrb_value
60 {
61  struct os_count_struct obj_count = { 0 };
62  mrb_int i;
64 
65  if (mrb_get_args(mrb, "|H", &hash) == 0) {
66  hash = mrb_hash_new(mrb);
67  }
68 
69  if (!mrb_hash_empty_p(mrb, hash)) {
70  mrb_hash_clear(mrb, hash);
71  }
72 
74 
77 
78  for (i = MRB_TT_FALSE; i < MRB_TT_MAXDEFINE; i++) {
80  switch (i) {
81 #define COUNT_TYPE(t) case (MRB_T ## t): type = mrb_symbol_value(mrb_intern_lit(mrb, #t)); break;
82  COUNT_TYPE(T_FALSE);
84  COUNT_TYPE(T_TRUE);
85  COUNT_TYPE(T_INTEGER);
86  COUNT_TYPE(T_SYMBOL);
87  COUNT_TYPE(T_UNDEF);
89  COUNT_TYPE(T_CPTR);
90  COUNT_TYPE(T_OBJECT);
91  COUNT_TYPE(T_CLASS);
92  COUNT_TYPE(T_MODULE);
93  COUNT_TYPE(T_ICLASS);
94  COUNT_TYPE(T_SCLASS);
95  COUNT_TYPE(T_PROC);
96  COUNT_TYPE(T_ARRAY);
97  COUNT_TYPE(T_HASH);
98  COUNT_TYPE(T_STRING);
99  COUNT_TYPE(T_RANGE);
100  COUNT_TYPE(T_EXCEPTION);
101  COUNT_TYPE(T_ENV);
102  COUNT_TYPE(T_DATA);
103  COUNT_TYPE(T_FIBER);
104  COUNT_TYPE(T_ISTRUCT);
105 #undef COUNT_TYPE
106  default:
107  type = mrb_fixnum_value(i); break;
108  }
109  if (obj_count.counts[i])
110  mrb_hash_set(mrb, hash, type, mrb_fixnum_value(obj_count.counts[i]));
111  }
112 
113  return hash;
114 }
115 
120 };
121 
122 static int
123 os_each_object_cb(mrb_state *mrb, struct RBasic *obj, void *ud)
124 {
125  struct os_each_object_data *d = (struct os_each_object_data*)ud;
126 
127  /* filter dead objects */
128  if (mrb_object_dead_p(mrb, obj)) {
129  return MRB_EACH_OBJ_OK;
130  }
131 
132  /* filter internal objects */
133  switch (obj->tt) {
134  case MRB_TT_FREE: case MRB_TT_ENV:
135  case MRB_TT_BREAK: case MRB_TT_ICLASS:
136  return MRB_EACH_OBJ_OK;
137  default:
138  break;
139  }
140 
141  /* filter half baked (or internal) objects */
142  if (!obj->c) return MRB_EACH_OBJ_OK;
143 
144  /* filter class kind if target module defined */
145  if (d->target_module && !mrb_obj_is_kind_of(mrb, mrb_obj_value(obj), d->target_module)) {
146  return MRB_EACH_OBJ_OK;
147  }
148 
149  mrb_yield(mrb, d->block, mrb_obj_value(obj));
150  ++d->count;
151  return MRB_EACH_OBJ_OK;
152 }
153 
154 /*
155  * call-seq:
156  * ObjectSpace.each_object([module]) {|obj| ... } -> fixnum
157  *
158  * Calls the block once for each object in this Ruby process.
159  * Returns the number of objects found.
160  * If the optional argument +module+ is given,
161  * calls the block for only those classes or modules
162  * that match (or are a subclass of) +module+.
163  *
164  * If no block is given, ArgumentError is raised.
165  *
166  */
167 
168 static mrb_value
170 {
171  struct os_each_object_data d = {0};
172  mrb_get_args(mrb, "&!|c", &d.block, &d.target_module);
174  return mrb_fixnum_value(d.count);
175 }
176 
177 void
179 {
180  struct RClass *os = mrb_define_module(mrb, "ObjectSpace");
181  mrb_define_class_method(mrb, os, "count_objects", os_count_objects, MRB_ARGS_OPT(1));
182  mrb_define_class_method(mrb, os, "each_object", os_each_object, MRB_ARGS_OPT(1));
183 }
184 
185 void
187 {
188 }
#define type(a)
Definition: aptex-macros.h:171
#define hash
Definition: aptex.h:388
#define MRB_SYM(name)
Definition: disable.h:20
struct rect data
Definition: dvipdfm.c:64
mrb_bool mrb_object_dead_p(struct mrb_state *mrb, struct RBasic *object)
Definition: gc.c:320
void mrb_objspace_each_objects(struct mrb_state *mrb, mrb_each_object_callback *callback, void *data)
Definition: gc.c:1588
#define MRB_EACH_OBJ_OK
Definition: gc.h:20
#define d(n)
Definition: gpos-common.c:151
const unsigned char FREE
Definition: image.cpp:34
small capitals from c petite p scientific i
Definition: afcover.h:80
#define T_FLOAT(a)
Definition: lcms2.h:697
#define T_FREE
Definition: minigimp.c:25
mrb_bool mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c)
Definition: object.c:485
#define MRB_ARGS_OPT(n)
Definition: mruby.h:845
mrb_int mrb_get_args(mrb_state *mrb, mrb_args_format format,...)
Definition: class.c:891
mrb_value mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg)
Definition: vm.c:726
static mrb_value os_each_object(mrb_state *mrb, mrb_value self)
static mrb_value os_count_objects(mrb_state *mrb, mrb_value self)
#define COUNT_TYPE(t)
static int os_count_object_type(mrb_state *mrb, struct RBasic *obj, void *data)
void mrb_mruby_objectspace_gem_final(mrb_state *mrb)
void mrb_mruby_objectspace_gem_init(mrb_state *mrb)
static int os_each_object_cb(mrb_state *mrb, struct RBasic *obj, void *ud)
void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
Definition: hash.c:1205
mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash)
Definition: hash.c:1496
mrb_value mrb_hash_new(mrb_state *mrb)
Definition: hash.c:1071
mrb_bool mrb_hash_empty_p(mrb_state *mrb, mrb_value self)
Definition: hash.c:1560
#define mrb_define_class_method(mrb, c, name, f, a)
Definition: scanning.h:16
#define mrb_define_module(mrb, name)
Definition: scanning.h:20
Definition: object.h:19
enum mrb_vtype tt
Definition: object.h:20
struct RClass * c
Definition: object.h:20
Definition: class.h:17
mrb_int counts[MRB_TT_MAXDEFINE+1]
struct RClass * target_module
int32_t mrb_int
Definition: value.h:35
@ MRB_TT_BREAK
Definition: value.h:131
@ MRB_TT_FREE
Definition: value.h:115
@ MRB_TT_FALSE
Definition: value.h:108
@ MRB_TT_ICLASS
Definition: value.h:119
@ MRB_TT_MAXDEFINE
Definition: value.h:132
@ MRB_TT_ENV
Definition: value.h:127
static mrb_value mrb_fixnum_value(mrb_int i)
Definition: value.h:301
static mrb_value mrb_symbol_value(mrb_sym i)
Definition: value.h:309
static mrb_value mrb_obj_value(void *p)
Definition: value.h:317