michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkPtrRecorder.h" michael@0: #include "SkTSearch.h" michael@0: michael@0: void SkPtrSet::reset() { michael@0: Pair* p = fList.begin(); michael@0: Pair* stop = fList.end(); michael@0: while (p < stop) { michael@0: this->decPtr(p->fPtr); michael@0: p += 1; michael@0: } michael@0: fList.reset(); michael@0: } michael@0: michael@0: bool SkPtrSet::Less(const Pair& a, const Pair& b) { michael@0: return (char*)a.fPtr < (char*)b.fPtr; michael@0: } michael@0: michael@0: uint32_t SkPtrSet::find(void* ptr) const { michael@0: if (NULL == ptr) { michael@0: return 0; michael@0: } michael@0: michael@0: int count = fList.count(); michael@0: Pair pair; michael@0: pair.fPtr = ptr; michael@0: michael@0: int index = SkTSearch(fList.begin(), count, pair, sizeof(pair)); michael@0: if (index < 0) { michael@0: return 0; michael@0: } michael@0: return fList[index].fIndex; michael@0: } michael@0: michael@0: uint32_t SkPtrSet::add(void* ptr) { michael@0: if (NULL == ptr) { michael@0: return 0; michael@0: } michael@0: michael@0: int count = fList.count(); michael@0: Pair pair; michael@0: pair.fPtr = ptr; michael@0: michael@0: int index = SkTSearch(fList.begin(), count, pair, sizeof(pair)); michael@0: if (index < 0) { michael@0: index = ~index; // turn it back into an index for insertion michael@0: this->incPtr(ptr); michael@0: pair.fIndex = count + 1; michael@0: *fList.insert(index) = pair; michael@0: return count + 1; michael@0: } else { michael@0: return fList[index].fIndex; michael@0: } michael@0: } michael@0: michael@0: void SkPtrSet::copyToArray(void* array[]) const { michael@0: int count = fList.count(); michael@0: if (count > 0) { michael@0: SkASSERT(array); michael@0: const Pair* p = fList.begin(); michael@0: // p->fIndex is base-1, so we need to subtract to find its slot michael@0: for (int i = 0; i < count; i++) { michael@0: int index = p[i].fIndex - 1; michael@0: SkASSERT((unsigned)index < (unsigned)count); michael@0: array[index] = p[i].fPtr; michael@0: } michael@0: } michael@0: }