blob: be169d7b0d9e0496673c15d7fb067fc14f87736e [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
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000011void SkPtrSet::reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000012 Pair* p = fList.begin();
13 Pair* stop = fList.end();
14 while (p < stop) {
15 this->decPtr(p->fPtr);
16 p += 1;
17 }
18 fList.reset();
19}
20
bsalomon@google.com20f7f172013-05-17 19:05:03 +000021bool SkPtrSet::Less(const Pair& a, const Pair& b) {
22 return (char*)a.fPtr < (char*)b.fPtr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023}
24
reed@google.com8d90eeb2011-05-04 18:03:00 +000025uint32_t SkPtrSet::find(void* ptr) const {
halcanary96fcdcc2015-08-27 07:41:13 -070026 if (nullptr == ptr) {
reed@google.com8d90eeb2011-05-04 18:03:00 +000027 return 0;
28 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000029
reed@google.com8d90eeb2011-05-04 18:03:00 +000030 int count = fList.count();
31 Pair pair;
32 pair.fPtr = ptr;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033
bsalomon@google.com20f7f172013-05-17 19:05:03 +000034 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
reed@google.com8d90eeb2011-05-04 18:03:00 +000035 if (index < 0) {
36 return 0;
37 }
38 return fList[index].fIndex;
39}
40
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000041uint32_t SkPtrSet::add(void* ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070042 if (nullptr == ptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 return 0;
44 }
45
46 int count = fList.count();
47 Pair pair;
48 pair.fPtr = ptr;
49
bsalomon@google.com20f7f172013-05-17 19:05:03 +000050 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 if (index < 0) {
52 index = ~index; // turn it back into an index for insertion
53 this->incPtr(ptr);
54 pair.fIndex = count + 1;
55 *fList.insert(index) = pair;
56 return count + 1;
57 } else {
58 return fList[index].fIndex;
59 }
60}
61
mike@reedtribe.orge9e08cc2011-04-29 01:44:52 +000062void SkPtrSet::copyToArray(void* array[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 int count = fList.count();
64 if (count > 0) {
65 SkASSERT(array);
66 const Pair* p = fList.begin();
67 // p->fIndex is base-1, so we need to subtract to find its slot
68 for (int i = 0; i < count; i++) {
69 int index = p[i].fIndex - 1;
70 SkASSERT((unsigned)index < (unsigned)count);
71 array[index] = p[i].fPtr;
72 }
73 }
74}