Changed luax_pushtype to retain the pushed object itself, but only if the object isn't in the Lua state already.

Previously, calling e.g. love.graphics.getFont() 10,000 times would retain the object 10,000 times (and release it 10,000 times when the Font object was garbage-collected in the Lua state.) Now it will only retain it once and release it once.
This commit is contained in:
Alex Szpakowski
2014-08-09 16:04:59 -03:00
parent 191b910f23
commit da7cd267ea
30 changed files with 113 additions and 118 deletions
+1 -1
View File
@@ -432,7 +432,6 @@ int Body::getFixtureList(lua_State *L) const
Fixture *fixture = (Fixture *)Memoizer::find(f);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
fixture->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, fixture);
lua_rawseti(L, -2, i);
i++;
@@ -458,6 +457,7 @@ int Body::getContactList(lua_State *L) const
contact->retain();
luax_pushtype(L, "Contact", PHYSICS_CONTACT_T, contact);
contact->release();
lua_rawseti(L, -2, i);
i++;
}
+2 -1
View File
@@ -125,8 +125,8 @@ int Physics::newPolygonShape(lua_State *L)
}
PolygonShape *p = new PolygonShape(s);
luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, p);
p->release();
return 1;
}
@@ -167,6 +167,7 @@ int Physics::newChainShape(lua_State *L)
ChainShape *c = new ChainShape(s);
luax_pushtype(L, "ChainShape", PHYSICS_CHAIN_SHAPE_T, c);
c->release();
return 1;
}
+2 -12
View File
@@ -57,10 +57,7 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse
{
Fixture *a = (Fixture *)Memoizer::find(contact->GetFixtureA());
if (a != 0)
{
a->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, a);
}
else
throw love::Exception("A fixture has escaped Memoizer!");
}
@@ -69,10 +66,7 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse
{
Fixture *b = (Fixture *)Memoizer::find(contact->GetFixtureB());
if (b != 0)
{
b->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, b);
}
else
throw love::Exception("A fixture has escaped Memoizer!");
}
@@ -84,6 +78,7 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse
cobj->retain();
luax_pushtype(L, "Contact", (PHYSICS_CONTACT_T), cobj);
cobj->release();
int args = 3;
if (impulse)
@@ -131,8 +126,6 @@ bool World::ContactFilter::process(Fixture *a, Fixture *b)
if (ref != 0)
{
a->retain();
b->retain();
lua_State *L = ref->getL();
ref->push();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, a);
@@ -163,7 +156,6 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
Fixture *f = (Fixture *)Memoizer::find(fixture);
if (!f)
throw love::Exception("A fixture has escaped Memoizer!");
f->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, f);
lua_call(L, 1, 1);
return luax_toboolean(L, -1);
@@ -191,7 +183,6 @@ float32 World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &
Fixture *f = (Fixture *)Memoizer::find(fixture);
if (!f)
throw love::Exception("A fixture has escaped Memoizer!");
f->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, f);
b2Vec2 scaledPoint = Physics::scaleUp(point);
lua_pushnumber(L, scaledPoint.x);
@@ -440,7 +431,6 @@ int World::getBodyList(lua_State *L) const
Body *body = (Body *)Memoizer::find(b);
if (!body)
throw love::Exception("A body has escaped Memoizer!");
body->retain();
luax_pushtype(L, "Body", PHYSICS_BODY_T, body);
lua_rawseti(L, -2, i);
i++;
@@ -459,7 +449,6 @@ int World::getJointList(lua_State *L) const
if (!j) break;
Joint *joint = (Joint *)Memoizer::find(j);
if (!joint) throw love::Exception("A joint has escaped Memoizer!");
joint->retain();
luax_pushtype(L, "Joint", PHYSICS_JOINT_T, joint);
lua_rawseti(L, -2, i);
i++;
@@ -482,6 +471,7 @@ int World::getContactList(lua_State *L) const
else
contact->retain();
luax_pushtype(L, "Contact", PHYSICS_CONTACT_T, contact);
contact->release();
lua_rawseti(L, -2, i);
i++;
}
-1
View File
@@ -526,7 +526,6 @@ int w_Body_getWorld(lua_State *L)
{
Body *t = luax_checkbody(L, 1);
World *world = t->getWorld();
world->retain();
luax_pushtype(L, "World", PHYSICS_WORLD_T, world);
return 1;
}
@@ -66,6 +66,7 @@ int w_ChainShape_getChildEdge(lua_State *L)
EdgeShape *e = 0;
luax_catchexcept(L, [&](){ e = c->getChildEdge(index); });
luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, e);
e->release();
return 1;
}
@@ -146,9 +146,7 @@ int w_Contact_getFixtures(lua_State *L)
Fixture *b = nullptr;
luax_catchexcept(L, [&](){ t->getFixtures(a, b); });
a->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, a);
b->retain();
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, b);
return 2;
}
+1 -1
View File
@@ -111,7 +111,6 @@ int w_Fixture_getBody(lua_State *L)
Body *body = t->getBody();
if (body == 0)
return 0;
body->retain();
luax_pushtype(L, "Body", PHYSICS_BODY_T, body);
return 1;
}
@@ -140,6 +139,7 @@ int w_Fixture_getShape(lua_State *L)
luax_pushtype(L, "Shape", PHYSICS_SHAPE_T, shape);
break;
}
shape->release();
return 1;
}
@@ -60,6 +60,7 @@ int w_newWorld(lua_State *L)
World *w;
luax_catchexcept(L, [&](){ w = instance()->newWorld(gx, gy, sleep); });
luax_pushtype(L, "World", PHYSICS_WORLD_T, w);
w->release();
return 1;
}
@@ -78,6 +79,7 @@ int w_newBody(lua_State *L)
Body *body;
luax_catchexcept(L, [&](){ body = instance()->newBody(world, x, y, btype); });
luax_pushtype(L, "Body", PHYSICS_BODY_T, body);
body->release();
return 1;
}
@@ -89,6 +91,7 @@ int w_newFixture(lua_State *L)
Fixture *fixture;
luax_catchexcept(L, [&](){ fixture = instance()->newFixture(body, shape, density); });
luax_pushtype(L, "Fixture", PHYSICS_FIXTURE_T, fixture);
fixture->release();
return 1;
}
@@ -102,6 +105,7 @@ int w_newCircleShape(lua_State *L)
CircleShape *shape;
luax_catchexcept(L, [&](){ shape = instance()->newCircleShape(radius); });
luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape);
shape->release();
return 1;
}
else if (top == 3)
@@ -112,6 +116,7 @@ int w_newCircleShape(lua_State *L)
CircleShape *shape;
luax_catchexcept(L, [&](){ shape = instance()->newCircleShape(x, y, radius); });
luax_pushtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, shape);
shape->release();
return 1;
}
else
@@ -129,6 +134,7 @@ int w_newRectangleShape(lua_State *L)
PolygonShape *shape;
luax_catchexcept(L, [&](){ shape = instance()->newRectangleShape(w, h); });
luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape);
shape->release();
return 1;
}
else if (top == 4 || top == 5)
@@ -141,6 +147,7 @@ int w_newRectangleShape(lua_State *L)
PolygonShape *shape;
luax_catchexcept(L, [&](){ shape = instance()->newRectangleShape(x, y, w, h, angle); });
luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, shape);
shape->release();
return 1;
}
else
@@ -156,6 +163,7 @@ int w_newEdgeShape(lua_State *L)
EdgeShape *shape;
luax_catchexcept(L, [&](){ shape = instance()->newEdgeShape(x1, y1, x2, y2); });
luax_pushtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, shape);
shape->release();
return 1;
}
@@ -187,6 +195,7 @@ int w_newDistanceJoint(lua_State *L)
j = instance()->newDistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);
});
luax_pushtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, j);
j->release();
return 1;
}
@@ -198,6 +207,7 @@ int w_newMouseJoint(lua_State *L)
MouseJoint *j;
luax_catchexcept(L, [&](){ j = instance()->newMouseJoint(body, x, y); });
luax_pushtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, j);
j->release();
return 1;
}
@@ -213,6 +223,7 @@ int w_newRevoluteJoint(lua_State *L)
j = instance()->newRevoluteJoint(body1, body2, x, y, collideConnected);
});
luax_pushtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, j);
j->release();
return 1;
}
@@ -245,6 +256,7 @@ int w_newPrismaticJoint(lua_State *L)
j = instance()->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
});
luax_pushtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, j);
j->release();
return 1;
}
@@ -268,6 +280,7 @@ int w_newPulleyJoint(lua_State *L)
j = instance()->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio, collideConnected);
});
luax_pushtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, j);
j->release();
return 1;
}
@@ -283,6 +296,7 @@ int w_newGearJoint(lua_State *L)
j = instance()->newGearJoint(joint1, joint2, ratio, collideConnected);
});
luax_pushtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, j);
j->release();
return 1;
}
@@ -311,6 +325,7 @@ int w_newFrictionJoint(lua_State *L)
j = instance()->newFrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);
});
luax_pushtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, j);
j->release();
return 1;
}
@@ -339,6 +354,7 @@ int w_newWeldJoint(lua_State *L)
j = instance()->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);
});
luax_pushtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, j);
j->release();
return 1;
}
@@ -372,6 +388,7 @@ int w_newWheelJoint(lua_State *L)
j = instance()->newWheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
});
luax_pushtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, j);
j->release();
return 1;
}
@@ -390,6 +407,7 @@ int w_newRopeJoint(lua_State *L)
j = instance()->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);
});
luax_pushtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, j);
j->release();
return 1;
}
@@ -410,6 +428,7 @@ int w_newMotorJoint(lua_State *L)
luax_catchexcept(L, [&](){ j = instance()->newMotorJoint(body1, body2); });
}
luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j);
j->release();
return 1;
}