aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkPtrRecorder.cpp
blob: e3a9eee3bb65e0f2d5647b1d1dc229958311377c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkPtrRecorder.h"
#include "SkTSearch.h"

void SkPtrSet::reset() {
    Pair* p = fList.begin();
    Pair* stop = fList.end();
    while (p < stop) {
        this->decPtr(p->fPtr);
        p += 1;
    }
    fList.reset();
}

int SkPtrSet::Cmp(const Pair& a, const Pair& b) {
    return (char*)a.fPtr - (char*)b.fPtr;
}

uint32_t SkPtrSet::find(void* ptr) const {
    if (NULL == ptr) {
        return 0;
    }
    
    int count = fList.count();
    Pair pair;
    pair.fPtr = ptr;
    
    int index = SkTSearch<Pair>(fList.begin(), count, pair, sizeof(pair), &Cmp);
    if (index < 0) {
        return 0;
    }
    return fList[index].fIndex;
}

uint32_t SkPtrSet::add(void* ptr) {
    if (NULL == ptr) {
        return 0;
    }

    int count = fList.count();
    Pair pair;
    pair.fPtr = ptr;

    int index = SkTSearch<Pair>(fList.begin(), count, pair, sizeof(pair), &Cmp);
    if (index < 0) {
        index = ~index; // turn it back into an index for insertion
        this->incPtr(ptr);
        pair.fIndex = count + 1;
        *fList.insert(index) = pair;
        return count + 1;
    } else {
        return fList[index].fIndex;
    }
}

void SkPtrSet::copyToArray(void* array[]) const {
    int count = fList.count();
    if (count > 0) {
        SkASSERT(array);
        const Pair* p = fList.begin();
        // p->fIndex is base-1, so we need to subtract to find its slot
        for (int i = 0; i < count; i++) {
            int index = p[i].fIndex - 1;
            SkASSERT((unsigned)index < (unsigned)count);
            array[index] = p[i].fPtr;
        }
    }
}