MRuby-Binding: Reduce number of object allocations
This is the same change as f067e0eff8
,
applied to the mruby backend.
This commit is contained in:
parent
bdcf1503bd
commit
0ab543fd75
|
@ -31,7 +31,6 @@ struct
|
|||
const char *str;
|
||||
} static const symData[] =
|
||||
{
|
||||
SYMD(priv_iv),
|
||||
SYMD(font),
|
||||
SYMD(viewport),
|
||||
SYMD(bitmap),
|
||||
|
@ -143,10 +142,8 @@ void raiseMrbExc(mrb_state *mrb, const Exception &exc)
|
|||
|
||||
MRB_METHOD_PUB(inspectObject)
|
||||
{
|
||||
mrb_value priv = mrb_obj_iv_get(mrb, mrb_obj_ptr(self), getSym(mrb, CSpriv_iv));
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -32,11 +32,7 @@
|
|||
|
||||
enum CommonSymbol
|
||||
{
|
||||
CSpriv_iv = 0, /* private data */
|
||||
|
||||
/* From here on out all symbols
|
||||
* have implicit '@' prefix */
|
||||
CSfont,
|
||||
CSfont = 0,
|
||||
CSviewport,
|
||||
CSbitmap,
|
||||
CScolor,
|
||||
|
@ -115,7 +111,10 @@ getMrbData(mrb_state *mrb)
|
|||
inline RClass*
|
||||
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) \
|
||||
|
@ -222,7 +221,7 @@ defineClass(mrb_state *mrb, const char *name)
|
|||
Klass *k = getPrivateData<Klass>(mrb, self); \
|
||||
mrb_value dupObj = mrb_obj_clone(mrb, self); \
|
||||
Klass *dupK = new Klass(*k); \
|
||||
setPrivateData(mrb, dupObj, dupK, Klass##Type); \
|
||||
setPrivateData(dupObj, dupK, Klass##Type); \
|
||||
return dupObj; \
|
||||
}
|
||||
|
||||
|
@ -257,62 +256,33 @@ template<typename T>
|
|||
inline T *
|
||||
getPrivateData(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_value priv = mrb_obj_iv_get(mrb,
|
||||
mrb_obj_ptr(self),
|
||||
getSym(mrb, CSpriv_iv));
|
||||
|
||||
return static_cast<T*>(DATA_PTR(priv));
|
||||
(void) mrb;
|
||||
return static_cast<T*>(DATA_PTR(self));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T *
|
||||
getPrivateDataCheck(mrb_state *mrb, mrb_value obj, const mrb_data_type &type)
|
||||
{
|
||||
static const char mesg[] = "wrong argument type %S (expected %S)";
|
||||
|
||||
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);
|
||||
return static_cast<T*>(mrb_check_datatype(mrb, obj, &type));
|
||||
}
|
||||
|
||||
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 =
|
||||
mrb_data_object_alloc(mrb,
|
||||
mrb_obj_class(mrb, self),
|
||||
p,
|
||||
&type);
|
||||
|
||||
mrb_obj_iv_set(mrb,
|
||||
mrb_obj_ptr(self),
|
||||
getSym(mrb, CSpriv_iv),
|
||||
mrb_obj_value(data));
|
||||
DATA_PTR(self) = p;
|
||||
DATA_TYPE(self) = &type;
|
||||
}
|
||||
|
||||
|
||||
inline mrb_value
|
||||
wrapObject(mrb_state *mrb, void *p, const mrb_data_type &type)
|
||||
{
|
||||
RClass *c = mrb_class_get(mrb, type.struct_name);
|
||||
RObject *o = (RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, c);
|
||||
mrb_value obj = mrb_obj_value(o);
|
||||
RClass *klass = mrb_class_get(mrb, type.struct_name);
|
||||
RData *data = mrb_data_object_alloc(mrb, klass, p, &type);
|
||||
mrb_value obj = mrb_obj_value(data);
|
||||
|
||||
setPrivateData(mrb, obj, p, type);
|
||||
setPrivateData(obj, p, type);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -384,12 +354,10 @@ objectLoad(mrb_state *mrb, mrb_value self, const mrb_data_type &type)
|
|||
int 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ MRB_METHOD(bitmapInitialize)
|
|||
GUARD_EXC( b = new Bitmap(width, height); )
|
||||
}
|
||||
|
||||
setPrivateData(mrb, self, b, BitmapType);
|
||||
setPrivateData(self, b, BitmapType);
|
||||
|
||||
/* Wrap properties */
|
||||
Font *font = new Font();
|
||||
|
|
|
@ -95,7 +95,7 @@ DEF_TYPE(Rect);
|
|||
param_type p1, p2, p3, p4 = last_param_def; \
|
||||
mrb_get_args(mrb, param_t_s, &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; \
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ MRB_METHOD(fontInitialize)
|
|||
|
||||
Font *f = new Font(name, size);
|
||||
|
||||
setPrivateData(mrb, self, f, FontType);
|
||||
setPrivateData(self, f, FontType);
|
||||
|
||||
/* Wrap property objects */
|
||||
f->setColor(new Color(*f->getColor()));
|
||||
|
|
|
@ -31,7 +31,7 @@ MRB_METHOD(planeInitialize)
|
|||
{
|
||||
Plane *p = viewportElementInitialize<Plane>(mrb, self);
|
||||
|
||||
setPrivateData(mrb, self, p, PlaneType);
|
||||
setPrivateData(self, p, PlaneType);
|
||||
|
||||
p->setColor(new Color);
|
||||
p->setTone(new Tone);
|
||||
|
|
|
@ -32,7 +32,7 @@ MRB_METHOD(spriteInitialize)
|
|||
{
|
||||
Sprite *s = viewportElementInitialize<Sprite>(mrb, self);
|
||||
|
||||
setPrivateData(mrb, self, s, SpriteType);
|
||||
setPrivateData(self, s, SpriteType);
|
||||
|
||||
/* Wrap property objects */
|
||||
s->setSrcRect(new Rect);
|
||||
|
|
|
@ -35,7 +35,7 @@ MRB_METHOD(tableInitialize)
|
|||
|
||||
Table *t = new Table(x, y, z);
|
||||
|
||||
setPrivateData(mrb, self, t, TableType);
|
||||
setPrivateData(self, t, TableType);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ MRB_METHOD(tilemapInitialize)
|
|||
/* Construct object */
|
||||
t = new Tilemap(viewport);
|
||||
|
||||
setPrivateData(mrb, self, t, TilemapType);
|
||||
setPrivateData(self, t, TilemapType);
|
||||
|
||||
setProperty(mrb, self, CSviewport, viewportObj);
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ MRB_METHOD(viewportInitialize)
|
|||
v = new Viewport(x, y, width, height);
|
||||
}
|
||||
|
||||
setPrivateData(mrb, self, v, ViewportType);
|
||||
setPrivateData(self, v, ViewportType);
|
||||
|
||||
/* Wrap property objects */
|
||||
v->setRect(new Rect(*v->getRect()));
|
||||
|
|
|
@ -30,7 +30,7 @@ MRB_METHOD(windowInitialize)
|
|||
{
|
||||
Window *w = viewportElementInitialize<Window>(mrb, self);
|
||||
|
||||
setPrivateData(mrb, self, w, WindowType);
|
||||
setPrivateData(self, w, WindowType);
|
||||
|
||||
w->setCursorRect(new Rect);
|
||||
wrapNilProperty(mrb, self, CSwindowskin);
|
||||
|
|
Loading…
Reference in New Issue