gfx/skia/trunk/src/core/SkPtrRecorder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkPtrRecorder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include "SkPtrRecorder.h"
    1.12 +#include "SkTSearch.h"
    1.13 +
    1.14 +void SkPtrSet::reset() {
    1.15 +    Pair* p = fList.begin();
    1.16 +    Pair* stop = fList.end();
    1.17 +    while (p < stop) {
    1.18 +        this->decPtr(p->fPtr);
    1.19 +        p += 1;
    1.20 +    }
    1.21 +    fList.reset();
    1.22 +}
    1.23 +
    1.24 +bool SkPtrSet::Less(const Pair& a, const Pair& b) {
    1.25 +    return (char*)a.fPtr < (char*)b.fPtr;
    1.26 +}
    1.27 +
    1.28 +uint32_t SkPtrSet::find(void* ptr) const {
    1.29 +    if (NULL == ptr) {
    1.30 +        return 0;
    1.31 +    }
    1.32 +
    1.33 +    int count = fList.count();
    1.34 +    Pair pair;
    1.35 +    pair.fPtr = ptr;
    1.36 +
    1.37 +    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
    1.38 +    if (index < 0) {
    1.39 +        return 0;
    1.40 +    }
    1.41 +    return fList[index].fIndex;
    1.42 +}
    1.43 +
    1.44 +uint32_t SkPtrSet::add(void* ptr) {
    1.45 +    if (NULL == ptr) {
    1.46 +        return 0;
    1.47 +    }
    1.48 +
    1.49 +    int count = fList.count();
    1.50 +    Pair pair;
    1.51 +    pair.fPtr = ptr;
    1.52 +
    1.53 +    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
    1.54 +    if (index < 0) {
    1.55 +        index = ~index; // turn it back into an index for insertion
    1.56 +        this->incPtr(ptr);
    1.57 +        pair.fIndex = count + 1;
    1.58 +        *fList.insert(index) = pair;
    1.59 +        return count + 1;
    1.60 +    } else {
    1.61 +        return fList[index].fIndex;
    1.62 +    }
    1.63 +}
    1.64 +
    1.65 +void SkPtrSet::copyToArray(void* array[]) const {
    1.66 +    int count = fList.count();
    1.67 +    if (count > 0) {
    1.68 +        SkASSERT(array);
    1.69 +        const Pair* p = fList.begin();
    1.70 +        // p->fIndex is base-1, so we need to subtract to find its slot
    1.71 +        for (int i = 0; i < count; i++) {
    1.72 +            int index = p[i].fIndex - 1;
    1.73 +            SkASSERT((unsigned)index < (unsigned)count);
    1.74 +            array[index] = p[i].fPtr;
    1.75 +        }
    1.76 +    }
    1.77 +}

mercurial