epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkPtrRecorder.h" |
| 9 | #include "SkTSearch.h" |
| 10 | |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 11 | SK_DEFINE_INST_COUNT(SkPtrSet) |
robertphillips@google.com | a22e211 | 2012-08-16 14:58:06 +0000 | [diff] [blame] | 12 | SK_DEFINE_INST_COUNT(SkNamedFactorySet) |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 13 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 14 | void SkPtrSet::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | Pair* p = fList.begin(); |
| 16 | Pair* stop = fList.end(); |
| 17 | while (p < stop) { |
| 18 | this->decPtr(p->fPtr); |
| 19 | p += 1; |
| 20 | } |
| 21 | fList.reset(); |
| 22 | } |
| 23 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 24 | bool SkPtrSet::Less(const Pair& a, const Pair& b) { |
| 25 | return (char*)a.fPtr < (char*)b.fPtr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | } |
| 27 | |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 28 | uint32_t SkPtrSet::find(void* ptr) const { |
| 29 | if (NULL == ptr) { |
| 30 | return 0; |
| 31 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 33 | int count = fList.count(); |
| 34 | Pair pair; |
| 35 | pair.fPtr = ptr; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 37 | int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 38 | if (index < 0) { |
| 39 | return 0; |
| 40 | } |
| 41 | return fList[index].fIndex; |
| 42 | } |
| 43 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 44 | uint32_t SkPtrSet::add(void* ptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | if (NULL == ptr) { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int count = fList.count(); |
| 50 | Pair pair; |
| 51 | pair.fPtr = ptr; |
| 52 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 53 | int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | if (index < 0) { |
| 55 | index = ~index; // turn it back into an index for insertion |
| 56 | this->incPtr(ptr); |
| 57 | pair.fIndex = count + 1; |
| 58 | *fList.insert(index) = pair; |
| 59 | return count + 1; |
| 60 | } else { |
| 61 | return fList[index].fIndex; |
| 62 | } |
| 63 | } |
| 64 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 65 | void SkPtrSet::copyToArray(void* array[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | int count = fList.count(); |
| 67 | if (count > 0) { |
| 68 | SkASSERT(array); |
| 69 | const Pair* p = fList.begin(); |
| 70 | // p->fIndex is base-1, so we need to subtract to find its slot |
| 71 | for (int i = 0; i < count; i++) { |
| 72 | int index = p[i].fIndex - 1; |
| 73 | SkASSERT((unsigned)index < (unsigned)count); |
| 74 | array[index] = p[i].fPtr; |
| 75 | } |
| 76 | } |
| 77 | } |