From bed3baba0c36f52f640bd2afbe7689a5e7892ee2 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 8 Jan 2018 10:52:44 +0100 Subject: Simplify allocation functions in the memory manager Reduce code duplication by using variadic templates. Change-Id: Ia12b67184f66190b433c99fb2157fca4e46cb2b6 Reviewed-by: Simon Hausmann --- src/qml/memory/qv4mm_p.h | 166 +++++------------------------------------------ 1 file changed, 16 insertions(+), 150 deletions(-) (limited to 'src/qml/memory') diff --git a/src/qml/memory/qv4mm_p.h b/src/qml/memory/qv4mm_p.h index 40670bcdc7..c0ccd80c69 100644 --- a/src/qml/memory/qv4mm_p.h +++ b/src/qml/memory/qv4mm_p.h @@ -160,28 +160,24 @@ public: { return (size + Chunk::SlotSize - 1) & ~(Chunk::SlotSize - 1); } template - inline typename ManagedType::Data *allocManaged(std::size_t size) + inline typename ManagedType::Data *allocManaged(std::size_t size, InternalClass *ic) { Q_STATIC_ASSERT(std::is_trivial< typename ManagedType::Data >::value); size = align(size); Heap::Base *o = allocData(size); - InternalClass *ic = ManagedType::defaultInternalClass(engine); - ic = ic->changeVTable(ManagedType::staticVTable()); o->internalClass = ic; Q_ASSERT(o->internalClass && o->internalClass->vtable); + Q_ASSERT(ic->vtable == ManagedType::staticVTable()); return static_cast(o); } template - inline typename ManagedType::Data *allocManaged(std::size_t size, InternalClass *ic) + inline typename ManagedType::Data *allocManaged(std::size_t size) { Q_STATIC_ASSERT(std::is_trivial< typename ManagedType::Data >::value); - size = align(size); - Heap::Base *o = allocData(size); - o->internalClass = ic; - Q_ASSERT(o->internalClass && o->internalClass->vtable); - Q_ASSERT(ic->vtable == ManagedType::staticVTable()); - return static_cast(o); + InternalClass *ic = ManagedType::defaultInternalClass(engine); + ic = ic->changeVTable(ManagedType::staticVTable()); + return allocManaged(size, ic); } template @@ -200,11 +196,7 @@ public: InternalClass *ic = ObjectType::defaultInternalClass(engine); ic = ic->changeVTable(ObjectType::staticVTable()); ic = ic->changePrototype(ObjectType::defaultPrototype(engine)->d()); - Heap::Object *o = allocObjectWithMemberData(ObjectType::staticVTable(), ic->size); - o->internalClass = ic; - Q_ASSERT(o->internalClass && o->internalClass->vtable); - Q_ASSERT(o->internalClass->prototype == ObjectType::defaultPrototype(engine)->d()); - return static_cast(o); + return allocateObject(ic); } template @@ -226,158 +218,32 @@ public: return t->d(); } - template - typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype) - { - Scope scope(engine); - Scoped t(scope, allocateObject(ic)); - Q_ASSERT(t->internalClass()->prototype == (prototype ? prototype->d() : nullptr)); - Q_UNUSED(prototype); - t->d_unchecked()->init(); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype, Arg1 arg1) - { - Scope scope(engine); - Scoped t(scope, allocateObject(ic)); - Q_ASSERT(t->internalClass()->prototype == (prototype ? prototype->d() : nullptr)); - Q_UNUSED(prototype); - t->d_unchecked()->init(arg1); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype, Arg1 arg1, Arg2 arg2) - { - Scope scope(engine); - Scoped t(scope, allocateObject(ic)); - Q_ASSERT(t->internalClass()->prototype == (prototype ? prototype->d() : nullptr)); - Q_UNUSED(prototype); - t->d_unchecked()->init(arg1, arg2); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype, Arg1 arg1, Arg2 arg2, Arg3 arg3) - { - Scope scope(engine); - Scoped t(scope, allocateObject(ic)); - Q_ASSERT(t->internalClass()->prototype == (prototype ? prototype->d() : nullptr)); - Q_UNUSED(prototype); - t->d_unchecked()->init(arg1, arg2, arg3); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) + template + typename ObjectType::Data *allocObject(InternalClass *ic, Object *prototype, Args... args) { Scope scope(engine); Scoped t(scope, allocateObject(ic)); Q_ASSERT(t->internalClass()->prototype == (prototype ? prototype->d() : nullptr)); Q_UNUSED(prototype); - t->d_unchecked()->init(arg1, arg2, arg3, arg4); - return t->d(); - } - - template - typename ObjectType::Data *allocObject() - { - Scope scope(engine); - Scoped t(scope, allocateObject()); - t->d_unchecked()->init(); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(Arg1 arg1) - { - Scope scope(engine); - Scoped t(scope, allocateObject()); - t->d_unchecked()->init(arg1); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(Arg1 arg1, Arg2 arg2) - { - Scope scope(engine); - Scoped t(scope, allocateObject()); - t->d_unchecked()->init(arg1, arg2); - return t->d(); - } - - template - typename ObjectType::Data *allocObject(Arg1 arg1, Arg2 arg2, Arg3 arg3) - { - Scope scope(engine); - Scoped t(scope, allocateObject()); - t->d_unchecked()->init(arg1, arg2, arg3); + t->d_unchecked()->init(args...); return t->d(); } - template - typename ObjectType::Data *allocObject(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) + template + typename ObjectType::Data *allocObject(Args... args) { Scope scope(engine); Scoped t(scope, allocateObject()); - t->d_unchecked()->init(arg1, arg2, arg3, arg4); - return t->d(); - } - - - template - typename ManagedType::Data *alloc() - { - Scope scope(engine); - Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(); - return t->d(); - } - - template - typename ManagedType::Data *alloc(Arg1 arg1) - { - Scope scope(engine); - Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(arg1); - return t->d(); - } - - template - typename ManagedType::Data *alloc(Arg1 arg1, Arg2 arg2) - { - Scope scope(engine); - Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(arg1, arg2); - return t->d(); - } - - template - typename ManagedType::Data *alloc(Arg1 arg1, Arg2 arg2, Arg3 arg3) - { - Scope scope(engine); - Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(arg1, arg2, arg3); - return t->d(); - } - - template - typename ManagedType::Data *alloc(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) - { - Scope scope(engine); - Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(arg1, arg2, arg3, arg4); + t->d_unchecked()->init(args...); return t->d(); } - template - typename ManagedType::Data *alloc(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) + template + typename ManagedType::Data *alloc(Args... args) { Scope scope(engine); Scoped t(scope, allocManaged(sizeof(typename ManagedType::Data))); - t->d_unchecked()->init(arg1, arg2, arg3, arg4, arg5); + t->d_unchecked()->init(args...); return t->d(); } -- cgit v1.2.3