epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDeque.h" |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 9 | #include "SkMalloc.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 11 | struct SkDeque::Block { |
| 12 | Block* fNext; |
| 13 | Block* fPrev; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | char* fBegin; // start of used section in this chunk |
| 15 | char* fEnd; // end of used section in this chunk |
| 16 | char* fStop; // end of the allocated chunk |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 17 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | char* start() { return (char*)(this + 1); } |
| 19 | const char* start() const { return (const char*)(this + 1); } |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | void init(size_t size) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | fNext = fPrev = nullptr; |
| 23 | fBegin = fEnd = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | fStop = (char*)this + size; |
| 25 | } |
| 26 | }; |
| 27 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 28 | SkDeque::SkDeque(size_t elemSize, int allocCount) |
| 29 | : fElemSize(elemSize) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | , fInitialStorage(nullptr) |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 31 | , fCount(0) |
| 32 | , fAllocCount(allocCount) { |
| 33 | SkASSERT(allocCount >= 1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | fFrontBlock = fBackBlock = nullptr; |
| 35 | fFront = fBack = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 38 | SkDeque::SkDeque(size_t elemSize, void* storage, size_t storageSize, int allocCount) |
| 39 | : fElemSize(elemSize) |
| 40 | , fInitialStorage(storage) |
| 41 | , fCount(0) |
| 42 | , fAllocCount(allocCount) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | SkASSERT(storageSize == 0 || storage != nullptr); |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 44 | SkASSERT(allocCount >= 1); |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 45 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 46 | if (storageSize >= sizeof(Block) + elemSize) { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 47 | fFrontBlock = (Block*)storage; |
| 48 | fFrontBlock->init(storageSize); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | fFrontBlock = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | } |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 52 | fBackBlock = fFrontBlock; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | fFront = fBack = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | SkDeque::~SkDeque() { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 57 | Block* head = fFrontBlock; |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 58 | Block* initialHead = (Block*)fInitialStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
| 60 | while (head) { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 61 | Block* next = head->fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | if (head != initialHead) { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 63 | this->freeBlock(head); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | head = next; |
| 66 | } |
| 67 | } |
| 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | void* SkDeque::push_front() { |
| 70 | fCount += 1; |
| 71 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | if (nullptr == fFrontBlock) { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 73 | fFrontBlock = this->allocateBlock(fAllocCount); |
| 74 | fBackBlock = fFrontBlock; // update our linklist |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 76 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 77 | Block* first = fFrontBlock; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | char* begin; |
| 79 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | if (nullptr == first->fBegin) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | INIT_CHUNK: |
| 82 | first->fEnd = first->fStop; |
| 83 | begin = first->fStop - fElemSize; |
| 84 | } else { |
| 85 | begin = first->fBegin - fElemSize; |
| 86 | if (begin < first->start()) { // no more room in this chunk |
| 87 | // should we alloc more as we accumulate more elements? |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 88 | first = this->allocateBlock(fAllocCount); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 89 | first->fNext = fFrontBlock; |
| 90 | fFrontBlock->fPrev = first; |
| 91 | fFrontBlock = first; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | goto INIT_CHUNK; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | first->fBegin = begin; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 97 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | if (nullptr == fFront) { |
| 99 | SkASSERT(nullptr == fBack); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 100 | fFront = fBack = begin; |
| 101 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | SkASSERT(fBack); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 103 | fFront = begin; |
| 104 | } |
| 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | return begin; |
| 107 | } |
| 108 | |
| 109 | void* SkDeque::push_back() { |
| 110 | fCount += 1; |
| 111 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | if (nullptr == fBackBlock) { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 113 | fBackBlock = this->allocateBlock(fAllocCount); |
| 114 | fFrontBlock = fBackBlock; // update our linklist |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | } |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 116 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 117 | Block* last = fBackBlock; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | char* end; |
| 119 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | if (nullptr == last->fBegin) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | INIT_CHUNK: |
| 122 | last->fBegin = last->start(); |
| 123 | end = last->fBegin + fElemSize; |
| 124 | } else { |
| 125 | end = last->fEnd + fElemSize; |
| 126 | if (end > last->fStop) { // no more room in this chunk |
| 127 | // should we alloc more as we accumulate more elements? |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 128 | last = this->allocateBlock(fAllocCount); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 129 | last->fPrev = fBackBlock; |
| 130 | fBackBlock->fNext = last; |
| 131 | fBackBlock = last; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | goto INIT_CHUNK; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | last->fEnd = end; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 137 | end -= fElemSize; |
| 138 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | if (nullptr == fBack) { |
| 140 | SkASSERT(nullptr == fFront); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 141 | fFront = fBack = end; |
| 142 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 143 | SkASSERT(fFront); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 144 | fBack = end; |
| 145 | } |
| 146 | |
| 147 | return end; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void SkDeque::pop_front() { |
| 151 | SkASSERT(fCount > 0); |
| 152 | fCount -= 1; |
| 153 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 154 | Block* first = fFrontBlock; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | SkASSERT(first != nullptr); |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 157 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | if (first->fBegin == nullptr) { // we were marked empty from before |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | first = first->fNext; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | first->fPrev = nullptr; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 161 | this->freeBlock(fFrontBlock); |
| 162 | fFrontBlock = first; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | SkASSERT(first != nullptr); // else we popped too far |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | char* begin = first->fBegin + fElemSize; |
| 167 | SkASSERT(begin <= first->fEnd); |
| 168 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 169 | if (begin < fFrontBlock->fEnd) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | first->fBegin = begin; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 171 | SkASSERT(first->fBegin); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 172 | fFront = first->fBegin; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | first->fBegin = first->fEnd = nullptr; // mark as empty |
| 175 | if (nullptr == first->fNext) { |
| 176 | fFront = fBack = nullptr; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 177 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 178 | SkASSERT(first->fNext->fBegin); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 179 | fFront = first->fNext->fBegin; |
| 180 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | void SkDeque::pop_back() { |
| 185 | SkASSERT(fCount > 0); |
| 186 | fCount -= 1; |
| 187 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 188 | Block* last = fBackBlock; |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 189 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 190 | SkASSERT(last != nullptr); |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 191 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | if (last->fEnd == nullptr) { // we were marked empty from before |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | last = last->fPrev; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | last->fNext = nullptr; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 195 | this->freeBlock(fBackBlock); |
| 196 | fBackBlock = last; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 197 | SkASSERT(last != nullptr); // else we popped too far |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | } |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 199 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | char* end = last->fEnd - fElemSize; |
| 201 | SkASSERT(end >= last->fBegin); |
| 202 | |
| 203 | if (end > last->fBegin) { |
| 204 | last->fEnd = end; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 205 | SkASSERT(last->fEnd); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 206 | fBack = last->fEnd - fElemSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 208 | last->fBegin = last->fEnd = nullptr; // mark as empty |
| 209 | if (nullptr == last->fPrev) { |
| 210 | fFront = fBack = nullptr; |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 211 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 212 | SkASSERT(last->fPrev->fEnd); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 213 | fBack = last->fPrev->fEnd - fElemSize; |
| 214 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 218 | int SkDeque::numBlocksAllocated() const { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 219 | int numBlocks = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 221 | for (const Block* temp = fFrontBlock; temp; temp = temp->fNext) { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 222 | ++numBlocks; |
| 223 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 224 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 225 | return numBlocks; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 228 | SkDeque::Block* SkDeque::allocateBlock(int allocCount) { |
| 229 | Block* newBlock = (Block*)sk_malloc_throw(sizeof(Block) + allocCount * fElemSize); |
| 230 | newBlock->init(sizeof(Block) + allocCount * fElemSize); |
| 231 | return newBlock; |
| 232 | } |
| 233 | |
| 234 | void SkDeque::freeBlock(Block* block) { |
| 235 | sk_free(block); |
| 236 | } |
| 237 | |
| 238 | /////////////////////////////////////////////////////////////////////////////// |
| 239 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 240 | SkDeque::Iter::Iter() : fCurBlock(nullptr), fPos(nullptr), fElemSize(0) {} |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 241 | |
| 242 | SkDeque::Iter::Iter(const SkDeque& d, IterStart startLoc) { |
| 243 | this->reset(d, startLoc); |
| 244 | } |
| 245 | |
| 246 | // Due to how reset and next work, next actually returns the current element |
| 247 | // pointed to by fPos and then updates fPos to point to the next one. |
| 248 | void* SkDeque::Iter::next() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | char* pos = fPos; |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 250 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | if (pos) { // if we were valid, try to move to the next setting |
| 252 | char* next = pos + fElemSize; |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 253 | SkASSERT(next <= fCurBlock->fEnd); |
| 254 | if (next == fCurBlock->fEnd) { // exhausted this chunk, move to next |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | do { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 256 | fCurBlock = fCurBlock->fNext; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 257 | } while (fCurBlock != nullptr && fCurBlock->fBegin == nullptr); |
| 258 | next = fCurBlock ? fCurBlock->fBegin : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
| 260 | fPos = next; |
| 261 | } |
| 262 | return pos; |
| 263 | } |
| 264 | |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 265 | // Like next, prev actually returns the current element pointed to by fPos and |
| 266 | // then makes fPos point to the previous element. |
| 267 | void* SkDeque::Iter::prev() { |
| 268 | char* pos = fPos; |
| 269 | |
| 270 | if (pos) { // if we were valid, try to move to the prior setting |
| 271 | char* prev = pos - fElemSize; |
| 272 | SkASSERT(prev >= fCurBlock->fBegin - fElemSize); |
| 273 | if (prev < fCurBlock->fBegin) { // exhausted this chunk, move to prior |
| 274 | do { |
| 275 | fCurBlock = fCurBlock->fPrev; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 276 | } while (fCurBlock != nullptr && fCurBlock->fEnd == nullptr); |
| 277 | prev = fCurBlock ? fCurBlock->fEnd - fElemSize : nullptr; |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 278 | } |
| 279 | fPos = prev; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 280 | } |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 281 | return pos; |
| 282 | } |
| 283 | |
| 284 | // reset works by skipping through the spare blocks at the start (or end) |
| 285 | // of the doubly linked list until a non-empty one is found. The fPos |
| 286 | // member is then set to the first (or last) element in the block. If |
| 287 | // there are no elements in the deque both fCurBlock and fPos will come |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 288 | // out of this routine nullptr. |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 289 | void SkDeque::Iter::reset(const SkDeque& d, IterStart startLoc) { |
| 290 | fElemSize = d.fElemSize; |
| 291 | |
| 292 | if (kFront_IterStart == startLoc) { |
| 293 | // initialize the iterator to start at the front |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 294 | fCurBlock = d.fFrontBlock; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 295 | while (fCurBlock && nullptr == fCurBlock->fBegin) { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 296 | fCurBlock = fCurBlock->fNext; |
| 297 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 298 | fPos = fCurBlock ? fCurBlock->fBegin : nullptr; |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 299 | } else { |
| 300 | // initialize the iterator to start at the back |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 301 | fCurBlock = d.fBackBlock; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 302 | while (fCurBlock && nullptr == fCurBlock->fEnd) { |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 303 | fCurBlock = fCurBlock->fPrev; |
| 304 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 305 | fPos = fCurBlock ? fCurBlock->fEnd - fElemSize : nullptr; |
robertphillips@google.com | 0a78b0f | 2012-07-16 16:58:49 +0000 | [diff] [blame] | 306 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 307 | } |