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 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 20 | |
| 21 | // ART specific morecore implementation defined in space.cc. |
| 22 | #define MORECORE(x) art_heap_morecore(m, x) |
| 23 | extern "C" void* art_heap_morecore(void* m, intptr_t increment); |
| 24 | |
| 25 | // Custom heap error handling. |
| 26 | #define PROCEED_ON_ERROR 0 |
| 27 | static void art_heap_corruption(const char* function); |
| 28 | static void art_heap_usage_error(const char* function, void* p); |
| 29 | #define CORRUPTION_ERROR_ACTION(m) art_heap_corruption(__FUNCTION__) |
Brian Carlstrom | b1eba21 | 2013-07-17 18:07:19 -0700 | [diff] [blame] | 30 | #define USAGE_ERROR_ACTION(m, p) art_heap_usage_error(__FUNCTION__, p) |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 31 | |
| 32 | // Ugly inclusion of C file so that ART specific #defines configure dlmalloc for our use for |
| 33 | // mspaces (regular dlmalloc is still declared in bionic). |
| 34 | #pragma GCC diagnostic ignored "-Wempty-body" |
| 35 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 36 | #include "../../../bionic/libc/upstream-dlmalloc/malloc.c" |
| 37 | #pragma GCC diagnostic warning "-Wstrict-aliasing" |
| 38 | #pragma GCC diagnostic warning "-Wempty-body" |
| 39 | |
| 40 | |
| 41 | static void art_heap_corruption(const char* function) { |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 42 | LOG(::art::FATAL) << "Corrupt heap detected in: " << function; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void art_heap_usage_error(const char* function, void* p) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 46 | LOG(::art::FATAL) << "Incorrect use of function '" << function << "' argument " << p |
| 47 | << " not expected"; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 48 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 49 | |
| 50 | #include "globals.h" |
| 51 | #include "utils.h" |
| 52 | #include <sys/mman.h> |
| 53 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 54 | extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 55 | // Is this chunk in use? |
| 56 | if (used_bytes != 0) { |
| 57 | return; |
| 58 | } |
| 59 | // Do we have any whole pages to give back? |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 60 | start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize)); |
| 61 | 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] | 62 | if (end > start) { |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 63 | 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] | 64 | int rc = madvise(start, length, MADV_DONTNEED); |
| 65 | if (UNLIKELY(rc != 0)) { |
| 66 | errno = rc; |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 67 | PLOG(::art::FATAL) << "madvise failed during heap trimming"; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | } |
| 69 | size_t* reclaimed = reinterpret_cast<size_t*>(arg); |
| 70 | *reclaimed += length; |
| 71 | } |
| 72 | } |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 73 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 74 | extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED, |
| 75 | void* end ATTRIBUTE_UNUSED, size_t used_bytes, |
| 76 | void* arg) { |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 77 | if (used_bytes == 0) { |
| 78 | return; |
| 79 | } |
| 80 | size_t* bytes_allocated = reinterpret_cast<size_t*>(arg); |
| 81 | *bytes_allocated += used_bytes + sizeof(size_t); |
| 82 | } |
| 83 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame^] | 84 | extern "C" void DlmallocObjectsAllocatedCallback(void* start, void* end, size_t used_bytes, |
| 85 | void* arg) { |
| 86 | UNUSED(start); |
| 87 | UNUSED(end); |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 88 | if (used_bytes == 0) { |
| 89 | return; |
| 90 | } |
| 91 | size_t* objects_allocated = reinterpret_cast<size_t*>(arg); |
| 92 | ++(*objects_allocated); |
| 93 | } |