Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "dlmalloc.h" |
| 18 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 21 | #include "base/bit_utils.h" |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 22 | |
| 23 | // ART specific morecore implementation defined in space.cc. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 24 | static void* art_heap_morecore(void* m, intptr_t increment); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 25 | #define MORECORE(x) art_heap_morecore(m, x) |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 26 | |
| 27 | // Custom heap error handling. |
| 28 | #define PROCEED_ON_ERROR 0 |
| 29 | static void art_heap_corruption(const char* function); |
| 30 | static void art_heap_usage_error(const char* function, void* p); |
| 31 | #define CORRUPTION_ERROR_ACTION(m) art_heap_corruption(__FUNCTION__) |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 32 | #define USAGE_ERROR_ACTION(m, p) art_heap_usage_error(__FUNCTION__, p) |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 33 | |
| 34 | // Ugly inclusion of C file so that ART specific #defines configure dlmalloc for our use for |
| 35 | // mspaces (regular dlmalloc is still declared in bionic). |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 36 | #pragma GCC diagnostic push |
| 37 | #pragma GCC diagnostic ignored "-Wredundant-decls" |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 38 | #pragma GCC diagnostic ignored "-Wempty-body" |
| 39 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
Yi Kong | 923a694 | 2017-12-20 17:06:02 -0800 | [diff] [blame] | 40 | #pragma GCC diagnostic ignored "-Wnull-pointer-arithmetic" |
Josh Gao | 3220a6d | 2016-01-22 11:00:27 -0800 | [diff] [blame] | 41 | #include "../../../external/dlmalloc/malloc.c" |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 42 | // Note: malloc.c uses a DEBUG define to drive debug code. This interferes with the DEBUG severity |
| 43 | // of libbase, so undefine it now. |
| 44 | #undef DEBUG |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 45 | #pragma GCC diagnostic pop |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 46 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 47 | static void* art_heap_morecore(void* m, intptr_t increment) { |
| 48 | return ::art::gc::allocator::ArtDlMallocMoreCore(m, increment); |
| 49 | } |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 50 | |
| 51 | static void art_heap_corruption(const char* function) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 52 | LOG(FATAL) << "Corrupt heap detected in: " << function; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static void art_heap_usage_error(const char* function, void* p) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 56 | LOG(FATAL) << "Incorrect use of function '" << function << "' argument " << p |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 57 | << " not expected"; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 58 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 59 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 60 | #include <sys/mman.h> |
| 61 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 62 | #include "base/globals.h" |
| 63 | #include "base/utils.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 64 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 65 | extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 66 | // Is this chunk in use? |
| 67 | if (used_bytes != 0) { |
| 68 | return; |
| 69 | } |
| 70 | // Do we have any whole pages to give back? |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 71 | start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize)); |
| 72 | end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::kPageSize)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 73 | if (end > start) { |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 74 | size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 75 | int rc = madvise(start, length, MADV_DONTNEED); |
| 76 | if (UNLIKELY(rc != 0)) { |
| 77 | errno = rc; |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 78 | PLOG(FATAL) << "madvise failed during heap trimming"; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 79 | } |
| 80 | size_t* reclaimed = reinterpret_cast<size_t*>(arg); |
| 81 | *reclaimed += length; |
| 82 | } |
| 83 | } |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 84 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 85 | extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED, |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 86 | void* end ATTRIBUTE_UNUSED, |
| 87 | size_t used_bytes, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 88 | void* arg) { |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 89 | if (used_bytes == 0) { |
| 90 | return; |
| 91 | } |
| 92 | size_t* bytes_allocated = reinterpret_cast<size_t*>(arg); |
| 93 | *bytes_allocated += used_bytes + sizeof(size_t); |
| 94 | } |
| 95 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 96 | extern "C" void DlmallocObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED, |
| 97 | void* end ATTRIBUTE_UNUSED, |
| 98 | size_t used_bytes, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 99 | void* arg) { |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 100 | if (used_bytes == 0) { |
| 101 | return; |
| 102 | } |
| 103 | size_t* objects_allocated = reinterpret_cast<size_t*>(arg); |
| 104 | ++(*objects_allocated); |
| 105 | } |