Home | History | Annotate | Download | only in core
      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 "SkPtrRecorder.h"
      8 #include "SkTSearch.h"
      9 
     10 void SkPtrSet::reset() {
     11     Pair* p = fList.begin();
     12     Pair* stop = fList.end();
     13     while (p < stop) {
     14         this->decPtr(p->fPtr);
     15         p += 1;
     16     }
     17     fList.reset();
     18 }
     19 
     20 bool SkPtrSet::Less(const Pair& a, const Pair& b) {
     21     return (char*)a.fPtr < (char*)b.fPtr;
     22 }
     23 
     24 uint32_t SkPtrSet::find(void* ptr) const {
     25     if (nullptr == ptr) {
     26         return 0;
     27     }
     28 
     29     int count = fList.count();
     30     Pair pair;
     31     pair.fPtr = ptr;
     32 
     33     int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
     34     if (index < 0) {
     35         return 0;
     36     }
     37     return fList[index].fIndex;
     38 }
     39 
     40 uint32_t SkPtrSet::add(void* ptr) {
     41     if (nullptr == ptr) {
     42         return 0;
     43     }
     44 
     45     int count = fList.count();
     46     Pair pair;
     47     pair.fPtr = ptr;
     48 
     49     int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
     50     if (index < 0) {
     51         index = ~index; // turn it back into an index for insertion
     52         this->incPtr(ptr);
     53         pair.fIndex = count + 1;
     54         *fList.insert(index) = pair;
     55         return count + 1;
     56     } else {
     57         return fList[index].fIndex;
     58     }
     59 }
     60 
     61 void SkPtrSet::copyToArray(void* array[]) const {
     62     int count = fList.count();
     63     if (count > 0) {
     64         SkASSERT(array);
     65         const Pair* p = fList.begin();
     66         // p->fIndex is base-1, so we need to subtract to find its slot
     67         for (int i = 0; i < count; i++) {
     68             int index = p[i].fIndex - 1;
     69             SkASSERT((unsigned)index < (unsigned)count);
     70             array[index] = p[i].fPtr;
     71         }
     72     }
     73 }
     74