• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SK_DEFINE_INST_COUNT(SkPtrSet)
SK_DEFINE_INST_COUNT(SkNamedFactorySet)12 SK_DEFINE_INST_COUNT(SkNamedFactorySet)
13 
14 void SkPtrSet::reset() {
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 
Cmp(const Pair * a,const Pair * b)24 int SkPtrSet::Cmp(const Pair* a, const Pair* b) {
25     return (char*)a->fPtr - (char*)b->fPtr;
26 }
27 
find(void * ptr) const28 uint32_t SkPtrSet::find(void* ptr) const {
29     if (NULL == ptr) {
30         return 0;
31     }
32 
33     int count = fList.count();
34     Pair pair;
35     pair.fPtr = ptr;
36 
37     int index = SkTSearch<Pair, Cmp>(fList.begin(), count, pair, sizeof(pair));
38     if (index < 0) {
39         return 0;
40     }
41     return fList[index].fIndex;
42 }
43 
add(void * ptr)44 uint32_t SkPtrSet::add(void* ptr) {
45     if (NULL == ptr) {
46         return 0;
47     }
48 
49     int count = fList.count();
50     Pair pair;
51     pair.fPtr = ptr;
52 
53     int index = SkTSearch<Pair, Cmp>(fList.begin(), count, pair, sizeof(pair));
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 
copyToArray(void * array[]) const65 void SkPtrSet::copyToArray(void* array[]) const {
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 }
78