SkPtrRecorder.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/core/SkPtrRecorder.h"
  8. #include "src/core/SkTSearch.h"
  9. void SkPtrSet::reset() {
  10. Pair* p = fList.begin();
  11. Pair* stop = fList.end();
  12. while (p < stop) {
  13. this->decPtr(p->fPtr);
  14. p += 1;
  15. }
  16. fList.reset();
  17. }
  18. bool SkPtrSet::Less(const Pair& a, const Pair& b) {
  19. return (char*)a.fPtr < (char*)b.fPtr;
  20. }
  21. uint32_t SkPtrSet::find(void* ptr) const {
  22. if (nullptr == ptr) {
  23. return 0;
  24. }
  25. int count = fList.count();
  26. Pair pair;
  27. pair.fPtr = ptr;
  28. int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
  29. if (index < 0) {
  30. return 0;
  31. }
  32. return fList[index].fIndex;
  33. }
  34. uint32_t SkPtrSet::add(void* ptr) {
  35. if (nullptr == ptr) {
  36. return 0;
  37. }
  38. int count = fList.count();
  39. Pair pair;
  40. pair.fPtr = ptr;
  41. int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
  42. if (index < 0) {
  43. index = ~index; // turn it back into an index for insertion
  44. this->incPtr(ptr);
  45. pair.fIndex = count + 1;
  46. *fList.insert(index) = pair;
  47. return count + 1;
  48. } else {
  49. return fList[index].fIndex;
  50. }
  51. }
  52. void SkPtrSet::copyToArray(void* array[]) const {
  53. int count = fList.count();
  54. if (count > 0) {
  55. SkASSERT(array);
  56. const Pair* p = fList.begin();
  57. // p->fIndex is base-1, so we need to subtract to find its slot
  58. for (int i = 0; i < count; i++) {
  59. int index = p[i].fIndex - 1;
  60. SkASSERT((unsigned)index < (unsigned)count);
  61. array[index] = p[i].fPtr;
  62. }
  63. }
  64. }