2006-01-19 Eric Seidel <eseidel@apple.com>
authoreseidel <eseidel@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Jan 2006 08:16:38 +0000 (08:16 +0000)
committereseidel <eseidel@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Jan 2006 08:16:38 +0000 (08:16 +0000)
        Reviewed by darin.

        Remove CFDictionary usage from KWQObject.*
        http://bugzilla.opendarwin.org/show_bug.cgi?id=6468

        * kwq/KWQObject.cpp:
        (KWQObjectTimer::KWQObjectTimer):
        (QObject::restartTimer):
        (QObject::timerIntervals):
        (KWQObjectTimer::deleteTimer):
        (QObject::killTimer):
        (QObject::killTimers):
        (sendDeferredTimerEvent):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@12231 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/kwq/KWQObject.cpp

index ffb1165..fbcdcb4 100644 (file)
@@ -1,3 +1,19 @@
+2006-01-19  Eric Seidel  <eseidel@apple.com>
+
+        Reviewed by darin.
+
+        Remove CFDictionary usage from KWQObject.*
+        http://bugzilla.opendarwin.org/show_bug.cgi?id=6468
+
+        * kwq/KWQObject.cpp:
+        (KWQObjectTimer::KWQObjectTimer):
+        (QObject::restartTimer):
+        (QObject::timerIntervals):
+        (KWQObjectTimer::deleteTimer):
+        (QObject::killTimer):
+        (QObject::killTimers):
+        (sendDeferredTimerEvent):
+
 2006-01-18  David Hyatt <hyatt@apple.com
 
         Fix for bug 6657, remove all uses of MacFrame from cross-platform
index 1d964e2..3f574bf 100644 (file)
 #include "KWQObject.h"
 
 #include <kxmlcore/Assertions.h>
+#include <kxmlcore/HashMap.h>
 
 struct KWQObjectTimer {
+    KWQObjectTimer(QObject *target, int timerId, bool defered = false);
+
+    void deleteTimer();
+
     QObject *target;
     int timerId;
     CFRunLoopTimerRef runLoopTimer; // non-0 for running timers
@@ -36,10 +41,18 @@ struct KWQObjectTimer {
     bool deleted;
 };
 
+KWQObjectTimer::KWQObjectTimer(QObject *tgt, int id, bool defer)
+    : target(tgt), timerId(id), runLoopTimer(0), deferred(defer), deleted(false)
+{
+}
+
 const QObject *QObject::_sender;
 bool QObject::_defersTimers;
 
-static CFMutableDictionaryRef timerDictionaries;
+typedef HashMap<int, KWQObjectTimer*, PointerHash<int> > IdToTimerMap;
+typedef HashMap<const QObject*, IdToTimerMap*, PointerHash<const QObject*> > ObjectToTimersMap;
+
+static ObjectToTimersMap *timerMaps;
 static CFMutableArrayRef deferredTimers;
 static CFRunLoopTimerRef sendDeferredTimerEventsTimer;
 static int lastTimerIdUsed;
@@ -143,29 +156,23 @@ void QObject::restartTimer(int timerId, int nextFireInterval, int repeatInterval
     ASSERT(timerId > 0);
     ASSERT(timerId <= lastTimerIdUsed);
 
-    if (!timerDictionaries)
-        timerDictionaries = CFDictionaryCreateMutable(0, 0, 0, &kCFTypeDictionaryValueCallBacks);
+    if (!timerMaps)
+        timerMaps = new ObjectToTimersMap;
 
-    CFMutableDictionaryRef timers = (CFMutableDictionaryRef)CFDictionaryGetValue(timerDictionaries, this);
+    IdToTimerMap *timers = timerMaps->get(this);
     if (!timers) {
-        timers = CFDictionaryCreateMutable(0, 0, 0, 0);
-        CFDictionarySetValue(timerDictionaries, this, timers);
-        CFRelease(timers);
+        timers = new IdToTimerMap;
+        timerMaps->set(this, timers);
     }
 
-    ASSERT(!CFDictionaryGetValue(timers, reinterpret_cast<void *>(timerId)));
+    ASSERT(!timers->contains(timerId));
 
-    KWQObjectTimer *timer = static_cast<KWQObjectTimer *>(fastMalloc(sizeof(KWQObjectTimer)));
-    timer->target = this;
-    timer->timerId = timerId;
+    KWQObjectTimer *timer = new KWQObjectTimer(this, timerId);
     CFRunLoopTimerContext context = { 0, timer, 0, 0, 0 };
     CFRunLoopTimerRef runLoopTimer = CFRunLoopTimerCreate(0, CFAbsoluteTimeGetCurrent() + nextFireInterval * 0.001,
         repeatInterval * 0.001, 0, 0, timerFired, &context);
     timer->runLoopTimer = runLoopTimer;
-    timer->deferred = false;
-    timer->deleted = false;
-
-    CFDictionarySetValue(timers, reinterpret_cast<void *>(timerId), timer);
+    timers->set(timerId, timer);
 
     CFRunLoopAddTimer(CFRunLoopGetCurrent(), runLoopTimer, kCFRunLoopDefaultMode);
 }
@@ -174,57 +181,56 @@ void QObject::timerIntervals(int timerId, int& nextFireInterval, int& repeatInte
 {
     nextFireInterval = -1;
     repeatInterval = -1;
-    if (!timerDictionaries)
+    if (!timerMaps)
         return;
-    CFMutableDictionaryRef timers = (CFMutableDictionaryRef)CFDictionaryGetValue(timerDictionaries, this);
+    IdToTimerMap *timers = timerMaps->get(this);
     if (!timers)
         return;
-    KWQObjectTimer *timer = (KWQObjectTimer *)CFDictionaryGetValue(timers, reinterpret_cast<void *>(timerId));
+    KWQObjectTimer *timer = timers->get(timerId);
     if (!timer)
         return;
     nextFireInterval = (int)((CFRunLoopTimerGetNextFireDate(timer->runLoopTimer) - CFAbsoluteTimeGetCurrent()) * 1000);
     repeatInterval = (int)(CFRunLoopTimerGetInterval(timer->runLoopTimer) * 1000);
 }
 
-static void deleteTimer(KWQObjectTimer *timer)
+void KWQObjectTimer::deleteTimer()
 {
-    CFRunLoopTimerInvalidate(timer->runLoopTimer);
-    CFRelease(timer->runLoopTimer);
+    CFRunLoopTimerInvalidate(runLoopTimer);
+    CFRelease(runLoopTimer);
 
-    if (timer->deferred)
-        timer->deleted = true;
+    if (deferred)
+        deleted = true;
     else
-        fastFree(timer);
+        delete this;
 }
 
 void QObject::killTimer(int timerId)
 {
-    if (!timerDictionaries)
+    if (!timerMaps)
         return;
-    CFMutableDictionaryRef timers = (CFMutableDictionaryRef)CFDictionaryGetValue(timerDictionaries, this);
+    IdToTimerMap *timers = timerMaps->get(this);
     if (!timers)
         return;
-    KWQObjectTimer *timer = (KWQObjectTimer *)CFDictionaryGetValue(timers, reinterpret_cast<void *>(timerId));
-    if (!timer)
-        return;
-    deleteTimer(timer);
-    CFDictionaryRemoveValue(timers, reinterpret_cast<void *>(timerId));
-}
-
-static void deleteOneTimer(const void *key, const void *value, void *context)
-{
-    deleteTimer((KWQObjectTimer *)value);
+    KWQObjectTimer *timer = timers->get(timerId);
+    if (timer) {
+        timer->deleteTimer();
+        timers->remove(timerId);
+    }
 }
 
 void QObject::killTimers()
 {
-    if (!timerDictionaries)
+    if (!timerMaps)
         return;
-    CFMutableDictionaryRef timers = (CFMutableDictionaryRef)CFDictionaryGetValue(timerDictionaries, this);
+    IdToTimerMap *timers = timerMaps->get(this);
     if (!timers)
         return;
-    CFDictionaryApplyFunction(timers, deleteOneTimer, 0);
-    CFDictionaryRemoveValue(timerDictionaries, this);
+    
+    IdToTimerMap::iterator end = timers->end();
+    for (IdToTimerMap::iterator it = timers->begin(); it != end; ++it)
+        it->second->deleteTimer();
+    timerMaps->remove(this);
+    delete timers;
 }
 
 static void sendDeferredTimerEvent(const void *value, void *context)
@@ -233,13 +239,13 @@ static void sendDeferredTimerEvent(const void *value, void *context)
     if (!timer)
         return;
     if (timer->deleted) {
-        fastFree(timer);
+        delete timer;
         return;
     }
     QTimerEvent event(timer->timerId);
     timer->target->timerEvent(&event);
     if (timer->deleted) {
-        fastFree(timer);
+        delete timer;
         return;
     }
     timer->deferred = false;