Christopher Ferris | e1f7a63 | 2019-01-24 12:22:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <malloc.h> |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | namespace unwindstack { |
| 23 | |
| 24 | void TestCheckForLeaks(void (*unwind_func)(void*), void* data) { |
| 25 | static constexpr size_t kNumLeakLoops = 200; |
| 26 | static constexpr size_t kMaxAllowedLeakBytes = 32 * 1024; |
| 27 | |
| 28 | size_t first_allocated_bytes = 0; |
| 29 | size_t last_allocated_bytes = 0; |
| 30 | for (size_t i = 0; i < kNumLeakLoops; i++) { |
| 31 | unwind_func(data); |
| 32 | |
| 33 | size_t allocated_bytes = mallinfo().uordblks; |
| 34 | if (first_allocated_bytes == 0) { |
| 35 | first_allocated_bytes = allocated_bytes; |
| 36 | } else if (last_allocated_bytes > first_allocated_bytes) { |
| 37 | // Check that the memory did not increase too much over the first loop. |
| 38 | ASSERT_LE(last_allocated_bytes - first_allocated_bytes, kMaxAllowedLeakBytes); |
| 39 | } |
| 40 | last_allocated_bytes = allocated_bytes; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | } // namespace unwindstack |