MRuby-Binding: Reduce number of object allocations

This is the same change as f067e0eff8,
applied to the mruby backend.
This commit is contained in:
Jonas Kulla 2014-08-09 10:38:42 +02:00
parent bdcf1503bd
commit 0ab543fd75
11 changed files with 28 additions and 63 deletions

View File

@ -31,7 +31,6 @@ struct
const char *str; const char *str;
} static const symData[] = } static const symData[] =
{ {
SYMD(priv_iv),
SYMD(font), SYMD(font),
SYMD(viewport), SYMD(viewport),
SYMD(bitmap), SYMD(bitmap),
@ -143,10 +142,8 @@ void raiseMrbExc(mrb_state *mrb, const Exception &exc)
MRB_METHOD_PUB(inspectObject) MRB_METHOD_PUB(inspectObject)
{ {
mrb_value priv = mrb_obj_iv_get(mrb, mrb_obj_ptr(self), getSym(mrb, CSpriv_iv));
static char buffer[64]; static char buffer[64];
snprintf(buffer, sizeof(buffer), "#<%s:%p>", DATA_TYPE(priv)->struct_name, DATA_PTR(priv)); snprintf(buffer, sizeof(buffer), "#<%s:%p>", DATA_TYPE(self)->struct_name, DATA_PTR(self));
return mrb_str_new_cstr(mrb, buffer); return mrb_str_new_cstr(mrb, buffer);
} }

View File

@ -32,11 +32,7 @@
enum CommonSymbol enum CommonSymbol
{ {
CSpriv_iv = 0, /* private data */ CSfont = 0,
/* From here on out all symbols
* have implicit '@' prefix */
CSfont,
CSviewport, CSviewport,
CSbitmap, CSbitmap,
CScolor, CScolor,
@ -115,7 +111,10 @@ getMrbData(mrb_state *mrb)
inline RClass* inline RClass*
defineClass(mrb_state *mrb, const char *name) defineClass(mrb_state *mrb, const char *name)
{ {
return mrb_define_class(mrb, name, mrb->object_class); RClass *klass = mrb_define_class(mrb, name, mrb->object_class);
MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
return klass;
} }
#define GUARD_EXC(exp) \ #define GUARD_EXC(exp) \
@ -222,7 +221,7 @@ defineClass(mrb_state *mrb, const char *name)
Klass *k = getPrivateData<Klass>(mrb, self); \ Klass *k = getPrivateData<Klass>(mrb, self); \
mrb_value dupObj = mrb_obj_clone(mrb, self); \ mrb_value dupObj = mrb_obj_clone(mrb, self); \
Klass *dupK = new Klass(*k); \ Klass *dupK = new Klass(*k); \
setPrivateData(mrb, dupObj, dupK, Klass##Type); \ setPrivateData(dupObj, dupK, Klass##Type); \
return dupObj; \ return dupObj; \
} }
@ -257,62 +256,33 @@ template<typename T>
inline T * inline T *
getPrivateData(mrb_state *mrb, mrb_value self) getPrivateData(mrb_state *mrb, mrb_value self)
{ {
mrb_value priv = mrb_obj_iv_get(mrb, (void) mrb;
mrb_obj_ptr(self), return static_cast<T*>(DATA_PTR(self));
getSym(mrb, CSpriv_iv));
return static_cast<T*>(DATA_PTR(priv));
} }
template<typename T> template<typename T>
inline T * inline T *
getPrivateDataCheck(mrb_state *mrb, mrb_value obj, const mrb_data_type &type) getPrivateDataCheck(mrb_state *mrb, mrb_value obj, const mrb_data_type &type)
{ {
static const char mesg[] = "wrong argument type %S (expected %S)"; return static_cast<T*>(mrb_check_datatype(mrb, obj, &type));
if (mrb_type(obj) != MRB_TT_OBJECT)
mrb_raisef(mrb, E_TYPE_ERROR, mesg,
mrb_str_new_cstr(mrb, (mrb_class_name(mrb, mrb_class(mrb, obj)))),
mrb_str_new_cstr(mrb, type.struct_name));
RObject *objP = mrb_obj_ptr(obj);
if (!mrb_obj_iv_defined(mrb, objP, getSym(mrb, CSpriv_iv)))
mrb_raisef(mrb, E_TYPE_ERROR, mesg,
mrb_str_new_cstr(mrb, (mrb_class_name(mrb, mrb_class(mrb, obj)))),
mrb_str_new_cstr(mrb, type.struct_name));
mrb_value priv = mrb_obj_iv_get(mrb, objP, getSym(mrb, CSpriv_iv));
void *p = mrb_check_datatype(mrb, priv, &type);
return static_cast<T*>(p);
} }
inline void inline void
setPrivateData(mrb_state *mrb, mrb_value self, void *p, const mrb_data_type &type) setPrivateData(mrb_value self, void *p, const mrb_data_type &type)
{ {
RData *data = DATA_PTR(self) = p;
mrb_data_object_alloc(mrb, DATA_TYPE(self) = &type;
mrb_obj_class(mrb, self),
p,
&type);
mrb_obj_iv_set(mrb,
mrb_obj_ptr(self),
getSym(mrb, CSpriv_iv),
mrb_obj_value(data));
} }
inline mrb_value inline mrb_value
wrapObject(mrb_state *mrb, void *p, const mrb_data_type &type) wrapObject(mrb_state *mrb, void *p, const mrb_data_type &type)
{ {
RClass *c = mrb_class_get(mrb, type.struct_name); RClass *klass = mrb_class_get(mrb, type.struct_name);
RObject *o = (RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, c); RData *data = mrb_data_object_alloc(mrb, klass, p, &type);
mrb_value obj = mrb_obj_value(o); mrb_value obj = mrb_obj_value(data);
setPrivateData(mrb, obj, p, type); setPrivateData(obj, p, type);
return obj; return obj;
} }
@ -384,12 +354,10 @@ objectLoad(mrb_state *mrb, mrb_value self, const mrb_data_type &type)
int data_len; int data_len;
mrb_get_args(mrb, "s", &data, &data_len); mrb_get_args(mrb, "s", &data, &data_len);
RObject *obj = (RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, klass);
mrb_value obj_value = mrb_obj_value(obj);
C *c = C::deserialize(data, data_len); C *c = C::deserialize(data, data_len);
setPrivateData(mrb, obj_value, c, type); RData *obj = mrb_data_object_alloc(mrb, klass, c, &type);
mrb_value obj_value = mrb_obj_value(obj);
return obj_value; return obj_value;
} }

View File

@ -49,7 +49,7 @@ MRB_METHOD(bitmapInitialize)
GUARD_EXC( b = new Bitmap(width, height); ) GUARD_EXC( b = new Bitmap(width, height); )
} }
setPrivateData(mrb, self, b, BitmapType); setPrivateData(self, b, BitmapType);
/* Wrap properties */ /* Wrap properties */
Font *font = new Font(); Font *font = new Font();

View File

@ -95,7 +95,7 @@ DEF_TYPE(Rect);
param_type p1, p2, p3, p4 = last_param_def; \ param_type p1, p2, p3, p4 = last_param_def; \
mrb_get_args(mrb, param_t_s, &p1, &p2, &p3, &p4); \ mrb_get_args(mrb, param_t_s, &p1, &p2, &p3, &p4); \
Klass *k = new Klass(p1, p2, p3, p4); \ Klass *k = new Klass(p1, p2, p3, p4); \
setPrivateData(mrb, self, k, Klass##Type); \ setPrivateData(self, k, Klass##Type); \
return self; \ return self; \
} }

View File

@ -45,7 +45,7 @@ MRB_METHOD(fontInitialize)
Font *f = new Font(name, size); Font *f = new Font(name, size);
setPrivateData(mrb, self, f, FontType); setPrivateData(self, f, FontType);
/* Wrap property objects */ /* Wrap property objects */
f->setColor(new Color(*f->getColor())); f->setColor(new Color(*f->getColor()));

View File

@ -31,7 +31,7 @@ MRB_METHOD(planeInitialize)
{ {
Plane *p = viewportElementInitialize<Plane>(mrb, self); Plane *p = viewportElementInitialize<Plane>(mrb, self);
setPrivateData(mrb, self, p, PlaneType); setPrivateData(self, p, PlaneType);
p->setColor(new Color); p->setColor(new Color);
p->setTone(new Tone); p->setTone(new Tone);

View File

@ -32,7 +32,7 @@ MRB_METHOD(spriteInitialize)
{ {
Sprite *s = viewportElementInitialize<Sprite>(mrb, self); Sprite *s = viewportElementInitialize<Sprite>(mrb, self);
setPrivateData(mrb, self, s, SpriteType); setPrivateData(self, s, SpriteType);
/* Wrap property objects */ /* Wrap property objects */
s->setSrcRect(new Rect); s->setSrcRect(new Rect);

View File

@ -35,7 +35,7 @@ MRB_METHOD(tableInitialize)
Table *t = new Table(x, y, z); Table *t = new Table(x, y, z);
setPrivateData(mrb, self, t, TableType); setPrivateData(self, t, TableType);
return self; return self;
} }

View File

@ -87,7 +87,7 @@ MRB_METHOD(tilemapInitialize)
/* Construct object */ /* Construct object */
t = new Tilemap(viewport); t = new Tilemap(viewport);
setPrivateData(mrb, self, t, TilemapType); setPrivateData(self, t, TilemapType);
setProperty(mrb, self, CSviewport, viewportObj); setProperty(mrb, self, CSviewport, viewportObj);

View File

@ -54,7 +54,7 @@ MRB_METHOD(viewportInitialize)
v = new Viewport(x, y, width, height); v = new Viewport(x, y, width, height);
} }
setPrivateData(mrb, self, v, ViewportType); setPrivateData(self, v, ViewportType);
/* Wrap property objects */ /* Wrap property objects */
v->setRect(new Rect(*v->getRect())); v->setRect(new Rect(*v->getRect()));

View File

@ -30,7 +30,7 @@ MRB_METHOD(windowInitialize)
{ {
Window *w = viewportElementInitialize<Window>(mrb, self); Window *w = viewportElementInitialize<Window>(mrb, self);
setPrivateData(mrb, self, w, WindowType); setPrivateData(self, w, WindowType);
w->setCursorRect(new Rect); w->setCursorRect(new Rect);
wrapNilProperty(mrb, self, CSwindowskin); wrapNilProperty(mrb, self, CSwindowskin);