Revision: 6099
Author: [email protected]
Date: Tue Sep  8 16:09:51 2009
Log: Add sublinear indexing of objects, outline of support for fixing the  
object
identity problem.

http://code.google.com/p/google-web-toolkit/source/detail?r=6099

Modified:
  /trunk/plugins/npapi/LocalObjectTable.cpp
  /trunk/plugins/npapi/LocalObjectTable.h

=======================================
--- /trunk/plugins/npapi/LocalObjectTable.cpp   Mon Aug  3 08:30:11 2009
+++ /trunk/plugins/npapi/LocalObjectTable.cpp   Tue Sep  8 16:09:51 2009
@@ -6,3 +6,16 @@
      freeAll();
    }
  }
+
+void* LocalObjectTable::getIdentityFrom(NPObject* obj) {
+  void** rawPtr = reinterpret_cast<void**>(reinterpret_cast<char*>(obj) +  
sizeof(NPClass*)
+      + sizeof(uint32_t));
+  Debug::log(Debug::Info) << "getIdentity(obj=" << (void*)obj << "):  
class=" << (void*)obj->_class
+      << ", bytes:";
+  for (int i = 0; i< 4; ++i) {
+    Debug::log(Debug::Info) << " " << rawPtr[i];
+  }
+  Debug::log(Debug::Info) << Debug::flush;
+  return obj;
+}
+
=======================================
--- /trunk/plugins/npapi/LocalObjectTable.h     Mon Aug  3 08:30:11 2009
+++ /trunk/plugins/npapi/LocalObjectTable.h     Tue Sep  8 16:09:51 2009
@@ -18,6 +18,7 @@

  #include <vector>
  #include <algorithm>
+#include <map>

  #include "Debug.h"

@@ -29,6 +30,7 @@

    int nextFree;
    std::vector<NPObject*> objects;
+  std::map<void*, int> objectIndex;
    bool dontFree;

    bool isFree(int id) {
@@ -41,6 +43,16 @@
      objects[id] = reinterpret_cast<NPObject*>((nextFree << 1) | 1LL);
      nextFree = id;
    }
+
+  int findIndex(void* ptr) {
+    std::map<void*, int>::iterator it = objectIndex.find(ptr);
+    if (it == objectIndex.end()) {
+      return -1;
+    }
+    return it->second;
+  }
+
+  static void* getIdentityFrom(NPObject* obj);
  public:
    LocalObjectTable() {
      nextFree = -1;
@@ -51,7 +63,8 @@
    virtual ~LocalObjectTable();

    int add(NPObject* obj) {
-    int id = find(obj);
+    void* objId = getIdentityFrom(obj);
+    int id = findIndex(objId);
      if (id >= 0) {
        Debug::log(Debug::Spam) << "LocalObjectTable::add(obj=" << obj
            << "): returning old id=" << id << Debug::flush;
@@ -65,6 +78,7 @@
        id = static_cast<int>(objects.size());
        objects.push_back(obj);
      }
+    objectIndex[objId] = id;
      Debug::log(Debug::Spam) << "LocalObjectTable::add(obj=" << obj << "):  
id=" << id
          << Debug::flush;
      // keep track that we hold a reference in the table
@@ -72,13 +86,9 @@
      return id;
    }

-  // TODO(jat): sublinear search
    int find(NPObject* obj) {
-    std::vector<NPObject*>::iterator it = std::find(objects.begin(),  
objects.end(), obj);
-    if (it == objects.end()) {
-      return -1;
-    }
-    return static_cast<int>(it - objects.begin());
+    void* objId = getIdentityFrom(obj);
+    return findIndex(objId);
    }

    void free(int id) {

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to