]> git.localhorst.tv Git - l2e.git/blobdiff - src/loader/Interpreter.cpp
better allocation in interpreter
[l2e.git] / src / loader / Interpreter.cpp
index a3d382ceba7618f9f1782bf718c1cf49293753be..5da1ff35a194f194606c573be575d797643e5486 100644 (file)
@@ -54,18 +54,9 @@ using std::vector;
 namespace loader {
 
 Interpreter::~Interpreter() {
-       for (vector<PostponedDefinition>::const_iterator i(postponedDefinitions.begin()), end(postponedDefinitions.end()); i != end; ++i) {
-               delete i->identifier;
-       }
        for (std::map<string, SDL_Surface *>::const_iterator i(imageCache.begin()), end(imageCache.end()); i != end; ++i) {
                SDL_FreeSurface(i->second);
        }
-       // TODO: maybe need to reverse the array deletion check if most objects turn out to be arrays (of char)
-       for (std::map<int, vector<void *> >::const_iterator i(values.begin()), end(values.end()); i != end; ++i) {
-               for (vector<void *>::const_iterator j(i->second.begin()), end(i->second.end()); j != end; ++j) {
-                       delete[] reinterpret_cast<char *>(*j);
-               }
-       }
 }
 
 
@@ -120,7 +111,7 @@ void Interpreter::ReadLiteral(const Definition &dfn) {
                        (dfn.GetLiteral()->GetType() == Literal::PATH
                                        || dfn.GetLiteral()->GetType() == Literal::STRING)
                        ? dfn.GetLiteral()->GetString().size() : td.Size());
-       char *object(new char[size]);
+       char *object(alloc.Alloc(size));
        if (dfn.GetLiteral()->GetType() == Literal::OBJECT) {
                ReadObject(typeId, id, object, *dfn.GetLiteral()->GetProperties());
        } else {
@@ -170,7 +161,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                if (v.GetLiteral().IsObject()) {
                        int typeId(TypeDescription::GetTypeId(v.GetLiteral().GetTypeName()));
                        const TypeDescription &td(TypeDescription::Get(typeId));
-                       char *object(new char[td.Size()]);
+                       char *object(alloc.Alloc(td.Size()));
                        td.Construct(object);
                        int id(values[typeId].size());
                        values[typeId].push_back(object);
@@ -190,7 +181,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                                typeId = TypeDescription::GetTypeId("Boolean");
                                                id = values[typeId].size();
                                                const TypeDescription &td(TypeDescription::Get(typeId));
-                                               char *object(new char[td.Size()]);
+                                               char *object(alloc.Alloc(td.Size()));
                                                values[typeId].push_back(new (object) bool(v.GetLiteral().GetBoolean()));
                                        }
                                        break;
@@ -199,7 +190,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                                typeId = TypeDescription::GetTypeId("Color");
                                                id = values[typeId].size();
                                                const TypeDescription &td(TypeDescription::Get(typeId));
-                                               char *object(new char[td.Size()]);
+                                               char *object(alloc.Alloc(td.Size()));
                                                values[typeId].push_back(new (object) Color(v.GetLiteral().GetRed(), v.GetLiteral().GetGreen(), v.GetLiteral().GetBlue(), v.GetLiteral().GetAlpha()));
                                        }
                                        break;
@@ -208,7 +199,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                                typeId = TypeDescription::GetTypeId("Number");
                                                id = values[typeId].size();
                                                const TypeDescription &td(TypeDescription::Get(typeId));
-                                               char *object(new char[td.Size()]);
+                                               char *object(alloc.Alloc(td.Size()));
                                                values[typeId].push_back(new (object) int(v.GetLiteral().GetNumber()));
                                        }
                                        break;
@@ -216,7 +207,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                        {
                                                typeId = TypeDescription::GetTypeId("Path");
                                                id = values[typeId].size();
-                                               char *str(new char[v.GetLiteral().GetString().size() + 1]);
+                                               char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
                                                std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
                                                str[v.GetLiteral().GetString().size()] = '\0';
                                                values[typeId].push_back(str);
@@ -226,7 +217,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                        {
                                                typeId = TypeDescription::GetTypeId("String");
                                                id = values[typeId].size();
-                                               char *str(new char[v.GetLiteral().GetString().size() + 1]);
+                                               char *str(alloc.Alloc(v.GetLiteral().GetString().size() + 1));
                                                std::memcpy(str, v.GetLiteral().GetString().c_str(), v.GetLiteral().GetString().size());
                                                str[v.GetLiteral().GetString().size()] = '\0';
                                                values[typeId].push_back(str);
@@ -237,7 +228,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                                typeId = TypeDescription::GetTypeId("Vector");
                                                id = values[typeId].size();
                                                const TypeDescription &td(TypeDescription::Get(typeId));
-                                               char *object(new char[td.Size()]);
+                                               char *object(alloc.Alloc(td.Size()));
                                                values[typeId].push_back(new (object) Vector<int>(v.GetLiteral().GetX(), v.GetLiteral().GetY()));
                                        }
                                        break;
@@ -246,7 +237,7 @@ void *Interpreter::GetObject(int typeId, const Value &v) {
                                                typeId = TypeDescription::GetTypeId(v.GetLiteral().GetTypeName());
                                                const TypeDescription &td(TypeDescription::Get(typeId));
                                                id = values[typeId].size();
-                                               char *object(new char[td.Size()]);
+                                               char *object(alloc.Alloc(td.Size()));
                                                td.Construct(object);
                                                ReadObject(typeId, id, object, *v.GetLiteral().GetProperties());
                                        }
@@ -265,7 +256,7 @@ void Interpreter::ReadObject(const Definition &dfn) {
        int typeId(TypeDescription::GetTypeId(dfn.TypeName()));
        const TypeDescription &td(TypeDescription::Get(typeId));
        int id(values[typeId].size());
-       char *object(new char[td.Size()]);
+       char *object(alloc.Alloc(td.Size()));
        td.Construct(object);
        values[typeId].push_back(object);
        ReadObject(typeId, id, object, *dfn.GetProperties());
@@ -282,7 +273,7 @@ void Interpreter::ReadObject(int typeId, int id, char *object, const PropertyLis
                        char *dest(object + fd.Offset());
                        if (fd.IsAggregate()) {
                                int arraySize(i->second->GetLiteral().ArraySize());
-                               char *aggregate(new char[fieldType.Size() * arraySize]);
+                               char *aggregate(alloc.Alloc(fieldType.Size() * arraySize));
                                char *iter(aggregate);
                                if (i->second->GetLiteral().GetType() == Literal::ARRAY_PROPS) {
                                        const vector<PropertyList *> &list(i->second->GetLiteral().GetPropertyLists());
@@ -340,7 +331,7 @@ bool Interpreter::CanLink(const Value &v) const {
 }
 
 void Interpreter::Postpone(int type, int id, std::ptrdiff_t offset, const std::string &identifier, int linkedType, bool inlined) {
-       char *str(new char[identifier.size() + 1]);
+       char *str(alloc.Alloc(identifier.size() + 1));
        std::memcpy(str, identifier.c_str(), identifier.size());
        str[identifier.size()] = '\0';
        postponedDefinitions.push_back(PostponedDefinition(type, id, offset, str, linkedType, inlined));