blob: 740b54faeb6e8201ceae769567a3d5786a64c792 [file] [log] [blame]
Colin Crossd6b3a2a2016-03-02 17:53:39 -08001/*
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
22class LeakFolding {
23 public:
24 LeakFolding(Allocator<void> allocator, HeapWalker& heap_walker)
Colin Crossa83881e2017-06-22 10:50:05 -070025 : allocator_(allocator),
26 heap_walker_(heap_walker),
27 leak_map_(allocator),
28 leak_graph_(allocator),
29 leak_scc_(allocator) {}
Colin Crossd6b3a2a2016-03-02 17:53:39 -080030
31 bool FoldLeaks();
32
33 struct Leak {
34 const Range range;
35 size_t referenced_count;
36 size_t referenced_size;
37 };
38
Colin Crossa83881e2017-06-22 10:50:05 -070039 bool Leaked(allocator::vector<Leak>& leaked, size_t* num_leaks_out, size_t* leak_bytes_out);
Colin Crossd6b3a2a2016-03-02 17:53:39 -080040
41 private:
42 DISALLOW_COPY_AND_ASSIGN(LeakFolding);
43 Allocator<void> allocator_;
44 HeapWalker& heap_walker_;
45
46 struct SCCInfo {
47 public:
48 Node<SCCInfo> node;
49
50 size_t count;
51 size_t size;
52
53 size_t cuumulative_count;
54 size_t cuumulative_size;
55
56 bool dominator;
57 SCCInfo* accumulator;
58
Colin Crossa83881e2017-06-22 10:50:05 -070059 explicit SCCInfo(Allocator<SCCInfo> allocator)
60 : node(this, allocator),
61 count(0),
62 size(0),
63 cuumulative_count(0),
64 cuumulative_size(0),
65 dominator(false),
66 accumulator(nullptr) {}
67
Colin Crossd6b3a2a2016-03-02 17:53:39 -080068 private:
69 SCCInfo(SCCInfo&&) = delete;
70 DISALLOW_COPY_AND_ASSIGN(SCCInfo);
71 };
72
73 struct LeakInfo {
74 public:
75 Node<LeakInfo> node;
76
77 const Range range;
78
79 SCCInfo* scc;
80
81 LeakInfo(const Range& range, Allocator<LeakInfo> allocator)
Colin Crossa83881e2017-06-22 10:50:05 -070082 : node(this, allocator), range(range), scc(nullptr) {}
Colin Crossd6b3a2a2016-03-02 17:53:39 -080083
84 private:
85 DISALLOW_COPY_AND_ASSIGN(LeakInfo);
86 };
87
88 void ComputeDAG();
89 void AccumulateLeaks(SCCInfo* dominator);
90
Colin Crosse4cbe0e2016-03-04 16:34:42 -080091 allocator::map<Range, LeakInfo, compare_range> leak_map_;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080092 Graph<LeakInfo> leak_graph_;
93 allocator::vector<Allocator<SCCInfo>::unique_ptr> leak_scc_;
94};
95
Colin Crossa83881e2017-06-22 10:50:05 -070096#endif // LIBMEMUNREACHABLE_LEAK_FOLDING_H_