From 42619d8df206b0bcd36d952909d972b8961e75de Mon Sep 17 00:00:00 2001 From: "bsalomon@google.com" Date: Mon, 3 Dec 2012 14:54:59 +0000 Subject: [PATCH] Rename SkTDLinkedList to SkTInternalLinked list, add some methods useful for forthcoming SkTLList. Review URL: https://codereview.appspot.com/6858101 git-svn-id: http://skia.googlecode.com/svn/trunk@6643 2bbb7eff-a529-9590-31e7-b0007b416f81 --- gyp/core.gypi | 2 +- .../core/{SkTDLinkedList.h => SkTInternalLList.h} | 83 ++++++++++++++-------- include/gpu/GrResource.h | 6 +- include/utils/SkThreadPool.h | 12 ++-- src/gpu/GrGpu.h | 2 +- src/gpu/GrResourceCache.h | 12 ++-- tests/TDLinkedListTest.cpp | 15 ++-- 7 files changed, 77 insertions(+), 55 deletions(-) rename include/core/{SkTDLinkedList.h => SkTInternalLList.h} (64%) diff --git a/gyp/core.gypi b/gyp/core.gypi index 6251ce6..960af33 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -253,7 +253,7 @@ '<(skia_include_path)/core/SkTDArray.h', '<(skia_include_path)/core/SkTDStack.h', '<(skia_include_path)/core/SkTDict.h', - '<(skia_include_path)/core/SkTDLinkedList.h', + '<(skia_include_path)/core/SkTInternalLList.h', '<(skia_include_path)/core/SkTRegistry.h', '<(skia_include_path)/core/SkTScopedPtr.h', '<(skia_include_path)/core/SkTSearch.h', diff --git a/include/core/SkTDLinkedList.h b/include/core/SkTInternalLList.h similarity index 64% rename from include/core/SkTDLinkedList.h rename to include/core/SkTInternalLList.h index 9247812..32245b5 100644 --- a/include/core/SkTDLinkedList.h +++ b/include/core/SkTInternalLList.h @@ -5,14 +5,13 @@ * found in the LICENSE file. */ -#ifndef SkTDLinkedList_DEFINED -#define SkTDLinkedList_DEFINED +#ifndef SkTInternalLList_DEFINED +#define SkTInternalLList_DEFINED #include "SkTypes.h" /** - * Helper class to automatically initialize the doubly linked list - * created pointers. + * Helper class to automatically initialize the doubly linked list created pointers. */ template class SkPtrWrapper { public: @@ -26,23 +25,22 @@ template class SkPtrWrapper { /** - * This macro creates the member variables required by - * the SkTDLinkedList class. It should be placed in the private section - * of any class that will be stored in a double linked list. + * This macro creates the member variables required by the SkTInternalLList class. It should be + * placed in the private section of any class that will be stored in a double linked list. */ -#define SK_DEFINE_DLINKEDLIST_INTERFACE(ClassName) \ - friend class SkTDLinkedList; \ - /* back pointer to the owning list - for debugging */ \ - SkDEBUGCODE(SkPtrWrapper > fList;)\ - SkPtrWrapper fPrev; \ - SkPtrWrapper fNext; +#define SK_DECLARE_INTERNAL_LLIST_INTERFACE(ClassName) \ + friend class SkTInternalLList; \ + /* back pointer to the owning list - for debugging */ \ + SkDEBUGCODE(SkPtrWrapper > fList;) \ + SkPtrWrapper fPrev; \ + SkPtrWrapper fNext /** * This class implements a templated internal doubly linked list data structure. */ -template class SkTDLinkedList : public SkNoncopyable { +template class SkTInternalLList : public SkNoncopyable { public: - SkTDLinkedList() + SkTInternalLList() : fHead(NULL) , fTail(NULL) { } @@ -92,6 +90,25 @@ public: #endif } + void addToTail(T* entry) { + SkASSERT(NULL == entry->fPrev && NULL == entry->fNext); + SkASSERT(NULL == entry->fList); + + entry->fPrev = fTail; + entry->fNext = NULL; + if (NULL != fTail) { + fTail->fNext = entry; + } + fTail = entry; + if (NULL == fHead) { + fHead = entry; + } + +#ifdef SK_DEBUG + entry->fList = this; +#endif + } + bool isEmpty() const { return NULL == fHead && NULL == fTail; } @@ -106,52 +123,56 @@ public: kTail_IterStart }; - Iter() : fCur(NULL) {} + Iter() : fCurr(NULL) {} + Iter(const Iter& iter) : fCurr(iter.fCurr) {} + Iter& operator= (const Iter& iter) { fCurr = iter.fCurr; return *this; } - T* init(SkTDLinkedList& list, IterStart startLoc) { + T* init(const SkTInternalLList& list, IterStart startLoc) { if (kHead_IterStart == startLoc) { - fCur = list.fHead; + fCurr = list.fHead; } else { SkASSERT(kTail_IterStart == startLoc); - fCur = list.fTail; + fCurr = list.fTail; } - return fCur; + return fCurr; } + T* get() { return fCurr; } + /** * Return the next/previous element in the list or NULL if at the end. */ T* next() { - if (NULL == fCur) { + if (NULL == fCurr) { return NULL; } - fCur = fCur->fNext; - return fCur; + fCurr = fCurr->fNext; + return fCurr; } T* prev() { - if (NULL == fCur) { + if (NULL == fCurr) { return NULL; } - fCur = fCur->fPrev; - return fCur; + fCurr = fCurr->fPrev; + return fCurr; } private: - T* fCur; + T* fCurr; }; #ifdef SK_DEBUG void validate() const { - GrAssert(!fHead == !fTail); + SkASSERT(!fHead == !fTail); } /** - * Debugging-only method that uses the list back pointer to check if - * 'entry' is indeed in 'this' list. + * Debugging-only method that uses the list back pointer to check if 'entry' is indeed in 'this' + * list. */ bool isInList(const T* entry) const { return entry->fList == this; @@ -176,4 +197,4 @@ private: typedef SkNoncopyable INHERITED; }; -#endif // SkTDLinkedList_DEFINED +#endif diff --git a/include/gpu/GrResource.h b/include/gpu/GrResource.h index aeab180..3c306f8 100644 --- a/include/gpu/GrResource.h +++ b/include/gpu/GrResource.h @@ -12,7 +12,7 @@ #include "GrRefCnt.h" -#include "SkTDLinkedList.h" +#include "SkTInternalLList.h" class GrGpu; class GrContext; @@ -93,8 +93,8 @@ private: // release() on all such resources in its // destructor. - // we're a dlinklist - SK_DEFINE_DLINKEDLIST_INTERFACE(GrResource); + // We're in an internal doubly linked list + SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResource); GrResourceEntry* fCacheEntry; // NULL if not in cache diff --git a/include/utils/SkThreadPool.h b/include/utils/SkThreadPool.h index a96f87b..cc45fc2 100644 --- a/include/utils/SkThreadPool.h +++ b/include/utils/SkThreadPool.h @@ -10,7 +10,7 @@ #include "SkCondVar.h" #include "SkTDArray.h" -#include "SkTDLinkedList.h" +#include "SkTInternalLList.h" class SkRunnable; class SkThread; @@ -36,13 +36,13 @@ public: SkRunnable* fRunnable; private: - SK_DEFINE_DLINKEDLIST_INTERFACE(LinkedRunnable) + SK_DECLARE_INTERNAL_LLIST_INTERFACE(LinkedRunnable); }; - SkTDLinkedList fQueue; - SkCondVar fReady; - SkTDArray fThreads; - bool fDone; + SkTInternalLList fQueue; + SkCondVar fReady; + SkTDArray fThreads; + bool fDone; static void Loop(void*); // Static because we pass in this. }; diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h index a4ed0ed..f9fd779 100644 --- a/src/gpu/GrGpu.h +++ b/src/gpu/GrGpu.h @@ -553,7 +553,7 @@ private: bool fContextIsDirty; - typedef SkTDLinkedList ResourceList; + typedef SkTInternalLList ResourceList; ResourceList fResourceList; // Given a rt, find or create a stencil buffer and attach it diff --git a/src/gpu/GrResourceCache.h b/src/gpu/GrResourceCache.h index c56edf6..2541930 100644 --- a/src/gpu/GrResourceCache.h +++ b/src/gpu/GrResourceCache.h @@ -14,7 +14,7 @@ #include "GrConfig.h" #include "GrTypes.h" #include "GrTHashCache.h" -#include "SkTDLinkedList.h" +#include "SkTInternalLList.h" class GrResource; @@ -159,8 +159,8 @@ private: GrResourceKey fKey; GrResource* fResource; - // we're a dlinklist - SK_DEFINE_DLINKEDLIST_INTERFACE(GrResourceEntry); + // we're a linked list + SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrResourceEntry); friend class GrResourceCache; friend class GrDLinkedList; @@ -312,8 +312,8 @@ private: class Key; GrTHashTable fCache; - // manage the dlink list - typedef SkTDLinkedList EntryList; + // We're an internal doubly linked list + typedef SkTInternalLList EntryList; EntryList fList; #if GR_DEBUG @@ -342,7 +342,7 @@ private: bool fPurging; #if GR_DEBUG - static size_t countBytes(const SkTDLinkedList& list); + static size_t countBytes(const SkTInternalLList& list); #endif }; diff --git a/tests/TDLinkedListTest.cpp b/tests/TDLinkedListTest.cpp index 8df39b8..b4ea0b7 100644 --- a/tests/TDLinkedListTest.cpp +++ b/tests/TDLinkedListTest.cpp @@ -6,7 +6,7 @@ */ #include "Test.h" -#include "SkTDLinkedList.h" +#include "SkTInternalLList.h" class ListElement { public: @@ -16,10 +16,10 @@ public: int fID; private: - SK_DEFINE_DLINKEDLIST_INTERFACE(ListElement); + SK_DECLARE_INTERNAL_LLIST_INTERFACE(ListElement); }; -static void CheckList(const SkTDLinkedList& list, +static void CheckList(const SkTInternalLList& list, skiatest::Reporter* reporter, bool empty, int numElements, @@ -37,7 +37,7 @@ static void CheckList(const SkTDLinkedList& list, } static void TestTDLinkedList(skiatest::Reporter* reporter) { - SkTDLinkedList list; + SkTInternalLList list; ListElement elements[4] = { ListElement(0), ListElement(1), @@ -59,14 +59,15 @@ static void TestTDLinkedList(skiatest::Reporter* reporter) { CheckList(list, reporter, false, 4, true, true, true, true, elements); // test out iterators - SkTDLinkedList::Iter iter; + typedef SkTInternalLList::Iter Iter; + Iter iter; - ListElement* cur = iter.init(list, SkTDLinkedList::Iter::kHead_IterStart); + ListElement* cur = iter.init(list, Iter::kHead_IterStart); for (int i = 0; NULL != cur; ++i, cur = iter.next()) { REPORTER_ASSERT(reporter, cur->fID == 3-i); } - cur = iter.init(list, SkTDLinkedList::Iter::kTail_IterStart); + cur = iter.init(list, Iter::kTail_IterStart); for (int i = 0; NULL != cur; ++i, cur = iter.prev()) { REPORTER_ASSERT(reporter, cur->fID == i); } -- 2.7.4