|
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 */ |
|
8 #include "SkPtrRecorder.h" |
|
9 #include "SkTSearch.h" |
|
10 |
|
11 void SkPtrSet::reset() { |
|
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 |
|
21 bool SkPtrSet::Less(const Pair& a, const Pair& b) { |
|
22 return (char*)a.fPtr < (char*)b.fPtr; |
|
23 } |
|
24 |
|
25 uint32_t SkPtrSet::find(void* ptr) const { |
|
26 if (NULL == ptr) { |
|
27 return 0; |
|
28 } |
|
29 |
|
30 int count = fList.count(); |
|
31 Pair pair; |
|
32 pair.fPtr = ptr; |
|
33 |
|
34 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
|
35 if (index < 0) { |
|
36 return 0; |
|
37 } |
|
38 return fList[index].fIndex; |
|
39 } |
|
40 |
|
41 uint32_t SkPtrSet::add(void* ptr) { |
|
42 if (NULL == ptr) { |
|
43 return 0; |
|
44 } |
|
45 |
|
46 int count = fList.count(); |
|
47 Pair pair; |
|
48 pair.fPtr = ptr; |
|
49 |
|
50 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair)); |
|
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 |
|
62 void SkPtrSet::copyToArray(void* array[]) const { |
|
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 } |