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