Line data Source code
1 : /*
2 : * Copyright 2011 Google Inc.
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 : #include "SkPtrRecorder.h"
8 : #include "SkTSearch.h"
9 :
10 0 : void SkPtrSet::reset() {
11 0 : Pair* p = fList.begin();
12 0 : Pair* stop = fList.end();
13 0 : while (p < stop) {
14 0 : this->decPtr(p->fPtr);
15 0 : p += 1;
16 : }
17 0 : fList.reset();
18 0 : }
19 :
20 0 : bool SkPtrSet::Less(const Pair& a, const Pair& b) {
21 0 : return (char*)a.fPtr < (char*)b.fPtr;
22 : }
23 :
24 0 : uint32_t SkPtrSet::find(void* ptr) const {
25 0 : if (nullptr == ptr) {
26 0 : return 0;
27 : }
28 :
29 0 : int count = fList.count();
30 : Pair pair;
31 0 : pair.fPtr = ptr;
32 :
33 0 : int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
34 0 : if (index < 0) {
35 0 : return 0;
36 : }
37 0 : return fList[index].fIndex;
38 : }
39 :
40 0 : uint32_t SkPtrSet::add(void* ptr) {
41 0 : if (nullptr == ptr) {
42 0 : return 0;
43 : }
44 :
45 0 : int count = fList.count();
46 : Pair pair;
47 0 : pair.fPtr = ptr;
48 :
49 0 : int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
50 0 : if (index < 0) {
51 0 : index = ~index; // turn it back into an index for insertion
52 0 : this->incPtr(ptr);
53 0 : pair.fIndex = count + 1;
54 0 : *fList.insert(index) = pair;
55 0 : return count + 1;
56 : } else {
57 0 : return fList[index].fIndex;
58 : }
59 : }
60 :
61 0 : void SkPtrSet::copyToArray(void* array[]) const {
62 0 : int count = fList.count();
63 0 : if (count > 0) {
64 0 : SkASSERT(array);
65 0 : const Pair* p = fList.begin();
66 : // p->fIndex is base-1, so we need to subtract to find its slot
67 0 : for (int i = 0; i < count; i++) {
68 0 : int index = p[i].fIndex - 1;
69 0 : SkASSERT((unsigned)index < (unsigned)count);
70 0 : array[index] = p[i].fPtr;
71 : }
72 : }
73 0 : }
|