blob: 4f774ec6ab5d2274b4d728833a2e129ad33e0c01 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#include "SkPtrRecorder.h"
2#include "SkTSearch.h"
3
4void SkPtrRecorder::reset() {
5 Pair* p = fList.begin();
6 Pair* stop = fList.end();
7 while (p < stop) {
8 this->decPtr(p->fPtr);
9 p += 1;
10 }
11 fList.reset();
12}
13
14int SkPtrRecorder::Cmp(const Pair& a, const Pair& b) {
15 return (char*)a.fPtr - (char*)b.fPtr;
16}
17
18uint32_t SkPtrRecorder::recordPtr(void* ptr) {
19 if (NULL == ptr) {
20 return 0;
21 }
22
23 int count = fList.count();
24 Pair pair;
25 pair.fPtr = ptr;
26
27 int index = SkTSearch<Pair>(fList.begin(), count, pair, sizeof(pair), &Cmp);
28 if (index < 0) {
29 index = ~index; // turn it back into an index for insertion
30 this->incPtr(ptr);
31 pair.fIndex = count + 1;
32 *fList.insert(index) = pair;
33 return count + 1;
34 } else {
35 return fList[index].fIndex;
36 }
37}
38
39void SkPtrRecorder::getPtrs(void* array[]) const {
40 int count = fList.count();
41 if (count > 0) {
42 SkASSERT(array);
43 const Pair* p = fList.begin();
44 // p->fIndex is base-1, so we need to subtract to find its slot
45 for (int i = 0; i < count; i++) {
46 int index = p[i].fIndex - 1;
47 SkASSERT((unsigned)index < (unsigned)count);
48 array[index] = p[i].fPtr;
49 }
50 }
51}
52
53