180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPtrRecorder.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTSearch.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkPtrSet::reset() {
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Pair* p = fList.begin();
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Pair* stop = fList.end();
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    while (p < stop) {
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->decPtr(p->fPtr);
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        p += 1;
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fList.reset();
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
217839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenbergerbool SkPtrSet::Less(const Pair& a, const Pair& b) {
227839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    return (char*)a.fPtr < (char*)b.fPtr;
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruuint32_t SkPtrSet::find(void* ptr) const {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == ptr) {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return 0;
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int count = fList.count();
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Pair pair;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pair.fPtr = ptr;
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
347839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (index < 0) {
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return 0;
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return fList[index].fIndex;
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruuint32_t SkPtrSet::add(void* ptr) {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == ptr) {
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return 0;
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int count = fList.count();
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Pair pair;
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    pair.fPtr = ptr;
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
507839ce1af63bf12fe7b3caa866970bbbb3afb13dDerek Sollenberger    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (index < 0) {
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        index = ~index; // turn it back into an index for insertion
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->incPtr(ptr);
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        pair.fIndex = count + 1;
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        *fList.insert(index) = pair;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return count + 1;
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } else {
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return fList[index].fIndex;
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkPtrSet::copyToArray(void* array[]) const {
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int count = fList.count();
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (count > 0) {
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkASSERT(array);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const Pair* p = fList.begin();
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // p->fIndex is base-1, so we need to subtract to find its slot
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (int i = 0; i < count; i++) {
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            int index = p[i].fIndex - 1;
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkASSERT((unsigned)index < (unsigned)count);
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            array[index] = p[i].fPtr;
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
75