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