blob: 65062208d687673adef23d0eada27dcc5c34b9a2 [file] [log] [blame]
Ian Rogers15bf2d32012-08-28 17:33:04 -07001/*
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 Gampe57943812017-12-06 21:39:13 -080019#include <android-base/logging.h>
20
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include "base/bit_utils.h"
Ian Rogers15bf2d32012-08-28 17:33:04 -070022
23// ART specific morecore implementation defined in space.cc.
Andreas Gampe277ccbd2014-11-03 21:36:10 -080024static void* art_heap_morecore(void* m, intptr_t increment);
Ian Rogers15bf2d32012-08-28 17:33:04 -070025#define MORECORE(x) art_heap_morecore(m, x)
Ian Rogers15bf2d32012-08-28 17:33:04 -070026
27// Custom heap error handling.
28#define PROCEED_ON_ERROR 0
29static void art_heap_corruption(const char* function);
30static void art_heap_usage_error(const char* function, void* p);
31#define CORRUPTION_ERROR_ACTION(m) art_heap_corruption(__FUNCTION__)
Brian Carlstromb1eba212013-07-17 18:07:19 -070032#define USAGE_ERROR_ACTION(m, p) art_heap_usage_error(__FUNCTION__, p)
Ian Rogers15bf2d32012-08-28 17:33:04 -070033
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 Gampe277ccbd2014-11-03 21:36:10 -080036#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wredundant-decls"
Ian Rogers15bf2d32012-08-28 17:33:04 -070038#pragma GCC diagnostic ignored "-Wempty-body"
39#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Josh Gao3220a6d2016-01-22 11:00:27 -080040#include "../../../external/dlmalloc/malloc.c"
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070041// Note: malloc.c uses a DEBUG define to drive debug code. This interferes with the DEBUG severity
42// of libbase, so undefine it now.
43#undef DEBUG
Andreas Gampe277ccbd2014-11-03 21:36:10 -080044#pragma GCC diagnostic pop
Ian Rogers15bf2d32012-08-28 17:33:04 -070045
Andreas Gampe277ccbd2014-11-03 21:36:10 -080046static void* art_heap_morecore(void* m, intptr_t increment) {
47 return ::art::gc::allocator::ArtDlMallocMoreCore(m, increment);
48}
Ian Rogers15bf2d32012-08-28 17:33:04 -070049
50static void art_heap_corruption(const char* function) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070051 LOG(FATAL) << "Corrupt heap detected in: " << function;
Ian Rogers15bf2d32012-08-28 17:33:04 -070052}
53
54static void art_heap_usage_error(const char* function, void* p) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070055 LOG(FATAL) << "Incorrect use of function '" << function << "' argument " << p
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070056 << " not expected";
Ian Rogers15bf2d32012-08-28 17:33:04 -070057}
Ian Rogers1d54e732013-05-02 21:10:01 -070058
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070059#include <sys/mman.h>
60
Ian Rogers1d54e732013-05-02 21:10:01 -070061#include "globals.h"
62#include "utils.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070063
Ian Rogers1d54e732013-05-02 21:10:01 -070064extern "C" void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) {
65 // Is this chunk in use?
66 if (used_bytes != 0) {
67 return;
68 }
69 // Do we have any whole pages to give back?
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070070 start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::kPageSize));
71 end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::kPageSize));
Ian Rogers1d54e732013-05-02 21:10:01 -070072 if (end > start) {
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070073 size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start);
Ian Rogers1d54e732013-05-02 21:10:01 -070074 int rc = madvise(start, length, MADV_DONTNEED);
75 if (UNLIKELY(rc != 0)) {
76 errno = rc;
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070077 PLOG(FATAL) << "madvise failed during heap trimming";
Ian Rogers1d54e732013-05-02 21:10:01 -070078 }
79 size_t* reclaimed = reinterpret_cast<size_t*>(arg);
80 *reclaimed += length;
81 }
82}
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070083
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070084extern "C" void DlmallocBytesAllocatedCallback(void* start ATTRIBUTE_UNUSED,
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010085 void* end ATTRIBUTE_UNUSED,
86 size_t used_bytes,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070087 void* arg) {
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070088 if (used_bytes == 0) {
89 return;
90 }
91 size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
92 *bytes_allocated += used_bytes + sizeof(size_t);
93}
94
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010095extern "C" void DlmallocObjectsAllocatedCallback(void* start ATTRIBUTE_UNUSED,
96 void* end ATTRIBUTE_UNUSED,
97 size_t used_bytes,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070098 void* arg) {
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -070099 if (used_bytes == 0) {
100 return;
101 }
102 size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
103 ++(*objects_allocated);
104}