blob: b33893397d267a808204764966e40929bdc85928 [file] [log] [blame]
Colin Crossab7c9f12016-01-14 15:35:40 -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_HEAP_WALKER_H_
18#define LIBMEMUNREACHABLE_HEAP_WALKER_H_
19
20#include "android-base/macros.h"
21
22#include "Allocator.h"
Colin Crosseccc5e42016-03-02 17:53:39 -080023#include "Tarjan.h"
Colin Crossab7c9f12016-01-14 15:35:40 -080024
25// A range [begin, end)
26struct Range {
27 uintptr_t begin;
28 uintptr_t end;
Colin Crosseccc5e42016-03-02 17:53:39 -080029
30 size_t size() const { return end - begin; };
Colin Crossab7c9f12016-01-14 15:35:40 -080031};
32
33// Comparator for Ranges that returns equivalence for overlapping ranges
34struct compare_range {
35 bool operator()(const Range& a, const Range& b) const {
36 return a.end <= b.begin;
37 }
38};
39
Colin Crossab7c9f12016-01-14 15:35:40 -080040class HeapWalker {
41 public:
42 HeapWalker(Allocator<HeapWalker> allocator) : allocator_(allocator),
43 allocations_(allocator), allocation_bytes_(0),
44 roots_(allocator), root_vals_(allocator) {
45 valid_allocations_range_.end = 0;
46 valid_allocations_range_.begin = ~valid_allocations_range_.end;
47 }
48 ~HeapWalker() {}
49 bool Allocation(uintptr_t begin, uintptr_t end);
50 void Root(uintptr_t begin, uintptr_t end);
51 void Root(const allocator::vector<uintptr_t>& vals);
52
53 bool DetectLeaks();
54
55 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks,
56 size_t* leak_bytes);
57 size_t Allocations();
58 size_t AllocationBytes();
59
Colin Crosseccc5e42016-03-02 17:53:39 -080060 template<class F>
61 void ForEachPtrInRange(const Range& range, F&& f);
62
63 template<class F>
64 void ForEachAllocation(F&& f);
65
66 struct AllocationInfo {
Colin Crossab7c9f12016-01-14 15:35:40 -080067 bool referenced_from_root;
Colin Crossab7c9f12016-01-14 15:35:40 -080068 };
Colin Crosseccc5e42016-03-02 17:53:39 -080069
70 private:
71
72 void RecurseRoot(const Range& root);
73 bool IsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
74
Colin Crossab7c9f12016-01-14 15:35:40 -080075 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
76 Allocator<HeapWalker> allocator_;
Colin Crosseccc5e42016-03-02 17:53:39 -080077 using AllocationMap = allocator::map<AllocationInfo, Range, compare_range>;
78 AllocationMap allocations_;
Colin Crossab7c9f12016-01-14 15:35:40 -080079 size_t allocation_bytes_;
80 Range valid_allocations_range_;
81
82 allocator::vector<Range> roots_;
83 allocator::vector<uintptr_t> root_vals_;
84};
85
Colin Crosseccc5e42016-03-02 17:53:39 -080086template<class F>
87inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) {
88 uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1);
89 // TODO(ccross): we might need to consider a pointer to the end of a buffer
90 // to be inside the buffer, which means the common case of a pointer to the
91 // beginning of a buffer may keep two ranges live.
92 for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) {
93 Range ref_range;
94 AllocationInfo* ref_info;
95 if (IsAllocationPtr(*reinterpret_cast<uintptr_t*>(i), &ref_range, &ref_info)) {
96 f(ref_range, ref_info);
97 }
98 }
99}
100
101template<class F>
102inline void HeapWalker::ForEachAllocation(F&& f) {
103 for (auto& it : allocations_) {
104 const Range& range = it.first;
105 HeapWalker::AllocationInfo& allocation = it.second;
106 f(range, allocation);
107 }
108}
109
Colin Crossab7c9f12016-01-14 15:35:40 -0800110#endif