Home | History | Annotate | Download | only in libmemunreachable
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef LIBMEMUNREACHABLE_LEAK_FOLDING_H_
     18 #define LIBMEMUNREACHABLE_LEAK_FOLDING_H_
     19 
     20 #include "HeapWalker.h"
     21 
     22 class LeakFolding {
     23  public:
     24   LeakFolding(Allocator<void> allocator, HeapWalker& heap_walker)
     25    : allocator_(allocator), heap_walker_(heap_walker),
     26      leak_map_(allocator), leak_graph_(allocator), leak_scc_(allocator) {}
     27 
     28   bool FoldLeaks();
     29 
     30   struct Leak {
     31     const Range range;
     32     size_t referenced_count;
     33     size_t referenced_size;
     34   };
     35 
     36   bool Leaked(allocator::vector<Leak>& leaked,
     37       size_t* num_leaks_out, size_t* leak_bytes_out);
     38 
     39  private:
     40   DISALLOW_COPY_AND_ASSIGN(LeakFolding);
     41   Allocator<void> allocator_;
     42   HeapWalker& heap_walker_;
     43 
     44   struct SCCInfo {
     45    public:
     46     Node<SCCInfo> node;
     47 
     48     size_t count;
     49     size_t size;
     50 
     51     size_t cuumulative_count;
     52     size_t cuumulative_size;
     53 
     54     bool dominator;
     55     SCCInfo* accumulator;
     56 
     57     SCCInfo(Allocator<SCCInfo> allocator) : node(this, allocator),
     58         count(0), size(0), cuumulative_count(0), cuumulative_size(0),
     59         dominator(false), accumulator(nullptr) {}
     60    private:
     61     SCCInfo(SCCInfo&&) = delete;
     62     DISALLOW_COPY_AND_ASSIGN(SCCInfo);
     63   };
     64 
     65   struct LeakInfo {
     66    public:
     67     Node<LeakInfo> node;
     68 
     69     const Range range;
     70 
     71     SCCInfo* scc;
     72 
     73     LeakInfo(const Range& range, Allocator<LeakInfo> allocator)
     74         : node(this, allocator), range(range),
     75           scc(nullptr) {}
     76 
     77    private:
     78     DISALLOW_COPY_AND_ASSIGN(LeakInfo);
     79   };
     80 
     81   void ComputeDAG();
     82   void AccumulateLeaks(SCCInfo* dominator);
     83 
     84   allocator::map<Range, LeakInfo, compare_range> leak_map_;
     85   Graph<LeakInfo> leak_graph_;
     86   allocator::vector<Allocator<SCCInfo>::unique_ptr> leak_scc_;
     87 };
     88 
     89 #endif // LIBMEMUNREACHABLE_LEAK_FOLDING_H_
     90