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 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 11 | void SkPtrSet::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | Pair* p = fList.begin(); |
| 13 | Pair* stop = fList.end(); |
| 14 | while (p < stop) { |
| 15 | this->decPtr(p->fPtr); |
| 16 | p += 1; |
| 17 | } |
| 18 | fList.reset(); |
| 19 | } |
| 20 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 21 | bool SkPtrSet::Less(const Pair& a, const Pair& b) { |
| 22 | return (char*)a.fPtr < (char*)b.fPtr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | } |
| 24 | |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 25 | uint32_t SkPtrSet::find(void* ptr) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | if (nullptr == ptr) { |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 27 | return 0; |
| 28 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 29 | |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 30 | int count = fList.count(); |
| 31 | Pair pair; |
| 32 | pair.fPtr = ptr; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 33 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 34 | int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
reed@google.com | 8d90eeb | 2011-05-04 18:03:00 +0000 | [diff] [blame] | 35 | if (index < 0) { |
| 36 | return 0; |
| 37 | } |
| 38 | return fList[index].fIndex; |
| 39 | } |
| 40 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 41 | uint32_t SkPtrSet::add(void* ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | if (nullptr == ptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | int count = fList.count(); |
| 47 | Pair pair; |
| 48 | pair.fPtr = ptr; |
| 49 | |
bsalomon@google.com | 20f7f17 | 2013-05-17 19:05:03 +0000 | [diff] [blame] | 50 | int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | if (index < 0) { |
| 52 | index = ~index; // turn it back into an index for insertion |
| 53 | this->incPtr(ptr); |
| 54 | pair.fIndex = count + 1; |
| 55 | *fList.insert(index) = pair; |
| 56 | return count + 1; |
| 57 | } else { |
| 58 | return fList[index].fIndex; |
| 59 | } |
| 60 | } |
| 61 | |
mike@reedtribe.org | e9e08cc | 2011-04-29 01:44:52 +0000 | [diff] [blame] | 62 | void SkPtrSet::copyToArray(void* array[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | int count = fList.count(); |
| 64 | if (count > 0) { |
| 65 | SkASSERT(array); |
| 66 | const Pair* p = fList.begin(); |
| 67 | // p->fIndex is base-1, so we need to subtract to find its slot |
| 68 | for (int i = 0; i < count; i++) { |
| 69 | int index = p[i].fIndex - 1; |
| 70 | SkASSERT((unsigned)index < (unsigned)count); |
| 71 | array[index] = p[i].fPtr; |
| 72 | } |
| 73 | } |
| 74 | } |