blob: 2acb5af9398b2c38e155d17cb6627be07da61b6f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkPtrRecorder.h"
9#include "SkTSearch.h"
10
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000011SK_DEFINE_INST_COUNT(SkPtrSet)
robertphillips@google.coma22e2112012-08-16 14:58:06 +000012SK_DEFINE_INST_COUNT(SkNamedFactorySet)
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000013
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000014void SkPtrSet::reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 Pair* p = fList.begin();
16 Pair* stop = fList.end();
17 while (p < stop) {
18 this->decPtr(p->fPtr);
19 p += 1;
20 }
21 fList.reset();
22}
23
bsalomon@google.com20f7f172013-05-17 19:05:03 +000024bool SkPtrSet::Less(const Pair& a, const Pair& b) {
25 return (char*)a.fPtr < (char*)b.fPtr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026}
27
reed@google.com8d90eeb2011-05-04 18:03:00 +000028uint32_t SkPtrSet::find(void* ptr) const {
29 if (NULL == ptr) {
30 return 0;
31 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032
reed@google.com8d90eeb2011-05-04 18:03:00 +000033 int count = fList.count();
34 Pair pair;
35 pair.fPtr = ptr;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036
bsalomon@google.com20f7f172013-05-17 19:05:03 +000037 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
reed@google.com8d90eeb2011-05-04 18:03:00 +000038 if (index < 0) {
39 return 0;
40 }
41 return fList[index].fIndex;
42}
43
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000044uint32_t SkPtrSet::add(void* ptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 if (NULL == ptr) {
46 return 0;
47 }
48
49 int count = fList.count();
50 Pair pair;
51 pair.fPtr = ptr;
52
bsalomon@google.com20f7f172013-05-17 19:05:03 +000053 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 if (index < 0) {
55 index = ~index; // turn it back into an index for insertion
56 this->incPtr(ptr);
57 pair.fIndex = count + 1;
58 *fList.insert(index) = pair;
59 return count + 1;
60 } else {
61 return fList[index].fIndex;
62 }
63}
64
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000065void SkPtrSet::copyToArray(void* array[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 int count = fList.count();
67 if (count > 0) {
68 SkASSERT(array);
69 const Pair* p = fList.begin();
70 // p->fIndex is base-1, so we need to subtract to find its slot
71 for (int i = 0; i < count; i++) {
72 int index = p[i].fIndex - 1;
73 SkASSERT((unsigned)index < (unsigned)count);
74 array[index] = p[i].fPtr;
75 }
76 }
77}