1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPtrRecorder.h"
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTSearch.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10e9e08cc7b29f97ee9e823e68c3daf0f55c84b21amike@reedtribe.orgvoid SkPtrSet::reset() {
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Pair* p = fList.begin();
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Pair* stop = fList.end();
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while (p < stop) {
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->decPtr(p->fPtr);
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        p += 1;
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fList.reset();
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2020f7f173e05b60f541910d0c1da9850ac73e2958bsalomon@google.combool SkPtrSet::Less(const Pair& a, const Pair& b) {
2120f7f173e05b60f541910d0c1da9850ac73e2958bsalomon@google.com    return (char*)a.fPtr < (char*)b.fPtr;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
248d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.comuint32_t SkPtrSet::find(void* ptr) const {
2596fcdcc219d2a0d3579719b84b28bede76efba64halcanary    if (nullptr == ptr) {
268d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com        return 0;
278d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    }
28fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
298d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    int count = fList.count();
308d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    Pair pair;
318d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    pair.fPtr = ptr;
32fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
3320f7f173e05b60f541910d0c1da9850ac73e2958bsalomon@google.com    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
348d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    if (index < 0) {
358d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com        return 0;
368d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    }
378d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com    return fList[index].fIndex;
388d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com}
398d90eeba09484cfc702e82a332c4a7a978a5cfc1reed@google.com
40e9e08cc7b29f97ee9e823e68c3daf0f55c84b21amike@reedtribe.orguint32_t SkPtrSet::add(void* ptr) {
4196fcdcc219d2a0d3579719b84b28bede76efba64halcanary    if (nullptr == ptr) {
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return 0;
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int count = fList.count();
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Pair pair;
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    pair.fPtr = ptr;
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
4920f7f173e05b60f541910d0c1da9850ac73e2958bsalomon@google.com    int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (index < 0) {
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        index = ~index; // turn it back into an index for insertion
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->incPtr(ptr);
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pair.fIndex = count + 1;
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        *fList.insert(index) = pair;
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return count + 1;
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } else {
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fList[index].fIndex;
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
61e9e08cc7b29f97ee9e823e68c3daf0f55c84b21amike@reedtribe.orgvoid SkPtrSet::copyToArray(void* array[]) const {
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    int count = fList.count();
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (count > 0) {
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(array);
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const Pair* p = fList.begin();
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // p->fIndex is base-1, so we need to subtract to find its slot
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (int i = 0; i < count; i++) {
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            int index = p[i].fIndex - 1;
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkASSERT((unsigned)index < (unsigned)count);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            array[index] = p[i].fPtr;
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
74