more horizontal refactoring
[swftools.git] / lib / python / tagmap.c
index 055d035..66429e5 100644 (file)
@@ -37,25 +37,26 @@ typedef struct {
 PyObject* tagmap_new()
 {
     PyObject* self = (PyObject*)PyObject_New(TagMapObject, &TagMapClass);
-    mylog("+%08x(%d) tagmap_new", (int)self, self->ob_refcnt);
     TagMapObject*tagmap = (TagMapObject*)self;
     tagmap->obj2id = PyDict_New();
     tagmap->id2obj = PyDict_New();
     tagmap->objlist = PyList_New(0);
     tagmap->currentID = 0; //IDs start at 1
+/*    mylog("+%08x(%d) tagmap_new %08x(%d) %08x(%d), %08x(%d)", (int)self, self->ob_refcnt,
+           tagmap->obj2id, tagmap->obj2id->ob_refcnt ,
+           tagmap->id2obj, tagmap->id2obj->ob_refcnt ,
+           tagmap->objlist, tagmap->objlist->ob_refcnt);*/
     return self;
 }
 
 //----------------------------------------------------------------------------
 int tagmap_obj2id(PyObject* self, PyObject* obj)
 {
-    mylog(" %08x(%d) tagmap_obj2id %08x", (int)self, self->ob_refcnt, obj);
     TagMapObject*tagmap = (TagMapObject*)self;
     PyObject*id = PyDict_GetItem(tagmap->obj2id, obj);
     if(id == 0)
        return -1;
     int _id = PyLong_AsLong(id);
-    Py_DECREF(id);
     return _id;
 }
 
@@ -65,14 +66,12 @@ PyObject* tagmap_id2obj(PyObject* self, int _id)
     TagMapObject*tagmap = (TagMapObject*)self;
     PyObject*id = PyLong_FromLong(_id);
     PyObject*obj = PyDict_GetItem(tagmap->id2obj, id);
-    mylog(" %08x(%d) tagmap_id2obj %d->%08x", (int)self, self->ob_refcnt, _id, obj);
     Py_DECREF(id);
     return obj;
 }
 //----------------------------------------------------------------------------
 int tagmap_getFreeID(PyObject*self)
 {
-    mylog(" %08x(%d) tagmap_getFreeID", (int)self, self->ob_refcnt);
     TagMapObject*tagmap = (TagMapObject*)self;
     int last = tagmap->currentID;
     do {
@@ -90,6 +89,27 @@ int tagmap_getFreeID(PyObject*self)
     return -1;
 }
 //----------------------------------------------------------------------------
+static void tagmap_add_mapping(PyObject*self, int id, PyObject* obj)
+{
+    TagMapObject*tagmap = (TagMapObject*)self;
+    PyList_Append(tagmap->objlist, obj);//Py_INCREF(obj); done by PyList_Append
+    PyObject*id_obj = PyLong_FromLong(id);
+    PyDict_SetItem(tagmap->obj2id, obj, id_obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
+    PyDict_SetItem(tagmap->id2obj, id_obj, obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
+    Py_DECREF(id_obj);
+}
+//----------------------------------------------------------------------------
+void tagmap_addMapping(PyObject*self, int id, PyObject* obj)
+{
+    TagMapObject*tagmap = (TagMapObject*)self;
+    int id2 = tagmap_obj2id(self, obj);
+    if(id2>=0) {
+       assert(id==id2);
+       return;
+    }
+    tagmap_add_mapping(self, id, obj);
+}
+//----------------------------------------------------------------------------
 int tagmap_add(PyObject* self, PyObject* obj)
 {
     TagMapObject*tagmap = (TagMapObject*)self;
@@ -99,11 +119,9 @@ int tagmap_add(PyObject* self, PyObject* obj)
        return id;
     }
     id = tagmap_getFreeID(self);
-    PyList_Append(tagmap->objlist, obj);//Py_INCREF(obj); done by PyList_Append
-    PyObject*id_obj = PyLong_FromLong(id);
-    PyDict_SetItem(tagmap->obj2id, obj, id_obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
-    PyDict_SetItem(tagmap->id2obj, id_obj, obj);//Py_INCREF(id_obj);Py_INCREF(obj); done by PyDict_SetItem
-    Py_DECREF(id_obj);
+    
+    tagmap_add_mapping(self, id, obj);
+
     mylog(" %08x(%d) tagmap_add %08x->%d", (int)self, self->ob_refcnt, (int)obj, id);
     return id;
 }
@@ -111,8 +129,12 @@ int tagmap_add(PyObject* self, PyObject* obj)
 //----------------------------------------------------------------------------
 void tagmap_dealloc(PyObject* self)
 {
-    mylog("-%08x(%d) tagmap_dealloc", (int)self, self->ob_refcnt);
     TagMapObject*tagmap = (TagMapObject*)self;
+    mylog("-%08x(%d) tagmap_dealloc %08x(%d) %08x(%d), %08x(%d)", (int)self, self->ob_refcnt,
+           tagmap->obj2id, tagmap->obj2id->ob_refcnt ,
+           tagmap->id2obj, tagmap->id2obj->ob_refcnt ,
+           tagmap->objlist, tagmap->objlist->ob_refcnt);
+
     Py_DECREF(tagmap->obj2id);
     tagmap->obj2id = 0;
     Py_DECREF(tagmap->id2obj);
@@ -126,7 +148,6 @@ PyObject* tagmap_getObjectList(PyObject* self)
 {
     mylog(" %08x(%d) tagmap_getObjectList", (int)self, self->ob_refcnt);
     TagMapObject*tagmap = (TagMapObject*)self;
-    Py_INCREF(tagmap->objlist);
     return tagmap->objlist;
 }
 //----------------------------------------------------------------------------